Interpreter Phase 4: Eval Block Support with die/return/ListNode#187
Merged
Interpreter Phase 4: Eval Block Support with die/return/ListNode#187
Conversation
Implement opcodes and infrastructure for eval blocks with exception handling. This lays the groundwork for full eval block support including die/return handling and $@ error variable management. Changes: - Add EVAL_TRY (83), EVAL_CATCH (84), EVAL_END (85) opcodes - Add tokenIndex tracking (pcToTokenIndex map) for error reporting - Implement eval block bytecode emission in BytecodeCompiler - Add exception handling stack in BytecodeInterpreter - Update InterpretedCode constructor to accept tokenIndex map - Add disassembler support for eval opcodes - Document eval opcodes in BYTECODE_DOCUMENTATION.md Architecture: - EVAL_TRY pushes catch handler PC onto eval stack, clears $@ - Block bytecode executes normally - EVAL_END pops catch handler, clears $@ on success - If exception: outer catch checks eval stack, calls WarnDie.catchEval(), jumps to catch PC, executes EVAL_CATCH to store undef - Matches compiler's useTryCatch implementation semantics Note: Full testing requires die/return operator support which will be added in follow-up commits. Current implementation provides the infrastructure and opcode framework. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add support for `return` and `die` operators in the bytecode compiler and interpreter. These are essential for eval block functionality. Changes: - Add `return` operator handler in BytecodeCompiler.visit(OperatorNode) - Add `die` operator handler in BytecodeCompiler.visit(OperatorNode) - Implement DIE (73) and WARN (74) opcodes in BytecodeInterpreter - Add disassembler support for DIE and WARN opcodes - Update InterpreterTest with eval block tests (die/return) Implementation: - return: Emits RETURN opcode with expression or undef - die: Emits DIE opcode with message, calls WarnDie.die() - Both track tokenIndex for error reporting - DIE throws PerlDieException which is caught by eval blocks Note: Full testing blocked by ListNode support (die/return operands are wrapped in ListNode by parser). Will be implemented in follow-up. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add complete ListNode implementation enabling eval blocks, subroutine
calls, and list operations in the interpreter. This unblocks die/return
operators and enables full eval block functionality.
New Opcode:
- CREATE_LIST (86): Highly optimized list creation
* count=0: Empty list (fast path)
* count=1: Single element (optimized path)
* count>1: Pre-allocated bulk add
Changes:
- Implement ListNode visitor with performance optimizations
- Add "@" operator support for array variables (@_)
- Add "->" operator support for subroutine application
- Enhance CALL_SUB to handle RuntimeList arguments
- Simplify eval exception handling (return empty list on die)
- Remove complex catch block bytecode execution
- Add CREATE_LIST disassembler support
Performance Optimizations:
1. Fast paths for empty and single-element lists
2. Direct register-to-list population (no intermediate objects)
3. Minimal boxing/unboxing
4. Inline evaluation of list elements
Architecture:
- ListNode creates RuntimeList with all elements
- CALL_SUB converts RuntimeList to RuntimeArray automatically
- Eval exceptions caught and return empty list (Perl semantics)
- $@ set by WarnDie.catchEval()
Test Results:
✅ eval { die 'error' } - Works correctly
✅ eval { return 42 } - Works correctly
✅ All InterpreterTest cases pass
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implement complete eval block support for the interpreter, including exception handling, die/return operators, and high-performance ListNode operations. This enables the interpreter to handle real-world Perl eval blocks with proper error handling.
Changes
1. Eval Block Infrastructure (b3fa767)
EVAL_TRY(83): Set up exception handler, clear $@EVAL_CATCH(84): Handle exceptions, store undefEVAL_END(85): Clear $@ on success2. Return and Die Operators (95ec9ae)
3. High-Performance ListNode Support (591cdac)
CREATE_LIST(86): Optimized list creationPerformance Optimizations
Architecture
Eval Block Flow:
List Operations:
Test Results
All InterpreterTest cases pass:
Standard Perl test suite also passes:
Documentation Updates
Related Issues
Closes requirements for:
Next Steps
With eval blocks working, the interpreter is ready for:
🤖 Generated with Claude Code (claude.ai/code)