From 4470c4597d013596dd32e6fe82c57b5a652e5046 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Thu, 7 May 2026 15:26:08 +0800 Subject: [PATCH] =?UTF-8?q?fix(std):=20=E4=BF=AE=E5=A4=8D=E6=A0=87?= =?UTF-8?q?=E5=87=86=E6=95=B0=E7=BB=84=E5=B8=83=E5=B0=94=E5=80=BC=E6=AF=94?= =?UTF-8?q?=E8=BE=83=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加了测试用例验证浮点数和布尔数组的功能 - 实现了标量布尔值检测方法 isScalarBool - 添加了布尔值获取方法 getBoolValue - 定义了 VALUE_FALSE 和 VALUE_TRUE 常量 - 实现了 parseCompareExpr 方法处理布尔值比较 - 修改相等和不等操作符解析使用新的比较表达式解析 - 重构了相同操作符解析逻辑并移除了重复代码 --- src/Php/AstNodeType.php | 10 ++++++++++ src/Php/CompilerBase.php | 37 ++++++++++++++++++++++-------------- tests/aot/std-array/004.phpt | 34 +++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 14 deletions(-) create mode 100644 tests/aot/std-array/004.phpt diff --git a/src/Php/AstNodeType.php b/src/Php/AstNodeType.php index f93fe4a9..75d7b1eb 100644 --- a/src/Php/AstNodeType.php +++ b/src/Php/AstNodeType.php @@ -100,6 +100,16 @@ trait AstNodeType return $expr instanceof Node\Scalar\Int_; } + protected function isScalarBool(NodeAbstract $expr): bool + { + return $expr instanceof Node\Expr\ConstFetch and in_array($expr->name->toString(), ['true', 'false']); + } + + protected function getBoolValue(Node\Expr\ConstFetch $expr): string + { + return $expr->name->toString() === 'true' ? self::VALUE_TRUE : self::VALUE_FALSE; + } + protected function isMatchExpr(NodeAbstract $expr): bool { return $expr instanceof Expr\Match_; diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index dd2b6a67..1137eaaa 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -76,6 +76,8 @@ class CompilerBase extends \PhpAot\Core\Translator public const string VALUE_INF = 'std::numeric_limits::infinity()'; public const string VALUE_NULL = 'php::null'; public const string VALUE_ZERO = 'php::zero'; + public const string VALUE_FALSE = 'php::false_value'; + public const string VALUE_TRUE = 'php::true_value'; public const string LITERAL_STRINGS = '_literal_strings'; public const string ANON_CLASS = '_anon_class_'; public const string STATIC_VAR = '_static_var_'; @@ -3659,14 +3661,33 @@ class CompilerBase extends \PhpAot\Core\Translator return $code . PHP_EOL; } + protected function parseCompareExpr(NodeAbstract $expr): string + { + // PHPX 与 bool 值比较会出现重载错误,所以需要转换成 bool 值 + if ($this->isScalarBool($expr)) { + return $this->getBoolValue($expr); + } + return $this->parseIdentifier($expr); + } + protected function parseBinaryOpEqual(Expr\BinaryOp\Equal $expr): string { - return 'php::equals(' . $this->parseExpr($expr->left) . ', ' . $this->parseExpr($expr->right) . ')'; + return 'php::equals(' . $this->parseCompareExpr($expr->left) . ', ' . $this->parseCompareExpr($expr->right) . ')'; } protected function parseBinaryOpNotEqual(Expr\BinaryOp\NotEqual $expr): string { - return '!php::equals(' . $this->parseExpr($expr->left) . ', ' . $this->parseExpr($expr->right) . ')'; + return '!php::equals(' . $this->parseCompareExpr($expr->left) . ', ' . $this->parseCompareExpr($expr->right) . ')'; + } + + protected function parseBinaryOpIdentical(Expr\BinaryOp $expr): string + { + $left = $this->parseCompareExpr($expr->left); + $right = $this->parseCompareExpr($expr->right); + if ($right === 'nullptr') { + return $left . '.isNull()'; + } + return 'php::same(' . $left . ', ' . $right . ')'; } /** @@ -3793,18 +3814,6 @@ class CompilerBase extends \PhpAot\Core\Translator return $code; } - protected function parseBinaryOpIdentical(Expr\BinaryOp $expr): string - { - $left = $this->parseIdentifier($expr->left); - $right = $this->parseIdentifier($expr->right); - - if ($right === 'nullptr') { - return $left . '.isNull()'; - } - - return 'php::same(' . $left . ', ' . $right . ')'; - } - protected function parseBinaryOpSpaceship(Expr\BinaryOp\Spaceship $expr): string { $left = $this->parseIdentifier($expr->left); diff --git a/tests/aot/std-array/004.phpt b/tests/aot/std-array/004.phpt new file mode 100644 index 00000000..1f50f3a9 --- /dev/null +++ b/tests/aot/std-array/004.phpt @@ -0,0 +1,34 @@ +--TEST-- +std array: 004 +--FILE-- + +--EXPECT-- +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) \ No newline at end of file