2016-02-29 22:43:45 +01:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::error::Error;
|
|
|
|
use std::any::Any;
|
|
|
|
use std::boxed::Box;
|
|
|
|
use std::fmt;
|
|
|
|
|
2016-04-17 04:32:18 +02:00
|
|
|
use parser::{lex, parse, Expr, Stmt, FnDef};
|
2016-02-29 22:43:45 +01:00
|
|
|
use fn_register::FnRegister;
|
2016-03-01 15:40:48 +01:00
|
|
|
|
|
|
|
use std::ops::{Add, Sub, Mul, Div};
|
|
|
|
use std::cmp::{Ord, Eq};
|
2016-02-29 22:43:45 +01:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
2016-03-03 15:31:42 +01:00
|
|
|
pub enum EvalAltResult {
|
|
|
|
ErrorFunctionNotFound,
|
|
|
|
ErrorFunctionArgMismatch,
|
|
|
|
ErrorFunctionCallNotSupported,
|
2016-03-26 18:46:28 +01:00
|
|
|
ErrorIndexMismatch,
|
2016-03-03 15:31:42 +01:00
|
|
|
ErrorIfGuardMismatch,
|
2016-03-10 20:21:36 +01:00
|
|
|
ErrorVariableNotFound(String),
|
2016-03-03 15:31:42 +01:00
|
|
|
ErrorFunctionArityNotSupported,
|
2016-03-10 20:21:36 +01:00
|
|
|
ErrorAssignmentToUnknownLHS,
|
2016-03-16 23:32:05 +01:00
|
|
|
ErrorMismatchOutputType,
|
2016-03-17 13:45:48 +01:00
|
|
|
ErrorCantOpenScriptFile,
|
2016-04-14 03:01:08 +02:00
|
|
|
InternalErrorMalformedDotExpression,
|
2016-03-03 15:31:42 +01:00
|
|
|
LoopBreak,
|
2016-04-17 04:32:18 +02:00
|
|
|
Return(Box<Any>),
|
2016-02-29 22:43:45 +01:00
|
|
|
}
|
|
|
|
|
2016-03-26 18:46:28 +01:00
|
|
|
|
2016-03-03 15:31:42 +01:00
|
|
|
impl Error for EvalAltResult {
|
2016-02-29 22:43:45 +01:00
|
|
|
fn description(&self) -> &str {
|
|
|
|
match *self {
|
2016-03-03 15:31:42 +01:00
|
|
|
EvalAltResult::ErrorFunctionNotFound => "Function not found",
|
|
|
|
EvalAltResult::ErrorFunctionArgMismatch => "Function argument types do not match",
|
2016-04-17 04:32:18 +02:00
|
|
|
EvalAltResult::ErrorFunctionCallNotSupported => {
|
|
|
|
"Function call with > 2 argument not supported"
|
|
|
|
}
|
2016-03-26 18:46:28 +01:00
|
|
|
EvalAltResult::ErrorIndexMismatch => "Index does not match array",
|
2016-03-03 15:31:42 +01:00
|
|
|
EvalAltResult::ErrorIfGuardMismatch => "If guards expect boolean expression",
|
2016-03-10 22:19:56 +01:00
|
|
|
EvalAltResult::ErrorVariableNotFound(_) => "Variable not found",
|
2016-04-17 04:32:18 +02:00
|
|
|
EvalAltResult::ErrorFunctionArityNotSupported => {
|
|
|
|
"Functions of more than 3 parameters are not yet supported"
|
|
|
|
}
|
|
|
|
EvalAltResult::ErrorAssignmentToUnknownLHS => {
|
|
|
|
"Assignment to an unsupported left-hand side"
|
|
|
|
}
|
2016-03-16 23:32:05 +01:00
|
|
|
EvalAltResult::ErrorMismatchOutputType => "Cast of output failed",
|
2016-03-17 13:45:48 +01:00
|
|
|
EvalAltResult::ErrorCantOpenScriptFile => "Cannot open script file",
|
2016-04-17 04:32:18 +02:00
|
|
|
EvalAltResult::InternalErrorMalformedDotExpression => {
|
|
|
|
"[Internal error] Unexpected expression in dot expression"
|
|
|
|
}
|
2016-03-03 15:31:42 +01:00
|
|
|
EvalAltResult::LoopBreak => "Loop broken before completion (not an error)",
|
2016-04-17 04:32:18 +02:00
|
|
|
EvalAltResult::Return(_) => "Function returned value (not an error)",
|
2016-02-29 22:43:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cause(&self) -> Option<&Error> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-03 15:31:42 +01:00
|
|
|
impl fmt::Display for EvalAltResult {
|
2016-02-29 22:43:45 +01:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{}", self.description())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-02 20:14:22 +01:00
|
|
|
pub enum FnType {
|
2016-04-17 04:32:18 +02:00
|
|
|
ExternalFn0(Box<Fn() -> Result<Box<Any>, EvalAltResult>>),
|
|
|
|
ExternalFn1(Box<Fn(&mut Box<Any>) -> Result<Box<Any>, EvalAltResult>>),
|
|
|
|
ExternalFn2(Box<Fn(&mut Box<Any>, &mut Box<Any>) -> Result<Box<Any>, EvalAltResult>>),
|
|
|
|
ExternalFn3(Box<Fn(&mut Box<Any>, &mut Box<Any>, &mut Box<Any>)
|
|
|
|
-> Result<Box<Any>, EvalAltResult>>),
|
|
|
|
ExternalFn4(Box<Fn(&mut Box<Any>, &mut Box<Any>, &mut Box<Any>, &mut Box<Any>)
|
|
|
|
-> Result<Box<Any>, EvalAltResult>>),
|
|
|
|
ExternalFn5(Box<Fn(&mut Box<Any>,
|
|
|
|
&mut Box<Any>,
|
|
|
|
&mut Box<Any>,
|
|
|
|
&mut Box<Any>,
|
|
|
|
&mut Box<Any>)
|
|
|
|
-> Result<Box<Any>, EvalAltResult>>),
|
|
|
|
ExternalFn6(Box<Fn(&mut Box<Any>,
|
|
|
|
&mut Box<Any>,
|
|
|
|
&mut Box<Any>,
|
|
|
|
&mut Box<Any>,
|
|
|
|
&mut Box<Any>,
|
|
|
|
&mut Box<Any>)
|
|
|
|
-> Result<Box<Any>, EvalAltResult>>),
|
|
|
|
|
|
|
|
InternalFn(FnDef),
|
2016-03-02 02:45:02 +01:00
|
|
|
}
|
|
|
|
|
2016-02-29 22:43:45 +01:00
|
|
|
pub struct Engine {
|
2016-04-17 04:32:18 +02:00
|
|
|
pub fns: HashMap<String, Vec<FnType>>,
|
2016-02-29 22:43:45 +01:00
|
|
|
}
|
|
|
|
|
2016-03-02 02:45:02 +01:00
|
|
|
pub type Scope = Vec<(String, Box<Any>)>;
|
|
|
|
|
2016-02-29 22:43:45 +01:00
|
|
|
impl Engine {
|
2016-04-17 04:32:18 +02:00
|
|
|
fn call_fn(&self,
|
|
|
|
name: &str,
|
|
|
|
arg1: Option<&mut Box<Any>>,
|
|
|
|
arg2: Option<&mut Box<Any>>,
|
|
|
|
arg3: Option<&mut Box<Any>>,
|
|
|
|
arg4: Option<&mut Box<Any>>,
|
|
|
|
arg5: Option<&mut Box<Any>>,
|
|
|
|
arg6: Option<&mut Box<Any>>)
|
|
|
|
-> Result<Box<Any>, EvalAltResult> {
|
2016-03-02 20:14:22 +01:00
|
|
|
|
|
|
|
match self.fns.get(name) {
|
2017-10-02 23:44:45 +02:00
|
|
|
Some(vf) => {
|
2016-03-02 20:14:22 +01:00
|
|
|
match (arg1, arg2, arg3, arg4, arg5, arg6) {
|
2016-04-17 04:32:18 +02:00
|
|
|
(Some(ref mut a1),
|
|
|
|
Some(ref mut a2),
|
|
|
|
Some(ref mut a3),
|
|
|
|
Some(ref mut a4),
|
|
|
|
Some(ref mut a5),
|
|
|
|
Some(ref mut a6)) => {
|
2017-10-02 23:44:45 +02:00
|
|
|
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);
|
2016-03-02 20:14:22 +01:00
|
|
|
}
|
|
|
|
}
|
2017-10-02 23:44:45 +02:00
|
|
|
FnType::InternalFn(ref f) => {
|
2016-04-17 04:32:18 +02:00
|
|
|
if f.params.len() != 6 {
|
|
|
|
return Err(EvalAltResult::ErrorFunctionArgMismatch);
|
|
|
|
}
|
2016-03-03 15:31:42 +01:00
|
|
|
|
2016-03-02 20:14:22 +01:00
|
|
|
let mut new_scope: Scope = Vec::new();
|
2016-04-17 04:32:18 +02:00
|
|
|
let result1 = self.call_fn("clone",
|
|
|
|
Some(a1),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None);
|
|
|
|
let result2 = self.call_fn("clone",
|
|
|
|
Some(a2),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None);
|
|
|
|
let result3 = self.call_fn("clone",
|
|
|
|
Some(a3),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None);
|
|
|
|
let result4 = self.call_fn("clone",
|
|
|
|
Some(a4),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None);
|
|
|
|
let result5 = self.call_fn("clone",
|
|
|
|
Some(a5),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None);
|
|
|
|
let result6 = self.call_fn("clone",
|
|
|
|
Some(a6),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None);
|
2016-03-02 20:14:22 +01:00
|
|
|
|
|
|
|
match (result1, result2, result3, result4, result5, result6) {
|
|
|
|
(Ok(r1), Ok(r2), Ok(r3), Ok(r4), Ok(r5), Ok(r6)) => {
|
|
|
|
new_scope.push((f.params[0].clone(), r1));
|
|
|
|
new_scope.push((f.params[1].clone(), r2));
|
|
|
|
new_scope.push((f.params[2].clone(), r3));
|
|
|
|
new_scope.push((f.params[3].clone(), r4));
|
|
|
|
new_scope.push((f.params[4].clone(), r5));
|
|
|
|
new_scope.push((f.params[5].clone(), r6));
|
2016-04-17 04:32:18 +02:00
|
|
|
}
|
|
|
|
_ => return Err(EvalAltResult::ErrorFunctionArgMismatch),
|
2016-03-03 15:31:42 +01:00
|
|
|
}
|
|
|
|
match self.eval_stmt(&mut new_scope, &*f.body) {
|
|
|
|
Err(EvalAltResult::Return(x)) => return Ok(x),
|
2016-04-17 04:32:18 +02:00
|
|
|
x => return x,
|
2016-03-03 15:31:42 +01:00
|
|
|
}
|
2016-03-02 20:14:22 +01:00
|
|
|
}
|
2016-04-17 04:32:18 +02:00
|
|
|
_ => (),
|
2016-03-02 20:14:22 +01:00
|
|
|
}
|
2016-03-02 02:45:02 +01:00
|
|
|
}
|
2017-10-02 23:44:45 +02:00
|
|
|
Err(EvalAltResult::ErrorFunctionArgMismatch)
|
2016-02-29 22:43:45 +01:00
|
|
|
}
|
2016-04-17 04:32:18 +02:00
|
|
|
(Some(ref mut a1),
|
|
|
|
Some(ref mut a2),
|
|
|
|
Some(ref mut a3),
|
|
|
|
Some(ref mut a4),
|
|
|
|
Some(ref mut a5),
|
|
|
|
None) => {
|
2017-10-02 23:44:45 +02:00
|
|
|
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);
|
2016-03-02 20:14:22 +01:00
|
|
|
}
|
|
|
|
}
|
2017-10-02 23:44:45 +02:00
|
|
|
FnType::InternalFn(ref f) => {
|
2016-04-17 04:32:18 +02:00
|
|
|
if f.params.len() != 5 {
|
|
|
|
return Err(EvalAltResult::ErrorFunctionArgMismatch);
|
|
|
|
}
|
2016-03-03 15:31:42 +01:00
|
|
|
|
2016-03-02 20:14:22 +01:00
|
|
|
let mut new_scope: Scope = Vec::new();
|
2016-04-17 04:32:18 +02:00
|
|
|
let result1 = self.call_fn("clone",
|
|
|
|
Some(a1),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None);
|
|
|
|
let result2 = self.call_fn("clone",
|
|
|
|
Some(a2),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None);
|
|
|
|
let result3 = self.call_fn("clone",
|
|
|
|
Some(a3),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None);
|
|
|
|
let result4 = self.call_fn("clone",
|
|
|
|
Some(a4),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None);
|
|
|
|
let result5 = self.call_fn("clone",
|
|
|
|
Some(a5),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None);
|
2016-03-03 14:20:55 +01:00
|
|
|
|
2016-03-02 20:14:22 +01:00
|
|
|
match (result1, result2, result3, result4, result5) {
|
|
|
|
(Ok(r1), Ok(r2), Ok(r3), Ok(r4), Ok(r5)) => {
|
|
|
|
new_scope.push((f.params[0].clone(), r1));
|
|
|
|
new_scope.push((f.params[1].clone(), r2));
|
|
|
|
new_scope.push((f.params[2].clone(), r3));
|
|
|
|
new_scope.push((f.params[3].clone(), r4));
|
|
|
|
new_scope.push((f.params[4].clone(), r5));
|
2016-04-17 04:32:18 +02:00
|
|
|
}
|
|
|
|
_ => return Err(EvalAltResult::ErrorFunctionArgMismatch),
|
2016-04-14 03:01:08 +02:00
|
|
|
}
|
2016-03-03 15:31:42 +01:00
|
|
|
match self.eval_stmt(&mut new_scope, &*f.body) {
|
|
|
|
Err(EvalAltResult::Return(x)) => return Ok(x),
|
2016-04-17 04:32:18 +02:00
|
|
|
x => return x,
|
2016-03-03 15:31:42 +01:00
|
|
|
}
|
2016-03-02 20:14:22 +01:00
|
|
|
}
|
2016-04-17 04:32:18 +02:00
|
|
|
_ => (),
|
2016-03-02 02:45:02 +01:00
|
|
|
}
|
|
|
|
}
|
2017-10-02 23:44:45 +02:00
|
|
|
Err(EvalAltResult::ErrorFunctionArgMismatch)
|
2016-02-29 22:43:45 +01:00
|
|
|
}
|
2016-04-17 04:32:18 +02:00
|
|
|
(Some(ref mut a1),
|
|
|
|
Some(ref mut a2),
|
|
|
|
Some(ref mut a3),
|
|
|
|
Some(ref mut a4),
|
|
|
|
None,
|
|
|
|
None) => {
|
2017-10-02 23:44:45 +02:00
|
|
|
for arr_f in vf {
|
|
|
|
match *arr_f {
|
|
|
|
FnType::ExternalFn4(ref f) => {
|
|
|
|
if let Ok(v) = f(*a1, *a2, *a3, *a4) {
|
|
|
|
return Ok(v)
|
2016-03-02 20:14:22 +01:00
|
|
|
}
|
|
|
|
}
|
2017-10-02 23:44:45 +02:00
|
|
|
FnType::InternalFn(ref f) => {
|
2016-04-17 04:32:18 +02:00
|
|
|
if f.params.len() != 4 {
|
|
|
|
return Err(EvalAltResult::ErrorFunctionArgMismatch);
|
|
|
|
}
|
2016-03-03 15:31:42 +01:00
|
|
|
|
2016-03-02 20:14:22 +01:00
|
|
|
let mut new_scope: Scope = Vec::new();
|
2016-04-17 04:32:18 +02:00
|
|
|
let result1 = self.call_fn("clone",
|
|
|
|
Some(a1),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None);
|
|
|
|
let result2 = self.call_fn("clone",
|
|
|
|
Some(a2),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None);
|
|
|
|
let result3 = self.call_fn("clone",
|
|
|
|
Some(a3),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None);
|
|
|
|
let result4 = self.call_fn("clone",
|
|
|
|
Some(a4),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None);
|
2016-03-02 20:14:22 +01:00
|
|
|
match (result1, result2, result3, result4) {
|
|
|
|
(Ok(r1), Ok(r2), Ok(r3), Ok(r4)) => {
|
|
|
|
new_scope.push((f.params[0].clone(), r1));
|
|
|
|
new_scope.push((f.params[1].clone(), r2));
|
|
|
|
new_scope.push((f.params[2].clone(), r3));
|
|
|
|
new_scope.push((f.params[3].clone(), r4));
|
2016-04-17 04:32:18 +02:00
|
|
|
}
|
|
|
|
_ => return Err(EvalAltResult::ErrorFunctionArgMismatch),
|
2016-04-14 03:01:08 +02:00
|
|
|
}
|
2016-03-03 15:31:42 +01:00
|
|
|
match self.eval_stmt(&mut new_scope, &*f.body) {
|
|
|
|
Err(EvalAltResult::Return(x)) => return Ok(x),
|
2016-04-17 04:32:18 +02:00
|
|
|
x => return x,
|
2016-03-03 15:31:42 +01:00
|
|
|
}
|
2016-03-02 20:14:22 +01:00
|
|
|
}
|
2016-04-17 04:32:18 +02:00
|
|
|
_ => (),
|
2016-03-02 02:45:02 +01:00
|
|
|
}
|
|
|
|
}
|
2017-10-02 23:44:45 +02:00
|
|
|
Err(EvalAltResult::ErrorFunctionArgMismatch)
|
2016-03-01 15:40:48 +01:00
|
|
|
}
|
2016-03-02 20:14:22 +01:00
|
|
|
(Some(ref mut a1), Some(ref mut a2), Some(ref mut a3), None, None, None) => {
|
2017-10-02 23:44:45 +02:00
|
|
|
for arr_f in vf {
|
|
|
|
match *arr_f {
|
|
|
|
FnType::ExternalFn3(ref f) => {
|
|
|
|
if let Ok(v) = f(*a1, *a2, *a3) {
|
|
|
|
return Ok(v);
|
2016-03-02 20:14:22 +01:00
|
|
|
}
|
|
|
|
}
|
2017-10-02 23:44:45 +02:00
|
|
|
FnType::InternalFn(ref f) => {
|
2016-04-17 04:32:18 +02:00
|
|
|
if f.params.len() != 3 {
|
|
|
|
return Err(EvalAltResult::ErrorFunctionArgMismatch);
|
|
|
|
}
|
2016-03-03 15:31:42 +01:00
|
|
|
|
2016-03-02 20:14:22 +01:00
|
|
|
let mut new_scope: Scope = Vec::new();
|
2016-04-17 04:32:18 +02:00
|
|
|
let result1 = self.call_fn("clone",
|
|
|
|
Some(a1),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None);
|
|
|
|
let result2 = self.call_fn("clone",
|
|
|
|
Some(a2),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None);
|
|
|
|
let result3 = self.call_fn("clone",
|
|
|
|
Some(a3),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None);
|
2016-03-02 20:14:22 +01:00
|
|
|
match (result1, result2, result3) {
|
|
|
|
(Ok(r1), Ok(r2), Ok(r3)) => {
|
|
|
|
new_scope.push((f.params[0].clone(), r1));
|
|
|
|
new_scope.push((f.params[1].clone(), r2));
|
|
|
|
new_scope.push((f.params[2].clone(), r3));
|
2016-04-17 04:32:18 +02:00
|
|
|
}
|
|
|
|
_ => return Err(EvalAltResult::ErrorFunctionArgMismatch),
|
2016-04-14 03:01:08 +02:00
|
|
|
}
|
2016-03-03 15:31:42 +01:00
|
|
|
match self.eval_stmt(&mut new_scope, &*f.body) {
|
|
|
|
Err(EvalAltResult::Return(x)) => return Ok(x),
|
2016-04-17 04:32:18 +02:00
|
|
|
x => return x,
|
2016-03-03 15:31:42 +01:00
|
|
|
}
|
2016-03-02 20:14:22 +01:00
|
|
|
}
|
2016-04-17 04:32:18 +02:00
|
|
|
_ => (),
|
2016-03-02 02:45:02 +01:00
|
|
|
}
|
|
|
|
}
|
2017-10-02 23:44:45 +02:00
|
|
|
Err(EvalAltResult::ErrorFunctionArgMismatch)
|
2016-03-01 21:16:10 +01:00
|
|
|
}
|
2016-03-02 20:14:22 +01:00
|
|
|
(Some(ref mut a1), Some(ref mut a2), None, None, None, None) => {
|
2017-10-02 23:44:45 +02:00
|
|
|
for arr_f in vf {
|
|
|
|
match *arr_f {
|
|
|
|
FnType::ExternalFn2(ref f) => {
|
|
|
|
if let Ok(v) = f(*a1, *a2) {
|
|
|
|
return Ok(v);
|
2016-03-02 20:14:22 +01:00
|
|
|
}
|
|
|
|
}
|
2017-10-02 23:44:45 +02:00
|
|
|
FnType::InternalFn(ref f) => {
|
2016-04-17 04:32:18 +02:00
|
|
|
if f.params.len() != 2 {
|
|
|
|
return Err(EvalAltResult::ErrorFunctionArgMismatch);
|
|
|
|
}
|
2016-03-03 15:31:42 +01:00
|
|
|
|
2016-03-02 20:14:22 +01:00
|
|
|
let mut new_scope: Scope = Vec::new();
|
2016-04-17 04:32:18 +02:00
|
|
|
let result1 = self.call_fn("clone",
|
|
|
|
Some(a1),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None);
|
|
|
|
let result2 = self.call_fn("clone",
|
|
|
|
Some(a2),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None);
|
2016-03-02 20:14:22 +01:00
|
|
|
match (result1, result2) {
|
|
|
|
(Ok(r1), Ok(r2)) => {
|
|
|
|
new_scope.push((f.params[0].clone(), r1));
|
|
|
|
new_scope.push((f.params[1].clone(), r2));
|
2016-04-17 04:32:18 +02:00
|
|
|
}
|
|
|
|
_ => return Err(EvalAltResult::ErrorFunctionArgMismatch),
|
2016-04-14 03:01:08 +02:00
|
|
|
}
|
2016-03-03 15:31:42 +01:00
|
|
|
match self.eval_stmt(&mut new_scope, &*f.body) {
|
|
|
|
Err(EvalAltResult::Return(x)) => return Ok(x),
|
2016-04-17 04:32:18 +02:00
|
|
|
x => return x,
|
2016-03-03 15:31:42 +01:00
|
|
|
}
|
2016-03-02 20:14:22 +01:00
|
|
|
}
|
2016-04-17 04:32:18 +02:00
|
|
|
_ => (),
|
2016-03-02 16:49:24 +01:00
|
|
|
}
|
|
|
|
}
|
2017-10-02 23:44:45 +02:00
|
|
|
Err(EvalAltResult::ErrorFunctionArgMismatch)
|
2016-03-02 16:49:24 +01:00
|
|
|
}
|
2016-03-02 20:14:22 +01:00
|
|
|
(Some(ref mut a1), None, None, None, None, None) => {
|
2017-10-02 23:44:45 +02:00
|
|
|
for arr_f in vf {
|
|
|
|
match *arr_f {
|
|
|
|
FnType::ExternalFn1(ref f) => {
|
|
|
|
if let Ok(v) = f(*a1) {
|
|
|
|
return Ok(v);
|
2016-03-02 20:14:22 +01:00
|
|
|
}
|
|
|
|
}
|
2017-10-02 23:44:45 +02:00
|
|
|
FnType::InternalFn(ref f) => {
|
2016-04-17 04:32:18 +02:00
|
|
|
if f.params.len() != 1 {
|
|
|
|
return Err(EvalAltResult::ErrorFunctionArgMismatch);
|
|
|
|
}
|
2016-03-03 15:31:42 +01:00
|
|
|
|
2016-03-02 20:14:22 +01:00
|
|
|
let mut new_scope: Scope = Vec::new();
|
2016-04-17 04:32:18 +02:00
|
|
|
let result1 = self.call_fn("clone",
|
|
|
|
Some(a1),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None);
|
2016-03-02 20:14:22 +01:00
|
|
|
match result1 {
|
|
|
|
Ok(r1) => {
|
|
|
|
new_scope.push((f.params[0].clone(), r1));
|
2016-04-17 04:32:18 +02:00
|
|
|
}
|
|
|
|
_ => return Err(EvalAltResult::ErrorFunctionArgMismatch),
|
2016-04-14 03:01:08 +02:00
|
|
|
}
|
2016-03-03 15:31:42 +01:00
|
|
|
match self.eval_stmt(&mut new_scope, &*f.body) {
|
|
|
|
Err(EvalAltResult::Return(x)) => return Ok(x),
|
2016-04-17 04:32:18 +02:00
|
|
|
x => return x,
|
2016-03-03 15:31:42 +01:00
|
|
|
}
|
2016-03-02 20:14:22 +01:00
|
|
|
}
|
2016-04-17 04:32:18 +02:00
|
|
|
_ => (),
|
2016-03-02 16:49:24 +01:00
|
|
|
}
|
|
|
|
}
|
2017-10-02 23:44:45 +02:00
|
|
|
Err(EvalAltResult::ErrorFunctionArgMismatch)
|
2016-03-02 16:49:24 +01:00
|
|
|
}
|
2016-03-02 20:14:22 +01:00
|
|
|
_ => {
|
2017-10-02 23:44:45 +02:00
|
|
|
for arr_f in vf {
|
|
|
|
match *arr_f {
|
|
|
|
FnType::ExternalFn0(ref f) => {
|
|
|
|
if let Ok(v) = f() {
|
|
|
|
return Ok(v);
|
2016-03-02 20:14:22 +01:00
|
|
|
}
|
|
|
|
}
|
2017-10-02 23:44:45 +02:00
|
|
|
FnType::InternalFn(ref f) => {
|
|
|
|
if !f.params.is_empty() {
|
2016-04-17 04:32:18 +02:00
|
|
|
return Err(EvalAltResult::ErrorFunctionArgMismatch);
|
|
|
|
}
|
2016-03-03 15:31:42 +01:00
|
|
|
|
2016-03-02 20:14:22 +01:00
|
|
|
let mut new_scope: Scope = Vec::new();
|
2016-03-03 15:31:42 +01:00
|
|
|
match self.eval_stmt(&mut new_scope, &*f.body) {
|
|
|
|
Err(EvalAltResult::Return(x)) => return Ok(x),
|
2016-04-17 04:32:18 +02:00
|
|
|
x => return x,
|
2016-03-03 15:31:42 +01:00
|
|
|
}
|
2016-03-02 20:14:22 +01:00
|
|
|
}
|
2016-04-17 04:32:18 +02:00
|
|
|
_ => (),
|
2016-03-02 16:49:24 +01:00
|
|
|
}
|
|
|
|
}
|
2017-10-02 23:44:45 +02:00
|
|
|
Err(EvalAltResult::ErrorFunctionArgMismatch)
|
2016-03-02 16:49:24 +01:00
|
|
|
}
|
2016-03-02 20:14:22 +01:00
|
|
|
}
|
2016-03-02 16:49:24 +01:00
|
|
|
}
|
2016-04-17 04:32:18 +02:00
|
|
|
None => Err(EvalAltResult::ErrorFunctionNotFound),
|
2016-03-02 16:49:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-17 04:32:18 +02:00
|
|
|
pub fn register_type<T: Clone + Any>(&mut self) {
|
|
|
|
fn clone_helper<T: Clone>(t: T) -> T {
|
|
|
|
t.clone()
|
|
|
|
};
|
2016-02-29 22:43:45 +01:00
|
|
|
|
2016-04-17 04:32:18 +02:00
|
|
|
self.register_fn("clone", clone_helper as fn(T) -> T);
|
2016-02-29 22:43:45 +01:00
|
|
|
}
|
|
|
|
|
2016-04-17 04:32:18 +02:00
|
|
|
pub fn register_get<T: Clone + Any, U: Clone + Any, F>(&mut self, name: &str, get_fn: F)
|
|
|
|
where F: 'static + Fn(&mut T) -> U
|
|
|
|
{
|
2016-04-14 03:01:08 +02:00
|
|
|
|
|
|
|
let get_name = "get$".to_string() + name;
|
2016-03-17 13:45:48 +01:00
|
|
|
self.register_fn(&get_name, get_fn);
|
|
|
|
}
|
|
|
|
|
2016-04-17 04:32:18 +02:00
|
|
|
pub fn register_set<T: Clone + Any, U: Clone + Any, F>(&mut self, name: &str, set_fn: F)
|
|
|
|
where F: 'static + Fn(&mut T, U) -> ()
|
|
|
|
{
|
2016-04-14 03:01:08 +02:00
|
|
|
|
|
|
|
let set_name = "set$".to_string() + name;
|
2016-03-17 13:45:48 +01:00
|
|
|
self.register_fn(&set_name, set_fn);
|
|
|
|
}
|
|
|
|
|
2016-04-17 04:32:18 +02:00
|
|
|
pub fn register_get_set<T: Clone + Any, U: Clone + Any, F, G>(&mut self,
|
|
|
|
name: &str,
|
|
|
|
get_fn: F,
|
|
|
|
set_fn: G)
|
|
|
|
where F: 'static + Fn(&mut T) -> U,
|
|
|
|
G: 'static + Fn(&mut T, U) -> ()
|
|
|
|
{
|
2016-03-17 13:45:48 +01:00
|
|
|
|
|
|
|
self.register_get(name, get_fn);
|
|
|
|
self.register_set(name, set_fn);
|
|
|
|
}
|
|
|
|
|
2016-04-17 04:32:18 +02:00
|
|
|
fn get_dot_val_helper(&self,
|
|
|
|
scope: &mut Scope,
|
|
|
|
this_ptr: &mut Box<Any>,
|
|
|
|
dot_rhs: &Expr)
|
|
|
|
-> Result<Box<Any>, EvalAltResult> {
|
2016-03-07 21:39:02 +01:00
|
|
|
match *dot_rhs {
|
2016-04-14 03:01:08 +02:00
|
|
|
Expr::FnCall(ref fn_name, ref args) => {
|
2017-10-02 23:44:45 +02:00
|
|
|
if args.is_empty() {
|
|
|
|
self.call_fn(fn_name, Some(this_ptr), None, None, None, None, None)
|
2016-04-17 04:32:18 +02:00
|
|
|
} else if args.len() == 1 {
|
2017-10-02 23:51:04 +02:00
|
|
|
let mut arg = self.eval_expr(scope, &args[0])?;
|
2016-03-07 21:39:02 +01:00
|
|
|
|
2017-10-02 23:44:45 +02:00
|
|
|
self.call_fn(fn_name,
|
|
|
|
Some(this_ptr),
|
|
|
|
Some(&mut arg),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None)
|
2016-04-17 04:32:18 +02:00
|
|
|
} else if args.len() == 2 {
|
2017-10-02 23:51:04 +02:00
|
|
|
let mut arg1 = self.eval_expr(scope, &args[0])?;
|
|
|
|
let mut arg2 = self.eval_expr(scope, &args[1])?;
|
2016-03-07 21:39:02 +01:00
|
|
|
|
2017-10-02 23:44:45 +02:00
|
|
|
self.call_fn(fn_name,
|
|
|
|
Some(this_ptr),
|
|
|
|
Some(&mut arg1),
|
|
|
|
Some(&mut arg2),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None)
|
2016-04-17 04:32:18 +02:00
|
|
|
} else if args.len() == 3 {
|
2017-10-02 23:51:04 +02:00
|
|
|
let mut arg1 = self.eval_expr(scope, &args[0])?;
|
|
|
|
let mut arg2 = self.eval_expr(scope, &args[1])?;
|
|
|
|
let mut arg3 = self.eval_expr(scope, &args[2])?;
|
2016-03-07 21:39:02 +01:00
|
|
|
|
2017-10-02 23:44:45 +02:00
|
|
|
self.call_fn(fn_name,
|
|
|
|
Some(this_ptr),
|
|
|
|
Some(&mut arg1),
|
|
|
|
Some(&mut arg2),
|
|
|
|
Some(&mut arg3),
|
|
|
|
None,
|
|
|
|
None)
|
2016-04-17 04:32:18 +02:00
|
|
|
} else if args.len() == 4 {
|
2017-10-02 23:51:04 +02:00
|
|
|
let mut arg1 = self.eval_expr(scope, &args[0])?;
|
|
|
|
let mut arg2 = self.eval_expr(scope, &args[1])?;
|
|
|
|
let mut arg3 = self.eval_expr(scope, &args[2])?;
|
|
|
|
let mut arg4 = self.eval_expr(scope, &args[3])?;
|
2016-03-07 21:39:02 +01:00
|
|
|
|
2017-10-02 23:44:45 +02:00
|
|
|
self.call_fn(fn_name,
|
|
|
|
Some(this_ptr),
|
|
|
|
Some(&mut arg1),
|
|
|
|
Some(&mut arg2),
|
|
|
|
Some(&mut arg3),
|
|
|
|
Some(&mut arg4),
|
|
|
|
None)
|
2016-04-17 04:32:18 +02:00
|
|
|
} else if args.len() == 5 {
|
2017-10-02 23:51:04 +02:00
|
|
|
let mut arg1 = self.eval_expr(scope, &args[0])?;
|
|
|
|
let mut arg2 = self.eval_expr(scope, &args[1])?;
|
|
|
|
let mut arg3 = self.eval_expr(scope, &args[2])?;
|
|
|
|
let mut arg4 = self.eval_expr(scope, &args[3])?;
|
|
|
|
let mut arg5 = self.eval_expr(scope, &args[4])?;
|
2016-03-07 21:39:02 +01:00
|
|
|
|
2017-10-02 23:44:45 +02:00
|
|
|
self.call_fn(fn_name,
|
|
|
|
Some(this_ptr),
|
|
|
|
Some(&mut arg1),
|
|
|
|
Some(&mut arg2),
|
|
|
|
Some(&mut arg3),
|
|
|
|
Some(&mut arg4),
|
|
|
|
Some(&mut arg5))
|
2016-04-17 04:32:18 +02:00
|
|
|
} else {
|
2016-03-07 21:39:02 +01:00
|
|
|
Err(EvalAltResult::ErrorFunctionCallNotSupported)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Expr::Identifier(ref id) => {
|
|
|
|
let get_fn_name = "get$".to_string() + id;
|
2017-10-02 23:44:45 +02:00
|
|
|
self.call_fn(&get_fn_name, Some(this_ptr), None, None, None, None, None)
|
2016-03-07 21:39:02 +01:00
|
|
|
}
|
2016-03-26 21:32:20 +01:00
|
|
|
Expr::Index(ref id, ref idx_raw) => {
|
2017-10-02 23:51:04 +02:00
|
|
|
let idx = self.eval_expr(scope, idx_raw)?;
|
2016-03-26 21:32:20 +01:00
|
|
|
|
|
|
|
let get_fn_name = "get$".to_string() + id;
|
2016-04-14 03:01:08 +02:00
|
|
|
|
2016-04-17 04:32:18 +02:00
|
|
|
if let Ok(mut val) = self.call_fn(&get_fn_name,
|
|
|
|
Some(this_ptr),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None) {
|
2016-04-13 04:52:27 +02:00
|
|
|
if let Ok(i) = idx.downcast::<i64>() {
|
2016-04-17 04:32:18 +02:00
|
|
|
if let Some(arr_typed) =
|
|
|
|
(*val).downcast_mut() as Option<&mut Vec<Box<Any>>> {
|
|
|
|
return self.call_fn("clone",
|
|
|
|
Some(&mut arr_typed[*i as usize]),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None);
|
|
|
|
} else {
|
2016-03-26 21:32:20 +01:00
|
|
|
return Err(EvalAltResult::ErrorIndexMismatch);
|
|
|
|
}
|
2016-04-17 04:32:18 +02:00
|
|
|
} else {
|
2016-03-26 21:32:20 +01:00
|
|
|
return Err(EvalAltResult::ErrorIndexMismatch);
|
|
|
|
}
|
2016-04-17 04:32:18 +02:00
|
|
|
} else {
|
2016-03-26 21:32:20 +01:00
|
|
|
return Err(EvalAltResult::ErrorIndexMismatch);
|
|
|
|
}
|
|
|
|
}
|
2016-03-10 20:21:36 +01:00
|
|
|
Expr::Dot(ref inner_lhs, ref inner_rhs) => {
|
|
|
|
match **inner_lhs {
|
|
|
|
Expr::Identifier(ref id) => {
|
|
|
|
let get_fn_name = "get$".to_string() + id;
|
2016-04-17 04:32:18 +02:00
|
|
|
let result = self.call_fn(&get_fn_name,
|
|
|
|
Some(this_ptr),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None);
|
2016-03-10 20:21:36 +01:00
|
|
|
|
|
|
|
match result {
|
2017-10-02 23:44:45 +02:00
|
|
|
Ok(mut v) => self.get_dot_val_helper(scope, &mut v, inner_rhs),
|
|
|
|
e => e,
|
2016-04-14 03:01:08 +02:00
|
|
|
}
|
2016-03-10 20:21:36 +01:00
|
|
|
}
|
2016-04-17 04:32:18 +02:00
|
|
|
_ => Err(EvalAltResult::InternalErrorMalformedDotExpression),
|
2016-03-10 20:21:36 +01:00
|
|
|
}
|
|
|
|
}
|
2016-04-17 04:32:18 +02:00
|
|
|
_ => Err(EvalAltResult::InternalErrorMalformedDotExpression),
|
2016-03-07 21:39:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-17 04:32:18 +02:00
|
|
|
fn get_dot_val(&self,
|
|
|
|
scope: &mut Scope,
|
|
|
|
dot_lhs: &Expr,
|
|
|
|
dot_rhs: &Expr)
|
|
|
|
-> Result<Box<Any>, EvalAltResult> {
|
2016-03-07 21:39:02 +01:00
|
|
|
match *dot_lhs {
|
|
|
|
Expr::Identifier(ref id) => {
|
2016-04-17 04:32:18 +02:00
|
|
|
let mut target: Option<Box<Any>> = None;
|
2016-03-07 21:39:02 +01:00
|
|
|
|
|
|
|
for &mut (ref name, ref mut val) in &mut scope.iter_mut().rev() {
|
|
|
|
if *id == *name {
|
2016-03-10 20:21:36 +01:00
|
|
|
let result = self.call_fn("clone", Some(val), None, None, None, None, None);
|
|
|
|
|
|
|
|
if let Ok(clone) = result {
|
2016-03-07 21:39:02 +01:00
|
|
|
target = Some(clone);
|
|
|
|
break;
|
2016-04-17 04:32:18 +02:00
|
|
|
} else {
|
2016-03-10 20:21:36 +01:00
|
|
|
return result;
|
2016-03-07 21:39:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(mut t) = target {
|
|
|
|
let result = self.get_dot_val_helper(scope, &mut t, dot_rhs);
|
|
|
|
|
|
|
|
for &mut (ref name, ref mut val) in &mut scope.iter_mut().rev() {
|
|
|
|
if *id == *name {
|
|
|
|
*val = t;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-10-02 23:44:45 +02:00
|
|
|
Err(EvalAltResult::ErrorVariableNotFound(id.clone()))
|
2016-03-07 21:39:02 +01:00
|
|
|
}
|
2016-03-26 21:32:20 +01:00
|
|
|
Expr::Index(ref id, ref idx_raw) => {
|
2017-10-02 23:51:04 +02:00
|
|
|
let idx_boxed = self.eval_expr(scope, idx_raw)?;
|
2016-04-17 04:32:18 +02:00
|
|
|
let idx = if let Ok(i) = idx_boxed.downcast::<i64>() {
|
|
|
|
i
|
|
|
|
} else {
|
|
|
|
return Err(EvalAltResult::ErrorIndexMismatch);
|
|
|
|
};
|
2016-04-14 03:01:08 +02:00
|
|
|
|
2016-04-17 04:32:18 +02:00
|
|
|
let mut target: Option<Box<Any>> = None;
|
2016-03-26 21:32:20 +01:00
|
|
|
|
|
|
|
for &mut (ref name, ref mut val) in &mut scope.iter_mut().rev() {
|
|
|
|
if *id == *name {
|
2016-04-17 04:32:18 +02:00
|
|
|
if let Some(arr_typed) =
|
|
|
|
(*val).downcast_mut() as Option<&mut Vec<Box<Any>>> {
|
|
|
|
let result = self.call_fn("clone",
|
|
|
|
Some(&mut arr_typed[*idx as usize]),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None);
|
2016-03-26 21:32:20 +01:00
|
|
|
|
|
|
|
if let Ok(clone) = result {
|
|
|
|
target = Some(clone);
|
|
|
|
break;
|
2016-04-17 04:32:18 +02:00
|
|
|
} else {
|
2016-03-26 21:32:20 +01:00
|
|
|
return result;
|
|
|
|
}
|
2016-04-17 04:32:18 +02:00
|
|
|
} else {
|
2016-03-26 21:32:20 +01:00
|
|
|
return Err(EvalAltResult::ErrorIndexMismatch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(mut t) = target {
|
|
|
|
let result = self.get_dot_val_helper(scope, &mut t, dot_rhs);
|
|
|
|
for &mut (ref name, ref mut val) in &mut scope.iter_mut().rev() {
|
|
|
|
if *id == *name {
|
2016-04-17 04:32:18 +02:00
|
|
|
if let Some(arr_typed) =
|
|
|
|
(*val).downcast_mut() as Option<&mut Vec<Box<Any>>> {
|
2016-03-26 21:32:20 +01:00
|
|
|
arr_typed[*idx as usize] = t;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(EvalAltResult::ErrorVariableNotFound(id.clone()))
|
|
|
|
}
|
2016-04-17 04:32:18 +02:00
|
|
|
_ => Err(EvalAltResult::InternalErrorMalformedDotExpression),
|
2016-03-07 21:39:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-17 04:32:18 +02:00
|
|
|
fn set_dot_val_helper(&self,
|
|
|
|
this_ptr: &mut Box<Any>,
|
|
|
|
dot_rhs: &Expr,
|
|
|
|
mut source_val: Box<Any>)
|
|
|
|
-> Result<Box<Any>, EvalAltResult> {
|
2016-03-07 21:39:02 +01:00
|
|
|
match *dot_rhs {
|
|
|
|
Expr::Identifier(ref id) => {
|
|
|
|
let set_fn_name = "set$".to_string() + id;
|
2016-04-17 04:32:18 +02:00
|
|
|
self.call_fn(&set_fn_name,
|
|
|
|
Some(this_ptr),
|
|
|
|
Some(&mut source_val),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None)
|
2016-03-07 21:39:02 +01:00
|
|
|
}
|
2016-03-10 20:21:36 +01:00
|
|
|
Expr::Dot(ref inner_lhs, ref inner_rhs) => {
|
|
|
|
match **inner_lhs {
|
|
|
|
Expr::Identifier(ref id) => {
|
|
|
|
let get_fn_name = "get$".to_string() + id;
|
2016-04-17 04:32:18 +02:00
|
|
|
let result = self.call_fn(&get_fn_name,
|
|
|
|
Some(this_ptr),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None);
|
2016-03-10 20:21:36 +01:00
|
|
|
|
|
|
|
match result {
|
|
|
|
Ok(mut v) => {
|
|
|
|
match self.set_dot_val_helper(&mut v, inner_rhs, source_val) {
|
|
|
|
Ok(_) => {
|
|
|
|
let set_fn_name = "set$".to_string() + id;
|
|
|
|
|
2016-04-17 04:32:18 +02:00
|
|
|
self.call_fn(&set_fn_name,
|
|
|
|
Some(this_ptr),
|
|
|
|
Some(&mut v),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None)
|
2016-03-10 20:21:36 +01:00
|
|
|
}
|
2016-04-17 04:32:18 +02:00
|
|
|
e => e,
|
2016-03-10 20:21:36 +01:00
|
|
|
}
|
2016-04-17 04:32:18 +02:00
|
|
|
}
|
|
|
|
e => e,
|
2016-04-14 03:01:08 +02:00
|
|
|
}
|
2016-03-10 20:21:36 +01:00
|
|
|
|
|
|
|
}
|
2016-04-17 04:32:18 +02:00
|
|
|
_ => Err(EvalAltResult::InternalErrorMalformedDotExpression),
|
2016-03-10 20:21:36 +01:00
|
|
|
}
|
|
|
|
}
|
2016-04-17 04:32:18 +02:00
|
|
|
_ => Err(EvalAltResult::InternalErrorMalformedDotExpression),
|
2016-03-07 21:39:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-17 04:32:18 +02:00
|
|
|
fn set_dot_val(&self,
|
|
|
|
scope: &mut Scope,
|
|
|
|
dot_lhs: &Expr,
|
|
|
|
dot_rhs: &Expr,
|
|
|
|
source_val: Box<Any>)
|
|
|
|
-> Result<Box<Any>, EvalAltResult> {
|
2016-03-07 21:39:02 +01:00
|
|
|
match *dot_lhs {
|
|
|
|
Expr::Identifier(ref id) => {
|
2016-04-17 04:32:18 +02:00
|
|
|
let mut target: Option<Box<Any>> = None;
|
2016-03-07 21:39:02 +01:00
|
|
|
|
|
|
|
for &mut (ref name, ref mut val) in &mut scope.iter_mut().rev() {
|
|
|
|
if *id == *name {
|
2016-04-17 04:32:18 +02:00
|
|
|
if let Ok(clone) = self.call_fn("clone",
|
|
|
|
Some(val),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None) {
|
2016-03-07 21:39:02 +01:00
|
|
|
target = Some(clone);
|
|
|
|
break;
|
2016-04-17 04:32:18 +02:00
|
|
|
} else {
|
2016-03-10 20:21:36 +01:00
|
|
|
return Err(EvalAltResult::ErrorVariableNotFound(id.clone()));
|
2016-03-07 21:39:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(mut t) = target {
|
|
|
|
let result = self.set_dot_val_helper(&mut t, dot_rhs, source_val);
|
|
|
|
|
|
|
|
for &mut (ref name, ref mut val) in &mut scope.iter_mut().rev() {
|
|
|
|
if *id == *name {
|
|
|
|
*val = t;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-10-02 23:44:45 +02:00
|
|
|
Err(EvalAltResult::ErrorAssignmentToUnknownLHS)
|
2016-03-07 21:39:02 +01:00
|
|
|
}
|
2016-03-26 21:32:20 +01:00
|
|
|
Expr::Index(ref id, ref idx_raw) => {
|
2017-10-02 23:51:04 +02:00
|
|
|
let idx_boxed = self.eval_expr(scope, idx_raw)?;
|
2016-04-17 04:32:18 +02:00
|
|
|
let idx = if let Ok(i) = idx_boxed.downcast::<i64>() {
|
|
|
|
i
|
|
|
|
} else {
|
|
|
|
return Err(EvalAltResult::ErrorIndexMismatch);
|
|
|
|
};
|
2016-04-14 03:01:08 +02:00
|
|
|
|
2016-04-17 04:32:18 +02:00
|
|
|
let mut target: Option<Box<Any>> = None;
|
2016-03-26 21:32:20 +01:00
|
|
|
|
|
|
|
for &mut (ref name, ref mut val) in &mut scope.iter_mut().rev() {
|
|
|
|
if *id == *name {
|
2016-04-17 04:32:18 +02:00
|
|
|
if let Some(arr_typed) =
|
|
|
|
(*val).downcast_mut() as Option<&mut Vec<Box<Any>>> {
|
|
|
|
let result = self.call_fn("clone",
|
|
|
|
Some(&mut arr_typed[*idx as usize]),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None);
|
2016-03-26 21:32:20 +01:00
|
|
|
|
|
|
|
if let Ok(clone) = result {
|
|
|
|
target = Some(clone);
|
|
|
|
break;
|
2016-04-17 04:32:18 +02:00
|
|
|
} else {
|
2016-03-26 21:32:20 +01:00
|
|
|
return result;
|
|
|
|
}
|
2016-04-17 04:32:18 +02:00
|
|
|
} else {
|
2016-03-26 21:32:20 +01:00
|
|
|
return Err(EvalAltResult::ErrorIndexMismatch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(mut t) = target {
|
|
|
|
let result = self.set_dot_val_helper(&mut t, dot_rhs, source_val);
|
|
|
|
for &mut (ref name, ref mut val) in &mut scope.iter_mut().rev() {
|
|
|
|
if *id == *name {
|
2016-04-17 04:32:18 +02:00
|
|
|
if let Some(arr_typed) =
|
|
|
|
(*val).downcast_mut() as Option<&mut Vec<Box<Any>>> {
|
2016-03-26 21:32:20 +01:00
|
|
|
arr_typed[*idx as usize] = t;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(EvalAltResult::ErrorVariableNotFound(id.clone()))
|
|
|
|
}
|
2016-04-17 04:32:18 +02:00
|
|
|
_ => Err(EvalAltResult::InternalErrorMalformedDotExpression),
|
2016-03-07 21:39:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-03 15:31:42 +01:00
|
|
|
fn eval_expr(&self, scope: &mut Scope, expr: &Expr) -> Result<Box<Any>, EvalAltResult> {
|
2016-02-29 22:43:45 +01:00
|
|
|
match *expr {
|
|
|
|
Expr::IntConst(i) => Ok(Box::new(i)),
|
2016-03-02 14:37:28 +01:00
|
|
|
Expr::StringConst(ref s) => Ok(Box::new(s.clone())),
|
2017-10-02 23:44:45 +02:00
|
|
|
Expr::CharConst(ref c) => Ok(Box::new(*c)),
|
2016-02-29 22:43:45 +01:00
|
|
|
Expr::Identifier(ref id) => {
|
2016-03-02 02:45:02 +01:00
|
|
|
for &mut (ref name, ref mut val) in &mut scope.iter_mut().rev() {
|
2016-04-14 03:01:08 +02:00
|
|
|
if *id == *name {
|
2016-03-02 20:14:22 +01:00
|
|
|
return self.call_fn("clone", Some(val), None, None, None, None, None);
|
2016-02-29 22:43:45 +01:00
|
|
|
}
|
|
|
|
}
|
2016-03-10 20:21:36 +01:00
|
|
|
Err(EvalAltResult::ErrorVariableNotFound(id.clone()))
|
2016-02-29 22:43:45 +01:00
|
|
|
}
|
2016-03-26 18:46:28 +01:00
|
|
|
Expr::Index(ref id, ref idx_raw) => {
|
2017-10-02 23:51:04 +02:00
|
|
|
let idx = self.eval_expr(scope, idx_raw)?;
|
2016-03-26 18:46:28 +01:00
|
|
|
|
|
|
|
for &mut (ref name, ref mut val) in &mut scope.iter_mut().rev() {
|
|
|
|
if *id == *name {
|
2016-04-13 04:52:27 +02:00
|
|
|
if let Ok(i) = idx.downcast::<i64>() {
|
2016-04-17 04:32:18 +02:00
|
|
|
if let Some(arr_typed) =
|
|
|
|
(*val).downcast_mut() as Option<&mut Vec<Box<Any>>> {
|
|
|
|
return self.call_fn("clone",
|
|
|
|
Some(&mut arr_typed[*i as usize]),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None);
|
|
|
|
} else {
|
2016-03-26 18:46:28 +01:00
|
|
|
return Err(EvalAltResult::ErrorIndexMismatch);
|
|
|
|
}
|
2016-04-17 04:32:18 +02:00
|
|
|
} else {
|
2016-03-26 18:46:28 +01:00
|
|
|
return Err(EvalAltResult::ErrorIndexMismatch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(EvalAltResult::ErrorVariableNotFound(id.clone()))
|
|
|
|
}
|
2016-02-29 22:43:45 +01:00
|
|
|
Expr::Assignment(ref id, ref 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
|
|
|
|
2016-02-29 22:43:45 +01:00
|
|
|
match **id {
|
|
|
|
Expr::Identifier(ref n) => {
|
2016-03-02 02:45:02 +01:00
|
|
|
for &mut (ref name, ref mut val) in &mut scope.iter_mut().rev() {
|
2016-02-29 22:43:45 +01:00
|
|
|
if *n == *name {
|
|
|
|
|
|
|
|
*val = rhs_val;
|
|
|
|
|
|
|
|
return Ok(Box::new(()));
|
|
|
|
}
|
|
|
|
}
|
2016-03-10 20:21:36 +01:00
|
|
|
Err(EvalAltResult::ErrorVariableNotFound(n.clone()))
|
2016-02-29 22:43:45 +01:00
|
|
|
}
|
2016-03-26 18:46:28 +01:00
|
|
|
Expr::Index(ref id, ref idx_raw) => {
|
2017-10-02 23:51:04 +02:00
|
|
|
let idx = self.eval_expr(scope, idx_raw)?;
|
2016-03-26 18:46:28 +01:00
|
|
|
|
|
|
|
for &mut (ref name, ref mut val) in &mut scope.iter_mut().rev() {
|
|
|
|
if *id == *name {
|
2016-04-13 04:52:27 +02:00
|
|
|
if let Ok(i) = idx.downcast::<i64>() {
|
2016-04-17 04:32:18 +02:00
|
|
|
if let Some(arr_typed) =
|
|
|
|
(*val).downcast_mut() as Option<&mut Vec<Box<Any>>> {
|
2016-03-26 18:46:28 +01:00
|
|
|
arr_typed[*i as usize] = rhs_val;
|
|
|
|
return Ok(Box::new(()));
|
2016-04-17 04:32:18 +02:00
|
|
|
} else {
|
2016-03-26 18:46:28 +01:00
|
|
|
return Err(EvalAltResult::ErrorIndexMismatch);
|
|
|
|
}
|
2016-04-17 04:32:18 +02:00
|
|
|
} else {
|
2016-03-26 18:46:28 +01:00
|
|
|
return Err(EvalAltResult::ErrorIndexMismatch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-07 21:39:02 +01:00
|
|
|
|
2016-04-14 03:01:08 +02:00
|
|
|
Err(EvalAltResult::ErrorVariableNotFound(id.clone()))
|
2016-03-26 18:46:28 +01:00
|
|
|
}
|
|
|
|
Expr::Dot(ref dot_lhs, ref dot_rhs) => {
|
2016-03-07 21:39:02 +01:00
|
|
|
self.set_dot_val(scope, dot_lhs, dot_rhs, rhs_val)
|
|
|
|
}
|
2016-04-17 04:32:18 +02:00
|
|
|
_ => Err(EvalAltResult::ErrorAssignmentToUnknownLHS),
|
2016-02-29 22:43:45 +01:00
|
|
|
}
|
|
|
|
}
|
2016-04-17 04:32:18 +02:00
|
|
|
Expr::Dot(ref lhs, ref rhs) => self.get_dot_val(scope, lhs, rhs),
|
2016-03-26 18:46:28 +01:00
|
|
|
Expr::Array(ref contents) => {
|
|
|
|
let mut arr = Vec::new();
|
|
|
|
|
2017-10-02 23:44:45 +02:00
|
|
|
for item in &(*contents) {
|
2017-10-02 23:51:04 +02:00
|
|
|
let arg = self.eval_expr(scope, item)?;
|
2016-03-26 18:46:28 +01:00
|
|
|
arr.push(arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Box::new(arr))
|
|
|
|
}
|
2016-03-01 21:16:10 +01:00
|
|
|
Expr::FnCall(ref fn_name, ref args) => {
|
2017-10-02 23:44:45 +02:00
|
|
|
if args.is_empty() {
|
|
|
|
self.call_fn(fn_name, None, None, None, None, None, None)
|
2016-04-17 04:32:18 +02:00
|
|
|
} else if args.len() == 1 {
|
2017-10-02 23:51:04 +02:00
|
|
|
let mut arg = self.eval_expr(scope, &args[0])?;
|
2016-03-01 21:16:10 +01:00
|
|
|
|
2017-10-02 23:44:45 +02:00
|
|
|
self.call_fn(fn_name, Some(&mut arg), None, None, None, None, None)
|
2016-04-17 04:32:18 +02:00
|
|
|
} else if args.len() == 2 {
|
2017-10-02 23:51:04 +02:00
|
|
|
let mut arg1 = self.eval_expr(scope, &args[0])?;
|
|
|
|
let mut arg2 = self.eval_expr(scope, &args[1])?;
|
2016-03-01 21:16:10 +01:00
|
|
|
|
2017-10-02 23:44:45 +02:00
|
|
|
self.call_fn(fn_name,
|
2016-04-17 04:32:18 +02:00
|
|
|
Some(&mut arg1),
|
|
|
|
Some(&mut arg2),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None)
|
|
|
|
} else if args.len() == 3 {
|
2017-10-02 23:51:04 +02:00
|
|
|
let mut arg1 = self.eval_expr(scope, &args[0])?;
|
|
|
|
let mut arg2 = self.eval_expr(scope, &args[1])?;
|
|
|
|
let mut arg3 = self.eval_expr(scope, &args[2])?;
|
2016-03-01 21:16:10 +01:00
|
|
|
|
2017-10-02 23:44:45 +02:00
|
|
|
self.call_fn(fn_name,
|
2016-04-17 04:32:18 +02:00
|
|
|
Some(&mut arg1),
|
|
|
|
Some(&mut arg2),
|
|
|
|
Some(&mut arg3),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None)
|
|
|
|
} else if args.len() == 4 {
|
2017-10-02 23:51:04 +02:00
|
|
|
let mut arg1 = self.eval_expr(scope, &args[0])?;
|
|
|
|
let mut arg2 = self.eval_expr(scope, &args[1])?;
|
|
|
|
let mut arg3 = self.eval_expr(scope, &args[2])?;
|
|
|
|
let mut arg4 = self.eval_expr(scope, &args[3])?;
|
2016-03-02 16:49:24 +01:00
|
|
|
|
2017-10-02 23:44:45 +02:00
|
|
|
self.call_fn(fn_name,
|
2016-04-17 04:32:18 +02:00
|
|
|
Some(&mut arg1),
|
|
|
|
Some(&mut arg2),
|
|
|
|
Some(&mut arg3),
|
|
|
|
Some(&mut arg4),
|
|
|
|
None,
|
|
|
|
None)
|
|
|
|
} else if args.len() == 5 {
|
2017-10-02 23:51:04 +02:00
|
|
|
let mut arg1 = self.eval_expr(scope, &args[0])?;
|
|
|
|
let mut arg2 = self.eval_expr(scope, &args[1])?;
|
|
|
|
let mut arg3 = self.eval_expr(scope, &args[2])?;
|
|
|
|
let mut arg4 = self.eval_expr(scope, &args[3])?;
|
|
|
|
let mut arg5 = self.eval_expr(scope, &args[4])?;
|
2016-03-02 16:49:24 +01:00
|
|
|
|
2017-10-02 23:44:45 +02:00
|
|
|
self.call_fn(fn_name,
|
2016-04-17 04:32:18 +02:00
|
|
|
Some(&mut arg1),
|
|
|
|
Some(&mut arg2),
|
|
|
|
Some(&mut arg3),
|
|
|
|
Some(&mut arg4),
|
|
|
|
Some(&mut arg5),
|
|
|
|
None)
|
|
|
|
} else if args.len() == 6 {
|
2017-10-02 23:51:04 +02:00
|
|
|
let mut arg1 = self.eval_expr(scope, &args[0])?;
|
|
|
|
let mut arg2 = self.eval_expr(scope, &args[1])?;
|
|
|
|
let mut arg3 = self.eval_expr(scope, &args[2])?;
|
|
|
|
let mut arg4 = self.eval_expr(scope, &args[3])?;
|
|
|
|
let mut arg5 = self.eval_expr(scope, &args[4])?;
|
|
|
|
let mut arg6 = self.eval_expr(scope, &args[5])?;
|
2016-03-02 16:49:24 +01:00
|
|
|
|
2017-10-02 23:44:45 +02:00
|
|
|
self.call_fn(fn_name,
|
2016-04-17 04:32:18 +02:00
|
|
|
Some(&mut arg1),
|
|
|
|
Some(&mut arg2),
|
|
|
|
Some(&mut arg3),
|
|
|
|
Some(&mut arg4),
|
|
|
|
Some(&mut arg5),
|
|
|
|
Some(&mut arg6))
|
|
|
|
} else {
|
2016-03-03 15:31:42 +01:00
|
|
|
Err(EvalAltResult::ErrorFunctionCallNotSupported)
|
2016-03-01 21:16:10 +01:00
|
|
|
}
|
|
|
|
}
|
2016-04-17 04:32:18 +02:00
|
|
|
Expr::True => Ok(Box::new(true)),
|
|
|
|
Expr::False => Ok(Box::new(false)),
|
2016-02-29 22:43:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-03 15:31:42 +01:00
|
|
|
fn eval_stmt(&self, scope: &mut Scope, stmt: &Stmt) -> Result<Box<Any>, EvalAltResult> {
|
2016-02-29 22:43:45 +01:00
|
|
|
match *stmt {
|
2016-04-17 04:32:18 +02:00
|
|
|
Stmt::Expr(ref e) => self.eval_expr(scope, e),
|
2016-02-29 22:43:45 +01:00
|
|
|
Stmt::Block(ref b) => {
|
2016-03-02 02:45:02 +01:00
|
|
|
let prev_len = scope.len();
|
2016-04-17 04:32:18 +02:00
|
|
|
let mut last_result: Result<Box<Any>, EvalAltResult> = Ok(Box::new(()));
|
2016-02-29 22:43:45 +01:00
|
|
|
|
|
|
|
for s in b.iter() {
|
2016-03-03 15:31:42 +01:00
|
|
|
last_result = self.eval_stmt(scope, s);
|
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
|
|
|
}
|
|
|
|
Stmt::If(ref guard, ref body) => {
|
2017-10-02 23:51:04 +02:00
|
|
|
let guard_result = self.eval_expr(scope, guard)?;
|
2016-02-29 22:43:45 +01:00
|
|
|
match guard_result.downcast::<bool>() {
|
|
|
|
Ok(g) => {
|
|
|
|
if *g {
|
2016-03-02 02:45:02 +01:00
|
|
|
self.eval_stmt(scope, body)
|
2016-04-17 04:32:18 +02:00
|
|
|
} else {
|
2016-02-29 22:43:45 +01:00
|
|
|
Ok(Box::new(()))
|
|
|
|
}
|
|
|
|
}
|
2016-04-17 04:32:18 +02:00
|
|
|
Err(_) => Err(EvalAltResult::ErrorIfGuardMismatch),
|
2016-02-29 22:43:45 +01:00
|
|
|
}
|
|
|
|
}
|
2016-03-03 14:20:55 +01:00
|
|
|
Stmt::IfElse(ref guard, ref body, ref else_body) => {
|
2017-10-02 23:51:04 +02:00
|
|
|
let guard_result = self.eval_expr(scope, guard)?;
|
2016-03-03 14:20:55 +01:00
|
|
|
match guard_result.downcast::<bool>() {
|
|
|
|
Ok(g) => {
|
|
|
|
if *g {
|
|
|
|
self.eval_stmt(scope, body)
|
2016-04-17 04:32:18 +02:00
|
|
|
} else {
|
2016-03-03 14:20:55 +01:00
|
|
|
self.eval_stmt(scope, else_body)
|
|
|
|
}
|
|
|
|
}
|
2016-04-17 04:32:18 +02:00
|
|
|
Err(_) => Err(EvalAltResult::ErrorIfGuardMismatch),
|
2016-03-03 14:20:55 +01:00
|
|
|
}
|
|
|
|
}
|
2016-02-29 22:43:45 +01:00
|
|
|
Stmt::While(ref guard, ref body) => {
|
|
|
|
loop {
|
2017-10-02 23:51:04 +02:00
|
|
|
let guard_result = self.eval_expr(scope, guard)?;
|
2016-02-29 22:43:45 +01:00
|
|
|
match guard_result.downcast::<bool>() {
|
|
|
|
Ok(g) => {
|
|
|
|
if *g {
|
2016-03-03 15:31:42 +01:00
|
|
|
match self.eval_stmt(scope, body) {
|
2016-04-17 04:32:18 +02:00
|
|
|
Err(EvalAltResult::LoopBreak) => {
|
|
|
|
return Ok(Box::new(()));
|
|
|
|
}
|
|
|
|
Err(x) => {
|
|
|
|
return Err(x);
|
|
|
|
}
|
|
|
|
_ => (),
|
2016-03-03 15:31:42 +01:00
|
|
|
}
|
2016-04-17 04:32:18 +02:00
|
|
|
} else {
|
2016-02-29 22:43:45 +01:00
|
|
|
return Ok(Box::new(()));
|
|
|
|
}
|
|
|
|
}
|
2016-04-17 04:32:18 +02:00
|
|
|
Err(_) => return Err(EvalAltResult::ErrorIfGuardMismatch),
|
2016-04-14 03:01:08 +02:00
|
|
|
}
|
2016-02-29 22:43:45 +01:00
|
|
|
}
|
|
|
|
}
|
2017-10-02 23:44:45 +02:00
|
|
|
Stmt::Break => Err(EvalAltResult::LoopBreak),
|
|
|
|
Stmt::Return => Err(EvalAltResult::Return(Box::new(()))),
|
2016-03-03 15:31:42 +01:00
|
|
|
Stmt::ReturnWithVal(ref a) => {
|
2017-10-02 23:51:04 +02:00
|
|
|
let result = self.eval_expr(scope, a)?;
|
2017-10-02 23:44:45 +02:00
|
|
|
Err(EvalAltResult::Return(result))
|
2016-03-03 15:31:42 +01:00
|
|
|
}
|
2016-02-29 22:43:45 +01:00
|
|
|
Stmt::Var(ref name, ref init) => {
|
2017-10-02 23:44:45 +02:00
|
|
|
match *init {
|
|
|
|
Some(ref v) => {
|
2017-10-02 23:51:04 +02:00
|
|
|
let i = self.eval_expr(scope, v)?;
|
2016-03-02 02:45:02 +01:00
|
|
|
scope.push((name.clone(), i));
|
2016-04-17 04:32:18 +02:00
|
|
|
}
|
2017-10-02 23:44:45 +02:00
|
|
|
None => {
|
2016-03-02 02:45:02 +01:00
|
|
|
scope.push((name.clone(), Box::new(())));
|
2016-02-29 22:43:45 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
Ok(Box::new(()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-17 04:32:18 +02:00
|
|
|
pub fn eval_file<T: Any + Clone>(&mut self, fname: &str) -> Result<T, EvalAltResult> {
|
2016-03-17 13:45:48 +01:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::prelude::*;
|
|
|
|
|
2017-10-02 23:44:45 +02:00
|
|
|
if let Ok(mut f) = File::open(fname) {
|
2016-03-17 13:45:48 +01:00
|
|
|
let mut contents = String::new();
|
2016-04-14 03:01:08 +02:00
|
|
|
|
2017-10-02 23:44:45 +02:00
|
|
|
if f.read_to_string(&mut contents).is_ok() {
|
2016-03-17 13:45:48 +01:00
|
|
|
self.eval::<T>(&contents)
|
2016-04-17 04:32:18 +02:00
|
|
|
} else {
|
2016-03-17 13:45:48 +01:00
|
|
|
Err(EvalAltResult::ErrorCantOpenScriptFile)
|
2016-04-14 03:01:08 +02:00
|
|
|
}
|
2016-04-17 04:32:18 +02:00
|
|
|
} else {
|
2016-03-17 13:45:48 +01:00
|
|
|
Err(EvalAltResult::ErrorCantOpenScriptFile)
|
2016-04-14 03:01:08 +02:00
|
|
|
}
|
2016-03-17 13:45:48 +01:00
|
|
|
}
|
|
|
|
|
2016-04-17 04:32:18 +02:00
|
|
|
pub fn eval<T: Any + Clone>(&mut self, input: &str) -> Result<T, EvalAltResult> {
|
2016-03-02 02:45:02 +01:00
|
|
|
let mut scope: Scope = Vec::new();
|
|
|
|
|
|
|
|
self.eval_with_scope(&mut scope, input)
|
|
|
|
}
|
|
|
|
|
2016-04-17 04:32:18 +02:00
|
|
|
pub fn eval_with_scope<T: Any + Clone>(&mut self,
|
|
|
|
scope: &mut Scope,
|
|
|
|
input: &str)
|
|
|
|
-> Result<T, EvalAltResult> {
|
2016-03-16 23:07:08 +01:00
|
|
|
let tokens = lex(input);
|
2016-02-29 22:43:45 +01:00
|
|
|
|
|
|
|
let mut peekables = tokens.peekable();
|
|
|
|
let tree = parse(&mut peekables);
|
|
|
|
|
|
|
|
match tree {
|
2016-03-01 21:16:10 +01:00
|
|
|
Ok((ref os, ref fns)) => {
|
2016-03-03 15:31:42 +01:00
|
|
|
let mut x: Result<Box<Any>, EvalAltResult> = Ok(Box::new(()));
|
2016-04-14 03:01:08 +02:00
|
|
|
|
2016-03-01 21:16:10 +01:00
|
|
|
for f in fns {
|
2016-03-02 20:14:22 +01:00
|
|
|
if f.params.len() > 6 {
|
2016-03-03 15:31:42 +01:00
|
|
|
return Err(EvalAltResult::ErrorFunctionArityNotSupported);
|
2016-03-01 21:16:10 +01:00
|
|
|
}
|
2016-03-02 20:14:22 +01:00
|
|
|
let name = f.name.clone();
|
|
|
|
let local_f = f.clone();
|
2017-10-02 23:44:45 +02:00
|
|
|
let ent = self.fns.entry(name).or_insert_with(Vec::new);
|
2016-04-14 03:01:08 +02:00
|
|
|
(*ent).push(FnType::InternalFn(local_f));
|
2016-03-01 21:16:10 +01:00
|
|
|
}
|
|
|
|
|
2016-02-29 22:43:45 +01:00
|
|
|
for o in os {
|
2017-10-02 23:44:45 +02:00
|
|
|
x = match self.eval_stmt(scope, o) {
|
2016-03-16 23:32:05 +01:00
|
|
|
Ok(v) => Ok(v),
|
2016-04-17 04:32:18 +02:00
|
|
|
Err(e) => return Err(e),
|
2016-03-16 23:32:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
match x {
|
|
|
|
Ok(v) => {
|
|
|
|
match v.downcast::<T>() {
|
|
|
|
Ok(out) => Ok(*out),
|
2016-04-17 04:32:18 +02:00
|
|
|
Err(_) => Err(EvalAltResult::ErrorMismatchOutputType),
|
2016-03-16 23:32:05 +01:00
|
|
|
}
|
|
|
|
}
|
2016-04-17 04:32:18 +02:00
|
|
|
Err(e) => Err(e),
|
2016-02-29 22:43:45 +01:00
|
|
|
}
|
|
|
|
}
|
2016-04-17 04:32:18 +02:00
|
|
|
Err(_) => Err(EvalAltResult::ErrorFunctionArgMismatch),
|
2016-02-29 22:43:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-15 17:50:39 +02:00
|
|
|
pub fn consume_file(&mut self, fname: &str) -> Result<(), 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() {
|
|
|
|
if let e @ Err(_) = self.consume(&contents) {
|
|
|
|
return e;
|
|
|
|
} else { return Ok(()); }
|
|
|
|
} else {
|
|
|
|
Err(EvalAltResult::ErrorCantOpenScriptFile)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Err(EvalAltResult::ErrorCantOpenScriptFile)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn consume_with_scope(&mut self, scope: &mut Scope, input: &str) -> Result<(), EvalAltResult> {
|
|
|
|
let tokens = lex(input);
|
|
|
|
|
|
|
|
let mut peekables = tokens.peekable();
|
|
|
|
let tree = parse(&mut peekables);
|
|
|
|
|
|
|
|
match tree {
|
|
|
|
Ok((ref os, ref fns)) => {
|
|
|
|
for f in fns {
|
|
|
|
if f.params.len() > 6 {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
let name = f.name.clone();
|
|
|
|
let local_f = f.clone();
|
|
|
|
let ent = self.fns.entry(name).or_insert_with(Vec::new);
|
|
|
|
(*ent).push(FnType::InternalFn(local_f));
|
|
|
|
}
|
|
|
|
|
|
|
|
for o in os {
|
|
|
|
if let Err(e) = self.eval_stmt(scope, o) {
|
|
|
|
return Err(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
},
|
|
|
|
Err(_) => Err(EvalAltResult::ErrorFunctionArgMismatch),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-01 15:40:48 +01:00
|
|
|
pub fn register_default_lib(engine: &mut Engine) {
|
|
|
|
engine.register_type::<i32>();
|
|
|
|
engine.register_type::<u32>();
|
|
|
|
engine.register_type::<i64>();
|
|
|
|
engine.register_type::<u64>();
|
|
|
|
engine.register_type::<f32>();
|
|
|
|
engine.register_type::<f64>();
|
|
|
|
engine.register_type::<String>();
|
|
|
|
engine.register_type::<char>();
|
|
|
|
engine.register_type::<bool>();
|
|
|
|
|
|
|
|
macro_rules! reg_op {
|
|
|
|
($engine:expr, $x:expr, $op:expr, $( $y:ty ),*) => (
|
|
|
|
$(
|
2016-03-16 23:07:08 +01:00
|
|
|
$engine.register_fn($x, ($op as fn(x: $y, y: $y)->$y));
|
2016-03-01 15:40:48 +01:00
|
|
|
)*
|
|
|
|
)
|
|
|
|
}
|
2016-04-14 03:01:08 +02:00
|
|
|
|
2016-03-01 15:40:48 +01:00
|
|
|
macro_rules! reg_cmp {
|
|
|
|
($engine:expr, $x:expr, $op:expr, $( $y:ty ),*) => (
|
|
|
|
$(
|
2016-03-16 23:07:08 +01:00
|
|
|
$engine.register_fn($x, ($op as fn(x: $y, y: $y)->bool));
|
2016-03-01 15:40:48 +01:00
|
|
|
)*
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-10-02 23:51:04 +02:00
|
|
|
fn add<T: Add>(x: T, y: T) -> <T as Add>::Output { 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 div<T: Div>(x: T, y: T) -> <T as Div>::Output { x / y }
|
|
|
|
fn lt<T: Ord>(x: T, y: T) -> bool { x < y }
|
|
|
|
fn lte<T: Ord>(x: T, y: T) -> bool { x <= y }
|
|
|
|
fn gt<T: Ord>(x: T, y: T) -> bool { x > y }
|
|
|
|
fn gte<T: Ord>(x: T, y: T) -> bool { x >= y }
|
|
|
|
fn eq<T: Eq>(x: T, y: T) -> bool { x == y }
|
|
|
|
fn ne<T: Eq>(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 concat(x: String, y: String) -> String { x + &y }
|
2016-03-02 16:49:24 +01:00
|
|
|
|
2016-03-01 15:40:48 +01:00
|
|
|
reg_op!(engine, "+", add, i32, i64, u32, u64, f32, f64);
|
|
|
|
reg_op!(engine, "-", sub, i32, i64, u32, u64, f32, f64);
|
|
|
|
reg_op!(engine, "*", mul, i32, i64, u32, u64, f32, f64);
|
|
|
|
reg_op!(engine, "/", div, i32, i64, u32, u64, f32, f64);
|
|
|
|
|
2016-08-18 03:33:31 +02:00
|
|
|
reg_cmp!(engine, "<", lt, i32, i64, u32, u64, String);
|
|
|
|
reg_cmp!(engine, "<=", lte, i32, i64, u32, u64, String);
|
|
|
|
reg_cmp!(engine, ">", gt, i32, i64, u32, u64, String);
|
|
|
|
reg_cmp!(engine, ">=", gte, i32, i64, u32, u64, String);
|
|
|
|
reg_cmp!(engine, "==", eq, i32, i64, u32, u64, bool, String);
|
|
|
|
reg_cmp!(engine, "!=", ne, i32, i64, u32, u64, bool, String);
|
2016-03-01 15:40:48 +01:00
|
|
|
|
|
|
|
reg_op!(engine, "||", or, bool);
|
|
|
|
reg_op!(engine, "&&", and, bool);
|
2016-03-26 18:46:28 +01:00
|
|
|
|
2016-08-18 03:33:31 +02:00
|
|
|
engine.register_fn("+", concat);
|
|
|
|
|
2016-04-17 04:32:18 +02:00
|
|
|
// engine.register_fn("[]", idx);
|
|
|
|
// FIXME? Registering array lookups are a special case because we want to return boxes
|
2017-10-02 23:44:45 +02:00
|
|
|
// directly let ent = engine.fns.entry("[]".to_string()).or_insert_with(Vec::new);
|
2016-04-17 04:32:18 +02:00
|
|
|
// (*ent).push(FnType::ExternalFn2(Box::new(idx)));
|
2016-03-01 15:40:48 +01:00
|
|
|
}
|
|
|
|
|
2016-02-29 22:43:45 +01:00
|
|
|
pub fn new() -> Engine {
|
2016-04-17 04:32:18 +02:00
|
|
|
let mut engine = Engine { fns: HashMap::new() };
|
2016-03-01 15:40:48 +01:00
|
|
|
|
|
|
|
Engine::register_default_lib(&mut engine);
|
|
|
|
|
|
|
|
engine
|
|
|
|
}
|
|
|
|
}
|