[][src]Struct wlambda::vval::Env

pub struct Env {
    pub args: Vec<VVal>,
    pub fun: Rc<VValFun>,
    pub bp: usize,
    pub sp: usize,
    pub argc: usize,
    pub user: Rc<RefCell<dyn Any>>,
    pub exports: HashMap<String, VVal>,
}

The runtime environment of the evaluator.

Fields

args: Vec<VVal>

The argument stack, limited to STACK_SIZE.

fun: Rc<VValFun>

A reference to the currently executed function.

Used for accessing the up values and other details about the function.

bp: usize

The basepointer to reference arguments and local variables.

  • bp + n (n >= 0) references a local variable
  • bp - n (n > 0) references an argument
sp: usize

The current stack pointer.

argc: usize

The argument count to the current call.

user: Rc<RefCell<dyn Any>>

A user defined variable that holds user context information. See also the with_user_do function.

exports: HashMap<String, VVal>

The exported names of this module.

Methods

impl Env[src]

pub fn new() -> Env[src]

pub fn new_with_user(user: Rc<RefCell<dyn Any>>) -> Env[src]

pub fn get_user(&self) -> Rc<RefCell<dyn Any>>[src]

Returns the passed in user context value.

pub fn with_user_do<T: 'static, F, X>(&mut self, f: F) -> X where
    F: Fn(&mut T) -> X, 
[src]

Easier access to the user data field in Env

In the following example the user supplies a registry vector for storing VVals. A callback is stored, which is then later executed.

use wlambda::{VVal, EvalContext, GlobalEnv};
use wlambda::vval::Env;
use std::rc::Rc;
use std::cell::RefCell;

let global = GlobalEnv::new_default();

global.borrow_mut().add_func("reg", |env: &mut Env, _argc: usize| {
    let fun = env.arg(0);
    env.with_user_do(|v: &mut Vec<VVal>| v.push(fun.clone()));
    Ok(VVal::Nul)
}, Some(1), Some(1));

let reg : Rc<RefCell<Vec<VVal>>> =
    Rc::new(RefCell::new(Vec::new()));

let mut ctx = EvalContext::new_with_user(global, reg.clone());
ctx.eval("reg { _ + 10 }").unwrap();

let n = reg.borrow_mut()[0].clone();
let ret = ctx.call(&n, &vec![VVal::Int(11)]).unwrap();
assert_eq!(ret.i(), 21);

pub fn export_name(&mut self, name: &str, value: &VVal)[src]

pub fn set_bp(&mut self, env_size: usize) -> usize[src]

pub fn reset_bp(&mut self, env_size: usize, oldbp: usize)[src]

pub fn with_local_call_info<T>(
    &mut self,
    argc: usize,
    f: T
) -> Result<VVal, StackAction> where
    T: Fn(&mut Env) -> Result<VVal, StackAction>, 
[src]

pub fn with_fun_info<T>(
    &mut self,
    fu: Rc<VValFun>,
    argc: usize,
    f: T
) -> Result<VVal, StackAction> where
    T: Fn(&mut Env) -> Result<VVal, StackAction>, 
[src]

pub fn with_restore_sp<T>(&mut self, f: T) -> Result<VVal, StackAction> where
    T: Fn(&mut Env) -> Result<VVal, StackAction>, 
[src]

pub fn with_pushed_sp<T>(&mut self, n: usize, f: T) -> Result<VVal, StackAction> where
    T: Fn(&mut Env) -> Result<VVal, StackAction>, 
[src]

pub fn push_sp(&mut self, n: usize)[src]

pub fn push(&mut self, v: VVal) -> usize[src]

pub fn popn(&mut self, n: usize)[src]

pub fn dump_stack(&self)[src]

Prints a dump of the stack state.

pub fn argv(&mut self) -> VVal[src]

pub fn get_up_raw(&mut self, i: usize) -> VVal[src]

pub fn get_up(&mut self, i: usize) -> VVal[src]

pub fn set_arg(&mut self, i: usize, v: VVal)[src]

pub fn arg_ref(&self, i: usize) -> Option<&VVal>[src]

pub fn arg_err_internal(&self, i: usize) -> Option<VVal>[src]

pub fn arg(&self, i: usize) -> VVal[src]

pub fn get_local(&mut self, i: usize) -> VVal[src]

pub fn set_up(&mut self, index: usize, value: &VVal)[src]

pub fn set_local(&mut self, i: usize, value: &VVal)[src]

pub fn set_consume(&mut self, i: usize, value: VVal)[src]

Trait Implementations

impl Clone for Env[src]

impl Default for Env[src]

impl Debug for Env[src]

Auto Trait Implementations

impl !Send for Env

impl !Sync for Env

impl Unpin for Env

impl !UnwindSafe for Env

impl !RefUnwindSafe for Env

Blanket Implementations

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]