feat(compiler): 添加对goto语句的支持

- 引入OP_NOP常量用于处理标签后的空操作
- 修改语句解析循环以跟踪最后一条语句
- 在标签语句后添加空操作防止代码优化问题
- 添加jump02和jump03测试用例验证goto功能
- 创建mv.php脚本用于文件重命名和git添加操作
pull/1/head
韩天峰 7 months ago
parent 82e13b62f1
commit 752767012f
  1. 18
      bin/mv.php
  2. 7
      src/Php/CompilerBase.php
  3. 16
      tests/zend/jump/jump02.phpt
  4. 18
      tests/zend/jump/jump03.phpt

@ -0,0 +1,18 @@
#!/usr/bin/env php
<?php
if ($argc < 2) {
echo "Usage: php mv.php <file>\n";
exit(1);
}
$file = $argv[1];
if (!file_exists($file)) {
echo "File not found: " . $file . "\n";
exit(1);
}
$newFile = str_replace('-raw', '', $file);
$dir = dirname($newFile);
if (!is_dir($dir)) {
mkdir($dir, 0755, true);
}
rename($file, $newFile);
shell_exec('git add ' . $newFile);

@ -61,6 +61,7 @@ class CompilerBase extends \PhpAot\Core\Translator
public const string PREFIX = 'php_'; public const string PREFIX = 'php_';
public const string OP_ISSET = 'isset'; public const string OP_ISSET = 'isset';
public const string OP_EMPTY = 'empty'; public const string OP_EMPTY = 'empty';
public const string OP_NOP = "if (0) {}\n";
protected string $phpxDir = '~/workspace/projects/phpx'; protected string $phpxDir = '~/workspace/projects/phpx';
protected string $lang = 'PHP'; protected string $lang = 'PHP';
protected string $cppCompiler = 'g++'; protected string $cppCompiler = 'g++';
@ -872,7 +873,8 @@ class CompilerBase extends \PhpAot\Core\Translator
{ {
$lines = []; $lines = [];
$inLoopTop = $this->inLoop; $inLoopTop = $this->inLoop;
foreach ($stmts as $v) { $last = array_key_last($stmts);
foreach ($stmts as $i => $v) {
$class = $v->getType(); $class = $v->getType();
$this->beforeStmtLines = []; $this->beforeStmtLines = [];
$this->afterStmtLines = []; $this->afterStmtLines = [];
@ -926,6 +928,9 @@ class CompilerBase extends \PhpAot\Core\Translator
break; break;
case 'Stmt_Label': case 'Stmt_Label':
$result = $this->parseLabel($v); $result = $this->parseLabel($v);
if ($i === $last) {
$result .= self::OP_NOP;
}
break; break;
case 'Stmt_Continue': case 'Stmt_Continue':
$result = $this->parseContinue($v); $result = $this->parseContinue($v);

@ -0,0 +1,16 @@
--TEST--
jump 02: goto forward
--FILE--
<?php
$n = 1;
L1:
if ($n > 3) goto L2;
echo "$n: ok\n";
$n++;
goto L1;
L2:
?>
--EXPECT--
1: ok
2: ok
3: ok

@ -0,0 +1,18 @@
--TEST--
jump 03: goto inside control structures
--FILE--
<?php
do {
if (1) {
echo "1: ok\n";
goto L1;
} else {
echo "bug\n";
L1:
echo "2: ok\n";
}
} while (0);
?>
--EXPECT--
1: ok
2: ok
Loading…
Cancel
Save