feat(php): 添加对箭头函数和接口声明的支持

- 实现了箭头函数语法解析和C++代码生成
- 在编译器基础类中添加了对Expr_ArrowFunction节点类型的处理
- 新增parseArrowFunction方法用于处理箭头函数的参数和返回值
- 更新预处理器跳过接口声明的处理逻辑
- 修改测试桩生成脚本以支持声明和分组使用语句的处理
pull/1/head
韩天峰 6 months ago
parent 28d12102a4
commit 3641cb3184
  1. 2
      bin/gen_stub.php
  2. 46
      src/Php/CompilerBase.php
  3. 1
      src/Php/Preprocessor.php
  4. 13
      tests/aot/arrow-func.phpt

@ -4502,7 +4502,7 @@ class FileInfo {
}
}
if ($stmt instanceof Stmt\Use_) {
if ($stmt instanceof Stmt\Use_ or $stmt instanceof Stmt\GroupUse or $stmt instanceof Stmt\Declare_) {
continue;
}

@ -398,6 +398,8 @@ class CompilerBase extends \PhpAot\Core\Translator
return $this->parseShellExec($expr);
case 'Expr_Closure':
return $this->parseClosure($expr);
case 'Expr_ArrowFunction':
return $this->parseArrowFunction($expr);
case 'Name_FullyQualified':
return $expr->name;
case 'Scalar_Int':
@ -3760,6 +3762,50 @@ class CompilerBase extends \PhpAot\Core\Translator
return $this->printer->prettyPrint([$stmt]);
}
protected function parseArrowFunction(Node\Expr\ArrowFunction $expr): string
{
$tmpVar = $this->genTmpVarName();
$fnCode = $this->getIndent() .
'php::ClosureFn ' . $tmpVar . ' = [&]('
. 'INTERNAL_FUNCTION_PARAMETERS, '
. self::TYPE_OBJECT . ' &this_, '
. self::TYPE_ARGS . ' &vars_) ' .
'-> ' . self::TYPE_VAR . ' {' . PHP_EOL;
$oriArgs = $this->arguments;
$this->arguments = [];
$oriInClosure = $this->inClosure;
$this->inClosure = true;
$this->indentLevel++;
foreach ($expr->params as $i => $param) {
$var = $this->parseIdentifier($param->var);
$fnCode .= 'auto ' . $var . ' = php::getCallArg(' . $i . ');' . PHP_EOL;
$this->addArgument($var, self::TYPE_VAR);
}
if ($this->methodDef) {
$this->addArgument('this_', self::TYPE_OBJECT);
}
$fnCode .= 'return ' . $this->parseExpr($expr->expr) . ';';
$this->indentLevel--;
$fnCode .= '};' . PHP_EOL;
$this->beforeStmtLines[] = $fnCode;
$this->arguments = $oriArgs;
$this->inClosure = $oriInClosure;
if ($this->methodDef) {
return 'php::newClosure(' . $tmpVar . ', { }, this_)';
} else {
return 'php::newClosure(' . $tmpVar . ', { })';
}
}
protected function parseClosure(Node\Expr\Closure $expr): string
{
$tmpVar = $this->genTmpVarName();

@ -144,6 +144,7 @@ class Preprocessor extends CompilerBase
break;
case 'Stmt_Use':
case 'Stmt_Const':
case 'Stmt_Interface':
break;
case 'Stmt_Expression':
$this->foundStrayCode($v2);

@ -0,0 +1,13 @@
--TEST--
arrow function
--FILE--
<?php
function main()
{
$y = 1;
$fn1 = fn($x) => $x + $y;
var_export($fn1(3));
}
?>
--EXPECT--
4
Loading…
Cancel
Save