feat(php): 添加对空合并运算符的支持

- 在编译器中实现对 Expr_BinaryOp_Coalesce 节点类型的解析
- 添加 parseBinaryOpCoalesce 方法处理空合并运算符逻辑
- 使用 isset 检查左侧表达式并返回相应的三元运算符结构
- 将左侧标识符解析为变量检查表达式,右侧作为默认值
pull/1/head
韩天峰 7 months ago
parent 668f440254
commit 4b42419be4
  1. 10
      src/Php/CompilerBase.php

@ -282,6 +282,8 @@ class CompilerBase extends \PhpAot\Core\Translator
return $this->parseBinaryOpGreaterOrEqual($expr);
case 'Expr_BinaryOp_Spaceship':
return $this->parseBinaryOpSpaceship($expr);
case 'Expr_BinaryOp_Coalesce':
return $this->parseBinaryOpCoalesce($expr);
case 'Expr_PreInc':
return $this->parsePreInc($expr);
case 'Expr_PostInc':
@ -2191,6 +2193,14 @@ class CompilerBase extends \PhpAot\Core\Translator
return 'php::compare(' . $left . ', ' . $right . ')';
}
protected function parseBinaryOpCoalesce(Node\Expr\BinaryOp\Coalesce $expr): string
{
$left = $this->parseIdentifier($expr->left);
$right = $this->parseIdentifier($expr->right);
return $this->parseVarCheckExpr($expr->left, 'isset') . ' ? ' . $left . ' : ' . $right;
}
protected function parseBinaryOpNotIdentical(Node\Expr\BinaryOp $expr): string
{
return '!(' . $this->parseBinaryOpIdentical($expr) . ')';

Loading…
Cancel
Save