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
impl SymbolTable
pub fn new() -> Self
Sourcepub fn list(&self) -> Vec<String>
pub fn list(&self) -> Vec<String>
This function returns all symbols defined in this SymbolTable. It’s mainly used for tests.
Sourcepub fn set(&mut self, name: &str, value: VVal)
pub fn set(&mut self, name: &str, value: VVal)
Sets the entry name
to the value
. So that the
value can be imported.
Sourcepub fn get(&self, name: &str) -> Option<&VVal>
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)));
Sourcepub fn fun<T>(
&mut self,
fnname: &str,
fun: T,
min_args: Option<usize>,
max_args: Option<usize>,
err_arg_ok: bool,
)
pub fn fun<T>( &mut self, fnname: &str, fun: T, min_args: Option<usize>, max_args: Option<usize>, err_arg_ok: bool, )
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
impl Clone for SymbolTable
Source§fn clone(&self) -> SymbolTable
fn clone(&self) -> SymbolTable
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreSource§impl Debug for SymbolTable
impl Debug for SymbolTable
Source§impl Default for SymbolTable
impl Default for SymbolTable
Source§fn default() -> SymbolTable
fn default() -> SymbolTable
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl Freeze for SymbolTable
impl !RefUnwindSafe for SymbolTable
impl !Send for SymbolTable
impl !Sync for SymbolTable
impl Unpin for SymbolTable
impl !UnwindSafe for SymbolTable
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more