feat(compiler): 添加对变量表达式检查和无变量捕获的支持

- 在方法调用解析中添加了变量表达式检查功能
- 实现了未定义变量错误提示机制
- 更新异常捕获解析以支持无变量名的捕获语法
- 添加了临时变量名生成器来处理匿名捕获变量
- 新增 try-catch-2 测试用例验证捕获语法兼容性
pull/1/head
韩天峰 6 months ago
parent 3641cb3184
commit b6e2741c31
  1. 1
      .gitignore
  2. 8
      src/Php/CompilerBase.php
  3. 14
      tests/aot/try-catch-2.phpt

1
.gitignore vendored

@ -7,5 +7,6 @@
/vendor
/tmp
/projects/wordpress
/projects/workerman
/.php-cs-fixer.cache
*.o

@ -3189,6 +3189,10 @@ class CompilerBase extends \PhpAot\Core\Translator
protected function parseMethodCall(Node\Expr\MethodCall $expr): string
{
if ($this->isVarExpr($expr->var)) {
$this->errorUndefinedVariable($expr->var);
}
$object = $this->convertToObject($expr->var);
if ($this->isTypedObject($object)) {
$class = $this->getObjectType($object);
@ -3451,10 +3455,10 @@ class CompilerBase extends \PhpAot\Core\Translator
return $code;
}
protected function parseCatch(mixed $catch, string $exVar): string
protected function parseCatch(Node\Stmt\Catch_ $catch, string $exVar): string
{
$types = $catch->types;
$var = $this->parseIdentifier($catch->var);
$var = $catch->var ? $this->parseIdentifier($catch->var) : $this->genTmpVarName();
if (!$this->hasVar($var)) {
$this->addLocalVar($var, self::TYPE_OBJECT);
}

@ -0,0 +1,14 @@
--TEST--
try catch 2
--FILE--
<?php
function main() {
try {
throw new Exception('test error');
} catch (Exception) {
echo "Caught exception\n";
}
}
?>
--EXPECT--
Caught exception
Loading…
Cancel
Save