pull/1/head
韩天峰 8 months ago
parent 348641cf01
commit 013addcafd
  1. 5
      bin/compiler.php
  2. 9
      examples/fannkuchredux.php
  3. 47
      src/Php/Translator.php
  4. 9
      src/functions.php

@ -8,7 +8,6 @@ if (empty($argv[1])) {
die("php compiler.php [file]\n");
}
try {
$file = $argv[1];
$translator = new Translator(ROOT_PATH);
$translator->setIndent(' ');
@ -20,7 +19,3 @@ try {
$objectFile = './tmp/' . $info['filename'] . '.cc.o';
$translator->compileBinary($info['filename'], $objectFile);
} catch (Error $error) {
echo "Parse error: {$error->getMessage()}\n";
return;
}

@ -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;

@ -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);
}
}

@ -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;
}
Loading…
Cancel
Save