[][src]Struct wlambda::compiler::EvalContext

pub struct EvalContext {
    pub global: GlobalEnvRef,
    pub local: Rc<RefCell<Env>>,
    // some fields omitted
}

This context holds all the data to compile and execute a piece of WLambda code. The context is not shareable between threads. For inter thread communication I suggest to look at wlambda::threads::MsgHandle.

It can be this easy to create a context:

 let mut ctx = wlambda::EvalContext::new_default();
 let ret = ctx.eval("10 + 20").unwrap().i();

 assert_eq!(ret, 30);

 // Also works beyond differnt eval() calls:
 ctx.eval("!:global X = 10").unwrap();

 let ret = ctx.eval("X").unwrap().i();
 assert_eq!(ret, 10);

 // You can access the global environment later too:
 assert_eq!(ctx.get_global_var("X").unwrap().i(),
            10);

 // You can even store top level local variables beyond one eval():
 ctx.eval("!toplevel_var = { _ + 20 }").unwrap();
 let ret = ctx.eval("toplevel_var 11").unwrap().i();

 assert_eq!(ret, 31);

You can also explicitly setup a global environment:

 use wlambda::{GlobalEnv, EvalContext, VVal};

 let genv = GlobalEnv::new_default();

 genv.borrow_mut().set_var("xyz", &VVal::Int(31347));

 let mut ctx = EvalContext::new(genv);
 let ret = ctx.eval("xyz - 10").unwrap().i();

 assert_eq!(ret, 31337);

Fields

global: GlobalEnvRef

Holds the reference to the supplied or internally created GlobalEnv.

local: Rc<RefCell<Env>>

Holds the top level environment data accross multiple eval() invocations.

Methods

impl EvalContext[src]

pub fn new(global: GlobalEnvRef) -> EvalContext[src]

pub fn new_empty_global_env() -> EvalContext[src]

pub fn new_default() -> EvalContext[src]

A shortcut: This creates a new EvalContext with a GlobalEnv::new_default() global environment.

This is a shorthand for:

 wlambda::compiler::EvalContext::new(
     wlambda::compiler::GlobalEnv::new_default());

pub fn get_exports(&self) -> SymbolTable[src]

pub fn new_with_user(
    global: GlobalEnvRef,
    user: Rc<RefCell<dyn Any>>
) -> EvalContext
[src]

pub fn eval_ast(&mut self, ast: &VVal) -> Result<VVal, EvalError>[src]

Evaluates an AST of WLambda code and executes it with the given EvalContext.

use wlambda::parser;
let mut ctx = wlambda::EvalContext::new_default();

let s = "$[1,2,3]";
let ast = parser::parse(s, "somefilename").unwrap();
let r = &mut ctx.eval_ast(&ast).unwrap();

println!("Res: {}", r.s());

pub fn eval(&mut self, s: &str) -> Result<VVal, EvalError>[src]

Evaluates a piece of WLambda code with the given EvalContext.

let mut ctx = wlambda::EvalContext::new_default();

let r = &mut ctx.eval("$[1,2,3]").unwrap();
println!("Res: {}", r.s());

pub fn eval_file(&mut self, filename: &str) -> Result<VVal, EvalError>[src]

Evaluates a WLambda code in a file with the given EvalContext.

let mut ctx = wlambda::EvalContext::new_default();

let r = &mut ctx.eval_file("examples/read_test.wl").unwrap();
assert_eq!(r.i(), 403, "matches contents!");

pub fn eval_string(
    &mut self,
    code: &str,
    filename: &str
) -> Result<VVal, EvalError>
[src]

Evaluates a WLambda code with the corresponding filename in the given EvalContext.

let mut ctx = wlambda::EvalContext::new_default();

let r = &mut ctx.eval_string("403", "examples/read_test.wl").unwrap();
assert_eq!(r.i(), 403, "matches contents!");

pub fn call(&mut self, f: &VVal, args: &[VVal]) -> Result<VVal, StackAction>[src]

Calls a wlambda function with the given EvalContext.

use wlambda::{VVal, EvalContext};
let mut ctx = EvalContext::new_default();

let returned_func = &mut ctx.eval("{ _ + _1 }").unwrap();
assert_eq!(
    ctx.call(returned_func,
             &vec![VVal::Int(10), VVal::Int(11)]).unwrap().i(),
    21);

pub fn set_global_var(&mut self, var: &str, val: &VVal)[src]

Sets a global variable for the scripts to access.

use wlambda::{VVal, EvalContext};
let mut ctx = EvalContext::new_default();

ctx.set_global_var("XXX", &VVal::Int(200));

assert_eq!(ctx.eval("XXX * 2").unwrap().i(), 400);

pub fn get_global_var(&mut self, var: &str) -> Option<VVal>[src]

Gets the value of a global variable from the script:

use wlambda::{VVal, EvalContext};
let mut ctx = EvalContext::new_default();

assert_eq!(ctx.eval("!:global XXX = 22 * 2; XXX").unwrap().i(), 44);

Trait Implementations

impl Clone for EvalContext[src]

impl Debug for EvalContext[src]

Auto Trait Implementations

Blanket Implementations

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

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

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

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

impl<T, U> Into<U> for T where
    U: From<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.