- 将函数调用优化代码从CompilerBase类中提取到FuncCallOptimizer trait - 在CompilerBase中引入FuncCallOptimizer trait - 使用parseFuncCallWithOptimizer方法替代原有的硬编码优化逻辑 - 添加对abs函数的优化支持 - 简化了原有条件判断结构,提高代码可维护性pull/1/head
parent
62e5615421
commit
91a607185e
2 changed files with 35 additions and 14 deletions
@ -0,0 +1,31 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace PhpAot\Php; |
||||||
|
|
||||||
|
use PhpParser\Node; |
||||||
|
|
||||||
|
trait FuncCallOptimizer |
||||||
|
{ |
||||||
|
protected function parseFuncCallWithOptimizer(string $name, Node\Expr\FuncCall $expr): string|false |
||||||
|
{ |
||||||
|
if ($name === 'strlen' or $name === 'sizeof' or $name === 'count') { |
||||||
|
return 'php::len(' . $this->parseIdentifier($expr->args[0]->value) . ')'; |
||||||
|
} |
||||||
|
if (count($expr->args) == 1) { |
||||||
|
switch ($name) { |
||||||
|
case 'intval': |
||||||
|
return $this->convertIntExpr($this->parseExpr($expr->args[0]->value)); |
||||||
|
case 'floatval': |
||||||
|
return $this->convertFloatExpr($this->parseExpr($expr->args[0]->value)); |
||||||
|
case 'boolval': |
||||||
|
return $this->convertBoolExpr($this->parseExpr($expr->args[0]->value)); |
||||||
|
default: |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
if ($name === 'abs') { |
||||||
|
return 'php::math::abs(' . $this->parseIdentifier($expr->args[0]->value) . ')'; |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue