From 8312de6c73e6d0771ce2830f2a396db3381e9f5c Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Tue, 7 Apr 2026 11:37:38 +0800 Subject: [PATCH] =?UTF-8?q?feat(compiler):=20=E6=B7=BB=E5=8A=A0=E5=8E=9F?= =?UTF-8?q?=E7=94=9F=E7=B1=BB=E5=9E=8B=E6=94=AF=E6=8C=81=E5=92=8C=E6=A0=87?= =?UTF-8?q?=E5=87=86=E5=BA=93=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 引入 native_types 类常量定义基本类型 - 添加 std 类提供 int、float、bool 类型转换方法 - 实现 objval 函数进行对象类型验证 - 在 CompilerBase 中添加 nativeTypes 属性控制类型行为 - 修改 getNativeType 方法使用 nativeTypes 判断 - 更新 use 语句解析支持 native_types 导入 - 在 micro_bench 示例中启用严格类型声明 - 添加 str_byte 示例文件测试字符串字节操作 --- examples/assign_cmp.php | 24 ----------------------- examples/micro_bench.php | 15 +++++++++++---- examples/str_byte.php | 7 +++++++ src/Php/CompilerBase.php | 41 +++++++++++++++++++++------------------- src/functions.php | 13 +++++++++---- src/polyfills.php | 40 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 89 insertions(+), 51 deletions(-) create mode 100644 examples/str_byte.php create mode 100644 src/polyfills.php diff --git a/examples/assign_cmp.php b/examples/assign_cmp.php index 97e9e4e6..573b4cc9 100644 --- a/examples/assign_cmp.php +++ b/examples/assign_cmp.php @@ -1,28 +1,4 @@ indentLevel = 0; - $this->strictTypes = false; - $this->classesDefineInFile = []; + $this->indentLevel = 0; + $this->strictTypes = false; + $this->nativeTypes = false; + $this->classesDefineInFile = []; $this->interfacesDefineInFile = []; - $this->functionDefineInFile = []; + $this->functionDefineInFile = []; } protected function resetNamespace(): void @@ -1307,7 +1308,7 @@ class CompilerBase extends \PhpAot\Core\Translator return $this->parseAssignFinally($left, $right); } - protected function parseAssignFinally(Node\Expr $left, Node\Expr $right): string + protected function parseAssignFinally(Expr $left, Expr $right): string { if ($left instanceof Expr\List_) { $items = $left->items; @@ -3392,7 +3393,7 @@ class CompilerBase extends \PhpAot\Core\Translator */ protected function getNativeType(string $type): string { - return $this->defaultNativeType ? $type : self::TYPE_VAR; + return $this->nativeTypes ? $type : self::TYPE_VAR; } protected function detectConstType($expr): string @@ -4018,11 +4019,6 @@ class CompilerBase extends \PhpAot\Core\Translator /** * @param NodeAbstract $expr 仅用于输出错误日志 - * @param string $property - * @param string $class - * @param string $namespace - * @param bool $static - * @return string|null */ protected function findNativeProperty(NodeAbstract $expr, string $property, string $class, string $namespace = '', bool $static = false): ?string { @@ -4320,7 +4316,7 @@ class CompilerBase extends \PhpAot\Core\Translator { $declares = $v->declares; foreach ($declares as $declare) { - $key = $this->parseIdentifier($declare->key); + $key = $this->parseIdentifier($declare->key); $value = $this->parseIdentifier($declare->value); if ($key === 'ticks') { $this->fatalError($v, 'declare(ticks=1) is not supported'); @@ -4328,8 +4324,11 @@ class CompilerBase extends \PhpAot\Core\Translator if (strtolower($value) !== 'utf-8') { $this->fatalError($v, 'declare(encoding="' . $value . '") is not supported, only UTF-8 is supported'); } + } elseif ($key === 'strict_types') { + $this->strictTypes = boolval(intval($value)); + } else { + $this->fatalError($v, 'declare(' . $key . '=' . $value . ') is not supported'); } - $this->strictTypes = boolval(intval($value)); } } @@ -4340,14 +4339,18 @@ class CompilerBase extends \PhpAot\Core\Translator $id = $this->parseIdentifier($use->name); if ($use->type === Node\Stmt\Use_::TYPE_FUNCTION) { $rpos = strrpos($id, '\\'); - $fn = substr($id, $rpos + 1); - $ns = substr($id, 0, $rpos); + $fn = substr($id, $rpos + 1); + $ns = substr($id, 0, $rpos); // fn => namespace $this->useFunctions[$fn] = $ns; } else { - $this->useNamespaces[] = $id; - if ($use->alias) { - $this->useAliases[$use->alias->toString()] = $id; + if ($id === 'native_types') { + $this->nativeTypes = true; + } else { + $this->useNamespaces[] = $id; + if ($use->alias) { + $this->useAliases[$use->alias->toString()] = $id; + } } } } diff --git a/src/functions.php b/src/functions.php index 7d2f371d..518db764 100644 --- a/src/functions.php +++ b/src/functions.php @@ -1,11 +1,17 @@