gsh/evaluator/evaluator
The evaluator module is the core module of the GSH REPL.
Because Gleam is statically typed and compiled, we cannot evaluate raw AST
dynamically like Elixir’s IEx. Instead, this module acts as a synthetic runtime,
taking the user’s input, injecting historical state (imports, bindings, types, functions),
and generating a unique gsh_eval_X.gleam file for execution.
Key Capabilities:
- Token Routing: Uses
glexerto parse input and accurately classify the statement (import, type, function, binding, or raw expression). - Side-Effect Caching: Wraps every variable assignment in an Erlang Process Dictionary
check, ensuring side effects (like
io.println) execute exactly once per session even as the file is continually recompiled. - Cache Collision Prevention: Appends the
prompt_countto module names to guarantee the Erlang VM loads fresh bytecode from disk on every execution.
Values
pub fn evaluate(
input: String,
bindings: List(binding.Binding),
imports: List(String),
types: List(String),
functions: List(String),
debug: Bool,
prompt_count: Int,
) -> result.Evaluation
The primary orchestration engine for REPL input execution.
Execution Pipeline:
- Lexical Analysis: Scans the raw string to intercept syntax errors (e.g., open strings) and strips whitespace/comments to correctly route the syntax tree.
- Code Generation: Passes the tokens to the appropriate source builder and recursively
injects the historical
ShellStatevariables to maintain lexical scope. - Compilation: Writes the generated code to a unique file in
test/and triggers therunnerfor background compilation and VM execution. - Cleanup & State: Deletes the temporary file, calculates execution latency for debug logs, and returns the extracted definitions so the REPL can update its active state.