diff --git a/bin/gen_stub.php b/bin/gen_stub.php index 7ddaeae7..dd7d7a08 100755 --- a/bin/gen_stub.php +++ b/bin/gen_stub.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", diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index ae5820d5..8f300203 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -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');