TopLevel

Struct TopLevel 

Source
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: Env

The 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: EvalCtx

The staging context: fresh-variable counter, ANF block stack, closure memo table, tower level, and the I/O implementation.

Implementations§

Source§

impl TopLevel

Source

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.

Source

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);
Source

pub fn eval_exp(&mut self, sexp: &Sexp) -> Result<Val, NarjuError>

Evaluate a single expression in the current environment.

Source

pub fn run_str(&mut self, src: &str) -> Result<Vec<RunResult>, NarjuError>

Parse and evaluate all forms in src, returning each result.

Source

pub fn run_file(&mut self, path: &str) -> Result<Vec<RunResult>, NarjuError>

Parse and evaluate a file.

Source

pub fn eval(&mut self, src: &str) -> Val

Evaluate a source string, panicking on error. For tests only.

Source

pub fn eval_int(&mut self, src: &str) -> i64

Evaluate and unwrap as i64, panicking on type mismatch. For tests only.

Trait Implementations§

Source§

impl Default for TopLevel

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.