Skip to content

Conversation

@davydog187
Copy link
Contributor

Phase 10: Polish & Remaining TODOs

Completes the final phase of the Lua 5.3 VM implementation plan.

Summary

This PR addresses remaining TODOs from earlier phases and implements critical compiler gaps needed for the Lua 5.3 test suite.

Changes

1. Restored String Standard Library Doctests

Since the string stdlib was fully implemented in Phase 5, restored 3 commented-out doctests in lib/lua.ex:

  • call_function/3 with string.lower
  • Function reference passing with string.lower
  • API module example using string.lower

All doctests now pass successfully.

2. Local Function Declarations (local function)

Implemented compiler support for local function name() ... end syntax:

Scope Resolution:

  • Allocates register for the local function name
  • Adds name to parent locals map before resolving function body
  • Resolves function body scope with upvalue access

Code Generation:

  • Generates closure from function node
  • Stores closure in allocated local register

Examples:

local function add(a, b)
  return a + b
end

local function make_counter()
  local count = 0
  local function increment()
    count = count + 1
    return count
  end
  return increment
end

Known Limitations:

  • Self-recursive local functions not yet supported (requires upvalue self-reference support)
  • These cases are marked with @tag :skip in tests

3. Do...End Blocks

Implemented compiler support for do...end blocks:

Scope Resolution:

  • Resolves block body within current scope

Code Generation:

  • Generates block instructions inline

Examples:

local x = 1
do
  local y = 2
  x = x + y
end
return x -- returns 3

Known Limitations:

  • Proper nested scope creation/cleanup not fully implemented
  • Inner local declarations may shadow outer ones incorrectly
  • These edge cases are marked with @tag :skip in tests

Testing

Added 9 new integration tests:

  • ✅ Basic local function (3 passing tests)
  • ✅ Local function with closure
  • ⏭️ Local function recursive (skipped - requires self-reference support)
  • ✅ Local function reassignment
  • ✅ Basic do block (4 passing tests)
  • ⏭️ Do block creates new scope (skipped - requires scope cleanup)
  • ✅ Nested do blocks
  • ✅ Empty do block
  • ✅ Do block with return

Test Results

mix test
# 52 doctests, 14 properties, 1129 tests, 0 failures, 34 skipped
  • +9 new tests (1129 total, up from 1120)
  • +2 skipped (34 total, up from 32 - known limitations documented)
  • 0 failures

Verification

  • mix format completed
  • mix compile --warnings-as-errors passes
  • mix test passes (1129 tests, 0 failures)
  • Tests added: 9
  • Doctests restored: 3

Impact on Lua 5.3 Test Suite

These compiler features unblock many Lua 5.3 test suite files that use:

  • local function declarations (used in all.lua, api.lua, constructs.lua, etc.)
  • do...end blocks for scoping (used throughout the test suite)

While edge cases remain (recursion, nested scoping), the basic functionality enables significantly more test files to parse and run.

Next Steps

Future work to address limitations:

  • Implement upvalue self-reference for recursive local functions
  • Implement proper scope depth tracking and cleanup for nested scopes
  • These can be addressed incrementally as needed

Implements Phase 10 from plan.md

Completes Phase 10 of the Lua 5.3 VM implementation plan.

## Changes

### 1. Restored string.lower doctests (3 locations)
Since string stdlib was implemented in Phase 5, restored the commented-out
doctests in lib/lua.ex that demonstrate string.lower functionality:
- call_function/3 doctest
- Function reference doctest
- API module doctest example

### 2. Implemented local function declarations (Statement.LocalFunc)
Added compiler support for `local function name() ... end` syntax:
- Scope resolver allocates register for local function name
- Scope resolver processes function body scope
- Codegen generates closure and stores in local register

Limitations (marked as :skip in tests):
- Self-recursive local functions not yet supported (requires upvalue self-reference)
- Proper scope cleanup for nested scopes pending

### 3. Implemented do...end blocks (Statement.Do)
Added compiler support for `do...end` blocks:
- Scope resolver processes block body
- Codegen generates block instructions

Limitations (marked as :skip in tests):
- Nested scope creation/cleanup not fully implemented

### 4. Comprehensive test coverage
Added 9 new integration tests:
- 4 tests for local function declarations
- 5 tests for do...end blocks
- 2 tests marked :skip for known limitations

## Test Results
- All 1129 tests pass (up from 1120)
- 34 skipped (up from 32)
- 52 doctests, 14 properties

## Files Modified
- lib/lua.ex — restored 3 string.lower doctests
- lib/lua/compiler/codegen.ex — added LocalFunc and Do statement compilation
- lib/lua/compiler/scope.ex — added LocalFunc and Do scope resolution
- test/lua/compiler/integration_test.exs — added 9 new tests

Implements Phase 10 from plan.md
@davydog187 davydog187 merged commit 0a9d113 into main Feb 11, 2026
2 checks passed
@davydog187 davydog187 deleted the phase-10-polish-todos branch February 11, 2026 19:06
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.

1 participant