From 257cc2d7969b5adfa92d8bc0627018b975ad0779 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Tue, 30 Dec 2025 12:50:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Php/Translator.php | 18 ++- tests/core/lang/constants/PHP_INT_32bit.phpt | 17 +++ tests/core/lang/constants/PHP_INT_64bit.phpt | 17 +++ tests/core/lang/include_files/echo.inc | 3 + tests/core/lang/include_files/eval.inc | 3 + tests/core/lang/include_files/function.inc | 3 + .../lang/integer_literals/binary_32bit.phpt | 82 +++++++++++ .../lang/integer_literals/binary_64bit.phpt | 82 +++++++++++ .../integer_literals/hexadecimal_32bit.phpt | 82 +++++++++++ .../integer_literals/hexadecimal_64bit.phpt | 83 +++++++++++ .../lang/integer_literals/octal_32bit.phpt | 118 ++++++++++++++++ .../lang/integer_literals/octal_64bit.phpt | 133 ++++++++++++++++++ tests/core/lang/string/unicode_escape.phpt | 19 +++ .../string/unicode_escape_incomplete.phpt | 11 ++ 14 files changed, 669 insertions(+), 2 deletions(-) create mode 100644 tests/core/lang/constants/PHP_INT_32bit.phpt create mode 100644 tests/core/lang/constants/PHP_INT_64bit.phpt create mode 100644 tests/core/lang/include_files/echo.inc create mode 100644 tests/core/lang/include_files/eval.inc create mode 100644 tests/core/lang/include_files/function.inc create mode 100644 tests/core/lang/integer_literals/binary_32bit.phpt create mode 100644 tests/core/lang/integer_literals/binary_64bit.phpt create mode 100644 tests/core/lang/integer_literals/hexadecimal_32bit.phpt create mode 100644 tests/core/lang/integer_literals/hexadecimal_64bit.phpt create mode 100644 tests/core/lang/integer_literals/octal_32bit.phpt create mode 100644 tests/core/lang/integer_literals/octal_64bit.phpt create mode 100644 tests/core/lang/string/unicode_escape.phpt create mode 100644 tests/core/lang/string/unicode_escape_incomplete.phpt diff --git a/src/Php/Translator.php b/src/Php/Translator.php index a69f516f..b0b238dd 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -21,6 +21,9 @@ class Translator extends \PhpAot\Core\Translator const string TYPE_ARRAY = 'php::Array'; const string TYPE_STR = 'php::Str'; + const string VALUE_NAN = 'std::numeric_limits::quiet_NaN()'; + const string VALUE_INF = 'std::numeric_limits::infinity()'; + private string $phpxDir = '~/workspace/projects/phpx'; protected string $lang = 'PHP'; private array $scope = []; @@ -231,9 +234,9 @@ class Translator extends \PhpAot\Core\Translator case 'Expr_Variable': return $expr->name; case 'Scalar_Int': - return $expr->value; + return $expr->value . 'L'; case 'Scalar_Float': - return $expr->getAttribute('rawValue'); + return $this->parseScalarFloat($expr); case 'Scalar_String': return '"' . $this->escapeString($expr->value) . '"'; case 'Expr_ConstFetch': @@ -1522,4 +1525,15 @@ class Translator extends \PhpAot\Core\Translator } return 'break;'; } + + private function parseScalarFloat(Node $expr): string + { + if (is_nan($expr->value)) { + return self::VALUE_NAN; + } elseif (is_infinite($expr->value)) { + return $expr->value > 0 ? self::VALUE_INF : '-' . self::VALUE_INF; + } else { + return number_format($expr->value, 1, '.', ''); + } + } } diff --git a/tests/core/lang/constants/PHP_INT_32bit.phpt b/tests/core/lang/constants/PHP_INT_32bit.phpt new file mode 100644 index 00000000..624ceb43 --- /dev/null +++ b/tests/core/lang/constants/PHP_INT_32bit.phpt @@ -0,0 +1,17 @@ +--TEST-- +Test PHP_INT_MIN, PHP_INT_MAX and PHP_INT_SIZE (32-bit) +--SKIPIF-- + +--FILE-- + +--EXPECT-- +int(-2147483648) +int(2147483647) +int(4) diff --git a/tests/core/lang/constants/PHP_INT_64bit.phpt b/tests/core/lang/constants/PHP_INT_64bit.phpt new file mode 100644 index 00000000..7756e59a --- /dev/null +++ b/tests/core/lang/constants/PHP_INT_64bit.phpt @@ -0,0 +1,17 @@ +--TEST-- +Test PHP_INT_MIN, PHP_INT_MAX and PHP_INT_SIZE (64-bit) +--SKIPIF-- + +--FILE-- + +--EXPECT-- +int(-9223372036854775808) +int(9223372036854775807) +int(8) diff --git a/tests/core/lang/include_files/echo.inc b/tests/core/lang/include_files/echo.inc new file mode 100644 index 00000000..64b30afe --- /dev/null +++ b/tests/core/lang/include_files/echo.inc @@ -0,0 +1,3 @@ + diff --git a/tests/core/lang/include_files/eval.inc b/tests/core/lang/include_files/eval.inc new file mode 100644 index 00000000..82c93cee --- /dev/null +++ b/tests/core/lang/include_files/eval.inc @@ -0,0 +1,3 @@ + diff --git a/tests/core/lang/include_files/function.inc b/tests/core/lang/include_files/function.inc new file mode 100644 index 00000000..528f46c5 --- /dev/null +++ b/tests/core/lang/include_files/function.inc @@ -0,0 +1,3 @@ + diff --git a/tests/core/lang/integer_literals/binary_32bit.phpt b/tests/core/lang/integer_literals/binary_32bit.phpt new file mode 100644 index 00000000..43189404 --- /dev/null +++ b/tests/core/lang/integer_literals/binary_32bit.phpt @@ -0,0 +1,82 @@ +--TEST-- +Binary integer strings (32bit) +--SKIPIF-- + +--FILE-- + +--EXPECT-- +int(2147483647) +int(2147483647) +float(1.9119287772983036E+25) +int(86) +int(86) +int(86) +int(86) +float(INF) +int(2147483647) +int(2147483647) +float(1.9119287772983036E+25) +int(86) +int(86) +int(86) +int(86) +float(INF) diff --git a/tests/core/lang/integer_literals/binary_64bit.phpt b/tests/core/lang/integer_literals/binary_64bit.phpt new file mode 100644 index 00000000..8d02c10d --- /dev/null +++ b/tests/core/lang/integer_literals/binary_64bit.phpt @@ -0,0 +1,82 @@ +--TEST-- +Binary integer strings (64bit) +--SKIPIF-- + +--FILE-- + +--EXPECT-- +int(9223372036854775807) +int(9223372036854775807) +float(1.9119287772983036E+25) +int(86) +int(86) +int(86) +int(86) +float(INF) +int(9223372036854775807) +int(9223372036854775807) +float(1.9119287772983036E+25) +int(86) +int(86) +int(86) +int(86) +float(INF) diff --git a/tests/core/lang/integer_literals/hexadecimal_32bit.phpt b/tests/core/lang/integer_literals/hexadecimal_32bit.phpt new file mode 100644 index 00000000..1793354a --- /dev/null +++ b/tests/core/lang/integer_literals/hexadecimal_32bit.phpt @@ -0,0 +1,82 @@ +--TEST-- +Hexadecimal integer strings (32bit) +--SKIPIF-- + +--FILE-- + +--EXPECT-- +int(2147483647) +int(2147483647) +float(8.070441274821732E+19) +int(28) +int(28) +int(28) +int(28) +float(INF) +int(2147483647) +int(2147483647) +float(8.070441274821732E+19) +int(28) +int(28) +int(28) +int(28) +float(INF) diff --git a/tests/core/lang/integer_literals/hexadecimal_64bit.phpt b/tests/core/lang/integer_literals/hexadecimal_64bit.phpt new file mode 100644 index 00000000..1cbf37fb --- /dev/null +++ b/tests/core/lang/integer_literals/hexadecimal_64bit.phpt @@ -0,0 +1,83 @@ +--TEST-- +Hexadecimal integer strings (64bit) +--SKIPIF-- + +--FILE-- + +--EXPECT-- +int(9223372036854775807) +int(9223372036854775807) +float(8.070441274821732E+19) +int(28) +int(28) +int(28) +int(28) +float(INF) +int(9223372036854775807) +int(9223372036854775807) +float(8.070441274821732E+19) +int(28) +int(28) +int(28) +int(28) +float(INF) diff --git a/tests/core/lang/integer_literals/octal_32bit.phpt b/tests/core/lang/integer_literals/octal_32bit.phpt new file mode 100644 index 00000000..472fe602 --- /dev/null +++ b/tests/core/lang/integer_literals/octal_32bit.phpt @@ -0,0 +1,118 @@ +--TEST-- +Octal integer strings (32bit) +--SKIPIF-- + +--FILE-- + +--EXPECT-- +int(2147483647) +int(2147483647) +float(1.7912166229916324E+23) +int(14) +int(14) +int(14) +int(14) +float(INF) +int(2147483647) +int(2147483647) +float(1.7912166229916324E+23) +int(14) +int(14) +int(14) +int(14) +float(INF) +int(2147483647) +int(2147483647) +float(1.7912166229916324E+23) +int(14) +int(14) +int(14) +int(14) +float(INF) diff --git a/tests/core/lang/integer_literals/octal_64bit.phpt b/tests/core/lang/integer_literals/octal_64bit.phpt new file mode 100644 index 00000000..0cdba743 --- /dev/null +++ b/tests/core/lang/integer_literals/octal_64bit.phpt @@ -0,0 +1,133 @@ +--TEST-- +Octal integer strings (64bit) +--SKIPIF-- + +--FILE-- + +--EXPECT-- +int(9223372036854775807) +int(9223372036854775807) +float(9.223372036854776E+18) +float(1.7912166229916324E+23) +int(14) +int(14) +int(14) +int(14) +float(INF) +int(9223372036854775807) +int(9223372036854775807) +float(9.223372036854776E+18) +float(1.7912166229916324E+23) +int(14) +int(14) +int(14) +int(14) +float(INF) +int(9223372036854775807) +int(9223372036854775807) +float(9.223372036854776E+18) +float(1.7912166229916324E+23) +int(14) +int(14) +int(14) +int(14) +float(INF) diff --git a/tests/core/lang/string/unicode_escape.phpt b/tests/core/lang/string/unicode_escape.phpt new file mode 100644 index 00000000..e6e63ea0 --- /dev/null +++ b/tests/core/lang/string/unicode_escape.phpt @@ -0,0 +1,19 @@ +--TEST-- +Valid Unicode escape sequences +--FILE-- + +--EXPECT-- +string(1) "a" +string(2) "ΓΏ" +string(2) "ΓΏ" +string(3) "β˜ƒ" +string(4) "πŸ˜‚" +string(4) "πŸ˜‚" diff --git a/tests/core/lang/string/unicode_escape_incomplete.phpt b/tests/core/lang/string/unicode_escape_incomplete.phpt new file mode 100644 index 00000000..b6d25b6f --- /dev/null +++ b/tests/core/lang/string/unicode_escape_incomplete.phpt @@ -0,0 +1,11 @@ +--TEST-- +Invalid Unicode escape sequence: Incomplete +--SKIPIF-- + +--FILE-- + +--EXPECTF-- +Parse error: Invalid UTF-8 codepoint escape sequence in %s on line %d