Struct wlambda::compiler::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
sourceimpl SymbolTable
impl SymbolTable
pub fn new() -> Self
sourcepub fn list(&self) -> Vec<String>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A> where
A: Allocator,
pub fn list(&self) -> Vec<String>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A> where
A: Allocator,
A: Allocator,
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
) where
T: 'static + Fn(&mut Env, usize) -> Result<VVal, StackAction>,
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
sourceimpl Clone for SymbolTable
impl Clone for SymbolTable
sourcefn clone(&self) -> SymbolTable
fn clone(&self) -> SymbolTable
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl Debug for SymbolTable
impl Debug for SymbolTable
sourceimpl Default for SymbolTable
impl Default for SymbolTable
sourcefn default() -> SymbolTable
fn default() -> SymbolTable
Returns the “default value” for a type. Read more
Auto Trait Implementations
impl !RefUnwindSafe for SymbolTable
impl !Send for SymbolTable
impl !Sync for SymbolTable
impl Unpin for SymbolTable
impl !UnwindSafe for SymbolTable
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
🔬 This is a nightly-only experimental API. (
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more