[−][src]Struct wlambda::compiler::EvalContext
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
impl EvalContext
[src]
pub fn new(global: GlobalEnvRef) -> EvalContext
[src]
pub fn new_empty_global_env() -> EvalContext
[src]
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")
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]
global: GlobalEnvRef,
user: Rc<RefCell<dyn Any>>
) -> EvalContext
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_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(&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_string(
&mut self,
code: &str,
filename: &str
) -> Result<VVal, EvalError>
[src]
&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!");
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(); ctx.eval("!:global XXX = 22 * 2;"); assert_eq!(ctx.get_global_var("XXX").unwrap().i(), 44);
Trait Implementations
impl Clone for EvalContext
[src]
fn clone(&self) -> EvalContext
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]
impl Debug for EvalContext
[src]
Auto Trait Implementations
impl !RefUnwindSafe for EvalContext
impl !Send for EvalContext
impl !Sync for EvalContext
impl Unpin for EvalContext
impl !UnwindSafe for EvalContext
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,