Struct wlambda::compiler::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
sourceimpl EvalContext
impl EvalContext
sourcepub fn new(global: GlobalEnvRef) -> EvalContext
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);
sourcepub fn new_empty_global_env() -> EvalContext
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")
sourcepub fn new_default() -> EvalContext
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());
sourcepub fn get_exports(&self) -> SymbolTable
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);
pub fn new_with_user(
global: GlobalEnvRef,
user: Rc<RefCell<dyn Any>>
) -> EvalContext
sourcepub fn eval_ast(&mut self, ast: &VVal) -> Result<VVal, EvalError>
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());
sourcepub fn eval_file(&mut self, filename: &str) -> Result<VVal, EvalError>
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!");
sourcepub fn eval(&mut self, s: &str) -> Result<VVal, EvalError>
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());
sourcepub fn eval_string(
&mut self,
code: &str,
filename: &str
) -> Result<VVal, EvalError>
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!");
sourcepub fn call(&mut self, f: &VVal, args: &[VVal]) -> Result<VVal, StackAction>
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);
sourcepub fn set_global_var(&mut self, var: &str, val: &VVal)
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);
sourcepub fn get_global_var(&mut self, var: &str) -> Option<VVal>
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
sourceimpl Clone for EvalContext
impl Clone for EvalContext
sourcefn clone(&self) -> EvalContext
fn clone(&self) -> EvalContext
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
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
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more