From 5edcfc21560009a3a0c445b723511091980d1935 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Hozda?= Date: Thu, 21 Dec 2017 12:28:59 +0100 Subject: [PATCH] minor refactor --- examples/arrays_and_structs.rs | 10 +-- examples/reuse_scope.rs | 7 +- scripts/assignment.rhai | 2 +- src/any.rs | 4 +- src/engine.rs | 126 +++++++++------------------------ src/lib.rs | 8 +-- src/parser.rs | 2 +- 7 files changed, 39 insertions(+), 120 deletions(-) diff --git a/examples/arrays_and_structs.rs b/examples/arrays_and_structs.rs index af7246fb..987e177f 100644 --- a/examples/arrays_and_structs.rs +++ b/examples/arrays_and_structs.rs @@ -24,12 +24,6 @@ fn main() { engine.register_fn("update", TestStruct::update); engine.register_fn("new_ts", TestStruct::new); - println!( - "{:?}", - engine.eval::("let x = new_ts(); x.update(); x") - ); - println!( - "{:?}", - engine.eval::("let x = [new_ts()]; x[0].update(); x[0]") - ); + println!("{:?}", engine.eval::("let x = new_ts(); x.update(); x")); + println!("{:?}", engine.eval::("let x = [new_ts()]; x[0].update(); x[0]")); } diff --git a/examples/reuse_scope.rs b/examples/reuse_scope.rs index c7a8726b..b01ec107 100644 --- a/examples/reuse_scope.rs +++ b/examples/reuse_scope.rs @@ -5,12 +5,7 @@ fn main() { let mut engine = Engine::new(); let mut scope: Scope = Vec::new(); - if !engine - .eval_with_scope::<()>(&mut scope, "let x = 4 + 5") - .is_ok() - { - assert!(false); - } + assert!(engine.eval_with_scope::<()>(&mut scope, "let x = 4 + 5").is_ok()); if let Ok(result) = engine.eval_with_scope::(&mut scope, "x") { println!("result: {}", result); diff --git a/scripts/assignment.rhai b/scripts/assignment.rhai index ea76f5a6..8e8d6047 100644 --- a/scripts/assignment.rhai +++ b/scripts/assignment.rhai @@ -1,2 +1,2 @@ -let x = 78 +let x = 78; print(x) diff --git a/src/any.rs b/src/any.rs index 114dad9b..df508a8f 100644 --- a/src/any.rs +++ b/src/any.rs @@ -25,9 +25,7 @@ impl Any for T Box::new(self.clone()) } - fn _closed(&self) -> _Private { - _Private - } + fn _closed(&self) -> _Private { _Private } } impl Any { diff --git a/src/engine.rs b/src/engine.rs index 88563a45..39f0a124 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -553,12 +553,8 @@ impl Engine { Ok(g) => { if *g { match self.eval_stmt(scope, body) { - Err(EvalAltResult::LoopBreak) => { - return Ok(Box::new(())); - } - Err(x) => { - return Err(x); - } + Err(EvalAltResult::LoopBreak) => return Ok(Box::new(())), + Err(x) => return Err(x), _ => (), } } else { @@ -570,12 +566,8 @@ impl Engine { }, Stmt::Loop(ref body) => loop { match self.eval_stmt(scope, body) { - Err(EvalAltResult::LoopBreak) => { - return Ok(Box::new(())); - } - Err(x) => { - return Err(x); - } + Err(EvalAltResult::LoopBreak) => return Ok(Box::new(())), + Err(x) => return Err(x), _ => (), } }, @@ -591,9 +583,7 @@ impl Engine { let i = self.eval_expr(scope, v)?; scope.push((name.clone(), i)); } - None => { - scope.push((name.clone(), Box::new(()))); - } + None => scope.push((name.clone(), Box::new(()))), }; Ok(Box::new(())) } @@ -682,9 +672,9 @@ impl Engine { if f.read_to_string(&mut contents).is_ok() { if let e @ Err(_) = self.consume(&contents) { - return e; + e } else { - return Ok(()); + Ok(()) } } else { Err(EvalAltResult::ErrorCantOpenScriptFile) @@ -698,11 +688,7 @@ impl Engine { /// Useful for when you don't need the result, but still need /// to keep track of possible errors pub fn consume(&mut self, input: &str) -> Result<(), EvalAltResult> { - let mut scope: Scope = Scope::new(); - - let res = self.consume_with_scope(&mut scope, input); - - res + self.consume_with_scope(&mut Scope::new(), input) } /// Evaluate a string with own scoppe, but only return errors, if there are any. @@ -784,78 +770,30 @@ impl Engine { ) } - fn add(x: T, y: T) -> ::Output { - x + y - } - fn sub(x: T, y: T) -> ::Output { - x - y - } - fn mul(x: T, y: T) -> ::Output { - x * y - } - fn div(x: T, y: T) -> ::Output { - x / y - } - fn neg(x: T) -> ::Output { - -x - } - fn lt(x: T, y: T) -> bool { - x < y - } - fn lte(x: T, y: T) -> bool { - x <= y - } - fn gt(x: T, y: T) -> bool { - x > y - } - fn gte(x: T, y: T) -> bool { - x >= y - } - fn eq(x: T, y: T) -> bool { - x == y - } - fn ne(x: T, y: T) -> bool { - x != y - } - fn and(x: bool, y: bool) -> bool { - x && y - } - fn or(x: bool, y: bool) -> bool { - x || y - } - fn not(x: bool) -> bool { - !x - } - fn concat(x: String, y: String) -> String { - x + &y - } - fn binary_and(x: T, y: T) -> ::Output { - x & y - } - fn binary_or(x: T, y: T) -> ::Output { - x | y - } - fn binary_xor(x: T, y: T) -> ::Output { - x ^ y - } - fn left_shift>(x: T, y: T) -> >::Output { - x.shl(y) - } - fn right_shift>(x: T, y: T) -> >::Output { - x.shr(y) - } - fn modulo>(x: T, y: T) -> >::Output { - x % y - } - fn pow_i64_i64(x: i64, y: i64) -> i64 { - x.pow(y as u32) - } - fn pow_f64_f64(x: f64, y: f64) -> f64 { - x.powf(y) - } - fn pow_f64_i64(x: f64, y: i64) -> f64 { - x.powi(y as i32) - } + fn add(x: T, y: T) -> ::Output { x + y } + fn sub(x: T, y: T) -> ::Output { x - y } + fn mul(x: T, y: T) -> ::Output { x * y } + fn div(x: T, y: T) -> ::Output { x / y } + fn neg(x: T) -> ::Output { -x } + fn lt(x: T, y: T) -> bool { x < y } + fn lte(x: T, y: T) -> bool { x <= y } + fn gt(x: T, y: T) -> bool { x > y } + fn gte(x: T, y: T) -> bool { x >= y } + fn eq(x: T, y: T) -> bool { x == y } + fn ne(x: T, y: T) -> bool { x != y } + fn and(x: bool, y: bool) -> bool { x && y } + fn or(x: bool, y: bool) -> bool { x || y } + fn not(x: bool) -> bool { !x } + fn concat(x: String, y: String) -> String { x + &y } + fn binary_and(x: T, y: T) -> ::Output { x & y } + fn binary_or(x: T, y: T) -> ::Output { x | y } + fn binary_xor(x: T, y: T) -> ::Output { x ^ y } + fn left_shift>(x: T, y: T) -> >::Output { x.shl(y) } + fn right_shift>(x: T, y: T) -> >::Output { x.shr(y) } + fn modulo>(x: T, y: T) -> >::Output { x % y } + fn pow_i64_i64(x: i64, y: i64) -> i64 { x.pow(y as u32) } + fn pow_f64_f64(x: f64, y: f64) -> f64 { x.powf(y) } + fn pow_f64_i64(x: f64, y: i64) -> f64 { x.powi(y as i32) } reg_op!(engine, "+", add, i32, i64, u32, u64, f32, f64); reg_op!(engine, "-", sub, i32, i64, u32, u64, f32, f64); diff --git a/src/lib.rs b/src/lib.rs index 0ffb0a62..f03ff9d5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,7 +16,7 @@ //! //! And the Rust part: //! -//! ```rust +//! ```rust,no_run //! use rhai::{Engine, RegisterFn}; //! //! fn compute_something(x: i64) -> bool { @@ -25,13 +25,7 @@ //! //! let mut engine = Engine::new(); //! engine.register_fn("compute_something", compute_something); -//! # // Very ugly hack incoming, TODO (maybe mark as no_run?) -//! # use std::fs::{File, remove_file}; -//! # use std::io::Write; -//! # let mut f = File::create("my_script.rhai").unwrap(); -//! # let _ = write!(f, "{}", "fn f(x) { if x == 1 { return 1; } x * f(x-1) } compute_something(f(10))"); //! assert_eq!(engine.eval_file::("my_script.rhai"), Ok(true)); -//! # let _ = remove_file("my_script.rhai"); //! ``` //! //! [Check out the README on GitHub for more information!](https://github.com/jonathandturner/rhai) diff --git a/src/parser.rs b/src/parser.rs index f3bcd15c..837f7395 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1225,7 +1225,7 @@ fn parse_fn<'a>(input: &mut Peekable>) -> Result