fix(std): 修复标准数组布尔值比较问题

- 添加了测试用例验证浮点数和布尔数组的功能
- 实现了标量布尔值检测方法 isScalarBool
- 添加了布尔值获取方法 getBoolValue
- 定义了 VALUE_FALSE 和 VALUE_TRUE 常量
- 实现了 parseCompareExpr 方法处理布尔值比较
- 修改相等和不等操作符解析使用新的比较表达式解析
- 重构了相同操作符解析逻辑并移除了重复代码
pull/1/head
韩天峰 4 months ago
parent 3f9f87ba7f
commit 4470c4597d
  1. 10
      src/Php/AstNodeType.php
  2. 37
      src/Php/CompilerBase.php
  3. 34
      tests/aot/std-array/004.phpt

@ -100,6 +100,16 @@ trait AstNodeType
return $expr instanceof Node\Scalar\Int_; 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 protected function isMatchExpr(NodeAbstract $expr): bool
{ {
return $expr instanceof Expr\Match_; return $expr instanceof Expr\Match_;

@ -76,6 +76,8 @@ class CompilerBase extends \PhpAot\Core\Translator
public const string VALUE_INF = 'std::numeric_limits<double>::infinity()'; public const string VALUE_INF = 'std::numeric_limits<double>::infinity()';
public const string VALUE_NULL = 'php::null'; public const string VALUE_NULL = 'php::null';
public const string VALUE_ZERO = 'php::zero'; 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 LITERAL_STRINGS = '_literal_strings';
public const string ANON_CLASS = '_anon_class_'; public const string ANON_CLASS = '_anon_class_';
public const string STATIC_VAR = '_static_var_'; public const string STATIC_VAR = '_static_var_';
@ -3659,14 +3661,33 @@ class CompilerBase extends \PhpAot\Core\Translator
return $code . PHP_EOL; 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 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 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; 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 protected function parseBinaryOpSpaceship(Expr\BinaryOp\Spaceship $expr): string
{ {
$left = $this->parseIdentifier($expr->left); $left = $this->parseIdentifier($expr->left);

@ -0,0 +1,34 @@
--TEST--
std array: 004
--FILE--
<?php
function main() {
$array = std::array(native_types::type_float, 100);
$array[99] = 2026.22;
$array[33] = 3.1415;
var_dump($array[99] == 2026.22);
var_dump($array[33] == 3.1415);
var_dump($array[11] == 0);
$array2 = std::array(native_types::type_bool, 100);
$array2[99] = 2026.22;
$array2[33] = "";
var_dump($array2[99] === true);
var_dump($array2[33] === false);
var_dump($array2[11] === false);
var_dump($array2[99] == true);
var_dump($array2[33] == false);
var_dump($array2[11] == false);
}
?>
--EXPECT--
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
Loading…
Cancel
Save