Env

Struct Env 

Source
pub struct Env {
Show 16 fields pub args: Vec<VVal>, pub call_stack: Vec<Rc<VValFun>>, pub unwind_stack: Vec<UnwindAction>, pub current_self: VVal, pub bp: usize, pub sp: usize, pub argc: usize, pub user: Rc<RefCell<dyn Any>>, pub exports: FnvHashMap<Symbol, VVal>, pub stdio: Stdio, pub accum_val: VVal, pub accum_fun: VVal, pub global: GlobalEnvRef, pub vm_nest: usize, pub loop_info: LoopInfo, pub iter: Option<Rc<RefCell<VValIter>>>,
}
Expand description

The runtime environment of the evaluator.

The easiest way to get an instance of this, is to create an EvalContext:

 use wlambda::{VVal, EvalContext};

 let mut ctx = EvalContext::new_default();
 let res =
     VVal::new_str("a")
     .call(&mut *ctx.local.borrow_mut(), &[VVal::new_str("b")])
     .unwrap();

 assert_eq!(res.s(), "\"ab\"");

Fields§

§args: Vec<VVal>

The argument stack, initialized to a size of START_STACK_SIZE.

§call_stack: Vec<Rc<VValFun>>

A stack of the currently called functions.

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

§unwind_stack: Vec<UnwindAction>

A stack that holds cleanup routines that need to be handled:

§current_self: VVal

Holds the object of the currently called method:

§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: FnvHashMap<Symbol, VVal>

The exported names of this module.

§stdio: Stdio

This is the standard output used for any functions in WLambda that print something. Such as std:displayln or std:writeln.

§accum_val: VVal

Current accumulator value:

§accum_fun: VVal

Current accumulator function:

§global: GlobalEnvRef

A pointer to the global environment, holding stuff like the module loader and thread creator.

§vm_nest: usize

A counter that counts the nesting depth in vm() calls

§loop_info: LoopInfo

Holds information to process ‘next’ and ‘break’ for loop constructs:

§iter: Option<Rc<RefCell<VValIter>>>

Holds the current iterator for the ‘iter’ construct.

Implementations§

Source§

impl Env

Source

pub fn new(global: GlobalEnvRef) -> Env

Source

pub fn new_with_user(global: GlobalEnvRef, user: Rc<RefCell<dyn Any>>) -> Env

Source

pub fn set_stdio(&mut self, stdio: Stdio)

Sets a custom stdio procedure. This can be used to redirect the standard input/output operations of WLambda to other sinks and sources. For instance writing and reading from a Buffer when using WASM or some other embedded application:

use wlambda::*;
use std::rc::Rc;
use std::cell::RefCell;

let new_output : Rc<RefCell<Vec<u8>>> =
    Rc::new(RefCell::new(vec![]));
let new_input =
    Rc::new(RefCell::new(std::io::Cursor::new("abc\ndef\n1 2 3 4\n"
                         .to_string().as_bytes().to_vec())));

let memory_stdio =
    vval::Stdio::new_from_mem(new_input, new_output.clone());

let mut ctx = EvalContext::new_default();
ctx.local.borrow_mut().set_stdio(memory_stdio);

ctx.eval("std:displayln :TEST 123").unwrap();

let output = String::from_utf8(new_output.borrow().clone()).unwrap();
assert_eq!(output, "TEST 123\n");

let out_lines =
    ctx.eval("!l = $[]; std:io:lines \\std:push l _; l").unwrap().s();
assert_eq!(out_lines, "$[\"abc\\n\",\"def\\n\",\"1 2 3 4\\n\"]");
Source

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

Returns the passed in user context value.

Source

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

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::None)
}, 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);
Source

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

Source

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

Source

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

Source

pub fn self_object(&self) -> VVal

Source

pub fn with_object<T>( &mut self, object: VVal, f: T, ) -> Result<VVal, StackAction>
where T: Fn(&mut Env) -> Result<VVal, StackAction>,

Source

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

Source

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

Source

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

Source

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

Source

pub fn stk(&self, offs: usize) -> &VVal

Source

pub fn stk_i(&self, offs: usize) -> i64

Source

pub fn inc_local(&mut self, idx: usize, inc: i16) -> i64

Source

pub fn pop(&mut self) -> VVal

Source

pub fn null_locals(&mut self, from: usize, to: usize)

Source

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

Source

pub fn dump_stack(&self)

Prints a dump of the stack state.

Source

pub fn stk2vec(&self, count: usize) -> VVal

Source

pub fn argv(&self) -> VVal

Source

pub fn argv_ref(&self) -> &[VVal]

Source

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

Source

pub fn get_up_captured_ref(&self, i: usize) -> VVal

Source

pub fn get_up(&self, i: usize) -> VVal

Source

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

Source

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

Source

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

Source

pub fn get_local_up_promotion(&mut self, i: usize) -> VVal

Source

pub fn get_local_captured_ref(&self, i: usize) -> VVal

Source

pub fn reg(&self, i: i32) -> VVal

Source

pub fn get_local(&self, i: usize) -> VVal

Source

pub fn assign_ref_up(&mut self, i: usize, value: VVal)

Source

pub fn assign_ref_local(&mut self, i: usize, value: VVal)

Source

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

Source

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

Source

pub fn unwind_depth(&self) -> usize

Source

pub fn push_unwind(&mut self, uwa: UnwindAction)

Source

pub fn unwind_to_depth(&mut self, depth: usize)

Source

pub fn push_fun_call(&mut self, fu: Rc<VValFun>, argc: usize)

Source

pub fn push_clear_locals(&mut self, from: usize, to: usize)

Source

pub fn push_unwind_self(&mut self, new_self: VVal)

Source

pub fn cleanup_loop(&mut self)

Source

pub fn push_loop_info( &mut self, current_pc: usize, break_pc: usize, uw_depth_offs: usize, )

Source

pub fn push_iter(&mut self, iter: Rc<RefCell<VValIter>>)

Source

pub fn dump_unwind_stack(&self) -> String

Source

pub fn unwind(&mut self, ua: UnwindAction)

Source

pub fn unwind_one(&mut self)

Source

pub fn setup_accumulator(&mut self, v: VVal)

Source

pub fn with_accum<T>(&mut self, v: VVal, acfun: T) -> Result<VVal, StackAction>
where T: Fn(&mut Env) -> Result<VVal, StackAction>,

Source

pub fn get_accum_value(&self) -> VVal

Source

pub fn get_accum_function(&self) -> VVal

Source

pub fn new_err(&self, s: String) -> VVal

Creates a new error VVal with the given string as error message. Takes care to annotate the error value with the current function information.

 use wlambda::compiler::EvalContext;
 use wlambda::vval::{VVal, VValFun, Env};

 let mut ctx = EvalContext::new_default();

 ctx.set_global_var("xyz",
     &VValFun::new_fun(
         move |env: &mut Env, argc: usize| {
             let ok = false;
             if !ok {
                 Ok(env.new_err(
                     format!("Something was not ok!")))
             } else {
                 Ok(VVal::Bol(true))
             }
         }, None, None, false));
Source

pub fn new_panic(&self, val: VVal) -> StackAction

Source

pub fn new_panic_argv(&self, val: VVal, args: VVal) -> StackAction

Trait Implementations§

Source§

impl Debug for Env

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Env

§

impl !RefUnwindSafe for Env

§

impl !Send for Env

§

impl !Sync for Env

§

impl Unpin for Env

§

impl !UnwindSafe for Env

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.