Clean up trailing space and fix rhai_runner print out
This commit is contained in:
parent
be3d360913
commit
4e38b9e611
@ -23,7 +23,7 @@ fn main() {
|
||||
|
||||
match engine.eval_file::<()>(&fname) {
|
||||
Ok(_) => (),
|
||||
Err(e) => {println!("Error: {:?}", e)}
|
||||
Err(e) => {println!("Error: {}", e)}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ fn main() {
|
||||
let mut engine = Engine::new();
|
||||
|
||||
engine.register_fn("add", add);
|
||||
|
||||
|
||||
if let Ok(result) = engine.eval::<i32>("add(40, 2)") {
|
||||
println!("Answer: {}", result); // prints 42
|
||||
}
|
||||
|
@ -22,7 +22,7 @@ pub enum EvalAltResult {
|
||||
ErrorAssignmentToUnknownLHS,
|
||||
ErrorMismatchOutputType,
|
||||
ErrorCantOpenScriptFile,
|
||||
InternalErrorMalformedDotExpression,
|
||||
InternalErrorMalformedDotExpression,
|
||||
LoopBreak,
|
||||
Return(Box<Any>)
|
||||
}
|
||||
@ -152,7 +152,7 @@ impl Engine {
|
||||
new_scope.push((f.params[4].clone(), r5));
|
||||
},
|
||||
_ => return Err(EvalAltResult::ErrorFunctionArgMismatch)
|
||||
}
|
||||
}
|
||||
match self.eval_stmt(&mut new_scope, &*f.body) {
|
||||
Err(EvalAltResult::Return(x)) => return Ok(x),
|
||||
x => return x
|
||||
@ -188,7 +188,7 @@ impl Engine {
|
||||
new_scope.push((f.params[3].clone(), r4));
|
||||
},
|
||||
_ => return Err(EvalAltResult::ErrorFunctionArgMismatch)
|
||||
}
|
||||
}
|
||||
match self.eval_stmt(&mut new_scope, &*f.body) {
|
||||
Err(EvalAltResult::Return(x)) => return Ok(x),
|
||||
x => return x
|
||||
@ -222,7 +222,7 @@ impl Engine {
|
||||
new_scope.push((f.params[2].clone(), r3));
|
||||
},
|
||||
_ => return Err(EvalAltResult::ErrorFunctionArgMismatch)
|
||||
}
|
||||
}
|
||||
match self.eval_stmt(&mut new_scope, &*f.body) {
|
||||
Err(EvalAltResult::Return(x)) => return Ok(x),
|
||||
x => return x
|
||||
@ -254,7 +254,7 @@ impl Engine {
|
||||
new_scope.push((f.params[1].clone(), r2));
|
||||
},
|
||||
_ => return Err(EvalAltResult::ErrorFunctionArgMismatch)
|
||||
}
|
||||
}
|
||||
match self.eval_stmt(&mut new_scope, &*f.body) {
|
||||
Err(EvalAltResult::Return(x)) => return Ok(x),
|
||||
x => return x
|
||||
@ -284,7 +284,7 @@ impl Engine {
|
||||
new_scope.push((f.params[0].clone(), r1));
|
||||
},
|
||||
_ => return Err(EvalAltResult::ErrorFunctionArgMismatch)
|
||||
}
|
||||
}
|
||||
match self.eval_stmt(&mut new_scope, &*f.body) {
|
||||
Err(EvalAltResult::Return(x)) => return Ok(x),
|
||||
x => return x
|
||||
@ -330,21 +330,21 @@ impl Engine {
|
||||
self.register_fn("clone", clone_helper as fn(T)->T);
|
||||
}
|
||||
|
||||
pub fn register_get<T: Clone+Any, U: Clone+Any, F>(&mut self, name: &str, get_fn: F)
|
||||
pub fn register_get<T: Clone+Any, U: Clone+Any, F>(&mut self, name: &str, get_fn: F)
|
||||
where F : 'static+Fn(&mut T)->U {
|
||||
|
||||
let get_name = "get$".to_string() + name;
|
||||
|
||||
let get_name = "get$".to_string() + name;
|
||||
self.register_fn(&get_name, get_fn);
|
||||
}
|
||||
|
||||
pub fn register_set<T: Clone+Any, U: Clone+Any, F>(&mut self, name: &str, set_fn: F)
|
||||
where F : 'static+Fn(&mut T, U)->() {
|
||||
|
||||
let set_name = "set$".to_string() + name;
|
||||
|
||||
let set_name = "set$".to_string() + name;
|
||||
self.register_fn(&set_name, set_fn);
|
||||
}
|
||||
|
||||
pub fn register_get_set<T: Clone+Any, U: Clone+Any, F, G>(&mut self, name: &str, get_fn: F, set_fn: G)
|
||||
pub fn register_get_set<T: Clone+Any, U: Clone+Any, F, G>(&mut self, name: &str, get_fn: F, set_fn: G)
|
||||
where F : 'static+Fn(&mut T)->U, G : 'static+Fn(&mut T, U)->() {
|
||||
|
||||
self.register_get(name, get_fn);
|
||||
@ -353,7 +353,7 @@ impl Engine {
|
||||
|
||||
fn get_dot_val_helper(&self, scope: &mut Scope, this_ptr: &mut Box<Any>, dot_rhs: &Expr) -> Result<Box<Any>, EvalAltResult> {
|
||||
match *dot_rhs {
|
||||
Expr::FnCall(ref fn_name, ref args) => {
|
||||
Expr::FnCall(ref fn_name, ref args) => {
|
||||
if args.len() == 0 {
|
||||
return self.call_fn(&fn_name, Some(this_ptr), None, None, None, None, None);
|
||||
}
|
||||
@ -381,7 +381,7 @@ impl Engine {
|
||||
let mut arg3 = try!(self.eval_expr(scope, &args[2]));
|
||||
let mut arg4 = try!(self.eval_expr(scope, &args[3]));
|
||||
|
||||
return self.call_fn(&fn_name, Some(this_ptr), Some(&mut arg1), Some(&mut arg2), Some(&mut arg3),
|
||||
return self.call_fn(&fn_name, Some(this_ptr), Some(&mut arg1), Some(&mut arg2), Some(&mut arg3),
|
||||
Some(&mut arg4), None);
|
||||
}
|
||||
else if args.len() == 5 {
|
||||
@ -391,7 +391,7 @@ impl Engine {
|
||||
let mut arg4 = try!(self.eval_expr(scope, &args[3]));
|
||||
let mut arg5 = try!(self.eval_expr(scope, &args[4]));
|
||||
|
||||
return self.call_fn(&fn_name, Some(this_ptr), Some(&mut arg1), Some(&mut arg2), Some(&mut arg3),
|
||||
return self.call_fn(&fn_name, Some(this_ptr), Some(&mut arg1), Some(&mut arg2), Some(&mut arg3),
|
||||
Some(&mut arg4), Some(&mut arg5));
|
||||
}
|
||||
else {
|
||||
@ -406,7 +406,7 @@ impl Engine {
|
||||
let idx = try!(self.eval_expr(scope, idx_raw));
|
||||
|
||||
let get_fn_name = "get$".to_string() + id;
|
||||
|
||||
|
||||
if let Ok(mut val) = self.call_fn(&get_fn_name, Some(this_ptr), None, None, None, None, None) {
|
||||
if let Ok(i) = idx.downcast::<i32>() {
|
||||
if let Some(arr_typed) = (*val).downcast_mut() as Option<&mut Vec<Box<Any>>> {
|
||||
@ -433,7 +433,7 @@ impl Engine {
|
||||
match result {
|
||||
Ok(mut v) => return self.get_dot_val_helper(scope, &mut v, inner_rhs),
|
||||
e => return e
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => Err(EvalAltResult::InternalErrorMalformedDotExpression)
|
||||
}
|
||||
@ -478,13 +478,13 @@ impl Engine {
|
||||
Expr::Index(ref id, ref idx_raw) => {
|
||||
let idx_boxed = try!(self.eval_expr(scope, idx_raw));
|
||||
let idx = if let Ok(i) = idx_boxed.downcast::<i32>() { i } else { return Err(EvalAltResult::ErrorIndexMismatch); };
|
||||
|
||||
|
||||
let mut target : Option<Box<Any>> = None;
|
||||
|
||||
for &mut (ref name, ref mut val) in &mut scope.iter_mut().rev() {
|
||||
if *id == *name {
|
||||
if let Some(arr_typed) = (*val).downcast_mut() as Option<&mut Vec<Box<Any>>> {
|
||||
let result = self.call_fn("clone", Some(&mut arr_typed[*idx as usize]),
|
||||
let result = self.call_fn("clone", Some(&mut arr_typed[*idx as usize]),
|
||||
None, None, None, None, None);
|
||||
|
||||
if let Ok(clone) = result {
|
||||
@ -544,14 +544,14 @@ impl Engine {
|
||||
}
|
||||
},
|
||||
e => e
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
_ => Err(EvalAltResult::InternalErrorMalformedDotExpression)
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
Err(EvalAltResult::InternalErrorMalformedDotExpression)
|
||||
Err(EvalAltResult::InternalErrorMalformedDotExpression)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -590,13 +590,13 @@ impl Engine {
|
||||
Expr::Index(ref id, ref idx_raw) => {
|
||||
let idx_boxed = try!(self.eval_expr(scope, idx_raw));
|
||||
let idx = if let Ok(i) = idx_boxed.downcast::<i32>() { i } else { return Err(EvalAltResult::ErrorIndexMismatch); };
|
||||
|
||||
|
||||
let mut target : Option<Box<Any>> = None;
|
||||
|
||||
for &mut (ref name, ref mut val) in &mut scope.iter_mut().rev() {
|
||||
if *id == *name {
|
||||
if let Some(arr_typed) = (*val).downcast_mut() as Option<&mut Vec<Box<Any>>> {
|
||||
let result = self.call_fn("clone", Some(&mut arr_typed[*idx as usize]),
|
||||
let result = self.call_fn("clone", Some(&mut arr_typed[*idx as usize]),
|
||||
None, None, None, None, None);
|
||||
|
||||
if let Ok(clone) = result {
|
||||
@ -638,7 +638,7 @@ impl Engine {
|
||||
Expr::StringConst(ref s) => Ok(Box::new(s.clone())),
|
||||
Expr::Identifier(ref id) => {
|
||||
for &mut (ref name, ref mut val) in &mut scope.iter_mut().rev() {
|
||||
if *id == *name {
|
||||
if *id == *name {
|
||||
return self.call_fn("clone", Some(val), None, None, None, None, None);
|
||||
}
|
||||
}
|
||||
@ -700,7 +700,7 @@ impl Engine {
|
||||
}
|
||||
}
|
||||
|
||||
Err(EvalAltResult::ErrorVariableNotFound(id.clone()))
|
||||
Err(EvalAltResult::ErrorVariableNotFound(id.clone()))
|
||||
}
|
||||
Expr::Dot(ref dot_lhs, ref dot_rhs) => {
|
||||
self.set_dot_val(scope, dot_lhs, dot_rhs, rhs_val)
|
||||
@ -758,7 +758,7 @@ impl Engine {
|
||||
let mut arg4 = try!(self.eval_expr(scope, &args[3]));
|
||||
let mut arg5 = try!(self.eval_expr(scope, &args[4]));
|
||||
|
||||
self.call_fn(&fn_name, Some(&mut arg1), Some(&mut arg2), Some(&mut arg3), Some(&mut arg4),
|
||||
self.call_fn(&fn_name, Some(&mut arg1), Some(&mut arg2), Some(&mut arg3), Some(&mut arg4),
|
||||
Some(&mut arg5), None)
|
||||
}
|
||||
else if args.len() == 6 {
|
||||
@ -769,7 +769,7 @@ impl Engine {
|
||||
let mut arg5 = try!(self.eval_expr(scope, &args[4]));
|
||||
let mut arg6 = try!(self.eval_expr(scope, &args[5]));
|
||||
|
||||
self.call_fn(&fn_name, Some(&mut arg1), Some(&mut arg2), Some(&mut arg3), Some(&mut arg4),
|
||||
self.call_fn(&fn_name, Some(&mut arg1), Some(&mut arg2), Some(&mut arg3), Some(&mut arg4),
|
||||
Some(&mut arg5), Some(&mut arg6))
|
||||
}
|
||||
else {
|
||||
@ -781,7 +781,7 @@ impl Engine {
|
||||
}
|
||||
Expr::False => {
|
||||
Ok(Box::new(false))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -853,7 +853,7 @@ impl Engine {
|
||||
}
|
||||
}
|
||||
Err(_) => return Err(EvalAltResult::ErrorIfGuardMismatch)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Stmt::Break => return Err(EvalAltResult::LoopBreak),
|
||||
@ -865,7 +865,7 @@ impl Engine {
|
||||
Stmt::Var(ref name, ref init) => {
|
||||
match init {
|
||||
& Some(ref v) => {
|
||||
let i = try!(self.eval_expr(scope, v));
|
||||
let i = try!(self.eval_expr(scope, v));
|
||||
scope.push((name.clone(), i));
|
||||
},
|
||||
& None => {
|
||||
@ -883,17 +883,17 @@ impl Engine {
|
||||
|
||||
if let Ok(mut f) = File::open(fname.clone()) {
|
||||
let mut contents = String::new();
|
||||
|
||||
|
||||
if let Ok(_) = f.read_to_string(&mut contents) {
|
||||
self.eval::<T>(&contents)
|
||||
}
|
||||
else {
|
||||
Err(EvalAltResult::ErrorCantOpenScriptFile)
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
Err(EvalAltResult::ErrorCantOpenScriptFile)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn eval<T:Any+Clone>(&mut self, input: &str) -> Result<T, EvalAltResult> {
|
||||
@ -911,7 +911,7 @@ impl Engine {
|
||||
match tree {
|
||||
Ok((ref os, ref fns)) => {
|
||||
let mut x: Result<Box<Any>, EvalAltResult> = Ok(Box::new(()));
|
||||
|
||||
|
||||
for f in fns {
|
||||
if f.params.len() > 6 {
|
||||
return Err(EvalAltResult::ErrorFunctionArityNotSupported);
|
||||
@ -919,7 +919,7 @@ impl Engine {
|
||||
let name = f.name.clone();
|
||||
let local_f = f.clone();
|
||||
let ent = self.fns.entry(name).or_insert(Vec::new());
|
||||
(*ent).push(FnType::InternalFn(local_f));
|
||||
(*ent).push(FnType::InternalFn(local_f));
|
||||
}
|
||||
|
||||
for o in os {
|
||||
@ -961,7 +961,7 @@ impl Engine {
|
||||
)*
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
macro_rules! reg_cmp {
|
||||
($engine:expr, $x:expr, $op:expr, $( $y:ty ),*) => (
|
||||
$(
|
||||
@ -1006,7 +1006,7 @@ impl Engine {
|
||||
}
|
||||
|
||||
pub fn new() -> Engine {
|
||||
let mut engine = Engine {
|
||||
let mut engine = Engine {
|
||||
fns: HashMap::new()
|
||||
};
|
||||
|
||||
@ -1045,7 +1045,7 @@ fn test_ops() {
|
||||
}
|
||||
else {
|
||||
assert!(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -1137,14 +1137,14 @@ fn test_var_scope() {
|
||||
let mut engine = Engine::new();
|
||||
let mut scope: Scope = Vec::new();
|
||||
|
||||
if let Ok(_) = engine.eval_with_scope::<()>(&mut scope, "var x = 4 + 5") { } else { assert!(false); }
|
||||
if let Ok(_) = engine.eval_with_scope::<()>(&mut scope, "var x = 4 + 5") { } else { assert!(false); }
|
||||
|
||||
if let Ok(result) = engine.eval_with_scope::<i32>(&mut scope, "x") {
|
||||
assert_eq!(result, 9);
|
||||
}
|
||||
else {
|
||||
assert!(false);
|
||||
}
|
||||
}
|
||||
|
||||
if let Ok(_) = engine.eval_with_scope::<()>(&mut scope, "x = x + 1; x = x + 2;") { } else { assert!(false); }
|
||||
|
||||
|
@ -8,26 +8,26 @@ pub trait FnRegister<A, RetVal, Args> {
|
||||
}
|
||||
|
||||
impl<'a, A, T, U, V, W, X, Y, Z> FnRegister<A, Z, (&'a mut T, U, V, W, X, Y)> for Engine
|
||||
where A: 'static+Fn(&mut T, U, V, W, X, Y) -> Z, T: Any, U: Clone+Any, V: Clone+Any, W: Clone+Any,
|
||||
where A: 'static+Fn(&mut T, U, V, W, X, Y) -> Z, T: Any, U: Clone+Any, V: Clone+Any, W: Clone+Any,
|
||||
X: Clone+Any, Y: Clone+Any, Z: Any
|
||||
{
|
||||
fn register_fn(&mut self, name: &str, fun: A) {
|
||||
let wrapped : Box<Fn(&mut Box<Any>, &mut Box<Any>, &mut Box<Any>, &mut Box<Any>, &mut Box<Any>,
|
||||
&mut Box<Any>)->Result<Box<Any>, EvalAltResult>> =
|
||||
|
||||
let wrapped : Box<Fn(&mut Box<Any>, &mut Box<Any>, &mut Box<Any>, &mut Box<Any>, &mut Box<Any>,
|
||||
&mut Box<Any>)->Result<Box<Any>, EvalAltResult>> =
|
||||
|
||||
Box::new(
|
||||
move |arg1: &mut Box<Any>, arg2: &mut Box<Any>, arg3: &mut Box<Any>, arg4: &mut Box<Any>,
|
||||
arg5: &mut Box<Any>, arg6: &mut Box<Any>| {
|
||||
|
||||
|
||||
let inside1 = (*arg1).downcast_mut() as Option<&mut T>;
|
||||
let inside2 = (*arg2).downcast_mut() as Option<&mut U>;
|
||||
let inside3 = (*arg3).downcast_mut() as Option<&mut V>;
|
||||
let inside4 = (*arg4).downcast_mut() as Option<&mut W>;
|
||||
let inside5 = (*arg5).downcast_mut() as Option<&mut X>;
|
||||
let inside6 = (*arg6).downcast_mut() as Option<&mut Y>;
|
||||
|
||||
|
||||
match (inside1, inside2, inside3, inside4, inside5, inside6) {
|
||||
(Some(b), Some(c), Some(d), Some(e), Some(f), Some(g)) => Ok(Box::new(fun(b, c.clone(), d.clone(),
|
||||
(Some(b), Some(c), Some(d), Some(e), Some(f), Some(g)) => Ok(Box::new(fun(b, c.clone(), d.clone(),
|
||||
e.clone(), f.clone(), g.clone())) as Box<Any>),
|
||||
_ => Err(EvalAltResult::ErrorFunctionArgMismatch)
|
||||
}
|
||||
@ -40,13 +40,13 @@ impl<'a, A, T, U, V, W, X, Y, Z> FnRegister<A, Z, (&'a mut T, U, V, W, X, Y)> fo
|
||||
}
|
||||
|
||||
impl<'a, A, T, U, V, W, X, Y, Z> FnRegister<A, Z, (&'a T, U, V, W, X, Y)> for Engine
|
||||
where A: 'static+Fn(T, U, V, W, X, Y) -> Z, T: Clone+Any, U: Clone+Any, V: Clone+Any, W: Clone+Any,
|
||||
where A: 'static+Fn(T, U, V, W, X, Y) -> Z, T: Clone+Any, U: Clone+Any, V: Clone+Any, W: Clone+Any,
|
||||
X: Clone+Any, Y: Clone+Any, Z: Any
|
||||
{
|
||||
fn register_fn(&mut self, name: &str, fun: A) {
|
||||
let wrapped : Box<Fn(&mut Box<Any>, &mut Box<Any>, &mut Box<Any>, &mut Box<Any>, &mut Box<Any>,
|
||||
&mut Box<Any>)->Result<Box<Any>, EvalAltResult>> =
|
||||
|
||||
let wrapped : Box<Fn(&mut Box<Any>, &mut Box<Any>, &mut Box<Any>, &mut Box<Any>, &mut Box<Any>,
|
||||
&mut Box<Any>)->Result<Box<Any>, EvalAltResult>> =
|
||||
|
||||
Box::new(
|
||||
move |arg1: &mut Box<Any>, arg2: &mut Box<Any>, arg3: &mut Box<Any>, arg4: &mut Box<Any>,
|
||||
arg5: &mut Box<Any>, arg6: &mut Box<Any>| {
|
||||
@ -57,9 +57,9 @@ impl<'a, A, T, U, V, W, X, Y, Z> FnRegister<A, Z, (&'a T, U, V, W, X, Y)> for En
|
||||
let inside4 = (*arg4).downcast_mut() as Option<&mut W>;
|
||||
let inside5 = (*arg5).downcast_mut() as Option<&mut X>;
|
||||
let inside6 = (*arg6).downcast_mut() as Option<&mut Y>;
|
||||
|
||||
|
||||
match (inside1, inside2, inside3, inside4, inside5, inside6) {
|
||||
(Some(b), Some(c), Some(d), Some(e), Some(f), Some(g)) => Ok(Box::new(fun(b.clone(), c.clone(), d.clone(),
|
||||
(Some(b), Some(c), Some(d), Some(e), Some(f), Some(g)) => Ok(Box::new(fun(b.clone(), c.clone(), d.clone(),
|
||||
e.clone(), f.clone(), g.clone())) as Box<Any>),
|
||||
_ => Err(EvalAltResult::ErrorFunctionArgMismatch)
|
||||
}
|
||||
@ -75,7 +75,7 @@ impl<'a, A, T, U, V, W, X, Y> FnRegister<A, Y, (&'a mut T, U, V, W, X)> for Engi
|
||||
where A: 'static+Fn(&mut T, U, V, W, X) -> Y, T: Any, U: Clone+Any, V: Clone+Any, W: Clone+Any, X: Clone+Any, Y: Any
|
||||
{
|
||||
fn register_fn(&mut self, name: &str, fun: A) {
|
||||
let wrapped : Box<Fn(&mut Box<Any>, &mut Box<Any>, &mut Box<Any>, &mut Box<Any>, &mut Box<Any>)->Result<Box<Any>, EvalAltResult>> =
|
||||
let wrapped : Box<Fn(&mut Box<Any>, &mut Box<Any>, &mut Box<Any>, &mut Box<Any>, &mut Box<Any>)->Result<Box<Any>, EvalAltResult>> =
|
||||
Box::new(
|
||||
move |arg1: &mut Box<Any>, arg2: &mut Box<Any>, arg3: &mut Box<Any>, arg4: &mut Box<Any>,
|
||||
arg5: &mut Box<Any>| {
|
||||
@ -85,9 +85,9 @@ impl<'a, A, T, U, V, W, X, Y> FnRegister<A, Y, (&'a mut T, U, V, W, X)> for Engi
|
||||
let inside3 = (*arg3).downcast_mut() as Option<&mut V>;
|
||||
let inside4 = (*arg4).downcast_mut() as Option<&mut W>;
|
||||
let inside5 = (*arg5).downcast_mut() as Option<&mut X>;
|
||||
|
||||
|
||||
match (inside1, inside2, inside3, inside4, inside5) {
|
||||
(Some(b), Some(c), Some(d), Some(e), Some(f)) => Ok(Box::new(fun(b, c.clone(), d.clone(),
|
||||
(Some(b), Some(c), Some(d), Some(e), Some(f)) => Ok(Box::new(fun(b, c.clone(), d.clone(),
|
||||
e.clone(), f.clone())) as Box<Any>),
|
||||
_ => Err(EvalAltResult::ErrorFunctionArgMismatch)
|
||||
}
|
||||
@ -103,7 +103,7 @@ impl<'a, A, T, U, V, W, X, Y> FnRegister<A, Y, (&'a T, U, V, W, X)> for Engine
|
||||
where A: 'static+Fn(T, U, V, W, X) -> Y, T: Clone+Any, U: Clone+Any, V: Clone+Any, W: Clone+Any, X: Clone+Any, Y: Any
|
||||
{
|
||||
fn register_fn(&mut self, name: &str, fun: A) {
|
||||
let wrapped : Box<Fn(&mut Box<Any>, &mut Box<Any>, &mut Box<Any>, &mut Box<Any>, &mut Box<Any>)->Result<Box<Any>, EvalAltResult>> =
|
||||
let wrapped : Box<Fn(&mut Box<Any>, &mut Box<Any>, &mut Box<Any>, &mut Box<Any>, &mut Box<Any>)->Result<Box<Any>, EvalAltResult>> =
|
||||
Box::new(
|
||||
move |arg1: &mut Box<Any>, arg2: &mut Box<Any>, arg3: &mut Box<Any>, arg4: &mut Box<Any>,
|
||||
arg5: &mut Box<Any>| {
|
||||
@ -113,9 +113,9 @@ impl<'a, A, T, U, V, W, X, Y> FnRegister<A, Y, (&'a T, U, V, W, X)> for Engine
|
||||
let inside3 = (*arg3).downcast_mut() as Option<&mut V>;
|
||||
let inside4 = (*arg4).downcast_mut() as Option<&mut W>;
|
||||
let inside5 = (*arg5).downcast_mut() as Option<&mut X>;
|
||||
|
||||
|
||||
match (inside1, inside2, inside3, inside4, inside5) {
|
||||
(Some(b), Some(c), Some(d), Some(e), Some(f)) => Ok(Box::new(fun(b.clone(), c.clone(), d.clone(),
|
||||
(Some(b), Some(c), Some(d), Some(e), Some(f)) => Ok(Box::new(fun(b.clone(), c.clone(), d.clone(),
|
||||
e.clone(), f.clone())) as Box<Any>),
|
||||
_ => Err(EvalAltResult::ErrorFunctionArgMismatch)
|
||||
}
|
||||
@ -131,14 +131,14 @@ impl<'a, A, T, U, V, W, X> FnRegister<A, X, (&'a mut T, U, V, W)> for Engine
|
||||
where A: 'static+Fn(&mut T, U, V, W) -> X, T: Any, U: Clone+Any, V: Clone+Any, W: Clone+Any, X: Any
|
||||
{
|
||||
fn register_fn(&mut self, name: &str, fun: A) {
|
||||
let wrapped : Box<Fn(&mut Box<Any>, &mut Box<Any>, &mut Box<Any>, &mut Box<Any>)->Result<Box<Any>, EvalAltResult>> =
|
||||
let wrapped : Box<Fn(&mut Box<Any>, &mut Box<Any>, &mut Box<Any>, &mut Box<Any>)->Result<Box<Any>, EvalAltResult>> =
|
||||
Box::new(
|
||||
move |arg1: &mut Box<Any>, arg2: &mut Box<Any>, arg3: &mut Box<Any>, arg4: &mut Box<Any>| {
|
||||
let inside1 = (*arg1).downcast_mut() as Option<&mut T>;
|
||||
let inside2 = (*arg2).downcast_mut() as Option<&mut U>;
|
||||
let inside3 = (*arg3).downcast_mut() as Option<&mut V>;
|
||||
let inside4 = (*arg4).downcast_mut() as Option<&mut W>;
|
||||
|
||||
|
||||
match (inside1, inside2, inside3, inside4) {
|
||||
(Some(b), Some(c), Some(d), Some(e)) => Ok(Box::new(fun(b, c.clone(), d.clone(), e.clone())) as Box<Any>),
|
||||
_ => Err(EvalAltResult::ErrorFunctionArgMismatch)
|
||||
@ -155,14 +155,14 @@ impl<'a, A, T, U, V, W, X> FnRegister<A, X, (&'a T, U, V, W)> for Engine
|
||||
where A: 'static+Fn(T, U, V, W) -> X, T: Clone+Any, U: Clone+Any, V: Clone+Any, W: Clone+Any, X: Any
|
||||
{
|
||||
fn register_fn(&mut self, name: &str, fun: A) {
|
||||
let wrapped : Box<Fn(&mut Box<Any>, &mut Box<Any>, &mut Box<Any>, &mut Box<Any>)->Result<Box<Any>, EvalAltResult>> =
|
||||
let wrapped : Box<Fn(&mut Box<Any>, &mut Box<Any>, &mut Box<Any>, &mut Box<Any>)->Result<Box<Any>, EvalAltResult>> =
|
||||
Box::new(
|
||||
move |arg1: &mut Box<Any>, arg2: &mut Box<Any>, arg3: &mut Box<Any>, arg4: &mut Box<Any>| {
|
||||
let inside1 = (*arg1).downcast_mut() as Option<&mut T>;
|
||||
let inside2 = (*arg2).downcast_mut() as Option<&mut U>;
|
||||
let inside3 = (*arg3).downcast_mut() as Option<&mut V>;
|
||||
let inside4 = (*arg4).downcast_mut() as Option<&mut W>;
|
||||
|
||||
|
||||
match (inside1, inside2, inside3, inside4) {
|
||||
(Some(b), Some(c), Some(d), Some(e)) => Ok(Box::new(fun(b.clone(), c.clone(), d.clone(), e.clone())) as Box<Any>),
|
||||
_ => Err(EvalAltResult::ErrorFunctionArgMismatch)
|
||||
@ -179,13 +179,13 @@ impl<'a, A, T, U, V, W> FnRegister<A, W, (&'a mut T, U, V)> for Engine
|
||||
where A: 'static+Fn(&mut T, U, V) -> W, T: Any, U: Clone+Any, V: Clone+Any, W: Any
|
||||
{
|
||||
fn register_fn(&mut self, name: &str, fun: A) {
|
||||
let wrapped : Box<Fn(&mut Box<Any>, &mut Box<Any>, &mut Box<Any>)->Result<Box<Any>, EvalAltResult>> =
|
||||
let wrapped : Box<Fn(&mut Box<Any>, &mut Box<Any>, &mut Box<Any>)->Result<Box<Any>, EvalAltResult>> =
|
||||
Box::new(
|
||||
move |arg1: &mut Box<Any>, arg2: &mut Box<Any>, arg3: &mut Box<Any>| {
|
||||
let inside1 = (*arg1).downcast_mut() as Option<&mut T>;
|
||||
let inside2 = (*arg2).downcast_mut() as Option<&mut U>;
|
||||
let inside3 = (*arg3).downcast_mut() as Option<&mut V>;
|
||||
|
||||
|
||||
match (inside1, inside2, inside3) {
|
||||
(Some(b), Some(c), Some(d)) => Ok(Box::new(fun(b, c.clone(), d.clone())) as Box<Any>),
|
||||
_ => Err(EvalAltResult::ErrorFunctionArgMismatch)
|
||||
@ -202,13 +202,13 @@ impl<'a, A, T, U, V, W> FnRegister<A, W, (&'a T, U, V)> for Engine
|
||||
where A: 'static+Fn(T, U, V) -> W, T: Clone+Any, U: Clone+Any, V: Clone+Any, W: Any
|
||||
{
|
||||
fn register_fn(&mut self, name: &str, fun: A) {
|
||||
let wrapped : Box<Fn(&mut Box<Any>, &mut Box<Any>, &mut Box<Any>)->Result<Box<Any>, EvalAltResult>> =
|
||||
let wrapped : Box<Fn(&mut Box<Any>, &mut Box<Any>, &mut Box<Any>)->Result<Box<Any>, EvalAltResult>> =
|
||||
Box::new(
|
||||
move |arg1: &mut Box<Any>, arg2: &mut Box<Any>, arg3: &mut Box<Any>| {
|
||||
let inside1 = (*arg1).downcast_mut() as Option<&mut T>;
|
||||
let inside2 = (*arg2).downcast_mut() as Option<&mut U>;
|
||||
let inside3 = (*arg3).downcast_mut() as Option<&mut V>;
|
||||
|
||||
|
||||
match (inside1, inside2, inside3) {
|
||||
(Some(b), Some(c), Some(d)) => Ok(Box::new(fun(b.clone(), c.clone(), d.clone())) as Box<Any>),
|
||||
_ => Err(EvalAltResult::ErrorFunctionArgMismatch)
|
||||
@ -225,12 +225,12 @@ impl<'a, A, T, U, V> FnRegister<A, V, (&'a mut T, U)> for Engine
|
||||
where A: 'static+Fn(&mut T, U) -> V, T: Any, U: Clone+Any, V: Any
|
||||
{
|
||||
fn register_fn(&mut self, name: &str, fun: A) {
|
||||
let wrapped : Box<Fn(&mut Box<Any>, &mut Box<Any>)->Result<Box<Any>, EvalAltResult>> =
|
||||
let wrapped : Box<Fn(&mut Box<Any>, &mut Box<Any>)->Result<Box<Any>, EvalAltResult>> =
|
||||
Box::new(
|
||||
move |arg1: &mut Box<Any>, arg2: &mut Box<Any>| {
|
||||
let inside1 = (*arg1).downcast_mut() as Option<&mut T>;
|
||||
let inside2 = (*arg2).downcast_mut() as Option<&mut U>;
|
||||
|
||||
|
||||
match (inside1, inside2) {
|
||||
(Some(b), Some(c)) => Ok(Box::new(fun(b, c.clone())) as Box<Any>),
|
||||
_ => Err(EvalAltResult::ErrorFunctionArgMismatch)
|
||||
@ -247,12 +247,12 @@ impl<'a, A, T, U, V> FnRegister<A, V, (&'a T, U)> for Engine
|
||||
where A: 'static+Fn(T, U) -> V, T: Clone+Any, U: Clone+Any, V: Any
|
||||
{
|
||||
fn register_fn(&mut self, name: &str, fun: A) {
|
||||
let wrapped : Box<Fn(&mut Box<Any>, &mut Box<Any>)->Result<Box<Any>, EvalAltResult>> =
|
||||
let wrapped : Box<Fn(&mut Box<Any>, &mut Box<Any>)->Result<Box<Any>, EvalAltResult>> =
|
||||
Box::new(
|
||||
move |arg1: &mut Box<Any>, arg2: &mut Box<Any>| {
|
||||
let inside1 = (*arg1).downcast_mut() as Option<&mut T>;
|
||||
let inside2 = (*arg2).downcast_mut() as Option<&mut U>;
|
||||
|
||||
|
||||
match (inside1, inside2) {
|
||||
(Some(b), Some(c)) => Ok(Box::new(fun(b.clone(), c.clone())) as Box<Any>),
|
||||
_ => Err(EvalAltResult::ErrorFunctionArgMismatch)
|
||||
@ -266,14 +266,14 @@ impl<'a, A, T, U, V> FnRegister<A, V, (&'a T, U)> for Engine
|
||||
}
|
||||
|
||||
impl<'a, A, T, U> FnRegister<A, U, (&'a mut T)> for Engine
|
||||
where A: 'static+Fn(&mut T) -> U, T: Any, U: Any
|
||||
where A: 'static+Fn(&mut T) -> U, T: Any, U: Any
|
||||
{
|
||||
fn register_fn(&mut self, name: &str, fun: A) {
|
||||
let wrapped : Box<Fn(&mut Box<Any>)->Result<Box<Any>, EvalAltResult>> =
|
||||
let wrapped : Box<Fn(&mut Box<Any>)->Result<Box<Any>, EvalAltResult>> =
|
||||
Box::new(
|
||||
move |arg: &mut Box<Any>| {
|
||||
let inside = (*arg).downcast_mut() as Option<&mut T>;
|
||||
|
||||
|
||||
match inside {
|
||||
Some(b) => Ok(Box::new(fun(b)) as Box<Any>),
|
||||
None => Err(EvalAltResult::ErrorFunctionArgMismatch)
|
||||
@ -291,7 +291,7 @@ impl<'a, A, T, U> FnRegister<A, U, (&'a T)> for Engine
|
||||
where A: 'static+Fn(T) -> U, T: Clone+Any, U: Any
|
||||
{
|
||||
fn register_fn(&mut self, name: &str, fun: A) {
|
||||
let wrapped : Box<Fn(&mut Box<Any>)->Result<Box<Any>, EvalAltResult>> =
|
||||
let wrapped : Box<Fn(&mut Box<Any>)->Result<Box<Any>, EvalAltResult>> =
|
||||
Box::new(
|
||||
move |arg: &mut Box<Any>| {
|
||||
let inside = (*arg).downcast_mut() as Option<&mut T>;
|
||||
@ -311,7 +311,7 @@ impl<A, T> FnRegister<A, T, ()> for Engine
|
||||
where A: 'static+Fn() -> T, T: Any
|
||||
{
|
||||
fn register_fn(&mut self, name: &str, fun: A) {
|
||||
let wrapped : Box<Fn()->Result<Box<Any>, EvalAltResult>> =
|
||||
let wrapped : Box<Fn()->Result<Box<Any>, EvalAltResult>> =
|
||||
Box::new(
|
||||
move || { Ok(Box::new(fun()) as Box<Any>) }
|
||||
);
|
||||
|
@ -86,11 +86,11 @@ pub struct FnDef {
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Stmt { If(Box<Expr>, Box<Stmt>), IfElse(Box<Expr>, Box<Stmt>, Box<Stmt>), While(Box<Expr>, Box<Stmt>),
|
||||
pub enum Stmt { If(Box<Expr>, Box<Stmt>), IfElse(Box<Expr>, Box<Stmt>, Box<Stmt>), While(Box<Expr>, Box<Stmt>),
|
||||
Var(String, Option<Box<Expr>>), Block(Box<Vec<Stmt>>), Expr(Box<Expr>), Break, Return, ReturnWithVal(Box<Expr>) }
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Expr { IntConst(i32), Identifier(String), StringConst(String), FnCall(String, Box<Vec<Expr>>),
|
||||
pub enum Expr { IntConst(i32), Identifier(String), StringConst(String), FnCall(String, Box<Vec<Expr>>),
|
||||
Assignment(Box<Expr>, Box<Expr>), Dot(Box<Expr>, Box<Expr>), Index(String, Box<Expr>), Array(Box<Vec<Expr>>), True, False }
|
||||
|
||||
#[derive(Debug)]
|
||||
@ -133,7 +133,7 @@ impl<'a> Iterator for TokenIterator<'a> {
|
||||
|
||||
while let Some(&nxt) = self.char_stream.peek() {
|
||||
match nxt {
|
||||
'0'...'9' | 'A'...'Z' | 'a'...'z' | '_' => {
|
||||
'0'...'9' | 'A'...'Z' | 'a'...'z' | '_' => {
|
||||
result.push(nxt); self.char_stream.next(); },
|
||||
_ => break
|
||||
}
|
||||
@ -281,37 +281,37 @@ impl<'a> Iterator for TokenIterator<'a> {
|
||||
':' => { return Some(Token::Colon); },
|
||||
',' => { return Some(Token::Comma); },
|
||||
'.' => { return Some(Token::Period); },
|
||||
'=' => {
|
||||
'=' => {
|
||||
match self.char_stream.peek() {
|
||||
Some(&'=') => {self.char_stream.next(); return Some(Token::EqualTo); },
|
||||
_ => { return Some(Token::Equals); }
|
||||
}
|
||||
},
|
||||
'<' => {
|
||||
'<' => {
|
||||
match self.char_stream.peek() {
|
||||
Some(&'=') => {self.char_stream.next(); return Some(Token::LessThanEqual); },
|
||||
_ => { return Some(Token::LessThan); }
|
||||
}
|
||||
}
|
||||
'>' => {
|
||||
'>' => {
|
||||
match self.char_stream.peek() {
|
||||
Some(&'=') => {self.char_stream.next(); return Some(Token::GreaterThanEqual); },
|
||||
_ => { return Some(Token::GreaterThan); }
|
||||
}
|
||||
},
|
||||
'!' => {
|
||||
'!' => {
|
||||
match self.char_stream.peek() {
|
||||
Some(&'=') => {self.char_stream.next(); return Some(Token::NotEqualTo); },
|
||||
_ => { return Some(Token::Bang); }
|
||||
}
|
||||
},
|
||||
'|' => {
|
||||
'|' => {
|
||||
match self.char_stream.peek() {
|
||||
Some(&'|') => {self.char_stream.next(); return Some(Token::Or); },
|
||||
_ => { return Some(Token::Pipe); }
|
||||
}
|
||||
},
|
||||
'&' => {
|
||||
'&' => {
|
||||
match self.char_stream.peek() {
|
||||
Some(&'&') => {self.char_stream.next(); return Some(Token::And); },
|
||||
_ => { return Some(Token::Ampersand); }
|
||||
@ -359,12 +359,12 @@ fn parse_paren_expr<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<Expr,
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_call_expr<'a>(id: String, input: &mut Peekable<TokenIterator<'a>>) -> Result<Expr, ParseError> {
|
||||
fn parse_call_expr<'a>(id: String, input: &mut Peekable<TokenIterator<'a>>) -> Result<Expr, ParseError> {
|
||||
let mut args = Vec::new();
|
||||
|
||||
match input.peek() {
|
||||
Some(&Token::RParen) => {
|
||||
input.next();
|
||||
input.next();
|
||||
return Ok(Expr::FnCall(id, Box::new(args)))
|
||||
},
|
||||
_ => ()
|
||||
@ -391,7 +391,7 @@ fn parse_call_expr<'a>(id: String, input: &mut Peekable<TokenIterator<'a>>) -> R
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_index_expr<'a>(id: String, input: &mut Peekable<TokenIterator<'a>>) -> Result<Expr, ParseError> {
|
||||
fn parse_index_expr<'a>(id: String, input: &mut Peekable<TokenIterator<'a>>) -> Result<Expr, ParseError> {
|
||||
if let Ok(idx) = parse_expr(input) {
|
||||
match input.peek() {
|
||||
Some(&Token::RSquare) => {
|
||||
@ -406,7 +406,7 @@ fn parse_index_expr<'a>(id: String, input: &mut Peekable<TokenIterator<'a>>) ->
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_ident_expr<'a>(id: String, input: &mut Peekable<TokenIterator<'a>>) -> Result<Expr, ParseError> {
|
||||
fn parse_ident_expr<'a>(id: String, input: &mut Peekable<TokenIterator<'a>>) -> Result<Expr, ParseError> {
|
||||
match input.peek() {
|
||||
Some(&Token::LParen) => {input.next(); parse_call_expr(id, input)},
|
||||
Some(&Token::LSquare) => {input.next(); parse_index_expr(id, input)},
|
||||
@ -426,7 +426,7 @@ fn parse_array_expr<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<Expr,
|
||||
while let Some(_) = input.peek() {
|
||||
arr.push(try!(parse_expr(input)));
|
||||
match input.peek() {
|
||||
Some(& Token::Comma) => {input.next();},
|
||||
Some(& Token::Comma) => {input.next();},
|
||||
_ => ()
|
||||
}
|
||||
|
||||
@ -434,7 +434,7 @@ fn parse_array_expr<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<Expr,
|
||||
Some(& Token::RSquare) => break,
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
match input.peek() {
|
||||
@ -469,7 +469,7 @@ fn parse_binop<'a>(input: &mut Peekable<TokenIterator<'a>>, prec: i32, lhs: Expr
|
||||
loop {
|
||||
let mut curr_prec = -1;
|
||||
|
||||
if let Some(curr_op) = input.peek() {
|
||||
if let Some(curr_op) = input.peek() {
|
||||
curr_prec = get_precedence(curr_op);
|
||||
}
|
||||
|
||||
@ -481,7 +481,7 @@ fn parse_binop<'a>(input: &mut Peekable<TokenIterator<'a>>, prec: i32, lhs: Expr
|
||||
let mut rhs = try!(parse_primary(input));
|
||||
|
||||
let mut next_prec = -1;
|
||||
|
||||
|
||||
if let Some(next_op) = input.peek() {
|
||||
next_prec = get_precedence(next_op);
|
||||
}
|
||||
@ -491,7 +491,7 @@ fn parse_binop<'a>(input: &mut Peekable<TokenIterator<'a>>, prec: i32, lhs: Expr
|
||||
}
|
||||
else if curr_prec >= 100 {
|
||||
//Always bind right to left for precedence over 100
|
||||
rhs = try!(parse_binop(input, curr_prec, rhs));
|
||||
rhs = try!(parse_binop(input, curr_prec, rhs));
|
||||
}
|
||||
|
||||
lhs_curr = match op_token {
|
||||
@ -585,7 +585,7 @@ fn parse_block<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<Stmt, Pars
|
||||
while let Some(_) = input.peek() {
|
||||
stmts.push(try!(parse_stmt(input)));
|
||||
match input.peek() {
|
||||
Some(& Token::Semicolon) => {input.next();},
|
||||
Some(& Token::Semicolon) => {input.next();},
|
||||
_ => ()
|
||||
}
|
||||
|
||||
@ -593,7 +593,7 @@ fn parse_block<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<Stmt, Pars
|
||||
Some(& Token::RCurly) => break,
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
match input.peek() {
|
||||
@ -603,7 +603,7 @@ fn parse_block<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<Stmt, Pars
|
||||
}
|
||||
|
||||
fn parse_expr_stmt<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<Stmt, ParseError> {
|
||||
let expr = try!(parse_expr(input));
|
||||
let expr = try!(parse_expr(input));
|
||||
Ok(Stmt::Expr(Box::new(expr)))
|
||||
}
|
||||
|
||||
@ -613,7 +613,7 @@ fn parse_stmt<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<Stmt, Parse
|
||||
Some(& Token::While) => parse_while(input),
|
||||
Some(& Token::Break) => {input.next(); Ok(Stmt::Break)},
|
||||
Some(& Token::Return) => {
|
||||
input.next();
|
||||
input.next();
|
||||
match input.peek() {
|
||||
Some(& Token::Semicolon) => Ok(Stmt::Return),
|
||||
_ => {let ret = try!(parse_expr(input)); Ok(Stmt::ReturnWithVal(Box::new(ret))) }
|
||||
@ -653,7 +653,7 @@ fn parse_fn<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<FnDef, ParseE
|
||||
Some(Token::Identifier(ref s)) => { params.push(s.clone()); },
|
||||
_ => return Err(ParseError::MalformedCallExpr)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let body = try!(parse_block(input));
|
||||
@ -672,7 +672,7 @@ fn parse_top_level<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<(Vec<S
|
||||
}
|
||||
|
||||
match input.peek() {
|
||||
Some(& Token::Semicolon) => {input.next();},
|
||||
Some(& Token::Semicolon) => {input.next();},
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user