pub struct EvalContext {
    pub global: GlobalEnvRef,
    pub local: Rc<RefCell<Env>>,
    /* private fields */
}
Expand description

This context holds all the data to compile and execute a piece of WLambda code. You can either use the default environment, or customize the EvalContext to your application’s needs. You can provide custom:

  • Global environemnt
  • Module resolver
  • Custom preset modules
  • Thread creator

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.

Implementations

Creates an EvalContext from a given GlobalEnv.

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

 let genv = GlobalEnv::new_default();
 let ctx = EvalContext::new(genv);

Creates a new EvalContext with an empty GlobalEnv. This means the EvalContext has none of the core or std library functions in it’s global environment and you have to provide them yourself. Either by loading them from WLambda by !@import std or !@wlambda. Or by manually binding in the corresponding SymbolTable(s).

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

 let mut ctx = EvalContext::new_empty_global_env();

 {
     let mut genv = ctx.global.borrow_mut();
     genv.set_module("wlambda", wlambda::prelude::core_symbol_table());
     genv.import_module_as("wlambda", "");
 }

 assert_eq!(ctx.eval("type $true").unwrap().s_raw(), "bool")

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());

Returns a SymbolTable of the locally exported symbols. This is useful, if you want to execute WLambda code yourself and use the local environment to export symbols.

 use wlambda::{EvalContext};

 let mut ctx = EvalContext::new_default();

 ctx.eval("!@export foo = 10").unwrap();

 let mut sym_tbl = ctx.get_exports();
 assert_eq!(sym_tbl.get("foo").unwrap().i(), 10);

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());

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!");

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());

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!");

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);

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);

Gets the value of a global variable from the script:

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

ctx.eval("!:global XXX = 22 * 2;");

assert_eq!(ctx.get_global_var("XXX").unwrap().i(), 44);

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.