<?php
$flg = false;
/*$myplotexpr = "plot(exp(x));
do
plot(sin(k*x));
\$k=k+1;
do
plot(exp(i*k*x));
\$i=i+1
while \$i<9
while \$k<4";
*/
$myplotexpr = file_get_contents ("./compiler.txt");
if (strstr ($myplotexpr, "PLOT:")) {
$myplotexpr = str_replace ("PLOT:", "", $myplotexpr);
$flg = true;
}
else
exit (1);
$myplotexpr = preg_replace("/\s+/", "", $myplotexpr);
$myplotexpr = str_replace ("<", "<", $myplotexpr);
$myplotexpr = str_replace (">", ">", $myplotexpr);
$upper = 4.0;
$lower = -4.0;
$i = 0;
$k = 0;
$stckptr = 0;
$while_count = 0;
for ($ch = 'a'; $ch <= 'z'; $ch++)
$a [$ch] = 0;
plot_start ();
sequnce ();
plot_ends ();
echo $a['k'];
function push ($val) {
global $stck;
global $stckptr;
global $myplotexpr;
if ($stckptr > 32)
exit ("error !!!!!!!!!");
$stck [$stckptr] = $val;
$stckptr ++;
}
function pop () {
global $stck;
global $stckptr;
$stckptr--;
if ($stckptr < 0)
exit ("bug!!!!!");
return $stck [$stckptr];
}
function plot_start () {
global $upper;
global $lower;
global $myImage;
global $myBlue;
global $myRed;
global $myplotexpr;
$myImage = imagecreatetruecolor(800, 600);
$trans_colour = imagecolorallocatealpha($myImage, 127, 127, 255, 0);
imagefill($myImage, 0, 0, $trans_colour);
$myBlue = imagecolorallocate($myImage, 10, 20, 200);
$myRed = imagecolorallocate($myImage, 200, 10, 10);
imageline ($myImage, 0-$lower*(800/($upper-$lower))-1, 0, 0-$lower*(800/($upper-$lower))-1, 600, $myRed);;
imageline ($myImage, 0-$lower*(800/($upper-$lower)), 0, 0-$lower*(800/($upper-$lower)), 600, $myRed);;
imageline ($myImage, 0-$lower*(800/($upper-$lower))+1, 0, 0-$lower*(800/($upper-$lower))+1, 600, $myRed);;
imageline ($myImage, 0, 300, 800, 300, $myRed);;
imageline ($myImage, 0, 301, 800, 301, $myRed);;
imageline ($myImage, 0, 299, 800, 299, $myRed);;
}
function plot_ends () {
global $myplotexpr;
global $myImage;
global $outstr;
ob_start();
imagepng($myImage, "./func000.png");
base64_encode(ob_get_clean());
sleep (1);
unset($outstr);
echo "<div id=\"idfuncplotdiv\">";
echo "<img width=\"98%\" id=\"idfuncplot\" src=\"./func000.png\">";
echo "</div>";
echo "<script src=\"./scripts/funcplot.js\"></script>";
}
function plot () {
global $k;
global $i;
global $j;
global $upper;
global $lower;
global $myImage;
global $myRed;
global $myBlue;
global $a;
global $myplotexpr;
$i = $k;
if (!(($myplotexpr [$i] == 'p') &&
($myplotexpr [$i+1] == 'l') &&
($myplotexpr [$i+2] == 'o') &&
($myplotexpr [$i+3] == 't') &&
($myplotexpr [$i+4] == '(')))
return false;
$i += 5;
$k += 5;
$a ['x'] = $lower;
$max = expr ();
for ($a ['x'] = $lower+(($upper-$lower)/800); $a ['x'] < $upper; $a ['x'] += (($upper-$lower)/800)) {
$i = $k;
if(($t = expr ()) > $max)
$max = $t;
}
$i = $k;
$a ['x'] = $lower;
$oldt = expr ();
$oldt = $oldt / $max;
$oldt = $t * 300 + 300;
$oldti = ceil ($oldt);
for ($a ['x'] = $lower+(($upper-$lower)/800), $j = 0; $a ['x'] < $upper; $a ['x'] += (($upper-$lower)/800), $j++) {
$i = $k;
$t = expr ();
$t = $t / $max;
$t = $t * 300 + 300;
$ti = ceil ($t);
imageline ($myImage, $j, 600-$oldti, $j+1, 600-$ti, $myBlue);;
imageline ($myImage, $j, 600-$oldti-1, $j+1, 600-$ti-1, $myBlue);;
imageline ($myImage, $j, 600-$oldti-2, $j+1, 600-$ti-2, $myBlue);;
$oldt = $t;
$oldti = ceil ($oldt);
}
if (($myplotexpr [$i] != ')')) {
exit ( "error!!!!");
return false;
}
$i++;
$k = $i;
return true;
}
function sequnce () {
global $myplotexpr;
global $k;
statement ();
if ($myplotexpr [$k] == ';') {
$k++;
sequnce ();
}
}
function statement () {
global $k;
global $i;
global $myplotexpr;
if (do_while ());
else if (assgnmnt ());
else if (plot ());
else {
echo $myplotexpr [$k];
echo $myplotexpr [$i];
echo $k;
exit ( "error!!!!!");
}
}
function do_while () {
global $myplotexpr;
global $a;
global $i;
global $k;
global $while_count;
global $myplotexpr;
$i = $k;
if (($myplotexpr [$i] == 'd') && ($myplotexpr [$i+1] == 'o')) {
push ($i);
$i+=2;
$k+=2;
sequnce ();
if (!(($myplotexpr [$i] == 'w') &&
($myplotexpr [$i+1] == 'h') &&
($myplotexpr [$i+2] == 'i') &&
($myplotexpr [$i+3] == 'l') &&
($myplotexpr [$i+4] == 'e')))
exit ("error!!!!!!!!!!");
$i += 5;
$k += 5;
if (($myplotexpr [$i] == '$') && (($myplotexpr [$i+1] >= 'a') && ($myplotexpr [$i+1] <= 'z'))) {
$ch = $myplotexpr [$i+1];
$i+=2;
$k+=2;
if ($myplotexpr [$i] == '<') {
$i+=1;
$k+=1;
$x = expr ();
if ($x > 9)
exit ("error!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
$k = $i;
if ($a [$ch] < $x) {
$i = pop ();
$k = $i;
if ($while_count > 16)
exit ("errror!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
$while_count++;
do_while ();
return true;
}
else {
pop ();
return true;
}
}
else
exit ("error!!!!!!!!!!!!!!!!!!!!!!!!!!!!!11");
}
else
exit ("error!!!!!!!!!!!!!!!!!!");
}
else
return false;
}
function assgnmnt () {
global $i;
global $k;
global $a;
global $myplotexpr;
$i = $k;
if (($myplotexpr [$i] == '$') && (($myplotexpr [$i+1] >= 'a') && ($myplotexpr [$i+1] <= 'z'))) {
$ch = $myplotexpr [$i+1];
$i+=2;
$k+=2;
if ($myplotexpr [$i] == '=') {
$i++;
$a [$ch] = expr ();
$k = $i;
return true;
}
else {
exit ( "error!!!!!!");
return false;
}
}
else
return false;
}
function expr () {
global $i;
global $myplotexpr;
$x = term ();
if ($myplotexpr [$i] == '+') {
$i++;
$y = expr ();
return $x + $y;
}
else if ($myplotexpr [$i] == '-') {
$i++;
$y = expr ();
return $x - $y;
}
return $x;
}
function term () {
global $i;
global $myplotexpr;
$x = factor ();
if ($myplotexpr [$i] == '*') {
$i++;
$y = term ();
return $x * $y;
}
else if ($myplotexpr [$i] == '/') {
$i++;
$y = term ();
return $x / $y;
}
return $x;
}
function factor () {
global $i;
global $a;
global $myplotexpr;
if ($myplotexpr [$i] == '(') {
$i++;
$x = expr ();
if ($myplotexpr [$i] == ')')
$i++;
else
exit ( "error!");
return $x;
}
else if (($myplotexpr [$i+0] == 'c') &&
($myplotexpr [$i+1] == 'o') &&
($myplotexpr [$i+2] == 's') &&
($myplotexpr [$i+3] == '(')) {
$i += 4;
$x = expr ();
$y = cos ($x);
if ($myplotexpr [$i] == ')')
$i++;
else
exit( "error!!");
return $y;
}
else if (($myplotexpr [$i+0] == 's') &&
($myplotexpr [$i+1] == 'i') &&
($myplotexpr [$i+2] == 'n') &&
($myplotexpr [$i+3] == '(')) {
$i += 4;
$x = expr ();
$y = sin ($x);
if ($myplotexpr [$i] == ')')
$i++;
else
exit( "error!!");
return $y;
}
else if (($myplotexpr [$i+0] == 't') &&
($myplotexpr [$i+1] == 'a') &&
($myplotexpr [$i+2] == 'n') &&
($myplotexpr [$i+3] == '(')) {
$i += 4;
$x = expr ();
$y = tan ($x);
if ($myplotexpr [$i] == ')')
$i++;
else
exit( "error!!");
return $y;
}
else if (($myplotexpr [$i+0] == 'e') &&
($myplotexpr [$i+1] == 'x') &&
($myplotexpr [$i+2] == 'p') &&
($myplotexpr [$i+3] == '(')) {
$i += 4;
$x = expr ();
$y = exp ($x);
if ($myplotexpr [$i] == ')')
$i++;
else
exit( "error!!");
return $y;
}
else if (($myplotexpr [$i] >= '0') && ($myplotexpr [$i] <= '9')) {
$x = floatval(ord($myplotexpr [$i]) - ord('0'));
$i++;
return $x;
}
else if (($myplotexpr [$i] >= 'a') && ($myplotexpr [$i] <= 'z')) {
$q = $a [$myplotexpr [$i]];
$i++;
return $q;
}
else
exit( "error!!!");
}
if ($flg)
exec ("python3 compilertwitterimage.py");
?>