clippy refactoring
This commit is contained in:
parent
92465e899e
commit
a1e92d006c
@ -5,7 +5,7 @@ fn main() {
|
||||
let mut engine = Engine::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") {
|
||||
println!("result: {}", result);
|
||||
|
246
src/engine.rs
246
src/engine.rs
@ -109,7 +109,7 @@ impl Engine {
|
||||
-> Result<Box<Any>, EvalAltResult> {
|
||||
|
||||
match self.fns.get(name) {
|
||||
Some(ref vf) => {
|
||||
Some(vf) => {
|
||||
match (arg1, arg2, arg3, arg4, arg5, arg6) {
|
||||
(Some(ref mut a1),
|
||||
Some(ref mut a2),
|
||||
@ -117,15 +117,14 @@ impl Engine {
|
||||
Some(ref mut a4),
|
||||
Some(ref mut a5),
|
||||
Some(ref mut a6)) => {
|
||||
for arr_f in *vf {
|
||||
match arr_f {
|
||||
&FnType::ExternalFn6(ref f) => {
|
||||
match f(*a1, *a2, *a3, *a4, *a5, *a6) {
|
||||
Ok(v) => return Ok(v),
|
||||
_ => (),
|
||||
for arr_f in vf {
|
||||
match *arr_f {
|
||||
FnType::ExternalFn6(ref f) => {
|
||||
if let Ok(v) = f(*a1, *a2, *a3, *a4, *a5, *a6) {
|
||||
return Ok(v);
|
||||
}
|
||||
}
|
||||
&FnType::InternalFn(ref f) => {
|
||||
FnType::InternalFn(ref f) => {
|
||||
if f.params.len() != 6 {
|
||||
return Err(EvalAltResult::ErrorFunctionArgMismatch);
|
||||
}
|
||||
@ -193,7 +192,7 @@ impl Engine {
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
return Err(EvalAltResult::ErrorFunctionArgMismatch);
|
||||
Err(EvalAltResult::ErrorFunctionArgMismatch)
|
||||
}
|
||||
(Some(ref mut a1),
|
||||
Some(ref mut a2),
|
||||
@ -201,15 +200,14 @@ impl Engine {
|
||||
Some(ref mut a4),
|
||||
Some(ref mut a5),
|
||||
None) => {
|
||||
for arr_f in *vf {
|
||||
match arr_f {
|
||||
&FnType::ExternalFn5(ref f) => {
|
||||
match f(*a1, *a2, *a3, *a4, *a5) {
|
||||
Ok(v) => return Ok(v),
|
||||
_ => (),
|
||||
for arr_f in vf {
|
||||
match *arr_f {
|
||||
FnType::ExternalFn5(ref f) => {
|
||||
if let Ok(v) = f(*a1, *a2, *a3, *a4, *a5) {
|
||||
return Ok(v);
|
||||
}
|
||||
}
|
||||
&FnType::InternalFn(ref f) => {
|
||||
FnType::InternalFn(ref f) => {
|
||||
if f.params.len() != 5 {
|
||||
return Err(EvalAltResult::ErrorFunctionArgMismatch);
|
||||
}
|
||||
@ -269,7 +267,7 @@ impl Engine {
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
return Err(EvalAltResult::ErrorFunctionArgMismatch);
|
||||
Err(EvalAltResult::ErrorFunctionArgMismatch)
|
||||
}
|
||||
(Some(ref mut a1),
|
||||
Some(ref mut a2),
|
||||
@ -277,15 +275,14 @@ impl Engine {
|
||||
Some(ref mut a4),
|
||||
None,
|
||||
None) => {
|
||||
for arr_f in *vf {
|
||||
match arr_f {
|
||||
&FnType::ExternalFn4(ref f) => {
|
||||
match f(*a1, *a2, *a3, *a4) {
|
||||
Ok(v) => return Ok(v),
|
||||
_ => (),
|
||||
for arr_f in vf {
|
||||
match *arr_f {
|
||||
FnType::ExternalFn4(ref f) => {
|
||||
if let Ok(v) = f(*a1, *a2, *a3, *a4) {
|
||||
return Ok(v)
|
||||
}
|
||||
}
|
||||
&FnType::InternalFn(ref f) => {
|
||||
FnType::InternalFn(ref f) => {
|
||||
if f.params.len() != 4 {
|
||||
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) => {
|
||||
for arr_f in *vf {
|
||||
match arr_f {
|
||||
&FnType::ExternalFn3(ref f) => {
|
||||
match f(*a1, *a2, *a3) {
|
||||
Ok(v) => return Ok(v),
|
||||
_ => (),
|
||||
for arr_f in vf {
|
||||
match *arr_f {
|
||||
FnType::ExternalFn3(ref f) => {
|
||||
if let Ok(v) = f(*a1, *a2, *a3) {
|
||||
return Ok(v);
|
||||
}
|
||||
}
|
||||
&FnType::InternalFn(ref f) => {
|
||||
FnType::InternalFn(ref f) => {
|
||||
if f.params.len() != 3 {
|
||||
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) => {
|
||||
for arr_f in *vf {
|
||||
match arr_f {
|
||||
&FnType::ExternalFn2(ref f) => {
|
||||
match f(*a1, *a2) {
|
||||
Ok(v) => return Ok(v),
|
||||
_ => (),
|
||||
for arr_f in vf {
|
||||
match *arr_f {
|
||||
FnType::ExternalFn2(ref f) => {
|
||||
if let Ok(v) = f(*a1, *a2) {
|
||||
return Ok(v);
|
||||
}
|
||||
}
|
||||
&FnType::InternalFn(ref f) => {
|
||||
FnType::InternalFn(ref f) => {
|
||||
if f.params.len() != 2 {
|
||||
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) => {
|
||||
for arr_f in *vf {
|
||||
match arr_f {
|
||||
&FnType::ExternalFn1(ref f) => {
|
||||
match f(*a1) {
|
||||
Ok(v) => return Ok(v),
|
||||
_ => (),
|
||||
for arr_f in vf {
|
||||
match *arr_f {
|
||||
FnType::ExternalFn1(ref f) => {
|
||||
if let Ok(v) = f(*a1) {
|
||||
return Ok(v);
|
||||
}
|
||||
}
|
||||
&FnType::InternalFn(ref f) => {
|
||||
FnType::InternalFn(ref f) => {
|
||||
if f.params.len() != 1 {
|
||||
return Err(EvalAltResult::ErrorFunctionArgMismatch);
|
||||
}
|
||||
@ -474,19 +468,18 @@ impl Engine {
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
return Err(EvalAltResult::ErrorFunctionArgMismatch);
|
||||
Err(EvalAltResult::ErrorFunctionArgMismatch)
|
||||
}
|
||||
_ => {
|
||||
for arr_f in *vf {
|
||||
match arr_f {
|
||||
&FnType::ExternalFn0(ref f) => {
|
||||
match f() {
|
||||
Ok(v) => return Ok(v),
|
||||
_ => (),
|
||||
for arr_f in vf {
|
||||
match *arr_f {
|
||||
FnType::ExternalFn0(ref f) => {
|
||||
if let Ok(v) = f() {
|
||||
return Ok(v);
|
||||
}
|
||||
}
|
||||
&FnType::InternalFn(ref f) => {
|
||||
if f.params.len() != 0 {
|
||||
FnType::InternalFn(ref f) => {
|
||||
if !f.params.is_empty() {
|
||||
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> {
|
||||
match *dot_rhs {
|
||||
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);
|
||||
if args.is_empty() {
|
||||
self.call_fn(fn_name, Some(this_ptr), None, None, None, None, None)
|
||||
} else if args.len() == 1 {
|
||||
let mut arg = try!(self.eval_expr(scope, &args[0]));
|
||||
|
||||
return self.call_fn(&fn_name,
|
||||
Some(this_ptr),
|
||||
Some(&mut arg),
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None);
|
||||
self.call_fn(fn_name,
|
||||
Some(this_ptr),
|
||||
Some(&mut arg),
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None)
|
||||
} else if args.len() == 2 {
|
||||
let mut arg1 = try!(self.eval_expr(scope, &args[0]));
|
||||
let mut arg2 = try!(self.eval_expr(scope, &args[1]));
|
||||
|
||||
return self.call_fn(&fn_name,
|
||||
Some(this_ptr),
|
||||
Some(&mut arg1),
|
||||
Some(&mut arg2),
|
||||
None,
|
||||
None,
|
||||
None);
|
||||
self.call_fn(fn_name,
|
||||
Some(this_ptr),
|
||||
Some(&mut arg1),
|
||||
Some(&mut arg2),
|
||||
None,
|
||||
None,
|
||||
None)
|
||||
} else if args.len() == 3 {
|
||||
let mut arg1 = try!(self.eval_expr(scope, &args[0]));
|
||||
let mut arg2 = try!(self.eval_expr(scope, &args[1]));
|
||||
let mut arg3 = try!(self.eval_expr(scope, &args[2]));
|
||||
|
||||
return self.call_fn(&fn_name,
|
||||
Some(this_ptr),
|
||||
Some(&mut arg1),
|
||||
Some(&mut arg2),
|
||||
Some(&mut arg3),
|
||||
None,
|
||||
None);
|
||||
self.call_fn(fn_name,
|
||||
Some(this_ptr),
|
||||
Some(&mut arg1),
|
||||
Some(&mut arg2),
|
||||
Some(&mut arg3),
|
||||
None,
|
||||
None)
|
||||
} else if args.len() == 4 {
|
||||
let mut arg1 = try!(self.eval_expr(scope, &args[0]));
|
||||
let mut arg2 = try!(self.eval_expr(scope, &args[1]));
|
||||
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),
|
||||
Some(&mut arg4),
|
||||
None);
|
||||
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 {
|
||||
let mut arg1 = try!(self.eval_expr(scope, &args[0]));
|
||||
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 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),
|
||||
Some(&mut arg4),
|
||||
Some(&mut arg5));
|
||||
self.call_fn(fn_name,
|
||||
Some(this_ptr),
|
||||
Some(&mut arg1),
|
||||
Some(&mut arg2),
|
||||
Some(&mut arg3),
|
||||
Some(&mut arg4),
|
||||
Some(&mut arg5))
|
||||
} else {
|
||||
Err(EvalAltResult::ErrorFunctionCallNotSupported)
|
||||
}
|
||||
}
|
||||
Expr::Identifier(ref 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) => {
|
||||
let idx = try!(self.eval_expr(scope, idx_raw));
|
||||
@ -665,8 +658,8 @@ impl Engine {
|
||||
None);
|
||||
|
||||
match result {
|
||||
Ok(mut v) => return self.get_dot_val_helper(scope, &mut v, inner_rhs),
|
||||
e => return e,
|
||||
Ok(mut v) => self.get_dot_val_helper(scope, &mut v, inner_rhs),
|
||||
e => e,
|
||||
}
|
||||
}
|
||||
_ => Err(EvalAltResult::InternalErrorMalformedDotExpression),
|
||||
@ -710,7 +703,7 @@ impl Engine {
|
||||
return result;
|
||||
}
|
||||
|
||||
return Err(EvalAltResult::ErrorVariableNotFound(id.clone()));
|
||||
Err(EvalAltResult::ErrorVariableNotFound(id.clone()))
|
||||
}
|
||||
Expr::Index(ref id, ref idx_raw) => {
|
||||
let idx_boxed = try!(self.eval_expr(scope, idx_raw));
|
||||
@ -861,7 +854,7 @@ impl Engine {
|
||||
return result;
|
||||
}
|
||||
|
||||
return Err(EvalAltResult::ErrorAssignmentToUnknownLHS);
|
||||
Err(EvalAltResult::ErrorAssignmentToUnknownLHS)
|
||||
}
|
||||
Expr::Index(ref id, ref idx_raw) => {
|
||||
let idx_boxed = try!(self.eval_expr(scope, idx_raw));
|
||||
@ -921,7 +914,7 @@ impl Engine {
|
||||
match *expr {
|
||||
Expr::IntConst(i) => Ok(Box::new(i)),
|
||||
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) => {
|
||||
for &mut (ref name, ref mut val) in &mut scope.iter_mut().rev() {
|
||||
if *id == *name {
|
||||
@ -1002,7 +995,7 @@ impl Engine {
|
||||
Expr::Array(ref contents) => {
|
||||
let mut arr = Vec::new();
|
||||
|
||||
for item in (*contents).iter() {
|
||||
for item in &(*contents) {
|
||||
let arg = try!(self.eval_expr(scope, item));
|
||||
arr.push(arg);
|
||||
}
|
||||
@ -1010,17 +1003,17 @@ impl Engine {
|
||||
Ok(Box::new(arr))
|
||||
}
|
||||
Expr::FnCall(ref fn_name, ref args) => {
|
||||
if args.len() == 0 {
|
||||
self.call_fn(&fn_name, None, None, None, None, None, None)
|
||||
if args.is_empty() {
|
||||
self.call_fn(fn_name, None, None, None, None, None, None)
|
||||
} else if args.len() == 1 {
|
||||
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 {
|
||||
let mut arg1 = try!(self.eval_expr(scope, &args[0]));
|
||||
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 arg2),
|
||||
None,
|
||||
@ -1032,7 +1025,7 @@ impl Engine {
|
||||
let mut arg2 = try!(self.eval_expr(scope, &args[1]));
|
||||
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 arg2),
|
||||
Some(&mut arg3),
|
||||
@ -1045,7 +1038,7 @@ impl Engine {
|
||||
let mut arg3 = try!(self.eval_expr(scope, &args[2]));
|
||||
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 arg2),
|
||||
Some(&mut arg3),
|
||||
@ -1059,7 +1052,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,
|
||||
self.call_fn(fn_name,
|
||||
Some(&mut arg1),
|
||||
Some(&mut arg2),
|
||||
Some(&mut arg3),
|
||||
@ -1074,7 +1067,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,
|
||||
self.call_fn(fn_name,
|
||||
Some(&mut arg1),
|
||||
Some(&mut arg2),
|
||||
Some(&mut arg3),
|
||||
@ -1099,12 +1092,9 @@ impl Engine {
|
||||
|
||||
for s in b.iter() {
|
||||
last_result = self.eval_stmt(scope, s);
|
||||
match last_result {
|
||||
Err(x) => {
|
||||
last_result = Err(x);
|
||||
break;
|
||||
}
|
||||
_ => (),
|
||||
if let Err(x) = last_result {
|
||||
last_result = Err(x);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1112,7 +1102,7 @@ impl Engine {
|
||||
scope.pop();
|
||||
}
|
||||
|
||||
return last_result;
|
||||
last_result
|
||||
}
|
||||
Stmt::If(ref guard, ref body) => {
|
||||
let guard_result = try!(self.eval_expr(scope, guard));
|
||||
@ -1163,19 +1153,19 @@ impl Engine {
|
||||
}
|
||||
}
|
||||
}
|
||||
Stmt::Break => return Err(EvalAltResult::LoopBreak),
|
||||
Stmt::Return => return Err(EvalAltResult::Return(Box::new(()))),
|
||||
Stmt::Break => Err(EvalAltResult::LoopBreak),
|
||||
Stmt::Return => Err(EvalAltResult::Return(Box::new(()))),
|
||||
Stmt::ReturnWithVal(ref a) => {
|
||||
let result = try!(self.eval_expr(scope, a));
|
||||
return Err(EvalAltResult::Return(result));
|
||||
Err(EvalAltResult::Return(result))
|
||||
}
|
||||
Stmt::Var(ref name, ref init) => {
|
||||
match init {
|
||||
&Some(ref v) => {
|
||||
match *init {
|
||||
Some(ref v) => {
|
||||
let i = try!(self.eval_expr(scope, v));
|
||||
scope.push((name.clone(), i));
|
||||
}
|
||||
&None => {
|
||||
None => {
|
||||
scope.push((name.clone(), Box::new(())));
|
||||
}
|
||||
};
|
||||
@ -1188,10 +1178,10 @@ impl Engine {
|
||||
use std::fs::File;
|
||||
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();
|
||||
|
||||
if let Ok(_) = f.read_to_string(&mut contents) {
|
||||
if f.read_to_string(&mut contents).is_ok() {
|
||||
self.eval::<T>(&contents)
|
||||
} else {
|
||||
Err(EvalAltResult::ErrorCantOpenScriptFile)
|
||||
@ -1226,12 +1216,12 @@ impl Engine {
|
||||
}
|
||||
let name = f.name.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));
|
||||
}
|
||||
|
||||
for o in os {
|
||||
x = match self.eval_stmt(scope, &o) {
|
||||
x = match self.eval_stmt(scope, o) {
|
||||
Ok(v) => Ok(v),
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
@ -1337,7 +1327,7 @@ impl Engine {
|
||||
|
||||
// engine.register_fn("[]", idx);
|
||||
// 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)));
|
||||
|
||||
}
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
@ -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));
|
||||
}
|
||||
}
|
||||
@ -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));
|
||||
}
|
||||
}
|
||||
@ -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));
|
||||
}
|
||||
}
|
||||
@ -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));
|
||||
}
|
||||
}
|
||||
@ -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));
|
||||
}
|
||||
}
|
||||
@ -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));
|
||||
}
|
||||
}
|
||||
@ -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));
|
||||
}
|
||||
}
|
||||
@ -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));
|
||||
}
|
||||
}
|
||||
@ -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));
|
||||
}
|
||||
}
|
||||
@ -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));
|
||||
}
|
||||
}
|
||||
@ -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));
|
||||
}
|
||||
}
|
||||
@ -412,7 +412,7 @@ impl<A, T> FnRegister<A, T, ()> for Engine
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,12 @@
|
||||
//! 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 fn_register;
|
||||
mod parser;
|
||||
|
105
src/parser.rs
105
src/parser.rs
@ -91,7 +91,7 @@ pub enum Stmt {
|
||||
IfElse(Box<Expr>, Box<Stmt>, Box<Stmt>),
|
||||
While(Box<Expr>, Box<Stmt>),
|
||||
Var(String, Option<Box<Expr>>),
|
||||
Block(Box<Vec<Stmt>>),
|
||||
Block(Vec<Stmt>),
|
||||
Expr(Box<Expr>),
|
||||
Break,
|
||||
Return,
|
||||
@ -104,11 +104,11 @@ pub enum Expr {
|
||||
Identifier(String),
|
||||
CharConst(char),
|
||||
StringConst(String),
|
||||
FnCall(String, Box<Vec<Expr>>),
|
||||
FnCall(String, Vec<Expr>),
|
||||
Assignment(Box<Expr>, Box<Expr>),
|
||||
Dot(Box<Expr>, Box<Expr>),
|
||||
Index(String, Box<Expr>),
|
||||
Array(Box<Vec<Expr>>),
|
||||
Array(Vec<Expr>),
|
||||
True,
|
||||
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() }
|
||||
}
|
||||
|
||||
@ -481,16 +481,16 @@ fn get_precedence(token: &Token) -> i32 {
|
||||
Token::Equals => 10,
|
||||
Token::Or => 11,
|
||||
Token::And => 12,
|
||||
Token::LessThan => 15,
|
||||
Token::LessThanEqual => 15,
|
||||
Token::GreaterThan => 15,
|
||||
Token::GreaterThanEqual => 15,
|
||||
Token::EqualTo => 15,
|
||||
Token::NotEqualTo => 15,
|
||||
Token::Plus => 20,
|
||||
Token::Minus => 20,
|
||||
Token::Divide => 40,
|
||||
Token::Multiply => 40,
|
||||
Token::LessThan
|
||||
| Token::LessThanEqual
|
||||
| Token::GreaterThan
|
||||
| Token::GreaterThanEqual
|
||||
| Token::EqualTo
|
||||
| Token::NotEqualTo => 15,
|
||||
Token::Plus
|
||||
| Token::Minus => 20,
|
||||
Token::Divide
|
||||
| Token::Multiply => 40,
|
||||
Token::Period => 100,
|
||||
_ => -1,
|
||||
}
|
||||
@ -510,12 +510,9 @@ fn parse_call_expr<'a>(id: String,
|
||||
-> Result<Expr, ParseError> {
|
||||
let mut args = Vec::new();
|
||||
|
||||
match input.peek() {
|
||||
Some(&Token::RParen) => {
|
||||
input.next();
|
||||
return Ok(Expr::FnCall(id, Box::new(args)));
|
||||
}
|
||||
_ => (),
|
||||
if let Some(&Token::RParen) = input.peek() {
|
||||
input.next();
|
||||
return Ok(Expr::FnCall(id, args));
|
||||
}
|
||||
|
||||
loop {
|
||||
@ -528,7 +525,7 @@ fn parse_call_expr<'a>(id: String,
|
||||
match input.peek() {
|
||||
Some(&Token::RParen) => {
|
||||
input.next();
|
||||
return Ok(Expr::FnCall(id, Box::new(args)));
|
||||
return Ok(Expr::FnCall(id, args));
|
||||
}
|
||||
Some(&Token::Comma) => (),
|
||||
_ => return Err(ParseError::MalformedCallExpr),
|
||||
@ -566,7 +563,7 @@ fn parse_ident_expr<'a>(id: String,
|
||||
input.next();
|
||||
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 {
|
||||
while let Some(_) = input.peek() {
|
||||
arr.push(try!(parse_expr(input)));
|
||||
match input.peek() {
|
||||
Some(&Token::Comma) => {
|
||||
input.next();
|
||||
}
|
||||
_ => (),
|
||||
if let Some(&Token::Comma) = input.peek() {
|
||||
input.next();
|
||||
}
|
||||
|
||||
match input.peek() {
|
||||
Some(&Token::RSquare) => break,
|
||||
_ => (),
|
||||
}
|
||||
if let Some(&Token::RSquare) = input.peek() { break }
|
||||
}
|
||||
}
|
||||
|
||||
match input.peek() {
|
||||
Some(&Token::RSquare) => {
|
||||
input.next();
|
||||
Ok(Expr::Array(Box::new(arr)))
|
||||
Ok(Expr::Array(arr))
|
||||
}
|
||||
_ => 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> {
|
||||
if let Some(token) = input.next() {
|
||||
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::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::LParen => parse_paren_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 {
|
||||
Token::Plus => Expr::FnCall("+".to_string(), Box::new(vec![lhs_curr, rhs])),
|
||||
Token::Minus => Expr::FnCall("-".to_string(), Box::new(vec![lhs_curr, rhs])),
|
||||
Token::Multiply => Expr::FnCall("*".to_string(), Box::new(vec![lhs_curr, rhs])),
|
||||
Token::Divide => 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(), vec![lhs_curr, rhs]),
|
||||
Token::Multiply => Expr::FnCall("*".to_string(), 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::Period => Expr::Dot(Box::new(lhs_curr), Box::new(rhs)),
|
||||
Token::EqualTo => Expr::FnCall("==".to_string(), Box::new(vec![lhs_curr, rhs])),
|
||||
Token::NotEqualTo => Expr::FnCall("!=".to_string(), Box::new(vec![lhs_curr, rhs])),
|
||||
Token::LessThan => 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(), vec![lhs_curr, rhs]),
|
||||
Token::LessThan => Expr::FnCall("<".to_string(), vec![lhs_curr, rhs]),
|
||||
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 => {
|
||||
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::And => 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(), vec![lhs_curr, rhs]),
|
||||
_ => return Err(ParseError::UnknownOperator),
|
||||
};
|
||||
}
|
||||
@ -755,24 +746,19 @@ fn parse_block<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<Stmt, Pars
|
||||
if !skip_body {
|
||||
while let Some(_) = input.peek() {
|
||||
stmts.push(try!(parse_stmt(input)));
|
||||
match input.peek() {
|
||||
Some(&Token::Semicolon) => {
|
||||
input.next();
|
||||
}
|
||||
_ => (),
|
||||
|
||||
if let Some(&Token::Semicolon) = input.peek() {
|
||||
input.next();
|
||||
}
|
||||
|
||||
match input.peek() {
|
||||
Some(&Token::RCurly) => break,
|
||||
_ => (),
|
||||
}
|
||||
if let Some(&Token::RCurly) = input.peek() { break }
|
||||
}
|
||||
}
|
||||
|
||||
match input.peek() {
|
||||
Some(&Token::RCurly) => {
|
||||
input.next();
|
||||
Ok(Stmt::Block(Box::new(stmts)))
|
||||
Ok(Stmt::Block(stmts))
|
||||
}
|
||||
_ => Err(ParseError::MissingRCurly),
|
||||
}
|
||||
@ -865,11 +851,8 @@ fn parse_top_level<'a>(input: &mut Peekable<TokenIterator<'a>>)
|
||||
_ => stmts.push(try!(parse_stmt(input))),
|
||||
}
|
||||
|
||||
match input.peek() {
|
||||
Some(&Token::Semicolon) => {
|
||||
input.next();
|
||||
}
|
||||
_ => (),
|
||||
if let Some(&Token::Semicolon) = input.peek() {
|
||||
input.next();
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user