feat(php): 添加枚举支持并优化代码打印功能

- 引入 PhpParser\PrettyPrinter 用于代码格式化
- 添加 PrettyPrinter 实例变量用于代码美化输出
- 实现 parseEnum 方法处理枚举语法解析
- 添加 genEmbeddedCode 方法生成嵌入式代码
- 在语法解析器中添加 Stmt_Enum 分支处理
- 修复 parseStatic 方法参数类型声明
- 优化原生静态方法调用逻辑增加空值检查
- 添加枚举功能的测试用例文件
pull/1/head
韩天峰 7 months ago
parent a72752828f
commit 9de6ca6a41
  1. 35
      src/Php/CompilerBase.php
  2. 26
      tests/aot/enum2.phpt

@ -27,6 +27,7 @@ use PhpParser\Node\UnionType;
use PhpParser\NodeAbstract;
use PhpParser\Parser;
use PhpParser\ParserFactory;
use PhpParser\PrettyPrinter;
class CompilerBase extends \PhpAot\Core\Translator
{
@ -200,14 +201,15 @@ class CompilerBase extends \PhpAot\Core\Translator
protected bool $stubFile = false;
protected bool $enableProfiler = false;
protected Parser $parser;
protected PrettyPrinter $printer;
public function __construct(string $rootPath)
{
$this->rootPath = $rootPath;
$this->parser = (new ParserFactory())->createForNewestSupportedVersion();
// $this->prettyPrinter = new PrettyPrinter\Standard;
$this->parser = (new ParserFactory())->createForNewestSupportedVersion();
$this->printer = new PrettyPrinter\Standard;
$this->setBuildDir($rootPath . '/build');
$climate = new CLImate();
$climate = new CLImate();
$this->climate = $climate;
// $this->noLiteralStrings = $climate->arguments->get('no-literal-strings');
}
@ -948,6 +950,9 @@ class CompilerBase extends \PhpAot\Core\Translator
case 'Stmt_Global':
$result = $this->parseGlobal($v);
break;
case 'Stmt_Enum':
$result = $this->parseEnum($v);
break;
case 'Stmt_Static':
$result = $this->parseStatic($v);
break;
@ -2797,7 +2802,7 @@ class CompilerBase extends \PhpAot\Core\Translator
return $var_def . $code;
}
protected function parseStatic(mixed $v): string
protected function parseStatic(Node\Stmt\Static_ $v): string
{
$list = [];
foreach ($v->vars as $var) {
@ -2812,6 +2817,11 @@ class CompilerBase extends \PhpAot\Core\Translator
return implode(PHP_EOL . $this->getIndent(), $list);
}
protected function parseEnum(Node\Stmt\Enum_ $v): string
{
return 'php::eval("' . $this->escapeString($this->genEmbeddedCode($v)) . '");';
}
protected function parseEval(mixed $expr): string
{
return 'php::eval(' . $this->parseIdentifier($expr->expr) . ')';
@ -3109,11 +3119,13 @@ class CompilerBase extends \PhpAot\Core\Translator
if ($this->isNameExpr($expr->class) and $this->isIdExpr($expr->name)) {
$nativeFunc = $this->getNativeStaticMethod($class, $method);
$args = $this->parseNativeCallArgs($expr->args, $nativeFunc);
if ($args) {
return self::PREFIX . $nativeFunc . '(php::null_object, ' . $args . ')';
} else {
return self::PREFIX . $nativeFunc . '(php::null_object)';
if ($nativeFunc) {
$args = $this->parseNativeCallArgs($expr->args, $nativeFunc);
if ($args) {
return self::PREFIX . $nativeFunc . '(php::null_object, ' . $args . ')';
} else {
return self::PREFIX . $nativeFunc . '(php::null_object)';
}
}
}
$ce = $this->getClassEntryPtr($class);
@ -3553,6 +3565,11 @@ class CompilerBase extends \PhpAot\Core\Translator
}
}
protected function genEmbeddedCode(NodeAbstract $stmt): string
{
return $this->printer->prettyPrint([$stmt]);
}
protected function parseClosure(Node\Expr\Closure $expr): string
{
$tmpVar = $this->genTmpVarName();

@ -0,0 +1,26 @@
--TEST--
enum 2
--FILE--
<?php
function main()
{
enum TestEnum2: int {
case Hearts = 2;
case Diamonds = 1;
case Clubs = 4;
case Spades = 3;
}
var_dump(TestEnum2::cases());
}
?>
--EXPECT--
array(4) {
[0]=>
enum(TestEnum2::Hearts)
[1]=>
enum(TestEnum2::Diamonds)
[2]=>
enum(TestEnum2::Clubs)
[3]=>
enum(TestEnum2::Spades)
}
Loading…
Cancel
Save