From 4e38b9e611b8f6ff5a05768afe17b478f766192a Mon Sep 17 00:00:00 2001 From: jonathandturner Date: Wed, 13 Apr 2016 18:01:08 -0700 Subject: [PATCH] Clean up trailing space and fix rhai_runner print out --- examples/rhai_runner.rs | 2 +- examples/simple_fn.rs | 2 +- src/engine.rs | 80 ++++++++++++++++++++--------------------- src/fn_register.rs | 72 ++++++++++++++++++------------------- src/parser.rs | 48 ++++++++++++------------- 5 files changed, 102 insertions(+), 102 deletions(-) diff --git a/examples/rhai_runner.rs b/examples/rhai_runner.rs index 2376ac91..abffb6ed 100644 --- a/examples/rhai_runner.rs +++ b/examples/rhai_runner.rs @@ -23,7 +23,7 @@ fn main() { match engine.eval_file::<()>(&fname) { Ok(_) => (), - Err(e) => {println!("Error: {:?}", e)} + Err(e) => {println!("Error: {}", e)} } } } diff --git a/examples/simple_fn.rs b/examples/simple_fn.rs index 483d5c2a..6a6c989c 100644 --- a/examples/simple_fn.rs +++ b/examples/simple_fn.rs @@ -9,7 +9,7 @@ fn main() { let mut engine = Engine::new(); engine.register_fn("add", add); - + if let Ok(result) = engine.eval::("add(40, 2)") { println!("Answer: {}", result); // prints 42 } diff --git a/src/engine.rs b/src/engine.rs index f4b0bb97..72f5daee 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -22,7 +22,7 @@ pub enum EvalAltResult { ErrorAssignmentToUnknownLHS, ErrorMismatchOutputType, ErrorCantOpenScriptFile, - InternalErrorMalformedDotExpression, + InternalErrorMalformedDotExpression, LoopBreak, Return(Box) } @@ -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(&mut self, name: &str, get_fn: F) + pub fn register_get(&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(&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(&mut self, name: &str, get_fn: F, set_fn: G) + pub fn register_get_set(&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, dot_rhs: &Expr) -> Result, 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::() { if let Some(arr_typed) = (*val).downcast_mut() as Option<&mut Vec>> { @@ -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::() { i } else { return Err(EvalAltResult::ErrorIndexMismatch); }; - + let mut target : Option> = 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>> { - 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::() { i } else { return Err(EvalAltResult::ErrorIndexMismatch); }; - + let mut target : Option> = 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>> { - 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::(&contents) } else { Err(EvalAltResult::ErrorCantOpenScriptFile) - } + } } else { Err(EvalAltResult::ErrorCantOpenScriptFile) - } + } } pub fn eval(&mut self, input: &str) -> Result { @@ -911,7 +911,7 @@ impl Engine { match tree { Ok((ref os, ref fns)) => { let mut x: Result, 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::(&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); } diff --git a/src/fn_register.rs b/src/fn_register.rs index 5c4f7fbf..d3d4cb15 100644 --- a/src/fn_register.rs +++ b/src/fn_register.rs @@ -8,26 +8,26 @@ pub trait FnRegister { } impl<'a, A, T, U, V, W, X, Y, Z> FnRegister 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, &mut Box, &mut Box, &mut Box, &mut Box, - &mut Box)->Result, EvalAltResult>> = - + let wrapped : Box, &mut Box, &mut Box, &mut Box, &mut Box, + &mut Box)->Result, EvalAltResult>> = + Box::new( move |arg1: &mut Box, arg2: &mut Box, arg3: &mut Box, arg4: &mut Box, arg5: &mut Box, arg6: &mut Box| { - + 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), _ => Err(EvalAltResult::ErrorFunctionArgMismatch) } @@ -40,13 +40,13 @@ impl<'a, A, T, U, V, W, X, Y, Z> FnRegister fo } impl<'a, A, T, U, V, W, X, Y, Z> FnRegister 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, &mut Box, &mut Box, &mut Box, &mut Box, - &mut Box)->Result, EvalAltResult>> = - + let wrapped : Box, &mut Box, &mut Box, &mut Box, &mut Box, + &mut Box)->Result, EvalAltResult>> = + Box::new( move |arg1: &mut Box, arg2: &mut Box, arg3: &mut Box, arg4: &mut Box, arg5: &mut Box, arg6: &mut Box| { @@ -57,9 +57,9 @@ impl<'a, A, T, U, V, W, X, Y, Z> FnRegister 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), _ => Err(EvalAltResult::ErrorFunctionArgMismatch) } @@ -75,7 +75,7 @@ impl<'a, A, T, U, V, W, X, Y> FnRegister 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, &mut Box, &mut Box, &mut Box, &mut Box)->Result, EvalAltResult>> = + let wrapped : Box, &mut Box, &mut Box, &mut Box, &mut Box)->Result, EvalAltResult>> = Box::new( move |arg1: &mut Box, arg2: &mut Box, arg3: &mut Box, arg4: &mut Box, arg5: &mut Box| { @@ -85,9 +85,9 @@ impl<'a, A, T, U, V, W, X, Y> FnRegister 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), _ => Err(EvalAltResult::ErrorFunctionArgMismatch) } @@ -103,7 +103,7 @@ impl<'a, A, T, U, V, W, X, Y> FnRegister 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, &mut Box, &mut Box, &mut Box, &mut Box)->Result, EvalAltResult>> = + let wrapped : Box, &mut Box, &mut Box, &mut Box, &mut Box)->Result, EvalAltResult>> = Box::new( move |arg1: &mut Box, arg2: &mut Box, arg3: &mut Box, arg4: &mut Box, arg5: &mut Box| { @@ -113,9 +113,9 @@ impl<'a, A, T, U, V, W, X, Y> FnRegister 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), _ => Err(EvalAltResult::ErrorFunctionArgMismatch) } @@ -131,14 +131,14 @@ impl<'a, A, T, U, V, W, X> FnRegister 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, &mut Box, &mut Box, &mut Box)->Result, EvalAltResult>> = + let wrapped : Box, &mut Box, &mut Box, &mut Box)->Result, EvalAltResult>> = Box::new( move |arg1: &mut Box, arg2: &mut Box, arg3: &mut Box, arg4: &mut Box| { 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), _ => Err(EvalAltResult::ErrorFunctionArgMismatch) @@ -155,14 +155,14 @@ impl<'a, A, T, U, V, W, X> FnRegister 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, &mut Box, &mut Box, &mut Box)->Result, EvalAltResult>> = + let wrapped : Box, &mut Box, &mut Box, &mut Box)->Result, EvalAltResult>> = Box::new( move |arg1: &mut Box, arg2: &mut Box, arg3: &mut Box, arg4: &mut Box| { 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), _ => Err(EvalAltResult::ErrorFunctionArgMismatch) @@ -179,13 +179,13 @@ impl<'a, A, T, U, V, W> FnRegister 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, &mut Box, &mut Box)->Result, EvalAltResult>> = + let wrapped : Box, &mut Box, &mut Box)->Result, EvalAltResult>> = Box::new( move |arg1: &mut Box, arg2: &mut Box, arg3: &mut Box| { 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), _ => Err(EvalAltResult::ErrorFunctionArgMismatch) @@ -202,13 +202,13 @@ impl<'a, A, T, U, V, W> FnRegister 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, &mut Box, &mut Box)->Result, EvalAltResult>> = + let wrapped : Box, &mut Box, &mut Box)->Result, EvalAltResult>> = Box::new( move |arg1: &mut Box, arg2: &mut Box, arg3: &mut Box| { 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), _ => Err(EvalAltResult::ErrorFunctionArgMismatch) @@ -225,12 +225,12 @@ impl<'a, A, T, U, V> FnRegister 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, &mut Box)->Result, EvalAltResult>> = + let wrapped : Box, &mut Box)->Result, EvalAltResult>> = Box::new( move |arg1: &mut Box, arg2: &mut Box| { 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), _ => Err(EvalAltResult::ErrorFunctionArgMismatch) @@ -247,12 +247,12 @@ impl<'a, A, T, U, V> FnRegister 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, &mut Box)->Result, EvalAltResult>> = + let wrapped : Box, &mut Box)->Result, EvalAltResult>> = Box::new( move |arg1: &mut Box, arg2: &mut Box| { 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), _ => Err(EvalAltResult::ErrorFunctionArgMismatch) @@ -266,14 +266,14 @@ impl<'a, A, T, U, V> FnRegister for Engine } impl<'a, A, T, U> FnRegister 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)->Result, EvalAltResult>> = + let wrapped : Box)->Result, EvalAltResult>> = Box::new( move |arg: &mut Box| { let inside = (*arg).downcast_mut() as Option<&mut T>; - + match inside { Some(b) => Ok(Box::new(fun(b)) as Box), None => Err(EvalAltResult::ErrorFunctionArgMismatch) @@ -291,7 +291,7 @@ impl<'a, A, T, U> FnRegister 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)->Result, EvalAltResult>> = + let wrapped : Box)->Result, EvalAltResult>> = Box::new( move |arg: &mut Box| { let inside = (*arg).downcast_mut() as Option<&mut T>; @@ -311,7 +311,7 @@ impl FnRegister for Engine where A: 'static+Fn() -> T, T: Any { fn register_fn(&mut self, name: &str, fun: A) { - let wrapped : BoxResult, EvalAltResult>> = + let wrapped : BoxResult, EvalAltResult>> = Box::new( move || { Ok(Box::new(fun()) as Box) } ); diff --git a/src/parser.rs b/src/parser.rs index 3a735ce9..26929e6f 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -86,11 +86,11 @@ pub struct FnDef { } #[derive(Debug, Clone)] -pub enum Stmt { If(Box, Box), IfElse(Box, Box, Box), While(Box, Box), +pub enum Stmt { If(Box, Box), IfElse(Box, Box, Box), While(Box, Box), Var(String, Option>), Block(Box>), Expr(Box), Break, Return, ReturnWithVal(Box) } #[derive(Debug, Clone)] -pub enum Expr { IntConst(i32), Identifier(String), StringConst(String), FnCall(String, Box>), +pub enum Expr { IntConst(i32), Identifier(String), StringConst(String), FnCall(String, Box>), Assignment(Box, Box), Dot(Box, Box), Index(String, Box), Array(Box>), 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>) -> Result(id: String, input: &mut Peekable>) -> Result { +fn parse_call_expr<'a>(id: String, input: &mut Peekable>) -> Result { 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>) -> R } } -fn parse_index_expr<'a>(id: String, input: &mut Peekable>) -> Result { +fn parse_index_expr<'a>(id: String, input: &mut Peekable>) -> Result { 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>) -> } } -fn parse_ident_expr<'a>(id: String, input: &mut Peekable>) -> Result { +fn parse_ident_expr<'a>(id: String, input: &mut Peekable>) -> Result { 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>) -> Result {input.next();}, + Some(& Token::Comma) => {input.next();}, _ => () } @@ -434,7 +434,7 @@ fn parse_array_expr<'a>(input: &mut Peekable>) -> Result break, _ => () } - } + } } match input.peek() { @@ -469,7 +469,7 @@ fn parse_binop<'a>(input: &mut Peekable>, 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>, 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>, 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>) -> Result {input.next();}, + Some(& Token::Semicolon) => {input.next();}, _ => () } @@ -593,7 +593,7 @@ fn parse_block<'a>(input: &mut Peekable>) -> Result break, _ => () } - } + } } match input.peek() { @@ -603,7 +603,7 @@ fn parse_block<'a>(input: &mut Peekable>) -> Result(input: &mut Peekable>) -> Result { - 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>) -> Result 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>) -> Result { 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>) -> Result<(Vec {input.next();}, + Some(& Token::Semicolon) => {input.next();}, _ => () } }