pull/1/head
韩天峰 8 months ago
parent 35e17771d7
commit a635ae4831
  1. 10
      examples/fib.php
  2. 25
      src/Php/Translator.php

@ -12,6 +12,16 @@ function main()
{ {
global $argv; global $argv;
$n = $argv[2]; $n = $argv[2];
var_dump($argv, $n);
if ($n > 100) {
echo "Too big number\n";
exit(1);
} elseif ($n < 1) {
echo "Too small number\n";
exit(1);
}
$begin = microtime(true); $begin = microtime(true);
echo fib($n) . "\n"; echo fib($n) . "\n";
echo "Time: " . (microtime(true) - $begin) . "\n"; echo "Time: " . (microtime(true) - $begin) . "\n";

@ -361,6 +361,8 @@ class Translator extends \PhpAot\Core\Translator
return $this->parseUnaryMinus($expr); return $this->parseUnaryMinus($expr);
case 'InterpolatedStringPart': case 'InterpolatedStringPart':
return $this->parseInterpolatedStringPart($expr); return $this->parseInterpolatedStringPart($expr);
case 'Expr_Exit':
return $this->parseExit($expr);
default: default:
debug($expr); debug($expr);
} }
@ -399,12 +401,7 @@ class Translator extends \PhpAot\Core\Translator
{ {
$type = $this->detectType($v->expr); $type = $this->detectType($v->expr);
$expr = $this->parseExpr($v->expr); $expr = $this->parseExpr($v->expr);
if ($this->returnType == self::TYPE_INT and $type == self::TYPE_VAR) { return 'return ' . $this->convertExprType($expr, $this->returnType, $type);
return 'return (' . $expr . ').toInt()';
} elseif ($this->returnType == self::TYPE_FLOAT and $type == self::TYPE_VAR) {
return 'return (' . $expr . ').toFloat()';
}
return 'return ' . $expr;
} }
private function parseBinaryOpMul(mixed $expr): string private function parseBinaryOpMul(mixed $expr): string
@ -1063,15 +1060,25 @@ class Translator extends \PhpAot\Core\Translator
{ {
$expr = $this->parseArg($arg); $expr = $this->parseArg($arg);
$type = $this->detectType($arg->value); $type = $this->detectType($arg->value);
if ($argInfo->type === self::TYPE_INT && $type !== self::TYPE_INT) { return $this->convertExprType($expr, $argInfo->type, $type);
}
private function convertExprType(string $expr, $leftType, $rightType): string
{
if ($leftType === self::TYPE_INT && $rightType !== self::TYPE_INT) {
return '(' . $expr . ').toInt()'; return '(' . $expr . ').toInt()';
} }
if ($argInfo->type === self::TYPE_FLOAT && $type !== self::TYPE_FLOAT) { if ($leftType === self::TYPE_FLOAT && $rightType !== self::TYPE_FLOAT) {
return '(' . $expr . ').toFloat()'; return '(' . $expr . ').toFloat()';
} }
if ($argInfo->type === self::TYPE_BOOL && $type !== self::TYPE_BOOL) { if ($leftType === self::TYPE_BOOL && $rightType !== self::TYPE_BOOL) {
return '(' . $expr . ').toBool()'; return '(' . $expr . ').toBool()';
} }
return $expr; return $expr;
} }
private function parseExit(Node $node): string
{
return 'php::exit(' . $this->parseIdentifier($node->expr) . ')';
}
} }

Loading…
Cancel
Save