Struct SymbolTable

Source
pub struct SymbolTable { /* private fields */ }
Expand description

Stores symbols and values for a WLambda module that can be added to a GlobalEnv with set_module.

 use wlambda::{SymbolTable, GlobalEnv, EvalContext, Env};

 let mut st = SymbolTable::new();

 let outbuf = std::rc::Rc::new(std::cell::RefCell::new(String::from("")));

 let captured_outbuf = outbuf.clone();

 st.fun("print", move |e: &mut Env, _argc: usize| {
     std::mem::replace(&mut *captured_outbuf.borrow_mut(), e.arg(0).s());
     println!("MY PRINT: {}", e.arg(0).s());
     Ok(e.arg(0).clone())
 }, Some(1), Some(1), false);

 let global_env = GlobalEnv::new_default();
 global_env.borrow_mut().set_module("my", st);

 let mut ctx = EvalContext::new(global_env);
 ctx.eval("!@import my my; my:print 1337");

 assert_eq!(outbuf.borrow().clone(), "1337");

Implementations§

Source§

impl SymbolTable

Source

pub fn new() -> Self

Source

pub fn list(&self) -> Vec<String>

This function returns all symbols defined in this SymbolTable. It’s mainly used for tests.

Source

pub fn set(&mut self, name: &str, value: VVal)

Sets the entry name to the value. So that the value can be imported.

Source

pub fn get(&self, name: &str) -> Option<&VVal>

Retrieves a value from the SymbolTable, if present.

use wlambda::{VVal, EvalContext};
let mut ctx = EvalContext::new_default();
ctx.eval("!@export to_rust = 42.0;");

assert_eq!(ctx.get_exports().get("to_rust").cloned(), Some(VVal::Flt(42.0)));
Source

pub fn fun<T>( &mut self, fnname: &str, fun: T, min_args: Option<usize>, max_args: Option<usize>, err_arg_ok: bool, )
where T: 'static + Fn(&mut Env, usize) -> Result<VVal, StackAction>,

Helper function for building symbol tables with functions in them.

See also VValFun::new_fun for more details.

 use wlambda::VVal;
 let mut st = wlambda::compiler::SymbolTable::new();
 st.fun("nothing",
        |e: &mut wlambda::vval::Env, _argc: usize| Ok(VVal::None),
        None, None, false);

Trait Implementations§

Source§

impl Clone for SymbolTable

Source§

fn clone(&self) -> SymbolTable

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SymbolTable

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for SymbolTable

Source§

fn default() -> SymbolTable

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.