minor refactor

This commit is contained in:
Lukáš Hozda 2017-12-21 12:28:59 +01:00
parent 13f7f43f98
commit 5edcfc2156
7 changed files with 39 additions and 120 deletions

View File

@ -24,12 +24,6 @@ fn main() {
engine.register_fn("update", TestStruct::update); engine.register_fn("update", TestStruct::update);
engine.register_fn("new_ts", TestStruct::new); engine.register_fn("new_ts", TestStruct::new);
println!( println!("{:?}", engine.eval::<TestStruct>("let x = new_ts(); x.update(); x"));
"{:?}", println!("{:?}", engine.eval::<TestStruct>("let x = [new_ts()]; x[0].update(); x[0]"));
engine.eval::<TestStruct>("let x = new_ts(); x.update(); x")
);
println!(
"{:?}",
engine.eval::<TestStruct>("let x = [new_ts()]; x[0].update(); x[0]")
);
} }

View File

@ -5,12 +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 !engine assert!(engine.eval_with_scope::<()>(&mut scope, "let x = 4 + 5").is_ok());
.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

@ -1,2 +1,2 @@
let x = 78 let x = 78;
print(x) print(x)

View File

@ -25,9 +25,7 @@ impl<T> Any for T
Box::new(self.clone()) Box::new(self.clone())
} }
fn _closed(&self) -> _Private { fn _closed(&self) -> _Private { _Private }
_Private
}
} }
impl Any { impl Any {

View File

@ -553,12 +553,8 @@ impl Engine {
Ok(g) => { Ok(g) => {
if *g { if *g {
match self.eval_stmt(scope, body) { match self.eval_stmt(scope, body) {
Err(EvalAltResult::LoopBreak) => { Err(EvalAltResult::LoopBreak) => return Ok(Box::new(())),
return Ok(Box::new(())); Err(x) => return Err(x),
}
Err(x) => {
return Err(x);
}
_ => (), _ => (),
} }
} else { } else {
@ -570,12 +566,8 @@ impl Engine {
}, },
Stmt::Loop(ref body) => loop { Stmt::Loop(ref body) => loop {
match self.eval_stmt(scope, body) { match self.eval_stmt(scope, body) {
Err(EvalAltResult::LoopBreak) => { Err(EvalAltResult::LoopBreak) => return Ok(Box::new(())),
return Ok(Box::new(())); Err(x) => return Err(x),
}
Err(x) => {
return Err(x);
}
_ => (), _ => (),
} }
}, },
@ -591,9 +583,7 @@ impl Engine {
let i = self.eval_expr(scope, v)?; let i = 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(())));
}
}; };
Ok(Box::new(())) Ok(Box::new(()))
} }
@ -682,9 +672,9 @@ impl Engine {
if f.read_to_string(&mut contents).is_ok() { if f.read_to_string(&mut contents).is_ok() {
if let e @ Err(_) = self.consume(&contents) { if let e @ Err(_) = self.consume(&contents) {
return e; e
} else { } else {
return Ok(()); Ok(())
} }
} else { } else {
Err(EvalAltResult::ErrorCantOpenScriptFile) Err(EvalAltResult::ErrorCantOpenScriptFile)
@ -698,11 +688,7 @@ impl Engine {
/// Useful for when you don't need the result, but still need /// Useful for when you don't need the result, but still need
/// to keep track of possible errors /// to keep track of possible errors
pub fn consume(&mut self, input: &str) -> Result<(), EvalAltResult> { pub fn consume(&mut self, input: &str) -> Result<(), EvalAltResult> {
let mut scope: Scope = Scope::new(); self.consume_with_scope(&mut Scope::new(), input)
let res = self.consume_with_scope(&mut scope, input);
res
} }
/// Evaluate a string with own scoppe, but only return errors, if there are any. /// Evaluate a string with own scoppe, but only return errors, if there are any.
@ -784,78 +770,30 @@ impl Engine {
) )
} }
fn add<T: Add>(x: T, y: T) -> <T as Add>::Output { fn add<T: Add>(x: T, y: T) -> <T as Add>::Output { x + y }
x + y fn sub<T: Sub>(x: T, y: T) -> <T as Sub>::Output { x - y }
} fn mul<T: Mul>(x: T, y: T) -> <T as Mul>::Output { x * y }
fn sub<T: Sub>(x: T, y: T) -> <T as Sub>::Output { fn div<T: Div>(x: T, y: T) -> <T as Div>::Output { x / y }
x - y fn neg<T: Neg>(x: T) -> <T as Neg>::Output { -x }
} fn lt<T: PartialOrd>(x: T, y: T) -> bool { x < y }
fn mul<T: Mul>(x: T, y: T) -> <T as Mul>::Output { fn lte<T: PartialOrd>(x: T, y: T) -> bool { x <= y }
x * y fn gt<T: PartialOrd>(x: T, y: T) -> bool { x > y }
} fn gte<T: PartialOrd>(x: T, y: T) -> bool { x >= y }
fn div<T: Div>(x: T, y: T) -> <T as Div>::Output { fn eq<T: PartialEq>(x: T, y: T) -> bool { x == y }
x / y fn ne<T: PartialEq>(x: T, y: T) -> bool { x != y }
} fn and(x: bool, y: bool) -> bool { x && y }
fn neg<T: Neg>(x: T) -> <T as Neg>::Output { fn or(x: bool, y: bool) -> bool { x || y }
-x fn not(x: bool) -> bool { !x }
} fn concat(x: String, y: String) -> String { x + &y }
fn lt<T: PartialOrd>(x: T, y: T) -> bool { fn binary_and<T: BitAnd>(x: T, y: T) -> <T as BitAnd>::Output { x & y }
x < y fn binary_or<T: BitOr>(x: T, y: T) -> <T as BitOr>::Output { x | y }
} fn binary_xor<T: BitXor>(x: T, y: T) -> <T as BitXor>::Output { x ^ y }
fn lte<T: PartialOrd>(x: T, y: T) -> bool { fn left_shift<T: Shl<T>>(x: T, y: T) -> <T as Shl<T>>::Output { x.shl(y) }
x <= y fn right_shift<T: Shr<T>>(x: T, y: T) -> <T as Shr<T>>::Output { x.shr(y) }
} fn modulo<T: Rem<T>>(x: T, y: T) -> <T as Rem<T>>::Output { x % y }
fn gt<T: PartialOrd>(x: T, y: T) -> bool { fn pow_i64_i64(x: i64, y: i64) -> i64 { x.pow(y as u32) }
x > y 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 gte<T: PartialOrd>(x: T, y: T) -> bool {
x >= y
}
fn eq<T: PartialEq>(x: T, y: T) -> bool {
x == y
}
fn ne<T: PartialEq>(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<T: BitAnd>(x: T, y: T) -> <T as BitAnd>::Output {
x & y
}
fn binary_or<T: BitOr>(x: T, y: T) -> <T as BitOr>::Output {
x | y
}
fn binary_xor<T: BitXor>(x: T, y: T) -> <T as BitXor>::Output {
x ^ y
}
fn left_shift<T: Shl<T>>(x: T, y: T) -> <T as Shl<T>>::Output {
x.shl(y)
}
fn right_shift<T: Shr<T>>(x: T, y: T) -> <T as Shr<T>>::Output {
x.shr(y)
}
fn modulo<T: Rem<T>>(x: T, y: T) -> <T as Rem<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, "+", add, i32, i64, u32, u64, f32, f64);
reg_op!(engine, "-", sub, i32, i64, u32, u64, f32, f64); reg_op!(engine, "-", sub, i32, i64, u32, u64, f32, f64);

View File

@ -16,7 +16,7 @@
//! //!
//! And the Rust part: //! And the Rust part:
//! //!
//! ```rust //! ```rust,no_run
//! use rhai::{Engine, RegisterFn}; //! use rhai::{Engine, RegisterFn};
//! //!
//! fn compute_something(x: i64) -> bool { //! fn compute_something(x: i64) -> bool {
@ -25,13 +25,7 @@
//! //!
//! let mut engine = Engine::new(); //! let mut engine = Engine::new();
//! engine.register_fn("compute_something", compute_something); //! 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::<bool>("my_script.rhai"), Ok(true)); //! assert_eq!(engine.eval_file::<bool>("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) //! [Check out the README on GitHub for more information!](https://github.com/jonathandturner/rhai)

View File

@ -1225,7 +1225,7 @@ fn parse_fn<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<FnDef, ParseE
} }
} }
let body = try!(parse_block(input)); let body = parse_block(input)?;
Ok(FnDef { Ok(FnDef {
name: name, name: name,