Merge pull request #97 from schungx/master

Handle arrays properly, plus other enhancements.
This commit is contained in:
Jonathan Turner 2020-02-25 05:18:56 +13:00 committed by GitHub
commit 2a663d5c07
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 1007 additions and 480 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
target/ target/
Cargo.lock Cargo.lock
.vscode/

15
scripts/for1.rhai Normal file
View File

@ -0,0 +1,15 @@
let arr = [1,2,3,4]
for a in arr {
for b in [10,20] {
print(a)
print(b)
}
if a == 3 {
break;
}
}
//print(a)
for i in range(0,5) {
print(i)
}

View File

@ -76,7 +76,7 @@ impl Clone for Box<dyn Any> {
impl fmt::Debug for dyn Any { impl fmt::Debug for dyn Any {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.pad("Any") f.pad("?")
} }
} }

View File

@ -4,36 +4,44 @@ use std::collections::HashMap;
use std::error::Error; use std::error::Error;
use std::fmt; use std::fmt;
use std::ops::{Add, BitAnd, BitOr, BitXor, Div, Mul, Neg, Rem, Shl, Shr, Sub}; use std::ops::{Add, BitAnd, BitOr, BitXor, Div, Mul, Neg, Rem, Shl, Shr, Sub};
use std::sync::Arc; use std::{convert::TryInto, sync::Arc};
use crate::any::{Any, AnyExt}; use crate::any::{Any, AnyExt};
use crate::call::FunArgs; use crate::call::FunArgs;
use crate::fn_register::RegisterFn; use crate::fn_register::{RegisterBoxFn, RegisterFn};
use crate::parser::{lex, parse, Expr, FnDef, Stmt}; use crate::parser::{lex, parse, Expr, FnDef, ParseError, Stmt, AST};
use fmt::{Debug, Display};
#[derive(Debug)] type Array = Vec<Box<dyn Any>>;
#[derive(Debug, Clone)]
pub enum EvalAltResult { pub enum EvalAltResult {
ErrorParseError(ParseError),
ErrorFunctionNotFound(String), ErrorFunctionNotFound(String),
ErrorFunctionArgMismatch, ErrorFunctionArgMismatch,
ErrorArrayOutOfBounds(usize, i64),
ErrorArrayMismatch,
ErrorIndexMismatch, ErrorIndexMismatch,
ErrorIfGuardMismatch, ErrorIfGuardMismatch,
ErrorForMismatch,
ErrorVariableNotFound(String), ErrorVariableNotFound(String),
ErrorAssignmentToUnknownLHS, ErrorAssignmentToUnknownLHS,
ErrorMismatchOutputType(String), ErrorMismatchOutputType(String),
ErrorCantOpenScriptFile, ErrorCantOpenScriptFile(String),
InternalErrorMalformedDotExpression, ErrorMalformedDotExpression,
LoopBreak, LoopBreak,
Return(Box<dyn Any>), Return(Box<dyn Any>),
} }
impl EvalAltResult { impl EvalAltResult {
fn as_str(&self) -> Option<&str> { fn as_str(&self) -> Option<&str> {
match *self { Some(match *self {
EvalAltResult::ErrorVariableNotFound(ref s) => Some(s.as_str()), EvalAltResult::ErrorCantOpenScriptFile(ref s)
EvalAltResult::ErrorFunctionNotFound(ref s) => Some(s.as_str()), | EvalAltResult::ErrorVariableNotFound(ref s)
EvalAltResult::ErrorMismatchOutputType(ref s) => Some(s.as_str()), | EvalAltResult::ErrorFunctionNotFound(ref s)
_ => None, | EvalAltResult::ErrorMismatchOutputType(ref s) => s,
} _ => return None,
})
} }
} }
@ -42,15 +50,21 @@ impl PartialEq for EvalAltResult {
use EvalAltResult::*; use EvalAltResult::*;
match (self, other) { match (self, other) {
(&ErrorParseError(ref a), &ErrorParseError(ref b)) => a == b,
(&ErrorFunctionNotFound(ref a), &ErrorFunctionNotFound(ref b)) => a == b, (&ErrorFunctionNotFound(ref a), &ErrorFunctionNotFound(ref b)) => a == b,
(&ErrorFunctionArgMismatch, &ErrorFunctionArgMismatch) => true, (&ErrorFunctionArgMismatch, &ErrorFunctionArgMismatch) => true,
(&ErrorIndexMismatch, &ErrorIndexMismatch) => true, (&ErrorIndexMismatch, &ErrorIndexMismatch) => true,
(&ErrorArrayMismatch, &ErrorArrayMismatch) => true,
(&ErrorArrayOutOfBounds(max1, index1), &ErrorArrayOutOfBounds(max2, index2)) => {
max1 == max2 && index1 == index2
}
(&ErrorIfGuardMismatch, &ErrorIfGuardMismatch) => true, (&ErrorIfGuardMismatch, &ErrorIfGuardMismatch) => true,
(&ErrorForMismatch, &ErrorForMismatch) => true,
(&ErrorVariableNotFound(ref a), &ErrorVariableNotFound(ref b)) => a == b, (&ErrorVariableNotFound(ref a), &ErrorVariableNotFound(ref b)) => a == b,
(&ErrorAssignmentToUnknownLHS, &ErrorAssignmentToUnknownLHS) => true, (&ErrorAssignmentToUnknownLHS, &ErrorAssignmentToUnknownLHS) => true,
(&ErrorMismatchOutputType(ref a), &ErrorMismatchOutputType(ref b)) => a == b, (&ErrorMismatchOutputType(ref a), &ErrorMismatchOutputType(ref b)) => a == b,
(&ErrorCantOpenScriptFile, &ErrorCantOpenScriptFile) => true, (&ErrorCantOpenScriptFile(ref a), &ErrorCantOpenScriptFile(ref b)) => a == b,
(&InternalErrorMalformedDotExpression, &InternalErrorMalformedDotExpression) => true, (&ErrorMalformedDotExpression, &ErrorMalformedDotExpression) => true,
(&LoopBreak, &LoopBreak) => true, (&LoopBreak, &LoopBreak) => true,
_ => false, _ => false,
} }
@ -60,21 +74,27 @@ impl PartialEq for EvalAltResult {
impl Error for EvalAltResult { impl Error for EvalAltResult {
fn description(&self) -> &str { fn description(&self) -> &str {
match *self { match *self {
EvalAltResult::ErrorParseError(ref p) => p.description(),
EvalAltResult::ErrorFunctionNotFound(_) => "Function not found", EvalAltResult::ErrorFunctionNotFound(_) => "Function not found",
EvalAltResult::ErrorFunctionArgMismatch => "Function argument types do not match", EvalAltResult::ErrorFunctionArgMismatch => "Function argument types do not match",
EvalAltResult::ErrorIndexMismatch => "Index does not match array", EvalAltResult::ErrorIndexMismatch => "Array access expects integer index",
EvalAltResult::ErrorArrayMismatch => "Indexing can only be performed on an array",
EvalAltResult::ErrorArrayOutOfBounds(_, index) if index < 0 => {
"Array access expects non-negative index"
}
EvalAltResult::ErrorArrayOutOfBounds(max, _) if max == 0 => "Access of empty array",
EvalAltResult::ErrorArrayOutOfBounds(_, _) => "Array index out of bounds",
EvalAltResult::ErrorIfGuardMismatch => "If guards expect boolean expression", EvalAltResult::ErrorIfGuardMismatch => "If guards expect boolean expression",
EvalAltResult::ErrorForMismatch => "For loops expect array",
EvalAltResult::ErrorVariableNotFound(_) => "Variable not found", EvalAltResult::ErrorVariableNotFound(_) => "Variable not found",
EvalAltResult::ErrorAssignmentToUnknownLHS => { EvalAltResult::ErrorAssignmentToUnknownLHS => {
"Assignment to an unsupported left-hand side" "Assignment to an unsupported left-hand side expression"
} }
EvalAltResult::ErrorMismatchOutputType(_) => "Cast of output failed", EvalAltResult::ErrorMismatchOutputType(_) => "Output type is incorrect",
EvalAltResult::ErrorCantOpenScriptFile => "Cannot open script file", EvalAltResult::ErrorCantOpenScriptFile(_) => "Cannot open script file",
EvalAltResult::InternalErrorMalformedDotExpression => { EvalAltResult::ErrorMalformedDotExpression => "Malformed dot expression",
"[Internal error] Unexpected expression in dot expression" EvalAltResult::LoopBreak => "[Not Error] Breaks out of loop",
} EvalAltResult::Return(_) => "[Not Error] Function returns value",
EvalAltResult::LoopBreak => "Loop broken before completion (not an error)",
EvalAltResult::Return(_) => "Function returned value (not an error)",
} }
} }
@ -88,8 +108,20 @@ impl fmt::Display for EvalAltResult {
if let Some(s) = self.as_str() { if let Some(s) = self.as_str() {
write!(f, "{}: {}", self.description(), s) write!(f, "{}: {}", self.description(), s)
} else { } else {
match self {
EvalAltResult::ErrorParseError(ref p) => write!(f, "Syntax error: {}", p),
EvalAltResult::ErrorArrayOutOfBounds(_, index) if *index < 0 => {
write!(f, "{}: {} < 0", self.description(), index)
}
EvalAltResult::ErrorArrayOutOfBounds(max, _) if *max == 0 => {
write!(f, "{}", self.description()) write!(f, "{}", self.description())
} }
EvalAltResult::ErrorArrayOutOfBounds(max, index) => {
write!(f, "{} (max {}): {}", self.description(), max - 1, index)
}
err => write!(f, "{}", err.description()),
}
}
} }
} }
@ -99,6 +131,8 @@ pub struct FnSpec {
args: Option<Vec<TypeId>>, args: Option<Vec<TypeId>>,
} }
type IteratorFn = dyn Fn(&Box<dyn Any>) -> Box<dyn Iterator<Item = Box<dyn Any>>>;
/// Rhai's engine type. This is what you use to run Rhai scripts /// Rhai's engine type. This is what you use to run Rhai scripts
/// ///
/// ```rust /// ```rust
@ -117,6 +151,7 @@ pub struct FnSpec {
pub struct Engine { pub struct Engine {
/// A hashmap containing all functions known to the engine /// A hashmap containing all functions known to the engine
pub fns: HashMap<FnSpec, Arc<FnIntExt>>, pub fns: HashMap<FnSpec, Arc<FnIntExt>>,
pub type_iterators: HashMap<TypeId, Arc<IteratorFn>>,
} }
pub enum FnIntExt { pub enum FnIntExt {
@ -167,16 +202,14 @@ impl Engine {
debug_println!( debug_println!(
"Trying to call function {:?} with args {:?}", "Trying to call function {:?} with args {:?}",
ident, ident,
args.iter().map(|x| (&**x).type_id()).collect::<Vec<_>>() args.iter()
.map(|x| Any::type_name(&**x))
.collect::<Vec<_>>()
); );
let spec = FnSpec { let spec = FnSpec {
ident: ident.clone(), ident: ident.clone(),
args: Some( args: Some(args.iter().map(|a| Any::type_id(&**a)).collect()),
args.iter()
.map(|a| <dyn Any as Any>::type_id(&**a))
.collect(),
),
}; };
self.fns self.fns
@ -193,7 +226,11 @@ impl Engine {
.iter() .iter()
.map(|x| (*(&**x).box_clone()).type_name()) .map(|x| (*(&**x).box_clone()).type_name())
.collect::<Vec<_>>(); .collect::<Vec<_>>();
EvalAltResult::ErrorFunctionNotFound(format!("{} ({})", ident, typenames.join(","))) EvalAltResult::ErrorFunctionNotFound(format!(
"{} ({})",
ident,
typenames.join(", ")
))
}) })
.and_then(move |f| match **f { .and_then(move |f| match **f {
FnIntExt::Ext(ref f) => f(args), FnIntExt::Ext(ref f) => f(args),
@ -228,6 +265,14 @@ impl Engine {
// currently a no-op, exists for future extensibility // currently a no-op, exists for future extensibility
} }
/// Register an iterator adapter for a type.
pub fn register_iterator<T: Any, F>(&mut self, f: F)
where
F: 'static + Fn(&Box<dyn Any>) -> Box<dyn Iterator<Item = Box<dyn Any>>>,
{
self.type_iterators.insert(TypeId::of::<T>(), Arc::new(f));
}
/// Register a get function for a member of a registered type /// Register a get function for a member of a registered type
pub fn register_get<T: Clone + Any, U: Clone + Any, F>(&mut self, name: &str, get_fn: F) pub fn register_get<T: Clone + Any, U: Clone + Any, F>(&mut self, name: &str, get_fn: F)
where where
@ -269,8 +314,8 @@ impl Engine {
use std::iter::once; use std::iter::once;
match *dot_rhs { match *dot_rhs {
Expr::FnCall(ref fn_name, ref args) => { Expr::FunctionCall(ref fn_name, ref args) => {
let mut args: Vec<Box<dyn Any>> = args let mut args: Array = args
.iter() .iter()
.map(|arg| self.eval_expr(scope, arg)) .map(|arg| self.eval_expr(scope, arg))
.collect::<Result<Vec<_>, _>>()?; .collect::<Result<Vec<_>, _>>()?;
@ -291,10 +336,20 @@ impl Engine {
let mut val = self.call_fn_raw(get_fn_name, vec![this_ptr])?; let mut val = self.call_fn_raw(get_fn_name, vec![this_ptr])?;
((*val).downcast_mut() as Option<&mut Vec<Box<dyn Any>>>) ((*val).downcast_mut() as Option<&mut Array>)
.and_then(|arr| idx.downcast_ref::<i64>().map(|idx| (arr, *idx as usize))) .ok_or(EvalAltResult::ErrorArrayMismatch)
.map(|(arr, idx)| arr[idx].clone()) .and_then(|arr| {
idx.downcast_ref::<i64>()
.map(|idx| (arr, *idx))
.ok_or(EvalAltResult::ErrorIndexMismatch) .ok_or(EvalAltResult::ErrorIndexMismatch)
})
.and_then(|(arr, idx)| match idx {
x if x < 0 => Err(EvalAltResult::ErrorArrayOutOfBounds(0, x)),
x => arr
.get(x as usize)
.cloned()
.ok_or(EvalAltResult::ErrorArrayOutOfBounds(arr.len(), x)),
})
} }
Expr::Dot(ref inner_lhs, ref inner_rhs) => match **inner_lhs { Expr::Dot(ref inner_lhs, ref inner_rhs) => match **inner_lhs {
Expr::Identifier(ref id) => { Expr::Identifier(ref id) => {
@ -302,9 +357,9 @@ impl Engine {
self.call_fn_raw(get_fn_name, vec![this_ptr]) self.call_fn_raw(get_fn_name, vec![this_ptr])
.and_then(|mut v| self.get_dot_val_helper(scope, v.as_mut(), inner_rhs)) .and_then(|mut v| self.get_dot_val_helper(scope, v.as_mut(), inner_rhs))
} }
_ => Err(EvalAltResult::InternalErrorMalformedDotExpression), _ => Err(EvalAltResult::ErrorMalformedDotExpression),
}, },
_ => Err(EvalAltResult::InternalErrorMalformedDotExpression), _ => Err(EvalAltResult::ErrorMalformedDotExpression),
} }
} }
@ -335,11 +390,19 @@ impl Engine {
.eval_expr(scope, idx)? .eval_expr(scope, idx)?
.downcast::<i64>() .downcast::<i64>()
.map_err(|_| EvalAltResult::ErrorIndexMismatch)?; .map_err(|_| EvalAltResult::ErrorIndexMismatch)?;
let idx = *idx_boxed as usize; let idx_raw = *idx_boxed;
let idx = match idx_raw {
x if x < 0 => return Err(EvalAltResult::ErrorArrayOutOfBounds(0, x)),
x => x as usize,
};
let (idx_sc, val) = Self::search_scope(scope, id, |val| { let (idx_sc, val) = Self::search_scope(scope, id, |val| {
((*val).downcast_mut() as Option<&mut Vec<Box<dyn Any>>>) ((*val).downcast_mut() as Option<&mut Array>)
.map(|arr| arr[idx].clone()) .ok_or(EvalAltResult::ErrorArrayMismatch)
.ok_or(EvalAltResult::ErrorIndexMismatch) .and_then(|arr| {
arr.get(idx)
.cloned()
.ok_or(EvalAltResult::ErrorArrayOutOfBounds(arr.len(), idx_raw))
})
})?; })?;
Ok((idx_sc, idx, val)) Ok((idx_sc, idx, val))
@ -368,11 +431,11 @@ impl Engine {
// In case the expression mutated `target`, we need to reassign it because // In case the expression mutated `target`, we need to reassign it because
// of the above `clone`. // of the above `clone`.
scope[sc_idx].1.downcast_mut::<Vec<Box<dyn Any>>>().unwrap()[idx] = target; scope[sc_idx].1.downcast_mut::<Array>().unwrap()[idx] = target;
value value
} }
_ => Err(EvalAltResult::InternalErrorMalformedDotExpression), _ => Err(EvalAltResult::ErrorMalformedDotExpression),
} }
} }
@ -401,9 +464,9 @@ impl Engine {
self.call_fn_raw(set_fn_name, vec![this_ptr, v.as_mut()]) self.call_fn_raw(set_fn_name, vec![this_ptr, v.as_mut()])
}) })
} }
_ => Err(EvalAltResult::InternalErrorMalformedDotExpression), _ => Err(EvalAltResult::ErrorMalformedDotExpression),
}, },
_ => Err(EvalAltResult::InternalErrorMalformedDotExpression), _ => Err(EvalAltResult::ErrorMalformedDotExpression),
} }
} }
@ -431,20 +494,20 @@ impl Engine {
// In case the expression mutated `target`, we need to reassign it because // In case the expression mutated `target`, we need to reassign it because
// of the above `clone`. // of the above `clone`.
scope[sc_idx].1.downcast_mut::<Vec<Box<dyn Any>>>().unwrap()[idx] = target; scope[sc_idx].1.downcast_mut::<Array>().unwrap()[idx] = target;
value value
} }
_ => Err(EvalAltResult::InternalErrorMalformedDotExpression), _ => Err(EvalAltResult::ErrorMalformedDotExpression),
} }
} }
fn eval_expr(&self, scope: &mut Scope, expr: &Expr) -> Result<Box<dyn Any>, EvalAltResult> { fn eval_expr(&self, scope: &mut Scope, expr: &Expr) -> Result<Box<dyn Any>, EvalAltResult> {
match *expr { match *expr {
Expr::IntConst(i) => Ok(Box::new(i)), Expr::IntegerConstant(i) => Ok(Box::new(i)),
Expr::FloatConst(i) => Ok(Box::new(i)), Expr::FloatConstant(i) => Ok(Box::new(i)),
Expr::StringConst(ref s) => Ok(Box::new(s.clone())), Expr::StringConstant(ref s) => Ok(Box::new(s.clone())),
Expr::CharConst(ref c) => Ok(Box::new(*c)), Expr::CharConstant(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 {
@ -475,18 +538,27 @@ impl Engine {
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 {
if let Some(i) = idx.downcast_ref::<i64>() { return if let Some(&i) = idx.downcast_ref::<i64>() {
if let Some(arr_typed) = if let Some(arr_typed) =
(*val).downcast_mut() as Option<&mut Vec<Box<dyn Any>>> (*val).downcast_mut() as Option<&mut Array>
{ {
arr_typed[*i as usize] = rhs_val; if i < 0 {
return Ok(Box::new(())); Err(EvalAltResult::ErrorArrayOutOfBounds(0, i))
} else if i as usize >= arr_typed.len() {
Err(EvalAltResult::ErrorArrayOutOfBounds(
arr_typed.len(),
i,
))
} else { } else {
return Err(EvalAltResult::ErrorIndexMismatch); arr_typed[i as usize] = rhs_val;
Ok(Box::new(()))
} }
} else { } else {
return Err(EvalAltResult::ErrorIndexMismatch); Err(EvalAltResult::ErrorIndexMismatch)
} }
} else {
Err(EvalAltResult::ErrorIndexMismatch)
};
} }
} }
@ -509,11 +581,11 @@ impl Engine {
Ok(Box::new(arr)) Ok(Box::new(arr))
} }
Expr::FnCall(ref fn_name, ref args) => self.call_fn_raw( Expr::FunctionCall(ref fn_name, ref args) => self.call_fn_raw(
fn_name.to_owned(), fn_name.to_owned(),
args.iter() args.iter()
.map(|ex| self.eval_expr(scope, ex)) .map(|ex| self.eval_expr(scope, ex))
.collect::<Result<Vec<Box<dyn Any>>, _>>()? .collect::<Result<Array, _>>()?
.iter_mut() .iter_mut()
.map(|b| b.as_mut()) .map(|b| b.as_mut())
.collect(), .collect(),
@ -595,13 +667,33 @@ impl Engine {
_ => (), _ => (),
} }
}, },
Stmt::For(ref name, ref expr, ref body) => {
let arr = self.eval_expr(scope, expr)?;
let tid = Any::type_id(&*arr);
if let Some(iter_fn) = self.type_iterators.get(&tid) {
scope.push((name.clone(), Box::new(())));
let idx = scope.len() - 1;
for a in iter_fn(&arr) {
scope[idx].1 = a;
match self.eval_stmt(scope, body) {
Err(EvalAltResult::LoopBreak) => break,
Err(x) => return Err(x),
_ => (),
}
}
scope.remove(idx);
Ok(Box::new(()))
} else {
return Err(EvalAltResult::ErrorForMismatch);
}
}
Stmt::Break => Err(EvalAltResult::LoopBreak), Stmt::Break => Err(EvalAltResult::LoopBreak),
Stmt::Return => Err(EvalAltResult::Return(Box::new(()))), Stmt::Return => Err(EvalAltResult::Return(Box::new(()))),
Stmt::ReturnWithVal(ref a) => { Stmt::ReturnWithVal(ref a) => {
let result = self.eval_expr(scope, a)?; let result = self.eval_expr(scope, a)?;
Err(EvalAltResult::Return(result)) Err(EvalAltResult::Return(result))
} }
Stmt::Var(ref name, ref init) => { Stmt::Let(ref name, ref init) => {
match *init { match *init {
Some(ref v) => { Some(ref v) => {
let i = self.eval_expr(scope, v)?; let i = self.eval_expr(scope, v)?;
@ -614,6 +706,34 @@ impl Engine {
} }
} }
/// Compile a string into an AST
pub fn compile(input: &str) -> Result<AST, ParseError> {
let tokens = lex(input);
let mut peekables = tokens.peekable();
let tree = parse(&mut peekables);
tree
}
/// Compile a file into an AST
pub fn compile_file(fname: &str) -> Result<AST, EvalAltResult> {
use std::fs::File;
use std::io::prelude::*;
if let Ok(mut f) = File::open(fname) {
let mut contents = String::new();
if f.read_to_string(&mut contents).is_ok() {
Self::compile(&contents).map_err(|err| EvalAltResult::ErrorParseError(err))
} else {
Err(EvalAltResult::ErrorCantOpenScriptFile(fname.to_owned()))
}
} else {
Err(EvalAltResult::ErrorCantOpenScriptFile(fname.to_owned()))
}
}
/// Evaluate a file /// Evaluate a file
pub fn eval_file<T: Any + Clone>(&mut self, fname: &str) -> Result<T, EvalAltResult> { pub fn eval_file<T: Any + Clone>(&mut self, fname: &str) -> Result<T, EvalAltResult> {
use std::fs::File; use std::fs::File;
@ -625,33 +745,42 @@ impl Engine {
if f.read_to_string(&mut contents).is_ok() { 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(fname.to_owned()))
} }
} else { } else {
Err(EvalAltResult::ErrorCantOpenScriptFile) Err(EvalAltResult::ErrorCantOpenScriptFile(fname.to_owned()))
} }
} }
/// Evaluate a string /// Evaluate a string
pub fn eval<T: Any + Clone>(&mut self, input: &str) -> Result<T, EvalAltResult> { pub fn eval<T: Any + Clone>(&mut self, input: &str) -> Result<T, EvalAltResult> {
let mut scope = Scope::new(); let mut scope = Scope::new();
self.eval_with_scope(&mut scope, input) self.eval_with_scope(&mut scope, input)
} }
/// Evaluate with own scope /// Evaluate a string with own scope
pub fn eval_with_scope<T: Any + Clone>( pub fn eval_with_scope<T: Any + Clone>(
&mut self, &mut self,
scope: &mut Scope, scope: &mut Scope,
input: &str, input: &str,
) -> Result<T, EvalAltResult> { ) -> Result<T, EvalAltResult> {
let tokens = lex(input); let ast = Self::compile(input).map_err(|err| EvalAltResult::ErrorParseError(err))?;
self.eval_ast_with_scope(scope, &ast)
}
let mut peekables = tokens.peekable(); /// Evaluate an AST
let tree = parse(&mut peekables); pub fn eval_ast<T: Any + Clone>(&mut self, ast: &AST) -> Result<T, EvalAltResult> {
let mut scope = Scope::new();
self.eval_ast_with_scope(&mut scope, ast)
}
match tree { /// Evaluate an AST with own scope
Ok((ref os, ref fns)) => { pub fn eval_ast_with_scope<T: Any + Clone>(
&mut self,
scope: &mut Scope,
ast: &AST,
) -> Result<T, EvalAltResult> {
let AST(os, fns) = ast;
let mut x: Result<Box<dyn Any>, EvalAltResult> = Ok(Box::new(())); let mut x: Result<Box<dyn Any>, EvalAltResult> = Ok(Box::new(()));
for f in fns { for f in fns {
@ -680,9 +809,6 @@ impl Engine {
Err(a) => Err(EvalAltResult::ErrorMismatchOutputType((*a).type_name())), Err(a) => Err(EvalAltResult::ErrorMismatchOutputType((*a).type_name())),
} }
} }
Err(_) => Err(EvalAltResult::ErrorFunctionArgMismatch),
}
}
/// Evaluate a file, but only return errors, if there are any. /// Evaluate a file, but only return errors, if there are any.
/// Useful for when you don't need the result, but still need /// Useful for when you don't need the result, but still need
@ -701,10 +827,10 @@ impl Engine {
Ok(()) Ok(())
} }
} else { } else {
Err(EvalAltResult::ErrorCantOpenScriptFile) Err(EvalAltResult::ErrorCantOpenScriptFile(fname.to_owned()))
} }
} else { } else {
Err(EvalAltResult::ErrorCantOpenScriptFile) Err(EvalAltResult::ErrorCantOpenScriptFile(fname.to_owned()))
} }
} }
@ -715,7 +841,7 @@ impl Engine {
self.consume_with_scope(&mut Scope::new(), input) self.consume_with_scope(&mut Scope::new(), input)
} }
/// Evaluate a string with own scoppe, but only return errors, if there are any. /// Evaluate a string with own scope, but only return errors, if there are any.
/// 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_with_scope( pub fn consume_with_scope(
@ -729,7 +855,7 @@ impl Engine {
let tree = parse(&mut peekables); let tree = parse(&mut peekables);
match tree { match tree {
Ok((ref os, ref fns)) => { Ok(AST(ref os, ref fns)) => {
for f in fns { for f in fns {
if f.params.len() > 6 { if f.params.len() > 6 {
return Ok(()); return Ok(());
@ -757,7 +883,7 @@ impl Engine {
} }
} }
/// Register the default library. That means, numberic types, char, bool /// Register the default library. That means, numeric types, char, bool
/// String, arithmetics and string concatenations. /// String, arithmetics and string concatenations.
pub fn register_default_lib(engine: &mut Engine) { pub fn register_default_lib(engine: &mut Engine) {
macro_rules! reg_op { macro_rules! reg_op {
@ -784,6 +910,30 @@ impl Engine {
) )
} }
macro_rules! reg_func1 {
($engine:expr, $x:expr, $op:expr, $r:ty, $( $y:ty ),*) => (
$(
$engine.register_fn($x, $op as fn(x: $y)->$r);
)*
)
}
macro_rules! reg_func2x {
($engine:expr, $x:expr, $op:expr, $v:ty, $r:ty, $( $y:ty ),*) => (
$(
$engine.register_fn($x, $op as fn(x: $v, y: $y)->$r);
)*
)
}
macro_rules! reg_func2y {
($engine:expr, $x:expr, $op:expr, $v:ty, $r:ty, $( $y:ty ),*) => (
$(
$engine.register_fn($x, $op as fn(y: $y, x: $v)->$r);
)*
)
}
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
} }
@ -896,12 +1046,79 @@ impl Engine {
// 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_with(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)));
// Register print and debug
fn print_debug<T: Debug>(x: T) {
println!("{:?}", x);
}
fn print<T: Display>(x: T) {
println!("{}", x);
}
reg_func1!(engine, "print", print, (), i32, i64, u32, u64);
reg_func1!(engine, "print", print, (), f32, f64, bool, String);
reg_func1!(engine, "print", print_debug, (), Array);
engine.register_fn("print", |_: ()| println!());
reg_func1!(engine, "debug", print_debug, (), i32, i64, u32, u64);
reg_func1!(engine, "debug", print_debug, (), f32, f64, bool, String);
reg_func1!(engine, "debug", print_debug, (), Array, ());
// Register array functions
fn push<T: Any + 'static>(list: &mut Array, item: T) {
list.push(Box::new(item));
}
reg_func2x!(engine, "push", push, &mut Array, (), i32, i64, u32, u64);
reg_func2x!(engine, "push", push, &mut Array, (), f32, f64, bool);
reg_func2x!(engine, "push", push, &mut Array, (), String, Array, ());
engine.register_box_fn("pop", |list: &mut Array| list.pop().unwrap());
engine.register_box_fn("shift", |list: &mut Array| list.remove(0));
engine.register_fn("len", |list: &mut Array| -> i64 {
list.len().try_into().unwrap()
});
// Register string concatenate functions
fn prepend<T: Display>(x: T, y: String) -> String {
format!("{}{}", x, y)
}
fn append<T: Display>(x: String, y: T) -> String {
format!("{}{}", x, y)
}
reg_func2x!(engine, "+", append, String, String, i32, i64, u32, u64, f32, f64, bool);
engine.register_fn("+", |x: String, y: Array| format!("{}{:?}", x, y));
engine.register_fn("+", |x: String, _: ()| format!("{}", x));
reg_func2y!(engine, "+", prepend, String, String, i32, i64, u32, u64, f32, f64, bool);
engine.register_fn("+", |x: Array, y: String| format!("{:?}{}", x, y));
engine.register_fn("+", |_: (), y: String| format!("{}", y));
// Register array iterator
engine.register_iterator::<Array, _>(|a| {
Box::new(a.downcast_ref::<Array>().unwrap().clone().into_iter())
});
// Register range function
use std::ops::Range;
engine.register_iterator::<Range<i64>, _>(|a| {
Box::new(
a.downcast_ref::<Range<i64>>()
.unwrap()
.clone()
.map(|n| Box::new(n) as Box<dyn Any>),
)
});
engine.register_fn("range", |i1: i64, i2: i64| (i1..i2));
} }
/// Make a new engine /// Make a new engine
pub fn new() -> Engine { pub fn new() -> Engine {
let mut engine = Engine { let mut engine = Engine {
fns: HashMap::new(), fns: HashMap::new(),
type_iterators: HashMap::new(),
}; };
Engine::register_default_lib(&mut engine); Engine::register_default_lib(&mut engine);

View File

@ -6,6 +6,9 @@ use crate::engine::{Engine, EvalAltResult};
pub trait RegisterFn<FN, ARGS, RET> { pub trait RegisterFn<FN, ARGS, RET> {
fn register_fn(&mut self, name: &str, f: FN); fn register_fn(&mut self, name: &str, f: FN);
} }
pub trait RegisterBoxFn<FN, ARGS> {
fn register_box_fn(&mut self, name: &str, f: FN);
}
pub struct Ref<A>(A); pub struct Ref<A>(A);
pub struct Mut<A>(A); pub struct Mut<A>(A);
@ -44,7 +47,37 @@ macro_rules! def_register {
// Call the user-supplied function using ($clone) to // Call the user-supplied function using ($clone) to
// potentially clone the value, otherwise pass the reference. // potentially clone the value, otherwise pass the reference.
Ok(Box::new(f($(($clone)($par)),*)) as Box<dyn Any>) let r = f($(($clone)($par)),*);
Ok(Box::new(r) as Box<dyn Any>)
};
self.register_fn_raw(name.to_owned(), Some(vec![$(TypeId::of::<$par>()),*]), Box::new(fun));
}
}
impl<$($par,)* FN> RegisterBoxFn<FN, ($($mark,)*)> for Engine
where
$($par: Any + Clone,)*
FN: Fn($($param),*) -> Box<dyn Any> + 'static
{
fn register_box_fn(&mut self, name: &str, f: FN) {
let fun = move |mut args: Vec<&mut dyn Any>| {
// Check for length at the beginning to avoid
// per-element bound checks.
if args.len() != count_args!($($par)*) {
return Err(EvalAltResult::ErrorFunctionArgMismatch);
}
#[allow(unused_variables, unused_mut)]
let mut drain = args.drain(..);
$(
// Downcast every element, return in case of a type mismatch
let $par = ((*drain.next().unwrap()).downcast_mut() as Option<&mut $par>)
.ok_or(EvalAltResult::ErrorFunctionArgMismatch)?;
)*
// Call the user-supplied function using ($clone) to
// potentially clone the value, otherwise pass the reference.
Ok(f($(($clone)($par)),*))
}; };
self.register_fn_raw(name.to_owned(), Some(vec![$(TypeId::of::<$par>()),*]), Box::new(fun)); self.register_fn_raw(name.to_owned(), Some(vec![$(TypeId::of::<$par>()),*]), Box::new(fun));
} }

View File

@ -47,4 +47,5 @@ mod parser;
pub use any::Any; pub use any::Any;
pub use engine::{Engine, EvalAltResult, Scope}; pub use engine::{Engine, EvalAltResult, Scope};
pub use fn_register::RegisterFn; pub use fn_register::{RegisterBoxFn, RegisterFn};
pub use parser::{ParseError, ParseErrorType, AST};

File diff suppressed because it is too large Load Diff

View File

@ -10,7 +10,7 @@ fn test_decrement() {
assert_eq!( assert_eq!(
engine.eval::<String>("let s = \"test\"; s -= \"ing\"; s"), engine.eval::<String>("let s = \"test\"; s -= \"ing\"; s"),
Err(EvalAltResult::ErrorFunctionNotFound( Err(EvalAltResult::ErrorFunctionNotFound(
"- (alloc::string::String,alloc::string::String)".to_string() "- (alloc::string::String, alloc::string::String)".to_string()
)) ))
); );
} }

24
tests/for.rs Normal file
View File

@ -0,0 +1,24 @@
use rhai::Engine;
#[test]
fn test_for() {
let mut engine = Engine::new();
let script = r"
let sum1 = 0;
let sum2 = 0;
let inputs = [1, 2, 3, 4, 5];
for x in inputs {
sum1 += x;
}
for x in range(1, 6) {
sum2 += x;
}
sum1 + sum2
";
assert_eq!(engine.eval::<i64>(script).unwrap(), 30);
}

View File

@ -6,8 +6,8 @@ fn test_mismatched_op() {
assert_eq!( assert_eq!(
engine.eval::<i64>("60 + \"hello\""), engine.eval::<i64>("60 + \"hello\""),
Err(EvalAltResult::ErrorFunctionNotFound( Err(EvalAltResult::ErrorMismatchOutputType(
"+ (i64,alloc::string::String)".into() "alloc::string::String".into()
)) ))
); );
} }
@ -32,7 +32,7 @@ fn test_mismatched_op_custom_type() {
assert_eq!( assert_eq!(
engine.eval::<i64>("60 + new_ts()"), engine.eval::<i64>("60 + new_ts()"),
Err(EvalAltResult::ErrorFunctionNotFound( Err(EvalAltResult::ErrorFunctionNotFound(
"+ (i64,mismatched_op::test_mismatched_op_custom_type::TestStruct)".into() "+ (i64, mismatched_op::test_mismatched_op_custom_type::TestStruct)".into()
)) ))
); );
} }