refactor(php): 优化编译器基础类中的变量赋值和数组维度获取逻辑

- 简化 Expr_Assign 和 Expr_AssignRef 的返回逻辑
- 在 checkVarFn 回调函数中添加对 isVarExpr 的检查以确保变量存在
- 移除 parseAssign 函数中关于数组维度获取和静态属性获取的特殊处理逻辑
- 在 parseArrayDimFetch 方法中添加对未定义变量的错误检查
- 添加对全局常量的处理支持
- 更新主函数中的全局变量声明以包含 definedConstants
- 在启动时初始化 definedConstants 变量
pull/1/head
韩天峰 7 months ago
parent b57ff37b18
commit 625604d375
  1. 16
      bin/gen_stub.php
  2. 34
      src/Php/CompilerBase.php

@ -2329,6 +2329,18 @@ class EvaluatedValue
return null;
}
global $definedConstants;
if (isset($definedConstants[$constName])) {
$constValue = $definedConstants[$constName];
if (is_scalar($constValue)) {
if (is_string($constValue)) {
return '"' . $constValue . '"';
} else {
return $constValue;
}
}
}
throw new Exception("Constant " . $constName . " cannot be found");
}
);
@ -6098,7 +6110,7 @@ function initPhpParser() {
function main()
{
global $argv, $argc, $translator;
global $argv, $argc, $translator, $definedConstants;
error_reporting(E_ALL & ~E_DEPRECATED);
ini_set("precision", "-1");
@ -6108,6 +6120,8 @@ function main()
$translator->setIndent("\t");
$translator->setIndentLevel(1);
$definedConstants = get_defined_constants();
$opt_index = 0;
$options = getopt(
"fh",

@ -249,13 +249,9 @@ class CompilerBase extends \PhpAot\Core\Translator
case 'Expr_Empty':
return $this->parseEmpty($expr);
case 'Expr_Assign':
$result = $this->parseAssign($expr);
return $result;
return $this->parseAssign($expr);
case 'Expr_AssignRef':
$result = $this->parseAssignRef($expr);
return $result;
return $this->parseAssignRef($expr);
case 'Expr_Print':
return $this->parsePrint($expr);
case 'Expr_BinaryOp_Equal':
@ -960,8 +956,10 @@ class CompilerBase extends \PhpAot\Core\Translator
$checkVarFn = function ($var) {
if ($this->isArrayDimFetch($var)) {
$array = $this->parseIdentifier($var->var);
if (!$this->hasVar($array)) {
$this->addLocalVar($array, self::TYPE_ARRAY);
if ($this->isVarExpr($var->var)) {
if (!$this->hasVar($array)) {
$this->addLocalVar($array, self::TYPE_ARRAY);
}
}
}
};
@ -1012,12 +1010,6 @@ class CompilerBase extends \PhpAot\Core\Translator
if ($right->getType() === 'Expr_Assign') {
return $this->parseRightAssociativeAssign($left, $right);
}
if ($this->isArrayDimFetch($left)) {
return $this->parseAssignArrayDim($left, $right);
}
if ($this->isStaticPropertyFetch($left)) {
return $this->parseAssignStaticProperty($left, $right);
}
return $this->parseFinallyAssign($left, $right);
}
@ -1683,12 +1675,18 @@ class CompilerBase extends \PhpAot\Core\Translator
protected function parseArrayDimFetch(Node\Expr\ArrayDimFetch $node, bool $write): string
{
$var = $this->parseIdentifier($node->var);
if ($this->isVarExpr($node->var) and $var === 'GLOBALS') {
if ($node->dim === null) {
$this->fatalError($node, 'Cannot use [] for GLOBALS');
if ($this->isVarExpr($node->var)) {
if (!$this->hasVar($var)) {
$this->fatalError($node->var, "The variable `{$node->var->name}` is undefined");
}
if ($var === 'GLOBALS') {
if ($node->dim === null) {
$this->fatalError($node, 'Cannot use [] for GLOBALS');
}
return 'php::global(' . $this->parseIdentifier($node->dim) . ')';
}
return 'php::global(' . $this->parseIdentifier($node->dim) . ')';
}
if ($node->dim === null) {
if (!$write) {
$this->fatalError($node, 'Cannot use [] for reading');

Loading…
Cancel
Save