feat(php): 添加对 exit 语句的支持

- 在 AstNodeType 中新增 isReturnExpr、isBreakExpr 和 isExitExpr 方法
- 修改 CompilerBase 中 switch case 的验证逻辑
- 支持 exit 语句作为 switch case 的结束语句
- 更新错误消息以包含 exit 语句的验证
- 重构条件判断使用新的辅助方法进行类型检查
pull/1/head
韩天峰 6 months ago
parent 2dac76a62d
commit 52b1effffb
  1. 18
      src/Php/AstNodeType.php
  2. 4
      src/Php/CompilerBase.php

@ -111,4 +111,22 @@ trait AstNodeType
{ {
return $expr instanceof VariadicPlaceholder; return $expr instanceof VariadicPlaceholder;
} }
protected function isReturnExpr(NodeAbstract $expr): bool
{
return $expr instanceof Node\Stmt\Return_;
}
protected function isBreakExpr(NodeAbstract $expr): bool
{
return $expr instanceof Node\Stmt\Break_;
}
protected function isExitExpr(NodeAbstract $expr): bool
{
if ($expr instanceof Node\Stmt) {
$expr = $expr->expr;
}
return $expr instanceof Node\Expr\Exit_;
}
} }

@ -3214,8 +3214,8 @@ class CompilerBase extends \PhpAot\Core\Translator
$stmts = $stmts[0]->stmts; $stmts = $stmts[0]->stmts;
} }
$lastExpr = end($stmts); $lastExpr = end($stmts);
if (!($lastExpr instanceof Node\Stmt\Return_ or $lastExpr instanceof Node\Stmt\Break_)) { if (!$this->isReturnExpr($lastExpr) and !$this->isExitExpr($lastExpr) and !$this->isBreakExpr($lastExpr)) {
$this->fatalError($case, 'switch case must end with return or break, given ' . $lastExpr->getType()); $this->fatalError($case, 'switch case must end with return or break or exit, ' . $lastExpr->getType() . ' given');
} }
if ($condList) { if ($condList) {

Loading…
Cancel
Save