Add support for functions declared in script
This commit is contained in:
parent
a8fa76c2fc
commit
d8dd673459
315
src/engine.rs
315
src/engine.rs
@ -4,7 +4,7 @@ use std::any::Any;
|
|||||||
use std::boxed::Box;
|
use std::boxed::Box;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
use parser::{lex, parse, Expr, Stmt };
|
use parser::{lex, parse, Expr, Stmt, FnDef };
|
||||||
use fn_register::FnRegister;
|
use fn_register::FnRegister;
|
||||||
|
|
||||||
use std::ops::{Add, Sub, Mul, Div};
|
use std::ops::{Add, Sub, Mul, Div};
|
||||||
@ -16,7 +16,8 @@ pub enum EvalError {
|
|||||||
FunctionArgMismatch,
|
FunctionArgMismatch,
|
||||||
FunctionCallNotSupported,
|
FunctionCallNotSupported,
|
||||||
IfGuardMismatch,
|
IfGuardMismatch,
|
||||||
VariableNotFound
|
VariableNotFound,
|
||||||
|
FunctionArityNotSupported
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Error for EvalError {
|
impl Error for EvalError {
|
||||||
@ -27,6 +28,7 @@ impl Error for EvalError {
|
|||||||
EvalError::FunctionCallNotSupported => "Function call with > 2 argument not supported",
|
EvalError::FunctionCallNotSupported => "Function call with > 2 argument not supported",
|
||||||
EvalError::IfGuardMismatch => "If guards expect boolean expression",
|
EvalError::IfGuardMismatch => "If guards expect boolean expression",
|
||||||
EvalError::VariableNotFound => "Variable not found",
|
EvalError::VariableNotFound => "Variable not found",
|
||||||
|
EvalError::FunctionArityNotSupported => "Functions of more than 3 parameters are not yet supported"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -89,23 +91,52 @@ fn or(x: bool, y: bool) -> bool {
|
|||||||
x || y
|
x || y
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Engine {
|
pub enum Arity3 {
|
||||||
pub fns_arity_3: HashMap<String, Vec<Box<Fn(&mut Box<Any>, &mut Box<Any>, &mut Box<Any>)->Result<Box<Any>, EvalError>>>>,
|
ExternalFn(Box<Fn(&mut Box<Any>, &mut Box<Any>, &mut Box<Any>)->Result<Box<Any>, EvalError>>),
|
||||||
pub fns_arity_2: HashMap<String, Vec<Box<Fn(&mut Box<Any>, &mut Box<Any>)->Result<Box<Any>, EvalError>>>>,
|
InternalFn(FnDef)
|
||||||
pub fns_arity_1: HashMap<String, Vec<Box<Fn(&mut Box<Any>)->Result<Box<Any>, EvalError>>>>,
|
|
||||||
pub fns_arity_0: HashMap<String, Vec<Box<Fn()->Result<Box<Any>, EvalError>>>>,
|
|
||||||
pub scope: Vec<(String, Box<Any>)>
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub enum Arity2 {
|
||||||
|
ExternalFn(Box<Fn(&mut Box<Any>, &mut Box<Any>)->Result<Box<Any>, EvalError>>),
|
||||||
|
InternalFn(FnDef)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum Arity1 {
|
||||||
|
ExternalFn(Box<Fn(&mut Box<Any>)->Result<Box<Any>, EvalError>>),
|
||||||
|
InternalFn(FnDef)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum Arity0 {
|
||||||
|
ExternalFn(Box<Fn()->Result<Box<Any>, EvalError>>),
|
||||||
|
InternalFn(FnDef)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Engine {
|
||||||
|
pub fns_arity_3: HashMap<String, Vec<Arity3>>,
|
||||||
|
pub fns_arity_2: HashMap<String, Vec<Arity2>>,
|
||||||
|
pub fns_arity_1: HashMap<String, Vec<Arity1>>,
|
||||||
|
pub fns_arity_0: HashMap<String, Vec<Arity0>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type Scope = Vec<(String, Box<Any>)>;
|
||||||
|
|
||||||
impl Engine {
|
impl Engine {
|
||||||
pub fn call_fn_0_arg(fns_arity_0: &HashMap<String, Vec<Box<Fn()->Result<Box<Any>, EvalError>>>>, name: &str) -> Result<Box<Any>, EvalError> {
|
fn call_fn_0_arg(&self, name: &str) -> Result<Box<Any>, EvalError> {
|
||||||
match fns_arity_0.get(name) {
|
match self.fns_arity_0.get(name) {
|
||||||
Some(vf) => {
|
Some(vf) => {
|
||||||
for f in vf {
|
for arr_f in vf {
|
||||||
let invoke = f();
|
match arr_f {
|
||||||
match invoke {
|
& Arity0::ExternalFn(ref f) => {
|
||||||
Ok(v) => return Ok(v),
|
let invoke = f();
|
||||||
_ => ()
|
match invoke {
|
||||||
|
Ok(v) => return Ok(v),
|
||||||
|
_ => ()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
& Arity0::InternalFn(ref f) => {
|
||||||
|
let mut new_scope: Scope = Vec::new();
|
||||||
|
return self.eval_stmt(&mut new_scope, &*f.body);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
Err(EvalError::FunctionArgMismatch)
|
Err(EvalError::FunctionArgMismatch)
|
||||||
@ -114,16 +145,27 @@ impl Engine {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn call_fn_1_arg(fns_arity_1: &HashMap<String, Vec<Box<Fn(&mut Box<Any>)->Result<Box<Any>, EvalError>>>>,
|
fn call_fn_1_arg(&self, name: &str, arg1: &mut Box<Any>) -> Result<Box<Any>, EvalError> {
|
||||||
name: &str, arg1: &mut Box<Any>) -> Result<Box<Any>, EvalError> {
|
match self.fns_arity_1.get(name) {
|
||||||
|
|
||||||
match fns_arity_1.get(name) {
|
|
||||||
Some(vf) => {
|
Some(vf) => {
|
||||||
for f in vf {
|
for arr_f in vf {
|
||||||
let invoke = f(arg1);
|
match arr_f {
|
||||||
match invoke {
|
& Arity1::ExternalFn(ref f) => {
|
||||||
Ok(v) => return Ok(v),
|
let invoke = f(arg1);
|
||||||
_ => ()
|
match invoke {
|
||||||
|
Ok(v) => return Ok(v),
|
||||||
|
_ => ()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
& Arity1::InternalFn(ref f) => {
|
||||||
|
let mut new_scope: Scope = Vec::new();
|
||||||
|
let result = self.call_fn_1_arg("clone", arg1);
|
||||||
|
match result {
|
||||||
|
Ok(r) => new_scope.push((f.params[0].clone(), r)),
|
||||||
|
_ => return Err(EvalError::FunctionArgMismatch)
|
||||||
|
}
|
||||||
|
return self.eval_stmt(&mut new_scope, &*f.body);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
Err(EvalError::FunctionArgMismatch)
|
Err(EvalError::FunctionArgMismatch)
|
||||||
@ -132,16 +174,31 @@ impl Engine {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn call_fn_2_arg(fns_arity_2: &HashMap<String, Vec<Box<Fn(&mut Box<Any>, &mut Box<Any>)->Result<Box<Any>, EvalError>>>>,
|
fn call_fn_2_arg(&self, name: &str, arg1: &mut Box<Any>, arg2: &mut Box<Any>) -> Result<Box<Any>, EvalError> {
|
||||||
name: &str, arg1: &mut Box<Any>, arg2: &mut Box<Any>) -> Result<Box<Any>, EvalError> {
|
match self.fns_arity_2.get(name) {
|
||||||
|
|
||||||
match fns_arity_2.get(name) {
|
|
||||||
Some(vf) => {
|
Some(vf) => {
|
||||||
for f in vf {
|
for arr_f in vf {
|
||||||
let invoke = f(arg1, arg2);
|
match arr_f {
|
||||||
match invoke {
|
& Arity2::ExternalFn(ref f) => {
|
||||||
Ok(v) => return Ok(v),
|
let invoke = f(arg1, arg2);
|
||||||
_ => ()
|
match invoke {
|
||||||
|
Ok(v) => return Ok(v),
|
||||||
|
_ => ()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
& Arity2::InternalFn(ref f) => {
|
||||||
|
let mut new_scope: Scope = Vec::new();
|
||||||
|
let result1 = self.call_fn_1_arg("clone", arg1);
|
||||||
|
let result2 = self.call_fn_1_arg("clone", arg2);
|
||||||
|
match (result1, result2) {
|
||||||
|
(Ok(r1), Ok(r2)) => {
|
||||||
|
new_scope.push((f.params[0].clone(), r1));
|
||||||
|
new_scope.push((f.params[1].clone(), r2));
|
||||||
|
},
|
||||||
|
_ => return Err(EvalError::FunctionArgMismatch)
|
||||||
|
}
|
||||||
|
return self.eval_stmt(&mut new_scope, &*f.body);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
Err(EvalError::FunctionArgMismatch)
|
Err(EvalError::FunctionArgMismatch)
|
||||||
@ -150,16 +207,33 @@ impl Engine {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn call_fn_3_arg(fns_arity_3: &HashMap<String, Vec<Box<Fn(&mut Box<Any>, &mut Box<Any>, &mut Box<Any>)->Result<Box<Any>, EvalError>>>>,
|
pub fn call_fn_3_arg(&self, name: &str, arg1: &mut Box<Any>, arg2: &mut Box<Any>, arg3: &mut Box<Any>) -> Result<Box<Any>, EvalError> {
|
||||||
name: &str, arg1: &mut Box<Any>, arg2: &mut Box<Any>, arg3: &mut Box<Any>) -> Result<Box<Any>, EvalError> {
|
match self.fns_arity_3.get(name) {
|
||||||
|
|
||||||
match fns_arity_3.get(name) {
|
|
||||||
Some(vf) => {
|
Some(vf) => {
|
||||||
for f in vf {
|
for arr_f in vf {
|
||||||
let invoke = f(arg1, arg2, arg3);
|
match arr_f {
|
||||||
match invoke {
|
& Arity3::ExternalFn(ref f) => {
|
||||||
Ok(v) => return Ok(v),
|
let invoke = f(arg1, arg2, arg3);
|
||||||
_ => ()
|
match invoke {
|
||||||
|
Ok(v) => return Ok(v),
|
||||||
|
_ => ()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
& Arity3::InternalFn(ref f) => {
|
||||||
|
let mut new_scope: Scope = Vec::new();
|
||||||
|
let result1 = self.call_fn_1_arg("clone", arg1);
|
||||||
|
let result2 = self.call_fn_1_arg("clone", arg2);
|
||||||
|
let result3 = self.call_fn_1_arg("clone", arg3);
|
||||||
|
match (result1, result2, result3) {
|
||||||
|
(Ok(r1), Ok(r2), Ok(r3)) => {
|
||||||
|
new_scope.push((f.params[0].clone(), r1));
|
||||||
|
new_scope.push((f.params[1].clone(), r2));
|
||||||
|
new_scope.push((f.params[2].clone(), r3));
|
||||||
|
},
|
||||||
|
_ => return Err(EvalError::FunctionArgMismatch)
|
||||||
|
}
|
||||||
|
return self.eval_stmt(&mut new_scope, &*f.body);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
Err(EvalError::FunctionArgMismatch)
|
Err(EvalError::FunctionArgMismatch)
|
||||||
@ -174,14 +248,15 @@ impl Engine {
|
|||||||
&(clone_helper as fn(T)->T).register(self, "clone");
|
&(clone_helper as fn(T)->T).register(self, "clone");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn eval_expr(&mut self, expr: &Expr) -> Result<Box<Any>, EvalError> {
|
fn eval_expr(&self, scope: &mut Scope, expr: &Expr) -> Result<Box<Any>, EvalError> {
|
||||||
match *expr {
|
match *expr {
|
||||||
Expr::IntConst(i) => Ok(Box::new(i)),
|
Expr::IntConst(i) => Ok(Box::new(i)),
|
||||||
Expr::Identifier(ref id) => {
|
Expr::Identifier(ref id) => {
|
||||||
for &mut (ref name, ref mut val) in &mut self.scope.iter_mut().rev() {
|
for &mut (ref name, ref mut val) in &mut scope.iter_mut().rev() {
|
||||||
if *id == *name {
|
if *id == *name {
|
||||||
//Ideally, we wouldn't have to inline this call above
|
//Ideally, we wouldn't have to inline this call above
|
||||||
//let result = self.call_fn_1_arg("clone", val);
|
let result = self.call_fn_1_arg("clone", val);
|
||||||
|
/*
|
||||||
let result = match self.fns_arity_1.get("clone") {
|
let result = match self.fns_arity_1.get("clone") {
|
||||||
Some(vf) => {
|
Some(vf) => {
|
||||||
for f in vf {
|
for f in vf {
|
||||||
@ -195,7 +270,7 @@ impl Engine {
|
|||||||
}
|
}
|
||||||
None => Err(EvalError::FunctionNotFound)
|
None => Err(EvalError::FunctionNotFound)
|
||||||
};
|
};
|
||||||
|
*/
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -204,8 +279,8 @@ impl Engine {
|
|||||||
Expr::Assignment(ref id, ref rhs) => {
|
Expr::Assignment(ref id, ref rhs) => {
|
||||||
match **id {
|
match **id {
|
||||||
Expr::Identifier(ref n) => {
|
Expr::Identifier(ref n) => {
|
||||||
let rhs_val = try!(self.eval_expr(rhs));
|
let rhs_val = try!(self.eval_expr(scope, rhs));
|
||||||
for &mut (ref name, ref mut val) in &mut self.scope.iter_mut().rev() {
|
for &mut (ref name, ref mut val) in &mut scope.iter_mut().rev() {
|
||||||
if *n == *name {
|
if *n == *name {
|
||||||
|
|
||||||
*val = rhs_val;
|
*val = rhs_val;
|
||||||
@ -220,25 +295,25 @@ impl Engine {
|
|||||||
}
|
}
|
||||||
Expr::FnCall(ref fn_name, ref args) => {
|
Expr::FnCall(ref fn_name, ref args) => {
|
||||||
if args.len() == 0 {
|
if args.len() == 0 {
|
||||||
Engine::call_fn_0_arg(&self.fns_arity_0, &fn_name)
|
self.call_fn_0_arg(&fn_name)
|
||||||
}
|
}
|
||||||
else if args.len() == 1 {
|
else if args.len() == 1 {
|
||||||
let mut arg = try!(self.eval_expr(&args[0]));
|
let mut arg = try!(self.eval_expr(scope, &args[0]));
|
||||||
|
|
||||||
Engine::call_fn_1_arg(&self.fns_arity_1, &fn_name, &mut arg)
|
self.call_fn_1_arg(&fn_name, &mut arg)
|
||||||
}
|
}
|
||||||
else if args.len() == 2 {
|
else if args.len() == 2 {
|
||||||
let mut arg1 = try!(self.eval_expr(&args[0]));
|
let mut arg1 = try!(self.eval_expr(scope, &args[0]));
|
||||||
let mut arg2 = try!(self.eval_expr(&args[1]));
|
let mut arg2 = try!(self.eval_expr(scope, &args[1]));
|
||||||
|
|
||||||
Engine::call_fn_2_arg(&self.fns_arity_2, &fn_name, &mut arg1, &mut arg2)
|
self.call_fn_2_arg(&fn_name, &mut arg1, &mut arg2)
|
||||||
}
|
}
|
||||||
else if args.len() == 3 {
|
else if args.len() == 3 {
|
||||||
let mut arg1 = try!(self.eval_expr(&args[0]));
|
let mut arg1 = try!(self.eval_expr(scope, &args[0]));
|
||||||
let mut arg2 = try!(self.eval_expr(&args[1]));
|
let mut arg2 = try!(self.eval_expr(scope, &args[1]));
|
||||||
let mut arg3 = try!(self.eval_expr(&args[1]));
|
let mut arg3 = try!(self.eval_expr(scope, &args[1]));
|
||||||
|
|
||||||
Engine::call_fn_3_arg(&self.fns_arity_3, &fn_name, &mut arg1, &mut arg2, &mut arg3)
|
self.call_fn_3_arg(&fn_name, &mut arg1, &mut arg2, &mut arg3)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Err(EvalError::FunctionCallNotSupported)
|
Err(EvalError::FunctionCallNotSupported)
|
||||||
@ -246,30 +321,30 @@ impl Engine {
|
|||||||
}
|
}
|
||||||
Expr::MethodCall(ref target, ref fn_name, ref args) => {
|
Expr::MethodCall(ref target, ref fn_name, ref args) => {
|
||||||
if args.len() == 0 {
|
if args.len() == 0 {
|
||||||
for &mut (ref name, ref mut val) in &mut self.scope.iter_mut().rev() {
|
for &mut (ref name, ref mut val) in &mut scope.iter_mut().rev() {
|
||||||
if *target == *name {
|
if *target == *name {
|
||||||
return Engine::call_fn_1_arg(&self.fns_arity_1, &fn_name, val);
|
return self.call_fn_1_arg(&fn_name, val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(EvalError::VariableNotFound)
|
Err(EvalError::VariableNotFound)
|
||||||
}
|
}
|
||||||
else if args.len() == 1 {
|
else if args.len() == 1 {
|
||||||
let mut arg = try!(self.eval_expr(&args[0]));
|
let mut arg = try!(self.eval_expr(scope, &args[0]));
|
||||||
|
|
||||||
for &mut (ref name, ref mut val) in &mut self.scope.iter_mut().rev() {
|
for &mut (ref name, ref mut val) in &mut scope.iter_mut().rev() {
|
||||||
if *target == *name {
|
if *target == *name {
|
||||||
return Engine::call_fn_2_arg(&self.fns_arity_2, &fn_name, val, &mut arg);
|
return self.call_fn_2_arg(&fn_name, val, &mut arg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(EvalError::VariableNotFound)
|
Err(EvalError::VariableNotFound)
|
||||||
}
|
}
|
||||||
else if args.len() == 2 {
|
else if args.len() == 2 {
|
||||||
let mut arg1 = try!(self.eval_expr(&args[0]));
|
let mut arg1 = try!(self.eval_expr(scope, &args[0]));
|
||||||
let mut arg2 = try!(self.eval_expr(&args[1]));
|
let mut arg2 = try!(self.eval_expr(scope, &args[1]));
|
||||||
|
|
||||||
for &mut (ref name, ref mut val) in &mut self.scope.iter_mut().rev() {
|
for &mut (ref name, ref mut val) in &mut scope.iter_mut().rev() {
|
||||||
if *target == *name {
|
if *target == *name {
|
||||||
return Engine::call_fn_3_arg(&self.fns_arity_3, &fn_name, val, &mut arg1, &mut arg2);
|
return self.call_fn_3_arg(&fn_name, val, &mut arg1, &mut arg2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(EvalError::VariableNotFound)
|
Err(EvalError::VariableNotFound)
|
||||||
@ -287,31 +362,31 @@ impl Engine {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn eval_stmt(&mut self, stmt: &Stmt) -> Result<Box<Any>, EvalError> {
|
fn eval_stmt(&self, scope: &mut Scope, stmt: &Stmt) -> Result<Box<Any>, EvalError> {
|
||||||
match *stmt {
|
match *stmt {
|
||||||
Stmt::Expr(ref e) => {
|
Stmt::Expr(ref e) => {
|
||||||
self.eval_expr(e)
|
self.eval_expr(scope, e)
|
||||||
}
|
}
|
||||||
Stmt::Block(ref b) => {
|
Stmt::Block(ref b) => {
|
||||||
let prev_len = self.scope.len();
|
let prev_len = scope.len();
|
||||||
let mut last_result : Result<Box<Any>, EvalError> = Ok(Box::new(0));
|
let mut last_result : Result<Box<Any>, EvalError> = Ok(Box::new(0));
|
||||||
|
|
||||||
for s in b.iter() {
|
for s in b.iter() {
|
||||||
last_result = self.eval_stmt(s)
|
last_result = self.eval_stmt(scope, s)
|
||||||
}
|
}
|
||||||
|
|
||||||
while self.scope.len() > prev_len {
|
while scope.len() > prev_len {
|
||||||
self.scope.pop();
|
scope.pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
return last_result;
|
return last_result;
|
||||||
}
|
}
|
||||||
Stmt::If(ref guard, ref body) => {
|
Stmt::If(ref guard, ref body) => {
|
||||||
let guard_result = try!(self.eval_expr(guard));
|
let guard_result = try!(self.eval_expr(scope, guard));
|
||||||
match guard_result.downcast::<bool>() {
|
match guard_result.downcast::<bool>() {
|
||||||
Ok(g) => {
|
Ok(g) => {
|
||||||
if *g {
|
if *g {
|
||||||
self.eval_stmt(body)
|
self.eval_stmt(scope, body)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Ok(Box::new(()))
|
Ok(Box::new(()))
|
||||||
@ -322,11 +397,11 @@ impl Engine {
|
|||||||
}
|
}
|
||||||
Stmt::While(ref guard, ref body) => {
|
Stmt::While(ref guard, ref body) => {
|
||||||
loop {
|
loop {
|
||||||
let guard_result = try!(self.eval_expr(guard));
|
let guard_result = try!(self.eval_expr(scope, guard));
|
||||||
match guard_result.downcast::<bool>() {
|
match guard_result.downcast::<bool>() {
|
||||||
Ok(g) => {
|
Ok(g) => {
|
||||||
if *g {
|
if *g {
|
||||||
try!(self.eval_stmt(body));
|
try!(self.eval_stmt(scope, body));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return Ok(Box::new(()));
|
return Ok(Box::new(()));
|
||||||
@ -339,11 +414,11 @@ impl Engine {
|
|||||||
Stmt::Var(ref name, ref init) => {
|
Stmt::Var(ref name, ref init) => {
|
||||||
match init {
|
match init {
|
||||||
& Some(ref v) => {
|
& Some(ref v) => {
|
||||||
let i = try!(self.eval_expr(v));
|
let i = try!(self.eval_expr(scope, v));
|
||||||
self.scope.push((name.clone(), i));
|
scope.push((name.clone(), i));
|
||||||
},
|
},
|
||||||
& None => {
|
& None => {
|
||||||
self.scope.push((name.clone(), Box::new(())));
|
scope.push((name.clone(), Box::new(())));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
Ok(Box::new(()))
|
Ok(Box::new(()))
|
||||||
@ -352,6 +427,12 @@ impl Engine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn eval(&mut self, input: String) -> Result<Box<Any>, EvalError> {
|
pub fn eval(&mut self, input: String) -> Result<Box<Any>, EvalError> {
|
||||||
|
let mut scope: Scope = Vec::new();
|
||||||
|
|
||||||
|
self.eval_with_scope(&mut scope, input)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn eval_with_scope(&mut self, scope: &mut Scope, input: String) -> Result<Box<Any>, EvalError> {
|
||||||
let tokens = lex(&input);
|
let tokens = lex(&input);
|
||||||
|
|
||||||
let mut peekables = tokens.peekable();
|
let mut peekables = tokens.peekable();
|
||||||
@ -360,23 +441,43 @@ impl Engine {
|
|||||||
match tree {
|
match tree {
|
||||||
Ok((ref os, ref fns)) => {
|
Ok((ref os, ref fns)) => {
|
||||||
let mut x: Result<Box<Any>, EvalError> = Ok(Box::new(()));
|
let mut x: Result<Box<Any>, EvalError> = Ok(Box::new(()));
|
||||||
|
|
||||||
for f in fns {
|
for f in fns {
|
||||||
if f.params.len() == 0 {
|
match f.params.len() {
|
||||||
let local_f = f.clone();
|
0 => {
|
||||||
let ent = self.fns_arity_0.entry(local_f.name).or_insert(Vec::new());
|
let name = f.name.clone();
|
||||||
let wrapped : Box<Fn()->Result<Box<Any>, EvalError>> =
|
let local_f = f.clone();
|
||||||
Box::new(move || { Ok(Box::new(0)) } );
|
let ent = self.fns_arity_0.entry(name).or_insert(Vec::new());
|
||||||
//move || { self.eval_stmt(&local_f.body) }
|
(*ent).push(Arity0::InternalFn(local_f));
|
||||||
(*ent).push(wrapped);
|
},
|
||||||
|
1 => {
|
||||||
|
let name = f.name.clone();
|
||||||
|
let local_f = f.clone();
|
||||||
|
let ent = self.fns_arity_1.entry(name).or_insert(Vec::new());
|
||||||
|
(*ent).push(Arity1::InternalFn(local_f));
|
||||||
|
},
|
||||||
|
2 => {
|
||||||
|
let name = f.name.clone();
|
||||||
|
let local_f = f.clone();
|
||||||
|
let ent = self.fns_arity_2.entry(name).or_insert(Vec::new());
|
||||||
|
(*ent).push(Arity2::InternalFn(local_f));
|
||||||
|
},
|
||||||
|
3 => {
|
||||||
|
let name = f.name.clone();
|
||||||
|
let local_f = f.clone();
|
||||||
|
let ent = self.fns_arity_3.entry(name).or_insert(Vec::new());
|
||||||
|
(*ent).push(Arity3::InternalFn(local_f));
|
||||||
|
},
|
||||||
|
_ => return Err(EvalError::FunctionArityNotSupported)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for o in os {
|
for o in os {
|
||||||
x = self.eval_stmt(&o)
|
x = self.eval_stmt(scope, &o);
|
||||||
}
|
}
|
||||||
x
|
x
|
||||||
}
|
}
|
||||||
Err(_) => Err(EvalError::FunctionArgMismatch),
|
Err(_) => Err(EvalError::FunctionArgMismatch)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -428,8 +529,7 @@ impl Engine {
|
|||||||
fns_arity_0: HashMap::new(),
|
fns_arity_0: HashMap::new(),
|
||||||
fns_arity_1: HashMap::new(),
|
fns_arity_1: HashMap::new(),
|
||||||
fns_arity_2: HashMap::new(),
|
fns_arity_2: HashMap::new(),
|
||||||
fns_arity_3: HashMap::new(),
|
fns_arity_3: HashMap::new()
|
||||||
scope: Vec::new()
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Engine::register_default_lib(&mut engine);
|
Engine::register_default_lib(&mut engine);
|
||||||
@ -464,7 +564,7 @@ fn test_addition() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_bool_op() {
|
fn test_bool_op1() {
|
||||||
let mut engine = Engine::new();
|
let mut engine = Engine::new();
|
||||||
|
|
||||||
if let Ok(result) = engine.eval("true && (false || true)".to_string()).unwrap().downcast::<bool>() {
|
if let Ok(result) = engine.eval("true && (false || true)".to_string()).unwrap().downcast::<bool>() {
|
||||||
@ -473,6 +573,11 @@ fn test_bool_op() {
|
|||||||
else {
|
else {
|
||||||
assert!(false);
|
assert!(false);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_bool_op2() {
|
||||||
|
let mut engine = Engine::new();
|
||||||
|
|
||||||
if let Ok(result) = engine.eval("false && (false || true)".to_string()).unwrap().downcast::<bool>() {
|
if let Ok(result) = engine.eval("false && (false || true)".to_string()).unwrap().downcast::<bool>() {
|
||||||
assert_eq!(*result, false);
|
assert_eq!(*result, false);
|
||||||
@ -507,30 +612,32 @@ fn test_if() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
||||||
fn test_var_scope() {
|
fn test_var_scope() {
|
||||||
let mut engine = Engine::new();
|
let mut engine = Engine::new();
|
||||||
|
let mut scope: Scope = Vec::new();
|
||||||
|
|
||||||
if let Ok(_) = engine.eval("var x = 4 + 5".to_string()) { } else { assert!(false); }
|
if let Ok(_) = engine.eval_with_scope(&mut scope, "var x = 4 + 5".to_string()) { } else { assert!(false); }
|
||||||
|
|
||||||
if let Ok(result) = engine.eval("x".to_string()).unwrap().downcast::<i32>() {
|
if let Ok(result) = engine.eval_with_scope(&mut scope, "x".to_string()).unwrap().downcast::<i32>() {
|
||||||
assert_eq!(*result, 9);
|
assert_eq!(*result, 9);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
assert!(false);
|
assert!(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Ok(_) = engine.eval("x = x + 1; x = x + 2;".to_string()) { } else { assert!(false); }
|
if let Ok(_) = engine.eval_with_scope(&mut scope, "x = x + 1; x = x + 2;".to_string()) { } else { assert!(false); }
|
||||||
|
|
||||||
if let Ok(result) = engine.eval("x".to_string()).unwrap().downcast::<i32>() {
|
if let Ok(result) = engine.eval_with_scope(&mut scope, "x".to_string()).unwrap().downcast::<i32>() {
|
||||||
assert_eq!(*result, 12);
|
assert_eq!(*result, 12);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
assert!(false);
|
assert!(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Ok(_) = engine.eval("{var x = 3}".to_string()) { } else { assert!(false); }
|
if let Ok(_) = engine.eval_with_scope(&mut scope, "{var x = 3}".to_string()) { } else { assert!(false); }
|
||||||
|
|
||||||
if let Ok(result) = engine.eval("x".to_string()).unwrap().downcast::<i32>() {
|
if let Ok(result) = engine.eval_with_scope(&mut scope, "x".to_string()).unwrap().downcast::<i32>() {
|
||||||
assert_eq!(*result, 12);
|
assert_eq!(*result, 12);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -570,3 +677,15 @@ fn test_method_call() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_internal_fn() {
|
||||||
|
let mut engine = Engine::new();
|
||||||
|
|
||||||
|
if let Ok(result) = engine.eval( "fn addme(a, b) { a+b } addme(3, 4)".to_string()).unwrap().downcast::<i32>() {
|
||||||
|
assert_eq!(*result, 7);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
assert!(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use std::any::Any;
|
use std::any::Any;
|
||||||
use std::boxed::Box;
|
use std::boxed::Box;
|
||||||
|
|
||||||
use engine::{EvalError, Engine};
|
use engine::{EvalError, Engine, Arity0, Arity1, Arity2, Arity3};
|
||||||
|
|
||||||
pub trait FnRegister {
|
pub trait FnRegister {
|
||||||
fn register(self, engine: &mut Engine, name: &str);
|
fn register(self, engine: &mut Engine, name: &str);
|
||||||
@ -24,7 +24,7 @@ impl<T: Any+Clone, U: Any+Clone, V: Any+Clone, W: Any+Clone> FnRegister for fn(&
|
|||||||
);
|
);
|
||||||
|
|
||||||
let ent = engine.fns_arity_3.entry(name.to_string()).or_insert(Vec::new());
|
let ent = engine.fns_arity_3.entry(name.to_string()).or_insert(Vec::new());
|
||||||
(*ent).push(wrapped);
|
(*ent).push(Arity3::ExternalFn(wrapped));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,7 +45,7 @@ impl<T: Any+Clone, U: Any+Clone, V: Any+Clone, W: Any+Clone> FnRegister for fn(T
|
|||||||
);
|
);
|
||||||
|
|
||||||
let ent = engine.fns_arity_3.entry(name.to_string()).or_insert(Vec::new());
|
let ent = engine.fns_arity_3.entry(name.to_string()).or_insert(Vec::new());
|
||||||
(*ent).push(wrapped);
|
(*ent).push(Arity3::ExternalFn(wrapped));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,7 +65,7 @@ impl<T: Any+Clone, U: Any+Clone, V: Any+Clone> FnRegister for fn(&mut T, U)->V {
|
|||||||
);
|
);
|
||||||
|
|
||||||
let ent = engine.fns_arity_2.entry(name.to_string()).or_insert(Vec::new());
|
let ent = engine.fns_arity_2.entry(name.to_string()).or_insert(Vec::new());
|
||||||
(*ent).push(wrapped);
|
(*ent).push(Arity2::ExternalFn(wrapped));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -85,7 +85,7 @@ impl<T: Any+Clone, U: Any+Clone, V: Any+Clone> FnRegister for fn(T, U)->V {
|
|||||||
);
|
);
|
||||||
|
|
||||||
let ent = engine.fns_arity_2.entry(name.to_string()).or_insert(Vec::new());
|
let ent = engine.fns_arity_2.entry(name.to_string()).or_insert(Vec::new());
|
||||||
(*ent).push(wrapped);
|
(*ent).push(Arity2::ExternalFn(wrapped));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -104,7 +104,7 @@ impl<T: Any+Clone, U: Any+Clone> FnRegister for fn(&mut T)->U {
|
|||||||
);
|
);
|
||||||
|
|
||||||
let ent = engine.fns_arity_1.entry(name.to_string()).or_insert(Vec::new());
|
let ent = engine.fns_arity_1.entry(name.to_string()).or_insert(Vec::new());
|
||||||
(*ent).push(wrapped);
|
(*ent).push(Arity1::ExternalFn(wrapped));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -122,7 +122,7 @@ impl<T: Any+Clone, U: Any+Clone> FnRegister for fn(T)->U {
|
|||||||
);
|
);
|
||||||
|
|
||||||
let ent = engine.fns_arity_1.entry(name.to_string()).or_insert(Vec::new());
|
let ent = engine.fns_arity_1.entry(name.to_string()).or_insert(Vec::new());
|
||||||
(*ent).push(wrapped);
|
(*ent).push(Arity1::ExternalFn(wrapped));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -134,6 +134,6 @@ impl<T: Any+Clone> FnRegister for fn()->T {
|
|||||||
);
|
);
|
||||||
|
|
||||||
let ent = engine.fns_arity_0.entry(name.to_string()).or_insert(Vec::new());
|
let ent = engine.fns_arity_0.entry(name.to_string()).or_insert(Vec::new());
|
||||||
(*ent).push(wrapped);
|
(*ent).push(Arity0::ExternalFn(wrapped));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
21
src/main.rs
21
src/main.rs
@ -23,17 +23,18 @@ fn showit<T: Display>(x: &mut T) -> () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut engine = Engine::new();
|
|
||||||
&(showit as fn(x: &mut i32)->()).register(&mut engine, "print");
|
|
||||||
&(showit as fn(x: &mut i64)->()).register(&mut engine, "print");
|
|
||||||
&(showit as fn(x: &mut u32)->()).register(&mut engine, "print");
|
|
||||||
&(showit as fn(x: &mut u64)->()).register(&mut engine, "print");
|
|
||||||
&(showit as fn(x: &mut f32)->()).register(&mut engine, "print");
|
|
||||||
&(showit as fn(x: &mut f64)->()).register(&mut engine, "print");
|
|
||||||
&(showit as fn(x: &mut bool)->()).register(&mut engine, "print");
|
|
||||||
&(showit as fn(x: &mut String)->()).register(&mut engine, "print");
|
|
||||||
|
|
||||||
for fname in env::args().skip(1) {
|
for fname in env::args().skip(1) {
|
||||||
|
let mut engine = Engine::new();
|
||||||
|
|
||||||
|
&(showit as fn(x: &mut i32)->()).register(&mut engine, "print");
|
||||||
|
&(showit as fn(x: &mut i64)->()).register(&mut engine, "print");
|
||||||
|
&(showit as fn(x: &mut u32)->()).register(&mut engine, "print");
|
||||||
|
&(showit as fn(x: &mut u64)->()).register(&mut engine, "print");
|
||||||
|
&(showit as fn(x: &mut f32)->()).register(&mut engine, "print");
|
||||||
|
&(showit as fn(x: &mut f64)->()).register(&mut engine, "print");
|
||||||
|
&(showit as fn(x: &mut bool)->()).register(&mut engine, "print");
|
||||||
|
&(showit as fn(x: &mut String)->()).register(&mut engine, "print");
|
||||||
|
|
||||||
if let Ok(mut f) = File::open(fname.clone()) {
|
if let Ok(mut f) = File::open(fname.clone()) {
|
||||||
let mut contents = String::new();
|
let mut contents = String::new();
|
||||||
if let Ok(_) = f.read_to_string(&mut contents) {
|
if let Ok(_) = f.read_to_string(&mut contents) {
|
||||||
|
Loading…
Reference in New Issue
Block a user