feat(compiler): 支持字符串到大数类型的转换

- 添加字符串到Decimal类型的转换逻辑
- 添加字符串到BigInt类型的转换支持
- 添加字符串到BigFloat类型的转换支持
- 为字符串字面量提供直接转换优化
- 添加测试用例验证链式调用场景
pull/1/head
韩天峰 3 months ago
parent 52fcc86660
commit 1bb0324605
  1. 12
      src/Php/CompilerBase.php
  2. 13
      tests/aot/var_convert/003.phpt

@ -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) . '))';
}

@ -0,0 +1,13 @@
--TEST--
var convert chained on array element
--FILE--
<?php
function main()
{
$v = '0.1'->toDecimal();
$r = $v + '0.2';
echo $r;
}
?>
--EXPECT--
0.3
Loading…
Cancel
Save