Struct EvalContext

Source
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§

Source§

impl EvalContext

Source

pub fn new(global: GlobalEnvRef) -> EvalContext

Creates an EvalContext from a given GlobalEnv.

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

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

pub fn new_empty_global_env() -> EvalContext

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")
Source

pub fn new_default() -> EvalContext

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

pub fn get_exports(&self) -> SymbolTable

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

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

Source

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

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

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

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

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

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

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

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

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

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

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

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

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

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§

Source§

impl Clone for EvalContext

Source§

fn clone(&self) -> EvalContext

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for EvalContext

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.