From 013addcafddaf0ae7eca9e82dc47aa726a13fe5b Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Mon, 29 Dec 2025 18:08:38 +0800 Subject: [PATCH] Update --- bin/compiler.php | 25 ++++++++------------ examples/fannkuchredux.php | 9 +++++++- src/Php/Translator.php | 47 +++++++++++++++++++++++++++++++++++++- src/functions.php | 9 ++++++++ 4 files changed, 73 insertions(+), 17 deletions(-) diff --git a/bin/compiler.php b/bin/compiler.php index 131d035e..b3c84b48 100755 --- a/bin/compiler.php +++ b/bin/compiler.php @@ -8,19 +8,14 @@ if (empty($argv[1])) { die("php compiler.php [file]\n"); } -try { - $file = $argv[1]; - $translator = new Translator(ROOT_PATH); - $translator->setIndent(' '); - $code = $translator->convert($file); - $info = pathinfo($file); - $cppFile = './tmp/' . $info['filename'] . '.cc'; - $translator->save($code, $cppFile); - $translator->compileFile($cppFile); +$file = $argv[1]; +$translator = new Translator(ROOT_PATH); +$translator->setIndent(' '); +$code = $translator->convert($file); +$info = pathinfo($file); +$cppFile = './tmp/' . $info['filename'] . '.cc'; +$translator->save($code, $cppFile); +$translator->compileFile($cppFile); - $objectFile = './tmp/' . $info['filename'] . '.cc.o'; - $translator->compileBinary($info['filename'], $objectFile); -} catch (Error $error) { - echo "Parse error: {$error->getMessage()}\n"; - return; -} +$objectFile = './tmp/' . $info['filename'] . '.cc.o'; +$translator->compileBinary($info['filename'], $objectFile); diff --git a/examples/fannkuchredux.php b/examples/fannkuchredux.php index 690334b5..a4ed263f 100644 --- a/examples/fannkuchredux.php +++ b/examples/fannkuchredux.php @@ -25,11 +25,16 @@ function fannkuch($n) while ($i < $n) { $perm[$i] = $perm1[$i]; $i += 1; + + var_dump($perm, $perm1); + var_dump('[i] i=' . $i); } // Count flips and update max and checksum $f = 0; $k = $perm[0]; + var_dump('[xxx] k=' . $k); + while ($k != 0) { $i = 0; while (2 * $i < $k) { @@ -40,9 +45,11 @@ function fannkuch($n) } $k = $perm[0]; $f += 1; + + var_dump('[--] r=' . $r . ', checksum=' . $checksum . ', i=' . $i . ', k=' . $k . ', f=' . $f . ', r=' . $r); } - var_dump('r=' . $r . ', nperm=' . $nperm . ', checksum=' . $checksum . ', i=' . $i . ', k=' . $k . ', f=' . $f . ', r=' . $r); + var_dump('[2] r=' . $r . ', nperm=' . $nperm . ', checksum=' . $checksum . ', i=' . $i . ', k=' . $k . ', f=' . $f . ', r=' . $r); if ($f > $flips) $flips = $f; if (($nperm & 0x1) == 0) $checksum += $f; else $checksum -= $f; diff --git a/src/Php/Translator.php b/src/Php/Translator.php index ad48e3a5..ef476df0 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -13,7 +13,7 @@ use PhpParser\PrettyPrinter; class Translator extends \PhpAot\Core\Translator { - const TYPE_VAR = 'php::Variant'; + const TYPE_VAR = 'php::Var'; const TYPE_BOOL = 'php::Bool'; const TYPE_INT = 'php::Int'; const TYPE_FLOAT = 'php::Float'; @@ -273,6 +273,9 @@ class Translator extends \PhpAot\Core\Translator case 'Stmt_Foreach': $lines[] = $this->parseForeach($v); break; + case 'Stmt_Switch': + $lines[] = $this->parseSwitch($v); + break; case 'Stmt_While': $lines[] = $this->parseWhile($v); break; @@ -291,6 +294,9 @@ class Translator extends \PhpAot\Core\Translator case 'Stmt_Global': $lines[] = $this->parseGlobal($v); break; + case 'Stmt_Static': + $lines[] = $this->parseStatic($v); + break; case 'Stmt_Unset': $lines[] = $this->parseUnset($v); break; @@ -1312,4 +1318,43 @@ class Translator extends \PhpAot\Core\Translator } return $name; } + + private function parseSwitch(mixed $v) + { + $cond = $v->cond; + $tmp_var = $this->addTmpVar(); + $type = $this->detectExprType($cond); + $code = $type . ' ' . $tmp_var . ' = ' . $this->parseExpr($cond) . ';' . PHP_EOL; + if ($type === self::TYPE_INT or $type === self::TYPE_FLOAT) { + $code .= 'switch (' . $tmp_var . ') {' . PHP_EOL; + $this->indentLevel++; + foreach ($v->cases as $case) { + if (empty($case->cond)) { + $code .= $this->getIndent() . 'default: {' . PHP_EOL; + } else { + $code .= $this->getIndent() . 'case ' . $this->parseIdentifier($case->cond) . ': {' . PHP_EOL; + } + $this->indentLevel++; + $code .= $this->parseStmts($case->stmts); + $this->indentLevel--; + $code .= $this->getIndent() . '}' . PHP_EOL; + } + $this->indentLevel--; + $code .= $this->getIndent() . '}'; + return $code; + } else { + + } + exit(2); + } + + private function parseStatic(mixed $v): string + { + $list = []; + foreach ($v->vars as $var) { + $type = $this->detectExprType($var->default); + $list[] = 'static ' . $type . ' ' . $this->parseIdentifier($var->var) . ' = ' . $this->parseIdentifier($var->default) . ';'; + } + return implode(PHP_EOL . $this->getIndent(), $list); + } } diff --git a/src/functions.php b/src/functions.php index bdceaa02..0e79092c 100644 --- a/src/functions.php +++ b/src/functions.php @@ -35,3 +35,12 @@ function if_not_empty_debug($if_expr, $v) abort($v); } } + + +function debug() +{ + foreach(func_get_args() as $arg) { + var_dump($arg); + } + exit; +} \ No newline at end of file