- Overview
- Main Interpreter Functions
- Flow Control with
InterpretResult
- Summary of Steps
- Example Execution Flow
- Error Handling
The interpreter takes the AST and executes it. In FlavorLang, this means:
- Evaluating Expressions
- Assigning / Retrieving Variables
- Managing Conditionals & Loops
- Handling Function Calls
- Printing & Error Handling
- interpret_node(...): The primary function that returns an
InterpretResult
, containing a LiteralValue plus a did_return flag indicating if a function return (deliver) occurred. interpret_assignment(...)
: Assigns values to variables in the environment.interpret_binary_op(...)
: Applies arithmetic or comparison operators to numeric or string values.interpret_conditional(...)
: Runsif
/elif
/else
logic, short-circuiting if adeliver
statement is hit.interpret_while_loop(...)
: Evaluates a loop’s condition repeatedly, stopping ifdid_return
is set or condition is false.interpret_function_call(...)
: Creates a local environment for function parameters, executes the function body, and handles the final return value.
interpret_node(...)
always returns anInterpretResult
:.value
= TheLiteralValue
result of the node..did_return
= true if a deliver statement was encountered, letting parent code know to halt further statements.- This approach ensures something like the following stops interpreting once
deliver 1;
is returned in the base case:
create factorial(n) {
if n <= 1 {
deliver 1;
} else {
deliver n * factorial(n - 1);
}
}
let result = factorial(3);
For x = 5 + 3
:
interpret_node(AST_ASSIGNMENT)
→ callsinterpret_assignment()
.interpret_node(RHS: AST_BINARY_OP)
→ callsinterpret_binary_op()
.interpret_node(left=5)
returns5
; similarlyright=3
returns3
.- After
5 + 3 = 8
, storex = 8
in the environment.
- Checks for undefined variables, invalid operator usage, division by zero, etc.
- On error, prints a message and calls
exit(1)
.
This project is licensed under the Apache 2.0 License — see the LICENSE file for details.
© 2024-2025 Kenneth Oliver. All rights reserved.