2017-12-20 12:16:14 +01:00
|
|
|
use std::any::TypeId;
|
2020-03-04 15:00:01 +01:00
|
|
|
use std::borrow::Cow;
|
2017-12-20 12:16:14 +01:00
|
|
|
use std::cmp::{PartialEq, PartialOrd};
|
2016-02-29 22:43:45 +01:00
|
|
|
use std::collections::HashMap;
|
2020-03-01 06:30:22 +01:00
|
|
|
use std::sync::Arc;
|
2016-02-29 22:43:45 +01:00
|
|
|
|
2020-02-25 08:02:27 +01:00
|
|
|
use crate::any::{Any, AnyExt, Dynamic, Variant};
|
2020-03-04 15:00:01 +01:00
|
|
|
use crate::parser::{Expr, FnDef, Position, Stmt};
|
|
|
|
use crate::result::EvalAltResult;
|
2020-03-03 08:20:20 +01:00
|
|
|
use crate::scope::Scope;
|
2020-02-23 15:48:46 +01:00
|
|
|
|
2020-03-04 15:00:01 +01:00
|
|
|
/// An dynamic array of `Dynamic` values.
|
2020-02-25 08:02:27 +01:00
|
|
|
pub type Array = Vec<Dynamic>;
|
2020-03-04 15:00:01 +01:00
|
|
|
|
2020-02-25 08:02:27 +01:00
|
|
|
pub type FnCallArgs<'a> = Vec<&'a mut Variant>;
|
2016-02-29 22:43:45 +01:00
|
|
|
|
2020-03-03 10:28:38 +01:00
|
|
|
const KEYWORD_PRINT: &'static str = "print";
|
|
|
|
const KEYWORD_DEBUG: &'static str = "debug";
|
|
|
|
const KEYWORD_TYPE_OF: &'static str = "type_of";
|
|
|
|
|
2020-03-05 13:28:03 +01:00
|
|
|
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
|
|
|
|
enum VariableType {
|
|
|
|
Array,
|
|
|
|
String,
|
|
|
|
Expression,
|
|
|
|
}
|
|
|
|
|
2017-12-20 12:16:14 +01:00
|
|
|
#[derive(Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
|
2020-03-04 15:00:01 +01:00
|
|
|
pub struct FnSpec<'a> {
|
|
|
|
pub name: Cow<'a, str>,
|
2020-03-02 07:28:42 +01:00
|
|
|
pub args: Option<Vec<TypeId>>,
|
2016-03-02 02:45:02 +01:00
|
|
|
}
|
|
|
|
|
2020-02-25 08:02:27 +01:00
|
|
|
type IteratorFn = dyn Fn(&Dynamic) -> Box<dyn Iterator<Item = Dynamic>>;
|
2020-02-23 15:48:46 +01:00
|
|
|
|
2020-03-04 15:00:01 +01:00
|
|
|
/// Rhai main scripting engine.
|
2017-10-30 16:08:44 +01:00
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use rhai::Engine;
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// let mut engine = Engine::new();
|
|
|
|
///
|
|
|
|
/// if let Ok(result) = engine.eval::<i64>("40 + 2") {
|
|
|
|
/// println!("Answer: {}", result); // prints 42
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
2020-03-04 15:00:01 +01:00
|
|
|
pub struct Engine<'a> {
|
2020-03-02 07:28:42 +01:00
|
|
|
/// A hashmap containing all compiled functions known to the engine
|
2020-03-04 16:06:05 +01:00
|
|
|
pub(crate) external_functions: HashMap<FnSpec<'a>, Arc<FnIntExt>>,
|
2020-03-02 07:28:42 +01:00
|
|
|
/// A hashmap containing all script-defined functions
|
2020-03-04 16:06:05 +01:00
|
|
|
pub(crate) script_functions: HashMap<FnSpec<'a>, Arc<FnIntExt>>,
|
2020-03-02 07:28:42 +01:00
|
|
|
/// A hashmap containing all iterators known to the engine
|
2020-03-04 15:00:01 +01:00
|
|
|
pub(crate) type_iterators: HashMap<TypeId, Arc<IteratorFn>>,
|
|
|
|
pub(crate) type_names: HashMap<String, String>,
|
2020-03-02 07:28:42 +01:00
|
|
|
|
2020-03-04 15:00:01 +01:00
|
|
|
pub(crate) on_print: Box<dyn FnMut(&str) + 'a>,
|
|
|
|
pub(crate) on_debug: Box<dyn FnMut(&str) + 'a>,
|
2017-12-20 12:16:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub enum FnIntExt {
|
|
|
|
Ext(Box<FnAny>),
|
|
|
|
Int(FnDef),
|
2016-02-29 22:43:45 +01:00
|
|
|
}
|
|
|
|
|
2020-03-02 15:13:14 +01:00
|
|
|
pub type FnAny = dyn Fn(FnCallArgs, Position) -> Result<Dynamic, EvalAltResult>;
|
2017-12-20 12:16:14 +01:00
|
|
|
|
2020-03-04 15:00:01 +01:00
|
|
|
impl Engine<'_> {
|
2017-10-30 16:08:44 +01:00
|
|
|
/// Universal method for calling functions, that are either
|
|
|
|
/// registered with the `Engine` or written in Rhai
|
2020-03-04 15:00:01 +01:00
|
|
|
pub(crate) fn call_fn_raw(
|
|
|
|
&mut self,
|
|
|
|
fn_name: &str,
|
2020-03-02 07:28:42 +01:00
|
|
|
args: FnCallArgs,
|
|
|
|
def_value: Option<&Dynamic>,
|
2020-03-02 15:13:14 +01:00
|
|
|
pos: Position,
|
2020-03-02 07:28:42 +01:00
|
|
|
) -> Result<Dynamic, EvalAltResult> {
|
2018-02-09 22:27:41 +01:00
|
|
|
debug_println!(
|
2020-03-04 15:00:01 +01:00
|
|
|
"Calling function: {} ({})",
|
|
|
|
fn_name,
|
2020-02-23 15:48:46 +01:00
|
|
|
args.iter()
|
2020-03-03 09:24:03 +01:00
|
|
|
.map(|x| (*x).type_name())
|
|
|
|
.map(|name| self.map_type_name(name))
|
2020-02-23 15:48:46 +01:00
|
|
|
.collect::<Vec<_>>()
|
2020-03-03 09:24:03 +01:00
|
|
|
.join(", ")
|
2017-12-20 21:09:53 +01:00
|
|
|
);
|
|
|
|
|
2020-03-04 15:00:01 +01:00
|
|
|
let mut spec = FnSpec {
|
|
|
|
name: fn_name.into(),
|
|
|
|
args: None,
|
|
|
|
};
|
2016-03-03 15:31:42 +01:00
|
|
|
|
2020-03-02 07:28:42 +01:00
|
|
|
// First search in script-defined functions (can override built-in),
|
2020-03-02 08:19:41 +01:00
|
|
|
// then in built-in's
|
2020-03-04 15:00:01 +01:00
|
|
|
let fn_def = self
|
2020-03-04 16:06:05 +01:00
|
|
|
.script_functions
|
2020-03-04 15:00:01 +01:00
|
|
|
.get(&spec)
|
|
|
|
.or_else(|| {
|
|
|
|
spec.args = Some(args.iter().map(|a| Any::type_id(&**a)).collect());
|
2020-03-04 16:06:05 +01:00
|
|
|
self.external_functions.get(&spec)
|
2020-03-04 15:00:01 +01:00
|
|
|
})
|
|
|
|
.map(|f| f.clone());
|
2020-03-02 07:28:42 +01:00
|
|
|
|
|
|
|
if let Some(f) = fn_def {
|
2020-03-04 15:00:01 +01:00
|
|
|
match *f {
|
2020-02-25 03:40:48 +01:00
|
|
|
FnIntExt::Ext(ref f) => {
|
2020-03-02 15:13:14 +01:00
|
|
|
let r = f(args, pos);
|
2020-03-01 17:11:00 +01:00
|
|
|
|
2020-02-25 03:40:48 +01:00
|
|
|
if r.is_err() {
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2020-03-04 15:00:01 +01:00
|
|
|
let callback = match spec.name.as_ref() {
|
|
|
|
KEYWORD_PRINT => self.on_print.as_mut(),
|
|
|
|
KEYWORD_DEBUG => self.on_debug.as_mut(),
|
2020-02-25 03:40:48 +01:00
|
|
|
_ => return r,
|
|
|
|
};
|
|
|
|
|
2020-03-03 10:28:38 +01:00
|
|
|
Ok(callback(
|
2020-03-04 15:00:01 +01:00
|
|
|
&r.unwrap()
|
2020-02-25 03:40:48 +01:00
|
|
|
.downcast::<String>()
|
2020-03-04 15:00:01 +01:00
|
|
|
.map(|s| *s)
|
|
|
|
.unwrap_or("error: not a string".into()),
|
2020-03-03 10:28:38 +01:00
|
|
|
)
|
|
|
|
.into_dynamic())
|
2020-02-25 03:40:48 +01:00
|
|
|
}
|
2017-12-20 21:09:53 +01:00
|
|
|
FnIntExt::Int(ref f) => {
|
2020-03-03 16:31:16 +01:00
|
|
|
if f.params.len() != args.len() {
|
|
|
|
return Err(EvalAltResult::ErrorFunctionArgsMismatch(
|
2020-03-04 15:00:01 +01:00
|
|
|
spec.name.into(),
|
2020-03-03 16:31:16 +01:00
|
|
|
f.params.len(),
|
|
|
|
args.len(),
|
|
|
|
pos,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2017-12-20 21:09:53 +01:00
|
|
|
let mut scope = Scope::new();
|
2020-03-01 17:11:00 +01:00
|
|
|
|
2017-12-20 21:09:53 +01:00
|
|
|
scope.extend(
|
|
|
|
f.params
|
|
|
|
.iter()
|
|
|
|
.cloned()
|
2020-03-03 09:24:03 +01:00
|
|
|
.zip(args.iter().map(|x| (*x).into_dynamic())),
|
2017-12-20 21:09:53 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
match self.eval_stmt(&mut scope, &*f.body) {
|
2020-03-02 15:13:14 +01:00
|
|
|
Err(EvalAltResult::Return(x, _)) => Ok(x),
|
2017-12-20 21:09:53 +01:00
|
|
|
other => other,
|
|
|
|
}
|
|
|
|
}
|
2020-03-02 07:28:42 +01:00
|
|
|
}
|
2020-03-04 15:00:01 +01:00
|
|
|
} else if spec.name == KEYWORD_TYPE_OF && args.len() == 1 {
|
2020-03-03 10:28:38 +01:00
|
|
|
Ok(self
|
|
|
|
.map_type_name(args[0].type_name())
|
|
|
|
.to_string()
|
|
|
|
.into_dynamic())
|
2020-03-02 07:28:42 +01:00
|
|
|
} else if let Some(val) = def_value {
|
|
|
|
// Return default value
|
|
|
|
Ok(val.clone())
|
|
|
|
} else {
|
2020-03-02 16:16:19 +01:00
|
|
|
let types_list = args
|
2020-03-02 07:28:42 +01:00
|
|
|
.iter()
|
2020-03-03 09:24:03 +01:00
|
|
|
.map(|x| (*x).type_name())
|
2020-03-02 16:16:19 +01:00
|
|
|
.map(|name| self.map_type_name(name))
|
2020-03-02 07:28:42 +01:00
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
2020-03-02 15:13:14 +01:00
|
|
|
Err(EvalAltResult::ErrorFunctionNotFound(
|
2020-03-04 15:00:01 +01:00
|
|
|
format!("{} ({})", spec.name, types_list.join(", ")),
|
2020-03-02 15:13:14 +01:00
|
|
|
pos,
|
|
|
|
))
|
2020-03-02 07:28:42 +01:00
|
|
|
}
|
2017-12-20 12:16:14 +01:00
|
|
|
}
|
2016-03-03 15:31:42 +01:00
|
|
|
|
2017-12-20 12:16:14 +01:00
|
|
|
fn get_dot_val_helper(
|
2020-03-04 15:00:01 +01:00
|
|
|
&mut self,
|
2017-12-20 12:16:14 +01:00
|
|
|
scope: &mut Scope,
|
2020-02-25 08:02:27 +01:00
|
|
|
this_ptr: &mut Variant,
|
2017-12-20 12:16:14 +01:00
|
|
|
dot_rhs: &Expr,
|
2020-02-25 08:02:27 +01:00
|
|
|
) -> Result<Dynamic, EvalAltResult> {
|
2017-12-20 12:16:14 +01:00
|
|
|
use std::iter::once;
|
|
|
|
|
2020-02-29 10:10:51 +01:00
|
|
|
match dot_rhs {
|
2020-03-02 15:13:14 +01:00
|
|
|
Expr::FunctionCall(fn_name, args, def_value, pos) => {
|
2020-02-23 15:48:46 +01:00
|
|
|
let mut args: Array = args
|
2019-09-18 12:21:07 +02:00
|
|
|
.iter()
|
2017-12-20 12:16:14 +01:00
|
|
|
.map(|arg| self.eval_expr(scope, arg))
|
|
|
|
.collect::<Result<Vec<_>, _>>()?;
|
2020-03-01 17:11:00 +01:00
|
|
|
|
2017-12-20 22:16:53 +01:00
|
|
|
let args = once(this_ptr)
|
|
|
|
.chain(args.iter_mut().map(|b| b.as_mut()))
|
|
|
|
.collect();
|
2017-12-20 12:16:14 +01:00
|
|
|
|
2020-03-04 15:00:01 +01:00
|
|
|
self.call_fn_raw(fn_name, args, def_value.as_ref(), *pos)
|
2016-03-07 21:39:02 +01:00
|
|
|
}
|
2020-03-01 17:11:00 +01:00
|
|
|
|
2020-03-02 15:13:14 +01:00
|
|
|
Expr::Identifier(id, pos) => {
|
2020-03-04 15:00:01 +01:00
|
|
|
let get_fn_name = format!("get${}", id);
|
2017-12-20 12:16:14 +01:00
|
|
|
|
2020-03-04 15:00:01 +01:00
|
|
|
self.call_fn_raw(&get_fn_name, vec![this_ptr], None, *pos)
|
2016-03-07 21:39:02 +01:00
|
|
|
}
|
2020-03-01 17:11:00 +01:00
|
|
|
|
2020-03-05 13:28:03 +01:00
|
|
|
Expr::Index(lhs, idx_expr) => {
|
2020-03-04 15:00:01 +01:00
|
|
|
let idx = *self
|
|
|
|
.eval_expr(scope, idx_expr)?
|
|
|
|
.downcast::<i64>()
|
|
|
|
.map_err(|_| EvalAltResult::ErrorIndexExpr(idx_expr.position()))?;
|
2020-03-01 06:30:22 +01:00
|
|
|
|
2020-03-05 13:28:03 +01:00
|
|
|
let (lhs_value, pos) = match lhs.as_ref() {
|
|
|
|
Expr::Identifier(id, pos) => {
|
|
|
|
let get_fn_name = format!("get${}", id);
|
|
|
|
(
|
|
|
|
self.call_fn_raw(&get_fn_name, vec![this_ptr], None, *pos)?,
|
|
|
|
*pos,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
expr => return Err(EvalAltResult::ErrorDotExpr(expr.position())),
|
|
|
|
};
|
|
|
|
|
|
|
|
Self::get_indexed_value(lhs_value, idx, pos).map(|(v, _)| v)
|
2016-03-26 21:32:20 +01:00
|
|
|
}
|
2020-03-01 17:11:00 +01:00
|
|
|
|
2020-03-04 15:00:01 +01:00
|
|
|
Expr::Dot(inner_lhs, inner_rhs) => match inner_lhs.as_ref() {
|
|
|
|
Expr::Identifier(id, pos) => {
|
|
|
|
let get_fn_name = format!("get${}", id);
|
|
|
|
|
|
|
|
self.call_fn_raw(&get_fn_name, vec![this_ptr], None, *pos)
|
|
|
|
.and_then(|mut v| self.get_dot_val_helper(scope, v.as_mut(), inner_rhs))
|
2020-03-01 06:30:22 +01:00
|
|
|
}
|
2020-03-05 13:28:03 +01:00
|
|
|
Expr::Index(lhs, idx_expr) => {
|
2020-03-04 15:00:01 +01:00
|
|
|
let idx = *self
|
|
|
|
.eval_expr(scope, idx_expr)?
|
|
|
|
.downcast::<i64>()
|
|
|
|
.map_err(|_| EvalAltResult::ErrorIndexExpr(idx_expr.position()))?;
|
|
|
|
|
2020-03-05 13:28:03 +01:00
|
|
|
let (lhs_value, pos) = match lhs.as_ref() {
|
|
|
|
Expr::Identifier(id, pos) => {
|
|
|
|
let get_fn_name = format!("get${}", id);
|
|
|
|
(
|
|
|
|
self.call_fn_raw(&get_fn_name, vec![this_ptr], None, *pos)?,
|
|
|
|
*pos,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
expr => return Err(EvalAltResult::ErrorDotExpr(expr.position())),
|
|
|
|
};
|
|
|
|
|
|
|
|
Self::get_indexed_value(lhs_value, idx, pos).and_then(|(mut value, _)| {
|
|
|
|
self.get_dot_val_helper(scope, value.as_mut(), inner_rhs)
|
2020-03-04 15:00:01 +01:00
|
|
|
})
|
2016-03-10 20:21:36 +01:00
|
|
|
}
|
2020-03-02 15:13:14 +01:00
|
|
|
_ => Err(EvalAltResult::ErrorDotExpr(inner_lhs.position())),
|
2017-12-20 12:16:14 +01:00
|
|
|
},
|
2020-03-02 15:13:14 +01:00
|
|
|
|
|
|
|
_ => Err(EvalAltResult::ErrorDotExpr(dot_rhs.position())),
|
2016-03-07 21:39:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-03 08:20:20 +01:00
|
|
|
fn search_scope<T>(
|
|
|
|
scope: &Scope,
|
2017-12-20 17:37:12 +01:00
|
|
|
id: &str,
|
2020-03-04 15:00:01 +01:00
|
|
|
map: impl FnOnce(Dynamic) -> Result<T, EvalAltResult>,
|
2020-03-02 15:13:14 +01:00
|
|
|
begin: Position,
|
2020-03-01 12:26:57 +01:00
|
|
|
) -> Result<(usize, T), EvalAltResult> {
|
2017-12-20 17:37:12 +01:00
|
|
|
scope
|
2020-03-03 08:20:20 +01:00
|
|
|
.get(id)
|
2020-03-02 15:13:14 +01:00
|
|
|
.ok_or_else(|| EvalAltResult::ErrorVariableNotFound(id.into(), begin))
|
2020-03-04 15:00:01 +01:00
|
|
|
.and_then(move |(idx, _, val)| map(val).map(|v| (idx, v)))
|
2017-12-20 17:37:12 +01:00
|
|
|
}
|
|
|
|
|
2020-03-04 15:00:01 +01:00
|
|
|
fn get_indexed_value(
|
|
|
|
val: Dynamic,
|
|
|
|
idx: i64,
|
|
|
|
pos: Position,
|
2020-03-05 13:28:03 +01:00
|
|
|
) -> Result<(Dynamic, VariableType), EvalAltResult> {
|
2020-03-04 15:00:01 +01:00
|
|
|
if val.is::<Array>() {
|
|
|
|
let arr = val.downcast::<Array>().unwrap();
|
|
|
|
|
|
|
|
if idx >= 0 {
|
|
|
|
arr.get(idx as usize)
|
|
|
|
.cloned()
|
2020-03-05 13:28:03 +01:00
|
|
|
.map(|v| (v, VariableType::Array))
|
2020-03-04 15:00:01 +01:00
|
|
|
.ok_or_else(|| EvalAltResult::ErrorArrayBounds(arr.len(), idx, pos))
|
|
|
|
} else {
|
|
|
|
Err(EvalAltResult::ErrorArrayBounds(arr.len(), idx, pos))
|
|
|
|
}
|
|
|
|
} else if val.is::<String>() {
|
|
|
|
let s = val.downcast::<String>().unwrap();
|
|
|
|
|
|
|
|
if idx >= 0 {
|
|
|
|
s.chars()
|
|
|
|
.nth(idx as usize)
|
2020-03-05 13:28:03 +01:00
|
|
|
.map(|ch| (ch.into_dynamic(), VariableType::String))
|
2020-03-04 15:00:01 +01:00
|
|
|
.ok_or_else(|| EvalAltResult::ErrorStringBounds(s.chars().count(), idx, pos))
|
|
|
|
} else {
|
|
|
|
Err(EvalAltResult::ErrorStringBounds(
|
|
|
|
s.chars().count(),
|
|
|
|
idx,
|
|
|
|
pos,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Err(EvalAltResult::ErrorIndexing(pos))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn eval_index_expr(
|
|
|
|
&mut self,
|
2017-12-20 21:09:53 +01:00
|
|
|
scope: &mut Scope,
|
2020-03-05 13:28:03 +01:00
|
|
|
lhs: &Expr,
|
|
|
|
idx_expr: &Expr,
|
|
|
|
) -> Result<(VariableType, Option<(String, usize)>, usize, Dynamic), EvalAltResult> {
|
2020-03-01 06:30:22 +01:00
|
|
|
let idx = *self
|
2020-03-05 13:28:03 +01:00
|
|
|
.eval_expr(scope, idx_expr)?
|
2017-12-20 17:37:12 +01:00
|
|
|
.downcast::<i64>()
|
2020-03-05 13:28:03 +01:00
|
|
|
.map_err(|_| EvalAltResult::ErrorIndexExpr(idx_expr.position()))?;
|
|
|
|
|
|
|
|
match lhs {
|
|
|
|
Expr::Identifier(id, _) => Self::search_scope(
|
|
|
|
scope,
|
|
|
|
&id,
|
|
|
|
|val| Self::get_indexed_value(val, idx, lhs.position()),
|
|
|
|
lhs.position(),
|
|
|
|
)
|
|
|
|
.map(|(src_idx, (val, source_type))| {
|
|
|
|
(source_type, Some((id.clone(), src_idx)), idx as usize, val)
|
|
|
|
}),
|
|
|
|
|
|
|
|
expr => Self::get_indexed_value(self.eval_expr(scope, expr)?, idx, lhs.position())
|
|
|
|
.map(|(val, _)| (VariableType::Expression, None, idx as usize, val)),
|
|
|
|
}
|
2020-03-01 06:30:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn str_replace_char(s: &mut String, idx: usize, new_ch: char) {
|
|
|
|
// The new character
|
|
|
|
let ch = s.chars().nth(idx).unwrap();
|
|
|
|
|
|
|
|
// See if changed - if so, update the String
|
|
|
|
if ch == new_ch {
|
|
|
|
return;
|
|
|
|
}
|
2017-12-20 17:37:12 +01:00
|
|
|
|
2020-03-01 06:30:22 +01:00
|
|
|
// Collect all the characters after the index
|
|
|
|
let mut chars: Vec<char> = s.chars().collect();
|
|
|
|
chars[idx] = new_ch;
|
2020-03-01 10:37:47 +01:00
|
|
|
s.clear();
|
2020-03-01 06:30:22 +01:00
|
|
|
chars.iter().for_each(|&ch| s.push(ch));
|
2017-12-20 17:37:12 +01:00
|
|
|
}
|
|
|
|
|
2017-12-20 12:16:14 +01:00
|
|
|
fn get_dot_val(
|
2020-03-04 15:00:01 +01:00
|
|
|
&mut self,
|
2017-12-20 12:16:14 +01:00
|
|
|
scope: &mut Scope,
|
|
|
|
dot_lhs: &Expr,
|
|
|
|
dot_rhs: &Expr,
|
2020-02-25 08:02:27 +01:00
|
|
|
) -> Result<Dynamic, EvalAltResult> {
|
2020-02-29 10:10:51 +01:00
|
|
|
match dot_lhs {
|
2020-03-02 15:13:14 +01:00
|
|
|
Expr::Identifier(id, pos) => {
|
2020-03-04 15:00:01 +01:00
|
|
|
let (sc_idx, mut target) = Self::search_scope(scope, id, Ok, *pos)?;
|
2017-12-20 21:52:26 +01:00
|
|
|
let value = self.get_dot_val_helper(scope, target.as_mut(), dot_rhs);
|
2017-12-20 17:37:12 +01:00
|
|
|
|
|
|
|
// In case the expression mutated `target`, we need to reassign it because
|
|
|
|
// of the above `clone`.
|
2020-03-03 08:20:20 +01:00
|
|
|
*scope.get_mut(id, sc_idx) = target;
|
2017-12-20 17:37:12 +01:00
|
|
|
|
|
|
|
value
|
2016-03-07 21:39:02 +01:00
|
|
|
}
|
2020-03-01 17:11:00 +01:00
|
|
|
|
2020-03-05 13:28:03 +01:00
|
|
|
Expr::Index(lhs, idx_expr) => {
|
|
|
|
let (source_type, src, idx, mut target) =
|
|
|
|
self.eval_index_expr(scope, lhs, idx_expr)?;
|
2017-12-20 21:52:26 +01:00
|
|
|
let value = self.get_dot_val_helper(scope, target.as_mut(), dot_rhs);
|
2017-12-20 17:37:12 +01:00
|
|
|
|
|
|
|
// In case the expression mutated `target`, we need to reassign it because
|
|
|
|
// of the above `clone`.
|
2020-03-01 06:30:22 +01:00
|
|
|
|
2020-03-05 13:28:03 +01:00
|
|
|
match source_type {
|
|
|
|
VariableType::Array => {
|
|
|
|
let src = src.unwrap();
|
|
|
|
scope
|
|
|
|
.get_mut(&src.0, src.1)
|
|
|
|
.downcast_mut::<Array>()
|
|
|
|
.unwrap()[idx] = target
|
|
|
|
}
|
|
|
|
|
|
|
|
VariableType::String => {
|
|
|
|
let src = src.unwrap();
|
|
|
|
|
|
|
|
Self::str_replace_char(
|
|
|
|
scope
|
|
|
|
.get_mut(&src.0, src.1)
|
|
|
|
.downcast_mut::<String>()
|
|
|
|
.unwrap(), // Root is a string
|
|
|
|
idx,
|
|
|
|
*target.downcast::<char>().unwrap(), // Target should be a char
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => panic!("source_type must be either Array or String"),
|
2020-03-01 06:30:22 +01:00
|
|
|
}
|
2017-12-20 17:37:12 +01:00
|
|
|
|
|
|
|
value
|
2016-03-26 21:32:20 +01:00
|
|
|
}
|
2020-03-02 15:13:14 +01:00
|
|
|
|
|
|
|
_ => Err(EvalAltResult::ErrorDotExpr(dot_lhs.position())),
|
2016-03-07 21:39:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-20 12:16:14 +01:00
|
|
|
fn set_dot_val_helper(
|
2020-03-04 15:00:01 +01:00
|
|
|
&mut self,
|
2020-02-25 08:02:27 +01:00
|
|
|
this_ptr: &mut Variant,
|
2017-12-20 12:16:14 +01:00
|
|
|
dot_rhs: &Expr,
|
2020-02-25 08:02:27 +01:00
|
|
|
mut source_val: Dynamic,
|
|
|
|
) -> Result<Dynamic, EvalAltResult> {
|
2020-02-29 10:10:51 +01:00
|
|
|
match dot_rhs {
|
2020-03-02 15:13:14 +01:00
|
|
|
Expr::Identifier(id, pos) => {
|
2020-03-04 15:00:01 +01:00
|
|
|
let set_fn_name = format!("set${}", id);
|
2020-03-01 17:11:00 +01:00
|
|
|
|
2020-03-04 15:00:01 +01:00
|
|
|
self.call_fn_raw(
|
|
|
|
&set_fn_name,
|
|
|
|
vec![this_ptr, source_val.as_mut()],
|
|
|
|
None,
|
|
|
|
*pos,
|
|
|
|
)
|
2016-03-07 21:39:02 +01:00
|
|
|
}
|
2020-03-01 17:11:00 +01:00
|
|
|
|
2020-03-04 15:00:01 +01:00
|
|
|
Expr::Dot(inner_lhs, inner_rhs) => match inner_lhs.as_ref() {
|
|
|
|
Expr::Identifier(id, pos) => {
|
|
|
|
let get_fn_name = format!("get${}", id);
|
2020-03-01 17:11:00 +01:00
|
|
|
|
2020-03-04 15:00:01 +01:00
|
|
|
self.call_fn_raw(&get_fn_name, vec![this_ptr], None, *pos)
|
2017-12-20 21:09:53 +01:00
|
|
|
.and_then(|mut v| {
|
2017-12-20 21:52:26 +01:00
|
|
|
self.set_dot_val_helper(v.as_mut(), inner_rhs, source_val)
|
2017-12-20 21:09:53 +01:00
|
|
|
.map(|_| v) // Discard Ok return value
|
|
|
|
})
|
2017-12-20 12:16:14 +01:00
|
|
|
.and_then(|mut v| {
|
2020-03-04 15:00:01 +01:00
|
|
|
let set_fn_name = format!("set${}", id);
|
2017-12-20 12:16:14 +01:00
|
|
|
|
2020-03-04 15:00:01 +01:00
|
|
|
self.call_fn_raw(&set_fn_name, vec![this_ptr, v.as_mut()], None, *pos)
|
2017-12-20 12:16:14 +01:00
|
|
|
})
|
2016-03-10 20:21:36 +01:00
|
|
|
}
|
2020-03-02 15:13:14 +01:00
|
|
|
_ => Err(EvalAltResult::ErrorDotExpr(inner_lhs.position())),
|
2017-12-20 12:16:14 +01:00
|
|
|
},
|
2020-03-02 15:13:14 +01:00
|
|
|
|
|
|
|
_ => Err(EvalAltResult::ErrorDotExpr(dot_rhs.position())),
|
2016-03-07 21:39:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-20 12:16:14 +01:00
|
|
|
fn set_dot_val(
|
2020-03-04 15:00:01 +01:00
|
|
|
&mut self,
|
2017-12-20 12:16:14 +01:00
|
|
|
scope: &mut Scope,
|
|
|
|
dot_lhs: &Expr,
|
|
|
|
dot_rhs: &Expr,
|
2020-02-25 08:02:27 +01:00
|
|
|
source_val: Dynamic,
|
|
|
|
) -> Result<Dynamic, EvalAltResult> {
|
2020-02-29 10:10:51 +01:00
|
|
|
match dot_lhs {
|
2020-03-02 15:13:14 +01:00
|
|
|
Expr::Identifier(id, pos) => {
|
2020-03-04 15:00:01 +01:00
|
|
|
let (sc_idx, mut target) = Self::search_scope(scope, id, Ok, *pos)?;
|
2017-12-20 21:52:26 +01:00
|
|
|
let value = self.set_dot_val_helper(target.as_mut(), dot_rhs, source_val);
|
2016-03-07 21:39:02 +01:00
|
|
|
|
2017-12-20 17:37:12 +01:00
|
|
|
// In case the expression mutated `target`, we need to reassign it because
|
|
|
|
// of the above `clone`.
|
2020-03-03 08:20:20 +01:00
|
|
|
*scope.get_mut(id, sc_idx) = target;
|
2016-03-07 21:39:02 +01:00
|
|
|
|
2017-12-20 17:37:12 +01:00
|
|
|
value
|
2016-03-07 21:39:02 +01:00
|
|
|
}
|
2020-03-01 17:11:00 +01:00
|
|
|
|
2020-03-05 13:28:03 +01:00
|
|
|
Expr::Index(lhs, idx_expr) => {
|
|
|
|
let (source_type, src, idx, mut target) =
|
|
|
|
self.eval_index_expr(scope, lhs, idx_expr)?;
|
2017-12-20 21:52:26 +01:00
|
|
|
let value = self.set_dot_val_helper(target.as_mut(), dot_rhs, source_val);
|
2016-04-14 03:01:08 +02:00
|
|
|
|
2017-12-20 17:37:12 +01:00
|
|
|
// In case the expression mutated `target`, we need to reassign it because
|
|
|
|
// of the above `clone`.
|
2020-03-05 13:28:03 +01:00
|
|
|
|
|
|
|
match source_type {
|
|
|
|
VariableType::Array => {
|
|
|
|
let src = src.unwrap();
|
|
|
|
let val = scope
|
|
|
|
.get_mut(&src.0, src.1)
|
|
|
|
.downcast_mut::<Array>()
|
|
|
|
.unwrap();
|
|
|
|
val[idx] = target
|
|
|
|
}
|
|
|
|
|
|
|
|
VariableType::String => {
|
|
|
|
let src = src.unwrap();
|
|
|
|
|
|
|
|
Self::str_replace_char(
|
|
|
|
scope
|
|
|
|
.get_mut(&src.0, src.1)
|
|
|
|
.downcast_mut::<String>()
|
|
|
|
.unwrap(), // Root is a string
|
|
|
|
idx,
|
|
|
|
*target.downcast::<char>().unwrap(), // Target should be a char
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => panic!("source_type must be either Array or String"),
|
2020-03-01 06:30:22 +01:00
|
|
|
}
|
2016-03-26 21:32:20 +01:00
|
|
|
|
2017-12-20 17:37:12 +01:00
|
|
|
value
|
2016-03-26 21:32:20 +01:00
|
|
|
}
|
2020-03-02 15:13:14 +01:00
|
|
|
|
|
|
|
_ => Err(EvalAltResult::ErrorDotExpr(dot_lhs.position())),
|
2016-03-07 21:39:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-04 15:00:01 +01:00
|
|
|
fn eval_expr(&mut self, scope: &mut Scope, expr: &Expr) -> Result<Dynamic, EvalAltResult> {
|
2020-02-29 10:10:51 +01:00
|
|
|
match expr {
|
2020-03-03 10:28:38 +01:00
|
|
|
Expr::IntegerConstant(i, _) => Ok((*i).into_dynamic()),
|
|
|
|
Expr::FloatConstant(i, _) => Ok((*i).into_dynamic()),
|
|
|
|
Expr::StringConstant(s, _) => Ok(s.into_dynamic()),
|
|
|
|
Expr::CharConstant(c, _) => Ok((*c).into_dynamic()),
|
2020-03-05 13:28:03 +01:00
|
|
|
Expr::Identifier(id, pos) => {
|
|
|
|
Self::search_scope(scope, id, Ok, *pos).map(|(_, val)| val)
|
|
|
|
}
|
|
|
|
Expr::Index(lhs, idx_expr) => self
|
|
|
|
.eval_index_expr(scope, lhs, idx_expr)
|
2020-03-02 15:13:14 +01:00
|
|
|
.map(|(_, _, _, x)| x),
|
2020-03-01 17:11:00 +01:00
|
|
|
|
2020-03-05 13:28:03 +01:00
|
|
|
Expr::Assignment(lhs, rhs) => {
|
2017-10-02 23:51:04 +02:00
|
|
|
let rhs_val = self.eval_expr(scope, rhs)?;
|
2016-03-26 18:46:28 +01:00
|
|
|
|
2020-03-05 13:28:03 +01:00
|
|
|
match lhs.as_ref() {
|
2020-03-04 15:00:01 +01:00
|
|
|
Expr::Identifier(name, pos) => {
|
2020-03-03 09:24:03 +01:00
|
|
|
if let Some((idx, _, _)) = scope.get(name) {
|
|
|
|
*scope.get_mut(name, idx) = rhs_val;
|
2020-03-03 10:28:38 +01:00
|
|
|
Ok(().into_dynamic())
|
2020-03-03 09:24:03 +01:00
|
|
|
} else {
|
2020-03-04 15:00:01 +01:00
|
|
|
Err(EvalAltResult::ErrorVariableNotFound(name.clone(), *pos))
|
2020-03-03 09:24:03 +01:00
|
|
|
}
|
|
|
|
}
|
2020-03-05 13:28:03 +01:00
|
|
|
|
|
|
|
Expr::Index(idx_lhs, idx_expr) => {
|
|
|
|
let (source_type, src, idx, _) =
|
|
|
|
self.eval_index_expr(scope, idx_lhs, idx_expr)?;
|
|
|
|
|
|
|
|
match source_type {
|
|
|
|
VariableType::Array => {
|
|
|
|
let src = src.unwrap();
|
|
|
|
scope
|
|
|
|
.get_mut(&src.0, src.1)
|
|
|
|
.downcast_mut::<Array>()
|
|
|
|
.map(|arr| (arr[idx as usize] = rhs_val).into_dynamic())
|
|
|
|
.ok_or_else(|| {
|
|
|
|
EvalAltResult::ErrorIndexExpr(idx_lhs.position())
|
|
|
|
})
|
2020-03-01 17:11:00 +01:00
|
|
|
}
|
2020-03-05 13:28:03 +01:00
|
|
|
|
|
|
|
VariableType::String => {
|
|
|
|
let src = src.unwrap();
|
|
|
|
scope
|
|
|
|
.get_mut(&src.0, src.1)
|
|
|
|
.downcast_mut::<String>()
|
|
|
|
.map(|s| {
|
|
|
|
Self::str_replace_char(
|
|
|
|
s,
|
|
|
|
idx as usize,
|
|
|
|
*rhs_val.downcast::<char>().unwrap(),
|
|
|
|
)
|
|
|
|
.into_dynamic()
|
|
|
|
})
|
|
|
|
.ok_or_else(|| {
|
|
|
|
EvalAltResult::ErrorIndexExpr(idx_lhs.position())
|
|
|
|
})
|
2020-03-01 17:11:00 +01:00
|
|
|
}
|
2020-03-05 13:28:03 +01:00
|
|
|
|
|
|
|
_ => Err(EvalAltResult::ErrorAssignmentToUnknownLHS(
|
|
|
|
idx_lhs.position(),
|
|
|
|
)),
|
2020-03-01 06:30:22 +01:00
|
|
|
}
|
2016-03-26 18:46:28 +01:00
|
|
|
}
|
2020-03-02 05:08:03 +01:00
|
|
|
|
2020-03-04 15:00:01 +01:00
|
|
|
Expr::Dot(dot_lhs, dot_rhs) => {
|
2016-03-07 21:39:02 +01:00
|
|
|
self.set_dot_val(scope, dot_lhs, dot_rhs, rhs_val)
|
|
|
|
}
|
2020-03-02 05:08:03 +01:00
|
|
|
|
2020-03-05 13:28:03 +01:00
|
|
|
_ => Err(EvalAltResult::ErrorAssignmentToUnknownLHS(lhs.position())),
|
2016-02-29 22:43:45 +01:00
|
|
|
}
|
|
|
|
}
|
2020-03-01 17:11:00 +01:00
|
|
|
|
2020-02-29 10:10:51 +01:00
|
|
|
Expr::Dot(lhs, rhs) => self.get_dot_val(scope, lhs, rhs),
|
2020-03-01 17:11:00 +01:00
|
|
|
|
2020-03-02 15:13:14 +01:00
|
|
|
Expr::Array(contents, _) => {
|
2016-03-26 18:46:28 +01:00
|
|
|
let mut arr = Vec::new();
|
|
|
|
|
2020-03-04 15:00:01 +01:00
|
|
|
contents
|
|
|
|
.iter()
|
|
|
|
.try_for_each::<_, Result<_, EvalAltResult>>(|item| {
|
|
|
|
let arg = self.eval_expr(scope, item)?;
|
|
|
|
arr.push(arg);
|
|
|
|
Ok(())
|
|
|
|
})?;
|
2016-03-26 18:46:28 +01:00
|
|
|
|
|
|
|
Ok(Box::new(arr))
|
|
|
|
}
|
2020-03-01 17:11:00 +01:00
|
|
|
|
2020-03-04 15:00:01 +01:00
|
|
|
Expr::FunctionCall(fn_name, args, def_value, pos) => {
|
|
|
|
let mut args = args
|
|
|
|
.iter()
|
2020-03-02 05:08:03 +01:00
|
|
|
.map(|expr| self.eval_expr(scope, expr))
|
2020-03-04 15:00:01 +01:00
|
|
|
.collect::<Result<Array, _>>()?;
|
|
|
|
|
|
|
|
self.call_fn_raw(
|
|
|
|
fn_name,
|
|
|
|
args.iter_mut().map(|b| b.as_mut()).collect(),
|
|
|
|
def_value.as_ref(),
|
|
|
|
*pos,
|
|
|
|
)
|
|
|
|
}
|
2020-03-01 17:11:00 +01:00
|
|
|
|
2020-03-02 05:08:03 +01:00
|
|
|
Expr::And(lhs, rhs) => Ok(Box::new(
|
|
|
|
*self
|
|
|
|
.eval_expr(scope, &*lhs)?
|
|
|
|
.downcast::<bool>()
|
2020-03-02 15:13:14 +01:00
|
|
|
.map_err(|_| {
|
|
|
|
EvalAltResult::ErrorBooleanArgMismatch("AND".into(), lhs.position())
|
|
|
|
})?
|
2020-03-02 05:08:03 +01:00
|
|
|
&& *self
|
|
|
|
.eval_expr(scope, &*rhs)?
|
|
|
|
.downcast::<bool>()
|
2020-03-02 15:13:14 +01:00
|
|
|
.map_err(|_| {
|
|
|
|
EvalAltResult::ErrorBooleanArgMismatch("AND".into(), rhs.position())
|
|
|
|
})?,
|
2020-03-02 05:08:03 +01:00
|
|
|
)),
|
|
|
|
|
|
|
|
Expr::Or(lhs, rhs) => Ok(Box::new(
|
|
|
|
*self
|
|
|
|
.eval_expr(scope, &*lhs)?
|
|
|
|
.downcast::<bool>()
|
2020-03-02 15:13:14 +01:00
|
|
|
.map_err(|_| {
|
|
|
|
EvalAltResult::ErrorBooleanArgMismatch("OR".into(), lhs.position())
|
|
|
|
})?
|
2020-03-02 05:08:03 +01:00
|
|
|
|| *self
|
|
|
|
.eval_expr(scope, &*rhs)?
|
|
|
|
.downcast::<bool>()
|
2020-03-02 15:13:14 +01:00
|
|
|
.map_err(|_| {
|
|
|
|
EvalAltResult::ErrorBooleanArgMismatch("OR".into(), rhs.position())
|
|
|
|
})?,
|
2020-03-02 05:08:03 +01:00
|
|
|
)),
|
|
|
|
|
2020-03-03 10:28:38 +01:00
|
|
|
Expr::True(_) => Ok(true.into_dynamic()),
|
|
|
|
Expr::False(_) => Ok(false.into_dynamic()),
|
|
|
|
Expr::Unit(_) => Ok(().into_dynamic()),
|
2016-02-29 22:43:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-02 07:28:42 +01:00
|
|
|
pub(crate) fn eval_stmt(
|
2020-03-04 15:00:01 +01:00
|
|
|
&mut self,
|
2020-03-02 07:28:42 +01:00
|
|
|
scope: &mut Scope,
|
|
|
|
stmt: &Stmt,
|
|
|
|
) -> Result<Dynamic, EvalAltResult> {
|
2020-02-29 10:10:51 +01:00
|
|
|
match stmt {
|
2020-03-01 06:30:22 +01:00
|
|
|
Stmt::Expr(expr) => self.eval_expr(scope, expr),
|
2020-03-01 17:11:00 +01:00
|
|
|
|
2020-03-01 06:30:22 +01:00
|
|
|
Stmt::Block(block) => {
|
2016-03-02 02:45:02 +01:00
|
|
|
let prev_len = scope.len();
|
2020-03-03 10:28:38 +01:00
|
|
|
let mut last_result: Result<Dynamic, EvalAltResult> = Ok(().into_dynamic());
|
2016-02-29 22:43:45 +01:00
|
|
|
|
2020-03-01 06:30:22 +01:00
|
|
|
for block_stmt in block.iter() {
|
|
|
|
last_result = self.eval_stmt(scope, block_stmt);
|
2020-03-01 17:11:00 +01:00
|
|
|
|
2017-10-02 23:44:45 +02:00
|
|
|
if let Err(x) = last_result {
|
|
|
|
last_result = Err(x);
|
|
|
|
break;
|
2016-03-03 15:31:42 +01:00
|
|
|
}
|
2016-02-29 22:43:45 +01:00
|
|
|
}
|
|
|
|
|
2016-03-02 02:45:02 +01:00
|
|
|
while scope.len() > prev_len {
|
|
|
|
scope.pop();
|
2016-02-29 22:43:45 +01:00
|
|
|
}
|
|
|
|
|
2017-10-02 23:44:45 +02:00
|
|
|
last_result
|
2016-02-29 22:43:45 +01:00
|
|
|
}
|
2020-03-01 17:11:00 +01:00
|
|
|
|
2020-03-01 06:30:22 +01:00
|
|
|
Stmt::IfElse(guard, body, else_body) => self
|
|
|
|
.eval_expr(scope, guard)?
|
|
|
|
.downcast::<bool>()
|
2020-03-02 15:13:14 +01:00
|
|
|
.map_err(|_| EvalAltResult::ErrorIfGuard(guard.position()))
|
2020-03-01 06:30:22 +01:00
|
|
|
.and_then(|guard_val| {
|
|
|
|
if *guard_val {
|
|
|
|
self.eval_stmt(scope, body)
|
2020-03-02 10:04:56 +01:00
|
|
|
} else if else_body.is_some() {
|
|
|
|
self.eval_stmt(scope, else_body.as_ref().unwrap())
|
2020-03-01 06:30:22 +01:00
|
|
|
} else {
|
2020-03-03 10:28:38 +01:00
|
|
|
Ok(().into_dynamic())
|
2016-03-03 14:20:55 +01:00
|
|
|
}
|
2020-03-01 06:30:22 +01:00
|
|
|
}),
|
2020-03-01 17:11:00 +01:00
|
|
|
|
2020-02-29 10:10:51 +01:00
|
|
|
Stmt::While(guard, body) => loop {
|
2020-03-01 06:30:22 +01:00
|
|
|
match self.eval_expr(scope, guard)?.downcast::<bool>() {
|
|
|
|
Ok(guard_val) => {
|
|
|
|
if *guard_val {
|
2017-12-20 12:16:14 +01:00
|
|
|
match self.eval_stmt(scope, body) {
|
2020-03-03 10:28:38 +01:00
|
|
|
Err(EvalAltResult::LoopBreak) => return Ok(().into_dynamic()),
|
2017-12-21 12:28:59 +01:00
|
|
|
Err(x) => return Err(x),
|
2017-12-20 12:16:14 +01:00
|
|
|
_ => (),
|
2016-02-29 22:43:45 +01:00
|
|
|
}
|
2017-12-20 12:16:14 +01:00
|
|
|
} else {
|
2020-03-03 10:28:38 +01:00
|
|
|
return Ok(().into_dynamic());
|
2016-02-29 22:43:45 +01:00
|
|
|
}
|
2016-04-14 03:01:08 +02:00
|
|
|
}
|
2020-03-02 15:13:14 +01:00
|
|
|
Err(_) => return Err(EvalAltResult::ErrorIfGuard(guard.position())),
|
2016-02-29 22:43:45 +01:00
|
|
|
}
|
2017-12-20 12:16:14 +01:00
|
|
|
},
|
2020-03-01 17:11:00 +01:00
|
|
|
|
2020-02-29 10:10:51 +01:00
|
|
|
Stmt::Loop(body) => loop {
|
2017-12-20 12:16:14 +01:00
|
|
|
match self.eval_stmt(scope, body) {
|
2020-03-03 10:28:38 +01:00
|
|
|
Err(EvalAltResult::LoopBreak) => return Ok(().into_dynamic()),
|
2017-12-21 12:28:59 +01:00
|
|
|
Err(x) => return Err(x),
|
2017-12-20 12:16:14 +01:00
|
|
|
_ => (),
|
2017-10-30 16:08:44 +01:00
|
|
|
}
|
2017-12-20 12:16:14 +01:00
|
|
|
},
|
2020-03-01 17:11:00 +01:00
|
|
|
|
2020-02-29 10:10:51 +01:00
|
|
|
Stmt::For(name, expr, body) => {
|
2020-02-23 15:48:46 +01:00
|
|
|
let arr = self.eval_expr(scope, expr)?;
|
|
|
|
let tid = Any::type_id(&*arr);
|
2020-03-01 17:11:00 +01:00
|
|
|
|
2020-02-23 15:48:46 +01:00
|
|
|
if let Some(iter_fn) = self.type_iterators.get(&tid) {
|
2020-03-03 08:20:20 +01:00
|
|
|
scope.push(name.clone(), ());
|
2020-02-23 15:48:46 +01:00
|
|
|
let idx = scope.len() - 1;
|
2020-03-01 17:11:00 +01:00
|
|
|
|
2020-02-23 15:48:46 +01:00
|
|
|
for a in iter_fn(&arr) {
|
2020-03-03 08:20:20 +01:00
|
|
|
*scope.get_mut(name, idx) = a;
|
2020-03-01 17:11:00 +01:00
|
|
|
|
2020-02-23 15:48:46 +01:00
|
|
|
match self.eval_stmt(scope, body) {
|
|
|
|
Err(EvalAltResult::LoopBreak) => break,
|
|
|
|
Err(x) => return Err(x),
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
2020-03-03 08:20:20 +01:00
|
|
|
scope.pop();
|
2020-03-03 10:28:38 +01:00
|
|
|
Ok(().into_dynamic())
|
2020-02-23 15:48:46 +01:00
|
|
|
} else {
|
2020-03-02 15:13:14 +01:00
|
|
|
return Err(EvalAltResult::ErrorFor(expr.position()));
|
2020-02-23 15:48:46 +01:00
|
|
|
}
|
|
|
|
}
|
2020-03-01 17:11:00 +01:00
|
|
|
|
2020-03-02 15:13:14 +01:00
|
|
|
Stmt::Break(_) => Err(EvalAltResult::LoopBreak),
|
2020-03-01 17:11:00 +01:00
|
|
|
|
2020-03-03 11:15:20 +01:00
|
|
|
// Empty return
|
|
|
|
Stmt::ReturnWithVal(None, true, pos) => {
|
|
|
|
Err(EvalAltResult::Return(().into_dynamic(), *pos))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return value
|
|
|
|
Stmt::ReturnWithVal(Some(a), true, pos) => {
|
|
|
|
Err(EvalAltResult::Return(self.eval_expr(scope, a)?, *pos))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Empty throw
|
|
|
|
Stmt::ReturnWithVal(None, false, pos) => {
|
|
|
|
Err(EvalAltResult::ErrorRuntime("".into(), *pos))
|
|
|
|
}
|
2020-03-01 17:11:00 +01:00
|
|
|
|
2020-03-03 11:15:20 +01:00
|
|
|
// Throw value
|
|
|
|
Stmt::ReturnWithVal(Some(a), false, pos) => {
|
|
|
|
let val = self.eval_expr(scope, a)?;
|
|
|
|
Err(EvalAltResult::ErrorRuntime(
|
2020-03-04 15:00:01 +01:00
|
|
|
val.downcast::<String>()
|
|
|
|
.map(|s| *s)
|
|
|
|
.unwrap_or("".to_string()),
|
2020-03-03 11:15:20 +01:00
|
|
|
*pos,
|
|
|
|
))
|
2016-03-03 15:31:42 +01:00
|
|
|
}
|
2020-03-01 17:11:00 +01:00
|
|
|
|
2020-03-02 15:13:14 +01:00
|
|
|
Stmt::Let(name, init, _) => {
|
2020-03-01 06:30:22 +01:00
|
|
|
if let Some(v) = init {
|
2020-03-03 08:20:20 +01:00
|
|
|
let val = self.eval_expr(scope, v)?;
|
|
|
|
scope.push_dynamic(name.clone(), val);
|
2020-03-01 06:30:22 +01:00
|
|
|
} else {
|
2020-03-03 08:20:20 +01:00
|
|
|
scope.push(name.clone(), ());
|
2020-03-01 06:30:22 +01:00
|
|
|
}
|
2020-03-03 10:28:38 +01:00
|
|
|
Ok(().into_dynamic())
|
2016-02-29 22:43:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-03 09:24:03 +01:00
|
|
|
pub(crate) fn map_type_name<'a>(&'a self, name: &'a str) -> &'a str {
|
2020-03-02 16:16:19 +01:00
|
|
|
self.type_names
|
2020-03-03 09:24:03 +01:00
|
|
|
.get(name)
|
|
|
|
.map(|s| s.as_str())
|
|
|
|
.unwrap_or(name)
|
2020-03-02 16:16:19 +01:00
|
|
|
}
|
|
|
|
|
2017-10-30 16:08:44 +01:00
|
|
|
/// Make a new engine
|
2020-03-04 15:00:01 +01:00
|
|
|
pub fn new<'a>() -> Engine<'a> {
|
|
|
|
use std::any::type_name;
|
|
|
|
|
2020-03-02 16:16:19 +01:00
|
|
|
// User-friendly names for built-in types
|
|
|
|
let type_names = [
|
2020-03-04 15:00:01 +01:00
|
|
|
(type_name::<String>(), "string"),
|
|
|
|
(type_name::<Array>(), "array"),
|
|
|
|
(type_name::<Dynamic>(), "dynamic"),
|
2020-03-02 16:16:19 +01:00
|
|
|
]
|
|
|
|
.iter()
|
|
|
|
.map(|(k, v)| (k.to_string(), v.to_string()))
|
|
|
|
.collect();
|
|
|
|
|
2020-03-04 15:00:01 +01:00
|
|
|
// Create the new scripting Engine
|
2017-12-20 12:16:14 +01:00
|
|
|
let mut engine = Engine {
|
2020-03-04 16:06:05 +01:00
|
|
|
external_functions: HashMap::new(),
|
|
|
|
script_functions: HashMap::new(),
|
2020-02-23 15:48:46 +01:00
|
|
|
type_iterators: HashMap::new(),
|
2020-03-02 16:16:19 +01:00
|
|
|
type_names,
|
2020-03-04 16:44:32 +01:00
|
|
|
on_print: Box::new(default_print), // default print/debug implementations
|
|
|
|
on_debug: Box::new(default_print),
|
2017-12-20 12:16:14 +01:00
|
|
|
};
|
2016-03-01 15:40:48 +01:00
|
|
|
|
2020-03-04 16:44:32 +01:00
|
|
|
engine.register_core_lib();
|
|
|
|
|
2020-03-05 03:18:46 +01:00
|
|
|
#[cfg(not(feature = "no_stdlib"))]
|
|
|
|
engine.register_stdlib(); // Register the standard library when no_stdlib is not set
|
2016-03-01 15:40:48 +01:00
|
|
|
|
|
|
|
engine
|
|
|
|
}
|
|
|
|
}
|
2020-03-04 16:44:32 +01:00
|
|
|
|
|
|
|
/// Print/debug to stdout
|
2020-03-05 03:18:46 +01:00
|
|
|
#[cfg(not(feature = "no_stdlib"))]
|
2020-03-04 16:44:32 +01:00
|
|
|
fn default_print(s: &str) {
|
|
|
|
println!("{}", s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// No-op
|
2020-03-05 03:18:46 +01:00
|
|
|
#[cfg(feature = "no_stdlib")]
|
2020-03-04 16:44:32 +01:00
|
|
|
fn default_print(_: &str) {}
|