From 1bb03246054ca6e43cfc78bda91f0da1ca25c0d0 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Mon, 1 Jun 2026 20:13:04 +0800 Subject: [PATCH] =?UTF-8?q?feat(compiler):=20=E6=94=AF=E6=8C=81=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2=E5=88=B0=E5=A4=A7=E6=95=B0=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E7=9A=84=E8=BD=AC=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加字符串到Decimal类型的转换逻辑 - 添加字符串到BigInt类型的转换支持 - 添加字符串到BigFloat类型的转换支持 - 为字符串字面量提供直接转换优化 - 添加测试用例验证链式调用场景 --- src/Php/CompilerBase.php | 12 ++++++++++++ tests/aot/var_convert/003.phpt | 13 +++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 tests/aot/var_convert/003.phpt diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index da9c7d26..9aed496f 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -4120,6 +4120,12 @@ class CompilerBase extends \PhpAot\Core\Translator } $this->fatalError($node, 'Cannot convert float expression to Decimal, use a literal value or string instead'); } + if ($fromType === self::TYPE_STR) { + if ($node instanceof Node\Scalar\String_) { + return 'php::newDecimal(' . $this->getLiteralString($node->value) . ')'; + } + return 'php::newDecimal(php::toString(' . $this->trimBrackets($expr) . '))'; + } if ($fromType === self::TYPE_INT) { return 'php::newDecimal(php::toString(' . $this->trimBrackets($expr) . '))'; } @@ -4137,6 +4143,9 @@ class CompilerBase extends \PhpAot\Core\Translator if ($fromType === self::TYPE_FLOAT) { $this->fatalError(null, 'Cannot convert float to BigInt, use string or int instead'); } + if ($fromType === self::TYPE_STR) { + return 'php::newBigInt(php::toString(' . $this->trimBrackets($expr) . '))'; + } return $expr; } @@ -4148,6 +4157,9 @@ class CompilerBase extends \PhpAot\Core\Translator if ($fromType === self::TYPE_FLOAT) { return 'php::newBigFloat(' . $this->trimBrackets($expr) . ')'; } + if ($fromType === self::TYPE_STR) { + return 'php::newBigFloat(php::toString(' . $this->trimBrackets($expr) . '))'; + } if ($fromType === self::TYPE_BIGINT) { return 'php::BigFloat::newInstance(php::BigInt::toString(' . $this->trimBrackets($expr) . '))'; } diff --git a/tests/aot/var_convert/003.phpt b/tests/aot/var_convert/003.phpt new file mode 100644 index 00000000..73507e93 --- /dev/null +++ b/tests/aot/var_convert/003.phpt @@ -0,0 +1,13 @@ +--TEST-- +var convert chained on array element +--FILE-- +toDecimal(); + $r = $v + '0.2'; + echo $r; +} +?> +--EXPECT-- +0.3