Skip to main content

Evaluator

Struct Evaluator 

Source
pub struct Evaluator<'a, const N: usize> { /* private fields */ }
Expand description

Maximum number of macros that can be defined The Lisp evaluator with full trampolined TCO

Implementations§

Source§

impl<'a, const N: usize> Evaluator<'a, N>

Source

pub fn new(lisp: &'a Lisp<N>) -> Result<Self, EvalError>

Create a new evaluator with standard environment

Source

pub fn lisp(&self) -> &Lisp<N>

Get the Lisp context

Source

pub fn global_env(&self) -> ArenaIndex

Get the global environment

Source

pub fn register_native( &mut self, name: &'static str, func: NativeFn<N>, ) -> Result<(), EvalError>

Register a native Rust function that can be called from Lisp.

The function will be bound to the given name in the global environment.

§Example
use grift_eval::{Lisp, Evaluator, ArenaIndex, ArenaResult, FromLisp, ToLisp};

fn my_double<const N: usize>(lisp: &Lisp<N>, args: ArenaIndex) -> ArenaResult<ArenaIndex> {
    let n = isize::from_lisp(lisp, lisp.car(args)?)?;
    (n * 2).to_lisp(lisp)
}

let lisp: Lisp<10000> = Lisp::new();
let mut eval = Evaluator::new(&lisp).unwrap();
eval.register_native("my-double", my_double).unwrap();

// Now you can call (my-double 5) from Lisp to get 10
let result = eval.eval_str("(my-double 5)").unwrap();
assert_eq!(lisp.get(result).unwrap().as_number(), Some(10));
Source

pub fn native_registry(&self) -> &NativeRegistry<N>

Get a reference to the native function registry.

Source

pub fn gc(&self) -> GcStats

Run GC with current roots (global env only)

Source

pub fn define(&mut self, name: ArenaIndex, value: ArenaIndex) -> EvalResult

Define in global environment (NOTE: only allowed at top-level)

Source

pub fn eval(&mut self, expr: ArenaIndex) -> EvalResult

Evaluate an expression (entry point)

Source

pub fn eval_in_env(&mut self, expr: ArenaIndex, env: ArenaIndex) -> EvalResult

Evaluate an expression in a given environment Uses full trampolining - no Rust recursion

This is public so the REPL can evaluate expressions for display

Source

pub fn eval_str(&mut self, input: &str) -> EvalResult

Evaluate a string

Auto Trait Implementations§

§

impl<'a, const N: usize> Freeze for Evaluator<'a, N>

§

impl<'a, const N: usize> !RefUnwindSafe for Evaluator<'a, N>

§

impl<'a, const N: usize> !Send for Evaluator<'a, N>

§

impl<'a, const N: usize> !Sync for Evaluator<'a, N>

§

impl<'a, const N: usize> Unpin for Evaluator<'a, N>

§

impl<'a, const N: usize> !UnwindSafe for Evaluator<'a, N>

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.