Skip to content

feat: implement goto/label support#216

Merged
esrrhs merged 3 commits into
masterfrom
feature/goto-label
Jun 25, 2026
Merged

feat: implement goto/label support#216
esrrhs merged 3 commits into
masterfrom
feature/goto-label

Conversation

@esrrhs

@esrrhs esrrhs commented Jun 24, 2026

Copy link
Copy Markdown
Owner

Summary

实现 Lua goto/label 语法支持。

语义分析(semantic_analysis.cpp

  • 移除 CheckGotoOrLabel() 的拦截抛异常逻辑
  • 新增 ValidateGotoInBlock():递归验证 goto 合法性
  • 验证规则:
    1. goto 目标 label 必须在可见作用域内
    2. goto 不能跳过局部变量声明(Lua 5.2+ 规则)
    3. label 在嵌套 block 内也可见(作用域传递)
    4. 函数是独立作用域,内部 label/goto 不影响外部

C 代码生成(c_gen.cpp

  • 新增 CompileStmtGoto():生成 goto flua_L_<name>;
  • 新增 CompileStmtLabel():生成 flua_L_<name>: ;

AST(syntax_tree.h

  • SyntaxTreeGoto 新增 GetLabel() 方法

测试

  • 新增 4 个测试:forward skip、backward loop、label-only、invalid goto
  • 移除 2 个旧的异常测试(goto/label 不再被拒绝)

全部 715 个测试通过

- Semantic analysis: replace rejection with proper scope validation
  - Goto targets must exist in visible scope
  - Goto cannot jump over local variable declarations
  - Recursive validation for nested blocks (if/while/repeat/for)
- CGen: add CompileStmtGoto and CompileStmtLabel
  - Labels emit C labels with flua_L_ prefix
  - Gotos emit C goto statements
- SyntaxTreeGoto: add GetLabel() method
- Remove old exception tests for unsupported goto/label
- Add 4 new test cases: forward skip, backward loop, label-only, invalid goto
@codecov-commenter

codecov-commenter commented Jun 24, 2026

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.77%. Comparing base (e28a39a) to head (44f546b).
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #216      +/-   ##
==========================================
+ Coverage   96.69%   96.77%   +0.07%     
==========================================
  Files          67       67              
  Lines       14008    14118     +110     
==========================================
+ Hits        13545    13662     +117     
+ Misses        463      456       -7     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

esrrhs added 2 commits June 25, 2026 10:31
Valid goto tests (test/lua/jit/):
- test_goto_forward_skip: forward goto skipping a reassignment
- test_goto_backward_loop: backward goto forming a loop
- test_goto_label_only: label with no goto
- test_goto_continue: goto simulating continue in for loop
- test_goto_break_out: goto breaking out of nested for loops
- test_goto_in_if_else: goto within if/else branches

Invalid goto tests (test/lua/exception/):
- test_goto_skip_single_local: goto jumps over single local
- test_goto_skip_local: goto jumps over multiple locals
- test_goto_nonexistent_label: goto to nonexistent label
- test_goto_cross_function: goto targeting label in another function

Removed old combined test files.
@esrrhs esrrhs merged commit 4a56dc5 into master Jun 25, 2026
7 checks passed
@esrrhs esrrhs deleted the feature/goto-label branch June 25, 2026 03:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants