clippy refactoring

This commit is contained in:
Lukáš Hozda 2017-10-02 23:44:45 +02:00
parent 92465e899e
commit a1e92d006c
5 changed files with 184 additions and 204 deletions

View File

@ -5,7 +5,7 @@ fn main() {
let mut engine = Engine::new(); let mut engine = Engine::new();
let mut scope: Scope = Vec::new(); let mut scope: Scope = Vec::new();
if let Ok(_) = engine.eval_with_scope::<()>(&mut scope, "let x = 4 + 5") { } else { assert!(false); } if !engine.eval_with_scope::<()>(&mut scope, "let x = 4 + 5").is_ok() { assert!(false); }
if let Ok(result) = engine.eval_with_scope::<i64>(&mut scope, "x") { if let Ok(result) = engine.eval_with_scope::<i64>(&mut scope, "x") {
println!("result: {}", result); println!("result: {}", result);

View File

@ -109,7 +109,7 @@ impl Engine {
-> Result<Box<Any>, EvalAltResult> { -> Result<Box<Any>, EvalAltResult> {
match self.fns.get(name) { match self.fns.get(name) {
Some(ref vf) => { Some(vf) => {
match (arg1, arg2, arg3, arg4, arg5, arg6) { match (arg1, arg2, arg3, arg4, arg5, arg6) {
(Some(ref mut a1), (Some(ref mut a1),
Some(ref mut a2), Some(ref mut a2),
@ -117,15 +117,14 @@ impl Engine {
Some(ref mut a4), Some(ref mut a4),
Some(ref mut a5), Some(ref mut a5),
Some(ref mut a6)) => { Some(ref mut a6)) => {
for arr_f in *vf { for arr_f in vf {
match arr_f { match *arr_f {
&FnType::ExternalFn6(ref f) => { FnType::ExternalFn6(ref f) => {
match f(*a1, *a2, *a3, *a4, *a5, *a6) { if let Ok(v) = f(*a1, *a2, *a3, *a4, *a5, *a6) {
Ok(v) => return Ok(v), return Ok(v);
_ => (),
} }
} }
&FnType::InternalFn(ref f) => { FnType::InternalFn(ref f) => {
if f.params.len() != 6 { if f.params.len() != 6 {
return Err(EvalAltResult::ErrorFunctionArgMismatch); return Err(EvalAltResult::ErrorFunctionArgMismatch);
} }
@ -193,7 +192,7 @@ impl Engine {
_ => (), _ => (),
} }
} }
return Err(EvalAltResult::ErrorFunctionArgMismatch); Err(EvalAltResult::ErrorFunctionArgMismatch)
} }
(Some(ref mut a1), (Some(ref mut a1),
Some(ref mut a2), Some(ref mut a2),
@ -201,15 +200,14 @@ impl Engine {
Some(ref mut a4), Some(ref mut a4),
Some(ref mut a5), Some(ref mut a5),
None) => { None) => {
for arr_f in *vf { for arr_f in vf {
match arr_f { match *arr_f {
&FnType::ExternalFn5(ref f) => { FnType::ExternalFn5(ref f) => {
match f(*a1, *a2, *a3, *a4, *a5) { if let Ok(v) = f(*a1, *a2, *a3, *a4, *a5) {
Ok(v) => return Ok(v), return Ok(v);
_ => (),
} }
} }
&FnType::InternalFn(ref f) => { FnType::InternalFn(ref f) => {
if f.params.len() != 5 { if f.params.len() != 5 {
return Err(EvalAltResult::ErrorFunctionArgMismatch); return Err(EvalAltResult::ErrorFunctionArgMismatch);
} }
@ -269,7 +267,7 @@ impl Engine {
_ => (), _ => (),
} }
} }
return Err(EvalAltResult::ErrorFunctionArgMismatch); Err(EvalAltResult::ErrorFunctionArgMismatch)
} }
(Some(ref mut a1), (Some(ref mut a1),
Some(ref mut a2), Some(ref mut a2),
@ -277,15 +275,14 @@ impl Engine {
Some(ref mut a4), Some(ref mut a4),
None, None,
None) => { None) => {
for arr_f in *vf { for arr_f in vf {
match arr_f { match *arr_f {
&FnType::ExternalFn4(ref f) => { FnType::ExternalFn4(ref f) => {
match f(*a1, *a2, *a3, *a4) { if let Ok(v) = f(*a1, *a2, *a3, *a4) {
Ok(v) => return Ok(v), return Ok(v)
_ => (),
} }
} }
&FnType::InternalFn(ref f) => { FnType::InternalFn(ref f) => {
if f.params.len() != 4 { if f.params.len() != 4 {
return Err(EvalAltResult::ErrorFunctionArgMismatch); return Err(EvalAltResult::ErrorFunctionArgMismatch);
} }
@ -336,18 +333,17 @@ impl Engine {
_ => (), _ => (),
} }
} }
return Err(EvalAltResult::ErrorFunctionArgMismatch); Err(EvalAltResult::ErrorFunctionArgMismatch)
} }
(Some(ref mut a1), Some(ref mut a2), Some(ref mut a3), None, None, None) => { (Some(ref mut a1), Some(ref mut a2), Some(ref mut a3), None, None, None) => {
for arr_f in *vf { for arr_f in vf {
match arr_f { match *arr_f {
&FnType::ExternalFn3(ref f) => { FnType::ExternalFn3(ref f) => {
match f(*a1, *a2, *a3) { if let Ok(v) = f(*a1, *a2, *a3) {
Ok(v) => return Ok(v), return Ok(v);
_ => (),
} }
} }
&FnType::InternalFn(ref f) => { FnType::InternalFn(ref f) => {
if f.params.len() != 3 { if f.params.len() != 3 {
return Err(EvalAltResult::ErrorFunctionArgMismatch); return Err(EvalAltResult::ErrorFunctionArgMismatch);
} }
@ -390,18 +386,17 @@ impl Engine {
_ => (), _ => (),
} }
} }
return Err(EvalAltResult::ErrorFunctionArgMismatch); Err(EvalAltResult::ErrorFunctionArgMismatch)
} }
(Some(ref mut a1), Some(ref mut a2), None, None, None, None) => { (Some(ref mut a1), Some(ref mut a2), None, None, None, None) => {
for arr_f in *vf { for arr_f in vf {
match arr_f { match *arr_f {
&FnType::ExternalFn2(ref f) => { FnType::ExternalFn2(ref f) => {
match f(*a1, *a2) { if let Ok(v) = f(*a1, *a2) {
Ok(v) => return Ok(v), return Ok(v);
_ => (),
} }
} }
&FnType::InternalFn(ref f) => { FnType::InternalFn(ref f) => {
if f.params.len() != 2 { if f.params.len() != 2 {
return Err(EvalAltResult::ErrorFunctionArgMismatch); return Err(EvalAltResult::ErrorFunctionArgMismatch);
} }
@ -436,18 +431,17 @@ impl Engine {
_ => (), _ => (),
} }
} }
return Err(EvalAltResult::ErrorFunctionArgMismatch); Err(EvalAltResult::ErrorFunctionArgMismatch)
} }
(Some(ref mut a1), None, None, None, None, None) => { (Some(ref mut a1), None, None, None, None, None) => {
for arr_f in *vf { for arr_f in vf {
match arr_f { match *arr_f {
&FnType::ExternalFn1(ref f) => { FnType::ExternalFn1(ref f) => {
match f(*a1) { if let Ok(v) = f(*a1) {
Ok(v) => return Ok(v), return Ok(v);
_ => (),
} }
} }
&FnType::InternalFn(ref f) => { FnType::InternalFn(ref f) => {
if f.params.len() != 1 { if f.params.len() != 1 {
return Err(EvalAltResult::ErrorFunctionArgMismatch); return Err(EvalAltResult::ErrorFunctionArgMismatch);
} }
@ -474,19 +468,18 @@ impl Engine {
_ => (), _ => (),
} }
} }
return Err(EvalAltResult::ErrorFunctionArgMismatch); Err(EvalAltResult::ErrorFunctionArgMismatch)
} }
_ => { _ => {
for arr_f in *vf { for arr_f in vf {
match arr_f { match *arr_f {
&FnType::ExternalFn0(ref f) => { FnType::ExternalFn0(ref f) => {
match f() { if let Ok(v) = f() {
Ok(v) => return Ok(v), return Ok(v);
_ => (),
} }
} }
&FnType::InternalFn(ref f) => { FnType::InternalFn(ref f) => {
if f.params.len() != 0 { if !f.params.is_empty() {
return Err(EvalAltResult::ErrorFunctionArgMismatch); return Err(EvalAltResult::ErrorFunctionArgMismatch);
} }
@ -499,7 +492,7 @@ impl Engine {
_ => (), _ => (),
} }
} }
return Err(EvalAltResult::ErrorFunctionArgMismatch); Err(EvalAltResult::ErrorFunctionArgMismatch)
} }
} }
} }
@ -550,54 +543,54 @@ impl Engine {
-> Result<Box<Any>, EvalAltResult> { -> Result<Box<Any>, EvalAltResult> {
match *dot_rhs { match *dot_rhs {
Expr::FnCall(ref fn_name, ref args) => { Expr::FnCall(ref fn_name, ref args) => {
if args.len() == 0 { if args.is_empty() {
return self.call_fn(&fn_name, Some(this_ptr), None, None, None, None, None); self.call_fn(fn_name, Some(this_ptr), None, None, None, None, None)
} else if args.len() == 1 { } else if args.len() == 1 {
let mut arg = try!(self.eval_expr(scope, &args[0])); let mut arg = try!(self.eval_expr(scope, &args[0]));
return self.call_fn(&fn_name, self.call_fn(fn_name,
Some(this_ptr), Some(this_ptr),
Some(&mut arg), Some(&mut arg),
None, None,
None, None,
None, None,
None); None)
} else if args.len() == 2 { } else if args.len() == 2 {
let mut arg1 = try!(self.eval_expr(scope, &args[0])); let mut arg1 = try!(self.eval_expr(scope, &args[0]));
let mut arg2 = try!(self.eval_expr(scope, &args[1])); let mut arg2 = try!(self.eval_expr(scope, &args[1]));
return self.call_fn(&fn_name, self.call_fn(fn_name,
Some(this_ptr), Some(this_ptr),
Some(&mut arg1), Some(&mut arg1),
Some(&mut arg2), Some(&mut arg2),
None, None,
None, None,
None); None)
} else if args.len() == 3 { } else if args.len() == 3 {
let mut arg1 = try!(self.eval_expr(scope, &args[0])); let mut arg1 = try!(self.eval_expr(scope, &args[0]));
let mut arg2 = try!(self.eval_expr(scope, &args[1])); let mut arg2 = try!(self.eval_expr(scope, &args[1]));
let mut arg3 = try!(self.eval_expr(scope, &args[2])); let mut arg3 = try!(self.eval_expr(scope, &args[2]));
return self.call_fn(&fn_name, self.call_fn(fn_name,
Some(this_ptr), Some(this_ptr),
Some(&mut arg1), Some(&mut arg1),
Some(&mut arg2), Some(&mut arg2),
Some(&mut arg3), Some(&mut arg3),
None, None,
None); None)
} else if args.len() == 4 { } else if args.len() == 4 {
let mut arg1 = try!(self.eval_expr(scope, &args[0])); let mut arg1 = try!(self.eval_expr(scope, &args[0]));
let mut arg2 = try!(self.eval_expr(scope, &args[1])); let mut arg2 = try!(self.eval_expr(scope, &args[1]));
let mut arg3 = try!(self.eval_expr(scope, &args[2])); let mut arg3 = try!(self.eval_expr(scope, &args[2]));
let mut arg4 = try!(self.eval_expr(scope, &args[3])); let mut arg4 = try!(self.eval_expr(scope, &args[3]));
return self.call_fn(&fn_name, self.call_fn(fn_name,
Some(this_ptr), Some(this_ptr),
Some(&mut arg1), Some(&mut arg1),
Some(&mut arg2), Some(&mut arg2),
Some(&mut arg3), Some(&mut arg3),
Some(&mut arg4), Some(&mut arg4),
None); None)
} else if args.len() == 5 { } else if args.len() == 5 {
let mut arg1 = try!(self.eval_expr(scope, &args[0])); let mut arg1 = try!(self.eval_expr(scope, &args[0]));
let mut arg2 = try!(self.eval_expr(scope, &args[1])); let mut arg2 = try!(self.eval_expr(scope, &args[1]));
@ -605,20 +598,20 @@ impl Engine {
let mut arg4 = try!(self.eval_expr(scope, &args[3])); let mut arg4 = try!(self.eval_expr(scope, &args[3]));
let mut arg5 = try!(self.eval_expr(scope, &args[4])); let mut arg5 = try!(self.eval_expr(scope, &args[4]));
return self.call_fn(&fn_name, self.call_fn(fn_name,
Some(this_ptr), Some(this_ptr),
Some(&mut arg1), Some(&mut arg1),
Some(&mut arg2), Some(&mut arg2),
Some(&mut arg3), Some(&mut arg3),
Some(&mut arg4), Some(&mut arg4),
Some(&mut arg5)); Some(&mut arg5))
} else { } else {
Err(EvalAltResult::ErrorFunctionCallNotSupported) Err(EvalAltResult::ErrorFunctionCallNotSupported)
} }
} }
Expr::Identifier(ref id) => { Expr::Identifier(ref id) => {
let get_fn_name = "get$".to_string() + id; let get_fn_name = "get$".to_string() + id;
return self.call_fn(&get_fn_name, Some(this_ptr), None, None, None, None, None); self.call_fn(&get_fn_name, Some(this_ptr), None, None, None, None, None)
} }
Expr::Index(ref id, ref idx_raw) => { Expr::Index(ref id, ref idx_raw) => {
let idx = try!(self.eval_expr(scope, idx_raw)); let idx = try!(self.eval_expr(scope, idx_raw));
@ -665,8 +658,8 @@ impl Engine {
None); None);
match result { match result {
Ok(mut v) => return self.get_dot_val_helper(scope, &mut v, inner_rhs), Ok(mut v) => self.get_dot_val_helper(scope, &mut v, inner_rhs),
e => return e, e => e,
} }
} }
_ => Err(EvalAltResult::InternalErrorMalformedDotExpression), _ => Err(EvalAltResult::InternalErrorMalformedDotExpression),
@ -710,7 +703,7 @@ impl Engine {
return result; return result;
} }
return Err(EvalAltResult::ErrorVariableNotFound(id.clone())); Err(EvalAltResult::ErrorVariableNotFound(id.clone()))
} }
Expr::Index(ref id, ref idx_raw) => { Expr::Index(ref id, ref idx_raw) => {
let idx_boxed = try!(self.eval_expr(scope, idx_raw)); let idx_boxed = try!(self.eval_expr(scope, idx_raw));
@ -861,7 +854,7 @@ impl Engine {
return result; return result;
} }
return Err(EvalAltResult::ErrorAssignmentToUnknownLHS); Err(EvalAltResult::ErrorAssignmentToUnknownLHS)
} }
Expr::Index(ref id, ref idx_raw) => { Expr::Index(ref id, ref idx_raw) => {
let idx_boxed = try!(self.eval_expr(scope, idx_raw)); let idx_boxed = try!(self.eval_expr(scope, idx_raw));
@ -921,7 +914,7 @@ impl Engine {
match *expr { match *expr {
Expr::IntConst(i) => Ok(Box::new(i)), Expr::IntConst(i) => Ok(Box::new(i)),
Expr::StringConst(ref s) => Ok(Box::new(s.clone())), Expr::StringConst(ref s) => Ok(Box::new(s.clone())),
Expr::CharConst(ref c) => Ok(Box::new(c.clone())), Expr::CharConst(ref c) => Ok(Box::new(*c)),
Expr::Identifier(ref id) => { Expr::Identifier(ref id) => {
for &mut (ref name, ref mut val) in &mut scope.iter_mut().rev() { for &mut (ref name, ref mut val) in &mut scope.iter_mut().rev() {
if *id == *name { if *id == *name {
@ -1002,7 +995,7 @@ impl Engine {
Expr::Array(ref contents) => { Expr::Array(ref contents) => {
let mut arr = Vec::new(); let mut arr = Vec::new();
for item in (*contents).iter() { for item in &(*contents) {
let arg = try!(self.eval_expr(scope, item)); let arg = try!(self.eval_expr(scope, item));
arr.push(arg); arr.push(arg);
} }
@ -1010,17 +1003,17 @@ impl Engine {
Ok(Box::new(arr)) Ok(Box::new(arr))
} }
Expr::FnCall(ref fn_name, ref args) => { Expr::FnCall(ref fn_name, ref args) => {
if args.len() == 0 { if args.is_empty() {
self.call_fn(&fn_name, None, None, None, None, None, None) self.call_fn(fn_name, None, None, None, None, None, None)
} else if args.len() == 1 { } else if args.len() == 1 {
let mut arg = try!(self.eval_expr(scope, &args[0])); let mut arg = try!(self.eval_expr(scope, &args[0]));
self.call_fn(&fn_name, Some(&mut arg), None, None, None, None, None) self.call_fn(fn_name, Some(&mut arg), None, None, None, None, None)
} else if args.len() == 2 { } else if args.len() == 2 {
let mut arg1 = try!(self.eval_expr(scope, &args[0])); let mut arg1 = try!(self.eval_expr(scope, &args[0]));
let mut arg2 = try!(self.eval_expr(scope, &args[1])); let mut arg2 = try!(self.eval_expr(scope, &args[1]));
self.call_fn(&fn_name, self.call_fn(fn_name,
Some(&mut arg1), Some(&mut arg1),
Some(&mut arg2), Some(&mut arg2),
None, None,
@ -1032,7 +1025,7 @@ impl Engine {
let mut arg2 = try!(self.eval_expr(scope, &args[1])); let mut arg2 = try!(self.eval_expr(scope, &args[1]));
let mut arg3 = try!(self.eval_expr(scope, &args[2])); let mut arg3 = try!(self.eval_expr(scope, &args[2]));
self.call_fn(&fn_name, self.call_fn(fn_name,
Some(&mut arg1), Some(&mut arg1),
Some(&mut arg2), Some(&mut arg2),
Some(&mut arg3), Some(&mut arg3),
@ -1045,7 +1038,7 @@ impl Engine {
let mut arg3 = try!(self.eval_expr(scope, &args[2])); let mut arg3 = try!(self.eval_expr(scope, &args[2]));
let mut arg4 = try!(self.eval_expr(scope, &args[3])); let mut arg4 = try!(self.eval_expr(scope, &args[3]));
self.call_fn(&fn_name, self.call_fn(fn_name,
Some(&mut arg1), Some(&mut arg1),
Some(&mut arg2), Some(&mut arg2),
Some(&mut arg3), Some(&mut arg3),
@ -1059,7 +1052,7 @@ impl Engine {
let mut arg4 = try!(self.eval_expr(scope, &args[3])); let mut arg4 = try!(self.eval_expr(scope, &args[3]));
let mut arg5 = try!(self.eval_expr(scope, &args[4])); let mut arg5 = try!(self.eval_expr(scope, &args[4]));
self.call_fn(&fn_name, self.call_fn(fn_name,
Some(&mut arg1), Some(&mut arg1),
Some(&mut arg2), Some(&mut arg2),
Some(&mut arg3), Some(&mut arg3),
@ -1074,7 +1067,7 @@ impl Engine {
let mut arg5 = try!(self.eval_expr(scope, &args[4])); let mut arg5 = try!(self.eval_expr(scope, &args[4]));
let mut arg6 = try!(self.eval_expr(scope, &args[5])); let mut arg6 = try!(self.eval_expr(scope, &args[5]));
self.call_fn(&fn_name, self.call_fn(fn_name,
Some(&mut arg1), Some(&mut arg1),
Some(&mut arg2), Some(&mut arg2),
Some(&mut arg3), Some(&mut arg3),
@ -1099,12 +1092,9 @@ impl Engine {
for s in b.iter() { for s in b.iter() {
last_result = self.eval_stmt(scope, s); last_result = self.eval_stmt(scope, s);
match last_result { if let Err(x) = last_result {
Err(x) => { last_result = Err(x);
last_result = Err(x); break;
break;
}
_ => (),
} }
} }
@ -1112,7 +1102,7 @@ impl Engine {
scope.pop(); scope.pop();
} }
return last_result; last_result
} }
Stmt::If(ref guard, ref body) => { Stmt::If(ref guard, ref body) => {
let guard_result = try!(self.eval_expr(scope, guard)); let guard_result = try!(self.eval_expr(scope, guard));
@ -1163,19 +1153,19 @@ impl Engine {
} }
} }
} }
Stmt::Break => return Err(EvalAltResult::LoopBreak), Stmt::Break => Err(EvalAltResult::LoopBreak),
Stmt::Return => return Err(EvalAltResult::Return(Box::new(()))), Stmt::Return => Err(EvalAltResult::Return(Box::new(()))),
Stmt::ReturnWithVal(ref a) => { Stmt::ReturnWithVal(ref a) => {
let result = try!(self.eval_expr(scope, a)); let result = try!(self.eval_expr(scope, a));
return Err(EvalAltResult::Return(result)); Err(EvalAltResult::Return(result))
} }
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(scope, v)); let i = try!(self.eval_expr(scope, v));
scope.push((name.clone(), i)); scope.push((name.clone(), i));
} }
&None => { None => {
scope.push((name.clone(), Box::new(()))); scope.push((name.clone(), Box::new(())));
} }
}; };
@ -1188,10 +1178,10 @@ impl Engine {
use std::fs::File; use std::fs::File;
use std::io::prelude::*; use std::io::prelude::*;
if let Ok(mut f) = File::open(fname.clone()) { if let Ok(mut f) = File::open(fname) {
let mut contents = String::new(); let mut contents = String::new();
if let Ok(_) = f.read_to_string(&mut contents) { if f.read_to_string(&mut contents).is_ok() {
self.eval::<T>(&contents) self.eval::<T>(&contents)
} else { } else {
Err(EvalAltResult::ErrorCantOpenScriptFile) Err(EvalAltResult::ErrorCantOpenScriptFile)
@ -1226,12 +1216,12 @@ impl Engine {
} }
let name = f.name.clone(); let name = f.name.clone();
let local_f = f.clone(); let local_f = f.clone();
let ent = self.fns.entry(name).or_insert(Vec::new()); let ent = self.fns.entry(name).or_insert_with(Vec::new);
(*ent).push(FnType::InternalFn(local_f)); (*ent).push(FnType::InternalFn(local_f));
} }
for o in os { for o in os {
x = match self.eval_stmt(scope, &o) { x = match self.eval_stmt(scope, o) {
Ok(v) => Ok(v), Ok(v) => Ok(v),
Err(e) => return Err(e), Err(e) => return Err(e),
} }
@ -1337,7 +1327,7 @@ impl Engine {
// engine.register_fn("[]", idx); // engine.register_fn("[]", idx);
// FIXME? Registering array lookups are a special case because we want to return boxes // FIXME? Registering array lookups are a special case because we want to return boxes
// directly let ent = engine.fns.entry("[]".to_string()).or_insert(Vec::new()); // directly let ent = engine.fns.entry("[]".to_string()).or_insert_with(Vec::new);
// (*ent).push(FnType::ExternalFn2(Box::new(idx))); // (*ent).push(FnType::ExternalFn2(Box::new(idx)));
} }

View File

@ -52,7 +52,7 @@ impl<'a, A, T, U, V, W, X, Y, Z> FnRegister<A, Z, (&'a mut T, U, V, W, X, Y)> fo
} }
}); });
let ent = self.fns.entry(name.to_string()).or_insert(Vec::new()); let ent = self.fns.entry(name.to_string()).or_insert_with(Vec::new);
(*ent).push(FnType::ExternalFn6(wrapped)); (*ent).push(FnType::ExternalFn6(wrapped));
} }
} }
@ -102,7 +102,7 @@ impl<'a, A, T, U, V, W, X, Y, Z> FnRegister<A, Z, (&'a T, U, V, W, X, Y)> for En
} }
}); });
let ent = self.fns.entry(name.to_string()).or_insert(Vec::new()); let ent = self.fns.entry(name.to_string()).or_insert_with(Vec::new);
(*ent).push(FnType::ExternalFn6(wrapped)); (*ent).push(FnType::ExternalFn6(wrapped));
} }
} }
@ -143,7 +143,7 @@ impl<'a, A, T, U, V, W, X, Y> FnRegister<A, Y, (&'a mut T, U, V, W, X)> for Engi
} }
}); });
let ent = self.fns.entry(name.to_string()).or_insert(Vec::new()); let ent = self.fns.entry(name.to_string()).or_insert_with(Vec::new);
(*ent).push(FnType::ExternalFn5(wrapped)); (*ent).push(FnType::ExternalFn5(wrapped));
} }
} }
@ -188,7 +188,7 @@ impl<'a, A, T, U, V, W, X, Y> FnRegister<A, Y, (&'a T, U, V, W, X)> for Engine
} }
}); });
let ent = self.fns.entry(name.to_string()).or_insert(Vec::new()); let ent = self.fns.entry(name.to_string()).or_insert_with(Vec::new);
(*ent).push(FnType::ExternalFn5(wrapped)); (*ent).push(FnType::ExternalFn5(wrapped));
} }
} }
@ -221,7 +221,7 @@ impl<'a, A, T, U, V, W, X> FnRegister<A, X, (&'a mut T, U, V, W)> for Engine
} }
}); });
let ent = self.fns.entry(name.to_string()).or_insert(Vec::new()); let ent = self.fns.entry(name.to_string()).or_insert_with(Vec::new);
(*ent).push(FnType::ExternalFn4(wrapped)); (*ent).push(FnType::ExternalFn4(wrapped));
} }
} }
@ -254,7 +254,7 @@ impl<'a, A, T, U, V, W, X> FnRegister<A, X, (&'a T, U, V, W)> for Engine
} }
}); });
let ent = self.fns.entry(name.to_string()).or_insert(Vec::new()); let ent = self.fns.entry(name.to_string()).or_insert_with(Vec::new);
(*ent).push(FnType::ExternalFn4(wrapped)); (*ent).push(FnType::ExternalFn4(wrapped));
} }
} }
@ -282,7 +282,7 @@ impl<'a, A, T, U, V, W> FnRegister<A, W, (&'a mut T, U, V)> for Engine
} }
}); });
let ent = self.fns.entry(name.to_string()).or_insert(Vec::new()); let ent = self.fns.entry(name.to_string()).or_insert_with(Vec::new);
(*ent).push(FnType::ExternalFn3(wrapped)); (*ent).push(FnType::ExternalFn3(wrapped));
} }
} }
@ -310,7 +310,7 @@ impl<'a, A, T, U, V, W> FnRegister<A, W, (&'a T, U, V)> for Engine
} }
}); });
let ent = self.fns.entry(name.to_string()).or_insert(Vec::new()); let ent = self.fns.entry(name.to_string()).or_insert_with(Vec::new);
(*ent).push(FnType::ExternalFn3(wrapped)); (*ent).push(FnType::ExternalFn3(wrapped));
} }
} }
@ -333,7 +333,7 @@ impl<'a, A, T, U, V> FnRegister<A, V, (&'a mut T, U)> for Engine
} }
}); });
let ent = self.fns.entry(name.to_string()).or_insert(Vec::new()); let ent = self.fns.entry(name.to_string()).or_insert_with(Vec::new);
(*ent).push(FnType::ExternalFn2(wrapped)); (*ent).push(FnType::ExternalFn2(wrapped));
} }
} }
@ -356,7 +356,7 @@ impl<'a, A, T, U, V> FnRegister<A, V, (&'a T, U)> for Engine
} }
}); });
let ent = self.fns.entry(name.to_string()).or_insert(Vec::new()); let ent = self.fns.entry(name.to_string()).or_insert_with(Vec::new);
(*ent).push(FnType::ExternalFn2(wrapped)); (*ent).push(FnType::ExternalFn2(wrapped));
} }
} }
@ -377,7 +377,7 @@ impl<'a, A, T, U> FnRegister<A, U, (&'a mut T)> for Engine
} }
}); });
let ent = self.fns.entry(name.to_string()).or_insert(Vec::new()); let ent = self.fns.entry(name.to_string()).or_insert_with(Vec::new);
(*ent).push(FnType::ExternalFn1(wrapped)); (*ent).push(FnType::ExternalFn1(wrapped));
} }
} }
@ -398,7 +398,7 @@ impl<'a, A, T, U> FnRegister<A, U, (&'a T)> for Engine
} }
}); });
let ent = self.fns.entry(name.to_string()).or_insert(Vec::new()); let ent = self.fns.entry(name.to_string()).or_insert_with(Vec::new);
(*ent).push(FnType::ExternalFn1(wrapped)); (*ent).push(FnType::ExternalFn1(wrapped));
} }
} }
@ -412,7 +412,7 @@ impl<A, T> FnRegister<A, T, ()> for Engine
Ok(Box::new(fun()) as Box<Any>) Ok(Box::new(fun()) as Box<Any>)
}); });
let ent = self.fns.entry(name.to_string()).or_insert(Vec::new()); let ent = self.fns.entry(name.to_string()).or_insert_with(Vec::new);
(*ent).push(FnType::ExternalFn0(wrapped)); (*ent).push(FnType::ExternalFn0(wrapped));
} }
} }

View File

@ -1,5 +1,12 @@
//! Rhai - embedded scripting for Rust //! Rhai - embedded scripting for Rust
// lints required by Rhai
#![allow(unknown_lints,
type_complexity,
new_without_default_derive,
needless_pass_by_value,
too_many_arguments)]
mod engine; mod engine;
mod fn_register; mod fn_register;
mod parser; mod parser;

View File

@ -91,7 +91,7 @@ pub enum Stmt {
IfElse(Box<Expr>, Box<Stmt>, Box<Stmt>), IfElse(Box<Expr>, Box<Stmt>, Box<Stmt>),
While(Box<Expr>, Box<Stmt>), While(Box<Expr>, Box<Stmt>),
Var(String, Option<Box<Expr>>), Var(String, Option<Box<Expr>>),
Block(Box<Vec<Stmt>>), Block(Vec<Stmt>),
Expr(Box<Expr>), Expr(Box<Expr>),
Break, Break,
Return, Return,
@ -104,11 +104,11 @@ pub enum Expr {
Identifier(String), Identifier(String),
CharConst(char), CharConst(char),
StringConst(String), StringConst(String),
FnCall(String, Box<Vec<Expr>>), FnCall(String, Vec<Expr>),
Assignment(Box<Expr>, Box<Expr>), Assignment(Box<Expr>, Box<Expr>),
Dot(Box<Expr>, Box<Expr>), Dot(Box<Expr>, Box<Expr>),
Index(String, Box<Expr>), Index(String, Box<Expr>),
Array(Box<Vec<Expr>>), Array(Vec<Expr>),
True, True,
False, False,
} }
@ -472,7 +472,7 @@ impl<'a> Iterator for TokenIterator<'a> {
} }
} }
pub fn lex<'a>(input: &'a str) -> TokenIterator<'a> { pub fn lex(input: &str) -> TokenIterator {
TokenIterator { char_stream: input.chars().peekable() } TokenIterator { char_stream: input.chars().peekable() }
} }
@ -481,16 +481,16 @@ fn get_precedence(token: &Token) -> i32 {
Token::Equals => 10, Token::Equals => 10,
Token::Or => 11, Token::Or => 11,
Token::And => 12, Token::And => 12,
Token::LessThan => 15, Token::LessThan
Token::LessThanEqual => 15, | Token::LessThanEqual
Token::GreaterThan => 15, | Token::GreaterThan
Token::GreaterThanEqual => 15, | Token::GreaterThanEqual
Token::EqualTo => 15, | Token::EqualTo
Token::NotEqualTo => 15, | Token::NotEqualTo => 15,
Token::Plus => 20, Token::Plus
Token::Minus => 20, | Token::Minus => 20,
Token::Divide => 40, Token::Divide
Token::Multiply => 40, | Token::Multiply => 40,
Token::Period => 100, Token::Period => 100,
_ => -1, _ => -1,
} }
@ -510,12 +510,9 @@ fn parse_call_expr<'a>(id: String,
-> Result<Expr, ParseError> { -> Result<Expr, ParseError> {
let mut args = Vec::new(); let mut args = Vec::new();
match input.peek() { if let Some(&Token::RParen) = input.peek() {
Some(&Token::RParen) => { input.next();
input.next(); return Ok(Expr::FnCall(id, args));
return Ok(Expr::FnCall(id, Box::new(args)));
}
_ => (),
} }
loop { loop {
@ -528,7 +525,7 @@ fn parse_call_expr<'a>(id: String,
match input.peek() { match input.peek() {
Some(&Token::RParen) => { Some(&Token::RParen) => {
input.next(); input.next();
return Ok(Expr::FnCall(id, Box::new(args))); return Ok(Expr::FnCall(id, args));
} }
Some(&Token::Comma) => (), Some(&Token::Comma) => (),
_ => return Err(ParseError::MalformedCallExpr), _ => return Err(ParseError::MalformedCallExpr),
@ -566,7 +563,7 @@ fn parse_ident_expr<'a>(id: String,
input.next(); input.next();
parse_index_expr(id, input) parse_index_expr(id, input)
} }
_ => return Ok(Expr::Identifier(id)), _ => Ok(Expr::Identifier(id)),
} }
} }
@ -581,24 +578,18 @@ fn parse_array_expr<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<Expr,
if !skip_contents { if !skip_contents {
while let Some(_) = input.peek() { while let Some(_) = input.peek() {
arr.push(try!(parse_expr(input))); arr.push(try!(parse_expr(input)));
match input.peek() { if let Some(&Token::Comma) = input.peek() {
Some(&Token::Comma) => { input.next();
input.next();
}
_ => (),
} }
match input.peek() { if let Some(&Token::RSquare) = input.peek() { break }
Some(&Token::RSquare) => break,
_ => (),
}
} }
} }
match input.peek() { match input.peek() {
Some(&Token::RSquare) => { Some(&Token::RSquare) => {
input.next(); input.next();
Ok(Expr::Array(Box::new(arr))) Ok(Expr::Array(arr))
} }
_ => Err(ParseError::MissingRSquare), _ => Err(ParseError::MissingRSquare),
} }
@ -608,9 +599,9 @@ fn parse_array_expr<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<Expr,
fn parse_primary<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<Expr, ParseError> { fn parse_primary<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<Expr, ParseError> {
if let Some(token) = input.next() { if let Some(token) = input.next() {
match token { match token {
Token::IntConst(ref x) => Ok(Expr::IntConst(x.clone())), Token::IntConst(ref x) => Ok(Expr::IntConst(*x)),
Token::StringConst(ref s) => Ok(Expr::StringConst(s.clone())), Token::StringConst(ref s) => Ok(Expr::StringConst(s.clone())),
Token::CharConst(ref c) => Ok(Expr::CharConst(c.clone())), Token::CharConst(ref c) => Ok(Expr::CharConst(*c)),
Token::Identifier(ref s) => parse_ident_expr(s.clone(), input), Token::Identifier(ref s) => parse_ident_expr(s.clone(), input),
Token::LParen => parse_paren_expr(input), Token::LParen => parse_paren_expr(input),
Token::LSquare => parse_array_expr(input), Token::LSquare => parse_array_expr(input),
@ -664,24 +655,24 @@ fn parse_binop<'a>(input: &mut Peekable<TokenIterator<'a>>,
} }
lhs_curr = match op_token { lhs_curr = match op_token {
Token::Plus => Expr::FnCall("+".to_string(), Box::new(vec![lhs_curr, rhs])), Token::Plus => Expr::FnCall("+".to_string(), vec![lhs_curr, rhs]),
Token::Minus => Expr::FnCall("-".to_string(), Box::new(vec![lhs_curr, rhs])), Token::Minus => Expr::FnCall("-".to_string(), vec![lhs_curr, rhs]),
Token::Multiply => Expr::FnCall("*".to_string(), Box::new(vec![lhs_curr, rhs])), Token::Multiply => Expr::FnCall("*".to_string(), vec![lhs_curr, rhs]),
Token::Divide => Expr::FnCall("/".to_string(), Box::new(vec![lhs_curr, rhs])), Token::Divide => Expr::FnCall("/".to_string(), vec![lhs_curr, rhs]),
Token::Equals => Expr::Assignment(Box::new(lhs_curr), Box::new(rhs)), Token::Equals => Expr::Assignment(Box::new(lhs_curr), Box::new(rhs)),
Token::Period => Expr::Dot(Box::new(lhs_curr), Box::new(rhs)), Token::Period => Expr::Dot(Box::new(lhs_curr), Box::new(rhs)),
Token::EqualTo => Expr::FnCall("==".to_string(), Box::new(vec![lhs_curr, rhs])), Token::EqualTo => Expr::FnCall("==".to_string(), vec![lhs_curr, rhs]),
Token::NotEqualTo => Expr::FnCall("!=".to_string(), Box::new(vec![lhs_curr, rhs])), Token::NotEqualTo => Expr::FnCall("!=".to_string(), vec![lhs_curr, rhs]),
Token::LessThan => Expr::FnCall("<".to_string(), Box::new(vec![lhs_curr, rhs])), Token::LessThan => Expr::FnCall("<".to_string(), vec![lhs_curr, rhs]),
Token::LessThanEqual => { Token::LessThanEqual => {
Expr::FnCall("<=".to_string(), Box::new(vec![lhs_curr, rhs])) Expr::FnCall("<=".to_string(), vec![lhs_curr, rhs])
} }
Token::GreaterThan => Expr::FnCall(">".to_string(), Box::new(vec![lhs_curr, rhs])), Token::GreaterThan => Expr::FnCall(">".to_string(), vec![lhs_curr, rhs]),
Token::GreaterThanEqual => { Token::GreaterThanEqual => {
Expr::FnCall(">=".to_string(), Box::new(vec![lhs_curr, rhs])) Expr::FnCall(">=".to_string(), vec![lhs_curr, rhs])
} }
Token::Or => Expr::FnCall("||".to_string(), Box::new(vec![lhs_curr, rhs])), Token::Or => Expr::FnCall("||".to_string(), vec![lhs_curr, rhs]),
Token::And => Expr::FnCall("&&".to_string(), Box::new(vec![lhs_curr, rhs])), Token::And => Expr::FnCall("&&".to_string(), vec![lhs_curr, rhs]),
_ => return Err(ParseError::UnknownOperator), _ => return Err(ParseError::UnknownOperator),
}; };
} }
@ -755,24 +746,19 @@ fn parse_block<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<Stmt, Pars
if !skip_body { if !skip_body {
while let Some(_) = input.peek() { while let Some(_) = input.peek() {
stmts.push(try!(parse_stmt(input))); stmts.push(try!(parse_stmt(input)));
match input.peek() {
Some(&Token::Semicolon) => { if let Some(&Token::Semicolon) = input.peek() {
input.next(); input.next();
}
_ => (),
} }
match input.peek() { if let Some(&Token::RCurly) = input.peek() { break }
Some(&Token::RCurly) => break,
_ => (),
}
} }
} }
match input.peek() { match input.peek() {
Some(&Token::RCurly) => { Some(&Token::RCurly) => {
input.next(); input.next();
Ok(Stmt::Block(Box::new(stmts))) Ok(Stmt::Block(stmts))
} }
_ => Err(ParseError::MissingRCurly), _ => Err(ParseError::MissingRCurly),
} }
@ -865,11 +851,8 @@ fn parse_top_level<'a>(input: &mut Peekable<TokenIterator<'a>>)
_ => stmts.push(try!(parse_stmt(input))), _ => stmts.push(try!(parse_stmt(input))),
} }
match input.peek() { if let Some(&Token::Semicolon) = input.peek() {
Some(&Token::Semicolon) => { input.next();
input.next();
}
_ => (),
} }
} }