From 673f3b3dc5ae929d8ed7e7ed95cb19210d6ae40e Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Wed, 6 May 2026 15:02:31 +0800 Subject: [PATCH] =?UTF-8?q?refactor(preprocessor):=20=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E5=A3=B0=E6=98=8E=E7=94=9F=E6=88=90=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加 genArgumentDeclaration 方法统一处理参数声明格式 - 将参数声明逻辑从多个位置提取到独立方法中 - 简化可变参数和普通参数的声明处理流程 - 更新版本号从 1002 到 103 - 删除废弃的示例文件 test1.cc - 添加混合类型声明的测试用例 --- examples/bench/test1.cc | 238 ----------------------------------- src/Php/Preprocessor.php | 15 ++- src/Php/Translator.php | 2 +- tests/aot/type_decl/009.phpt | 23 ++++ version.txt | 2 +- 5 files changed, 35 insertions(+), 245 deletions(-) delete mode 100644 examples/bench/test1.cc create mode 100644 tests/aot/type_decl/009.phpt diff --git a/examples/bench/test1.cc b/examples/bench/test1.cc deleted file mode 100644 index 531c4895..00000000 --- a/examples/bench/test1.cc +++ /dev/null @@ -1,238 +0,0 @@ -#include -#include -#include -#include - -extern php::Var _GET; -extern php::Var _POST; -extern php::Var _COOKIE; -extern php::Var _SERVER; -extern php::Var _FILES; -extern php::Var _SESSION; -extern php::Var _REQUEST; -extern php::Var GLOBALS; -extern php::Var argc; -extern php::Var argv; -extern php::Var _literal_strings[0]; - -php::Bool php_write_mandelbrot_to_stream(php::Var w, php::Var h, php::Var stream, php::Var bitmap) { - php::Int bit_num; - php::Int byte_acc; - php::Int iter; - php::Float yfac; - php::Float xfac; - php::Str ochars; - php::Str pack_format; - php::Int y; - php::Float Ci; - php::Int x; - php::Float Zr; - php::Float Zi; - php::Float Tr; - php::Float Ti; - php::Float Cr; - php::Int i; - php::Object tmp_var_0; - php::Object e; - - // Stmt_If [15:16] - if (bitmap) { - // Stmt_Expression [16:16] - php::call(php::fprintf, {stream, "P4\n%d %d\n", w, h}); - } - - // Stmt_Expression [18:18] - bit_num = php::toInt(128L); - // Stmt_Expression [19:19] - byte_acc = php::toInt(0L); - // Stmt_Expression [20:20] - iter = php::toInt(50L); - // Stmt_Expression [22:22] - yfac = php::to_float(2.0 / (h)); - // Stmt_Expression [23:23] - xfac = php::to_float(2.0 / (w)); - // Stmt_Expression [25:25] - ochars = " .:-;!/>)|&IH%*#"; - // Stmt_Expression [26:26] - pack_format = "c*"; - // Stmt_For [28:78] - y = php::toInt(0L); - for (; php::to_bool((y) < (php::to_int(h))); ++y) { - // Stmt_Expression [29:29] - Ci = php::to_float((((php::to_float(y)) * (yfac))) - (php::to_float(1.0))); - // Stmt_For [30:68] - x = php::toInt(0L); - for (; php::to_bool((x) < (php::to_int(w))); ++x) { - // Stmt_Expression [31:31] - Zr = php::to_float(0.0); - // Stmt_Expression [32:32] - Zi = php::to_float(0.0); - // Stmt_Expression [33:33] - Tr = php::to_float(0.0); - // Stmt_Expression [34:34] - Ti = php::to_float(0.0); - // Stmt_Expression [35:35] - Cr = php::to_float((((php::to_float(x)) * (xfac))) - (php::to_float(1.5))); - // Stmt_TryCatch [37:51] - zend_try { - // Stmt_Do [38:49] - do { - // Stmt_For [39:47] - i = php::toInt(0L); - for (; php::to_bool((i) < (php::toInt(iter))); ++i) { - // Stmt_Expression [40:40] - Zi = php::to_float((((((2.0) * (php::to_float(Zr)))) * (php::to_float(Zi)))) + - (php::to_float(Ci))); - // Stmt_Expression [41:41] - Zr = php::to_float((((Tr) - (php::to_float(Ti)))) + (php::to_float(Cr))); - // Stmt_Expression [42:42] - Tr = php::to_float((Zr) * (php::to_float(Zr))); - // Stmt_If [44:46] - if (php::to_bool((((Tr) + (php::to_float(Ti = php::to_float((Zi) * (php::to_float(Zi))))))) > - (php::to_float(4.0)))) { - // Stmt_Expression [45:45] - php::throwException(php::newObject("Exception", "break 2")); - } - } - - // Stmt_Expression [48:48] - byte_acc += php::toInt(bit_num); - } while (false); - } - zend_catch { - tmp_var_0 = php::catchException(); - e = tmp_var_0; - if (e && php::instanceOf(e, "Exception")) { - tmp_var_0.unset(); - } - } - zend_end_try(); - if (tmp_var_0) { - php::throwException(tmp_var_0); - } - // Stmt_If [53:67] - if (bitmap) { - // Stmt_If [54:60] - if (php::same(bit_num, 1L)) { - // Stmt_Expression [55:55] - php::call(php::fwrite, {stream, php::call(php::pack, {pack_format, byte_acc})}); - // Stmt_Expression [56:56] - bit_num = php::toInt(128L); - // Stmt_Expression [57:57] - byte_acc = php::toInt(0L); - } else { - // Stmt_Expression [59:59] - bit_num >>= 1L; - } - - } else { - // Stmt_If [62:66] - if (php::equals(i, iter)) { - // Stmt_Expression [63:63] - php::call(php::fwrite, {stream, ochars.offsetGet(0L)}); - } else { - // Stmt_Expression [65:65] - php::call(php::fwrite, - {stream, ochars.offsetGet((((i) + (php::toInt(1L)))) & (php::toInt(15L)))}); - } - } - } - - // Stmt_If [69:77] - if (bitmap) { - // Stmt_If [70:74] - if (!(php::same(bit_num, 128L))) { - // Stmt_Expression [71:71] - php::call(php::fwrite, {stream, php::call(php::pack, {pack_format, byte_acc})}); - // Stmt_Expression [72:72] - bit_num = php::toInt(128L); - // Stmt_Expression [73:73] - byte_acc = php::toInt(0L); - } - - } else { - // Stmt_Expression [76:76] - php::call(php::fwrite, {stream, "\n"}); - } - } - - // Stmt_Return [79:79] - return php::to_bool(true); -} - -php::Bool php_treffynnon_mandelbrot_to_file(php::Var filename, php::Var w, php::Var h, php::Var binary_output) { - php::Str file_open_type; - php::Var stream; - - // Stmt_Expression [84:84] - file_open_type = "w"; - // Stmt_If [85:86] - if (binary_output) { - // Stmt_Expression [86:86] - file_open_type = "wb"; - } - - // Stmt_Expression [88:88] - stream = php::call(php::fopen, {php::to_string(filename), file_open_type}); - // Stmt_If [89:90] - if (php::same(false, stream)) { - // Stmt_Return [90:90] - return php::to_bool(false); - } - - // Stmt_Expression [92:92] - php_write_mandelbrot_to_stream(php::to_int(w), php::to_int(h), stream, php::to_bool(binary_output)); - // Stmt_Expression [93:93] - php::call(php::fclose, {stream}); - // Stmt_Return [94:94] - return php::to_bool(true); -} - -php::Str php_treffynnon_mandelbrot_to_mem(php::Var w, php::Var h, php::Var binary_output) { - php::Str file_open_type; - php::Var stream; - php::Var ret; - - // Stmt_Expression [99:99] - file_open_type = "w+"; - // Stmt_If [100:101] - if (binary_output) { - // Stmt_Expression [101:101] - file_open_type = "w+b"; - } - - // Stmt_Expression [103:103] - stream = php::call(php::fopen, {"php://memory", file_open_type}); - // Stmt_If [104:105] - if (php::same(false, stream)) { - // Stmt_Return [105:105] - return ""; - } - - // Stmt_Expression [107:107] - php_write_mandelbrot_to_stream(php::to_int(w), php::to_int(h), stream, php::to_bool(binary_output)); - // Stmt_Expression [108:108] - php::call(php::rewind, {stream}); - // Stmt_Expression [109:109] - ret = php::call(php::stream_get_contents, {stream}); - // Stmt_Expression [110:110] - php::call(php::fclose, {stream}); - // Stmt_Return [111:111] - return ret; -} - -void php_main() { - // Stmt_Global [116:116] - - // Stmt_If [117:120] - if (php::to_bool((php::len(argv)) < (php::toInt(3L)))) { - // Stmt_Echo [118:118] - php::echo("Usage: php mandelbrot.php []\n"); - // Stmt_Expression [119:119] - php::exit(1L); - } - - // Stmt_Echo [121:121] - php::echo(php_treffynnon_mandelbrot_to_mem( - php::to_int(argv.offsetGet(2L)), php::to_int(argv.offsetGet(2L)), php::to_bool(false))); -} diff --git a/src/Php/Preprocessor.php b/src/Php/Preprocessor.php index e25617e7..ce720228 100644 --- a/src/Php/Preprocessor.php +++ b/src/Php/Preprocessor.php @@ -63,6 +63,11 @@ class Preprocessor extends CompilerBase $this->climate->lightBlue('prepare completed: ' . count($list) . ' source files in total'); } + protected function genArgumentDeclaration(ArgInfo $argInfo): string + { + return $argInfo->type . ' ' . $argInfo->name; + } + public function getCppFile(string $file): string { $info = pathinfo($file); @@ -279,11 +284,6 @@ class Preprocessor extends CompilerBase } $argInfo = new ArgInfo(); $type = $this->parseParameterType($param, $argInfo, $name); - if ($param->variadic) { - $list[] = self::TYPE_ARRAY . ' ' . $name; - } else { - $list[] = $type . ' ' . $name; - } $argInfo->name = $name; $argInfo->type = $type; $argInfo->byRef = $param->byRef; @@ -292,6 +292,11 @@ class Preprocessor extends CompilerBase if ($param->type and $param->type instanceof NullableType) { $argInfo->nullable = true; } + if ($param->variadic) { + $list[] = self::TYPE_ARRAY . ' ' . $name; + } else { + $list[] = $this->genArgumentDeclaration($argInfo); + } if ($param->default) { if ($param->byRef) { if ($this->isEmptyArray($param->default)) { diff --git a/src/Php/Translator.php b/src/Php/Translator.php index 83880198..3ffb7ab2 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -1035,7 +1035,7 @@ CODE; if ($argInfo->variadic) { $arg = self::TYPE_ARRAY . ' ' . $argInfo->name . ' = {}'; } else { - $arg = $argInfo->type . ' ' . $argInfo->name; + $arg = $this->genArgumentDeclaration($argInfo); if ($argInfo->default) { $arg .= ' = ' . $argInfo->default; } diff --git a/tests/aot/type_decl/009.phpt b/tests/aot/type_decl/009.phpt new file mode 100644 index 00000000..58bd698d --- /dev/null +++ b/tests/aot/type_decl/009.phpt @@ -0,0 +1,23 @@ +--TEST-- +Type Declarations +--FILE-- + +--EXPECTF-- +int(2026) +string(4) "test" +array(2) { + [0]=> + int(2) + [1]=> + int(8) +} +object(stdClass)#%d (0) { +} \ No newline at end of file diff --git a/version.txt b/version.txt index 7d802a3e..baccd039 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -1002 +1003