Skip to content

Conversation

@nvt
Copy link
Owner

@nvt nvt commented Dec 11, 2025

Lambdas returning symbol values (like 'DONE or 'SUCCESS) were causing crashes with VM_ERROR_BYTECODE. The VM was attempting to evaluate the returned symbol as a procedure name, which failed.

This fix distinguishes between intermediate tail-recursive calls (which need evaluation to continue recursion) and final return values (which should not be re-evaluated). This preserves tail call optimization while preventing the error.

nvt added 2 commits December 11, 2025 22:25
Lambda frames reuse argv[0] throughout execution: initially for the lambda
form, then for return values during tail-recursive execution. When a lambda
body completes, its return value is copied to argv[0] (vm-sched.c:270).

For tail recursion, this is correct: when the return is a form like
(count (- n 1)), vm_eval_expr evaluates it to continue recursion.

For final values, vm_eval_expr should not be called. When the return is a
symbol like 'DONE, vm_eval_expr tries to look it up as a procedure name,
fails, and raises VM_ERROR_BYTECODE.

The fix checks if the lambda frame is complete (VM_EVAL_COMPLETED_ALL)
before calling vm_eval_expr. Complete frames don't need re-evaluation,
while incomplete frames continue tail recursion normally.
previous commit. The tests validate that lambda return values work correctly
for both non-recursive and tail-recursive scenarios, including mutual recursion.
@nvt nvt changed the title Fix tail call optimization Fix error when lambdas return symbols Dec 11, 2025
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