pub struct TopLevel {
pub env: Env,
pub names: Vec<SmolStr>,
pub ctx: EvalCtx,
}Expand description
Accumulated evaluator state: environment vector and staging context. Exists purely as a convenience for multi-expression REPL sessions.
Fields§
§env: EnvThe base runtime environment. The floor has no top-level
define, so ordinary evaluation never extends this — every form
is closed, and only ctx carries state between forms. It exists
so an embedding can pre-seed bindings: slot i holds the value
of names[i], and lowering resolves free names against names
positionally.
names: Vec<SmolStr>Surface names for the slots of env, in lockstep.
ctx: EvalCtxThe staging context: fresh-variable counter, ANF block stack, closure memo table, tower level, and the I/O implementation.
Implementations§
Source§impl TopLevel
impl TopLevel
Sourcepub fn new() -> Self
pub fn new() -> Self
Construct over a real terminal (TerminalIo).
Panics when no terminal is available, so tests and other
non-interactive embeddings must use with_io
with a HeadlessIo instead.
Sourcepub fn with_io(io: Box<dyn NarjuIo>) -> Self
pub fn with_io(io: Box<dyn NarjuIo>) -> Self
Construct with an explicit I/O implementation.
Use this in tests to avoid depending on a real terminal:
use narju::io::headless::HeadlessIo;
use narju::repl::TopLevel;
let mut top = TopLevel::with_io(Box::new(HeadlessIo::empty()));
assert_eq!(top.eval_int("(+ 1 2)"), 3);Sourcepub fn eval_exp(&mut self, sexp: &Sexp) -> Result<Val, NarjuError>
pub fn eval_exp(&mut self, sexp: &Sexp) -> Result<Val, NarjuError>
Evaluate a single expression in the current environment.
Sourcepub fn run_str(&mut self, src: &str) -> Result<Vec<RunResult>, NarjuError>
pub fn run_str(&mut self, src: &str) -> Result<Vec<RunResult>, NarjuError>
Parse and evaluate all forms in src, returning each result.
Sourcepub fn run_file(&mut self, path: &str) -> Result<Vec<RunResult>, NarjuError>
pub fn run_file(&mut self, path: &str) -> Result<Vec<RunResult>, NarjuError>
Parse and evaluate a file.