Add line/position in compilation errors; Add compile to AST; FIx infinite loop in close parens.
This commit is contained in:
parent
b3247204c6
commit
e4e471fd20
220
src/engine.rs
220
src/engine.rs
@ -9,13 +9,14 @@ 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::{RegisterBoxFn, 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};
|
use fmt::{Debug, Display};
|
||||||
|
|
||||||
type Array = Vec<Box<dyn Any>>;
|
type Array = Vec<Box<dyn Any>>;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum EvalAltResult {
|
pub enum EvalAltResult {
|
||||||
|
ErrorParseError(ParseError),
|
||||||
ErrorFunctionNotFound(String),
|
ErrorFunctionNotFound(String),
|
||||||
ErrorFunctionArgMismatch,
|
ErrorFunctionArgMismatch,
|
||||||
ErrorArrayOutOfBounds(usize, i64),
|
ErrorArrayOutOfBounds(usize, i64),
|
||||||
@ -27,20 +28,20 @@ pub enum EvalAltResult {
|
|||||||
ErrorAssignmentToUnknownLHS,
|
ErrorAssignmentToUnknownLHS,
|
||||||
ErrorMismatchOutputType(String),
|
ErrorMismatchOutputType(String),
|
||||||
ErrorCantOpenScriptFile(String),
|
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::ErrorCantOpenScriptFile(ref s) => Some(s.as_str()),
|
EvalAltResult::ErrorCantOpenScriptFile(ref s)
|
||||||
EvalAltResult::ErrorVariableNotFound(ref s) => Some(s.as_str()),
|
| EvalAltResult::ErrorVariableNotFound(ref s)
|
||||||
EvalAltResult::ErrorFunctionNotFound(ref s) => Some(s.as_str()),
|
| EvalAltResult::ErrorFunctionNotFound(ref s)
|
||||||
EvalAltResult::ErrorMismatchOutputType(ref s) => Some(s.as_str()),
|
| EvalAltResult::ErrorMismatchOutputType(ref s) => s,
|
||||||
_ => None,
|
_ => return None,
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,6 +50,7 @@ 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,
|
||||||
@ -62,7 +64,7 @@ impl PartialEq for EvalAltResult {
|
|||||||
(&ErrorAssignmentToUnknownLHS, &ErrorAssignmentToUnknownLHS) => true,
|
(&ErrorAssignmentToUnknownLHS, &ErrorAssignmentToUnknownLHS) => true,
|
||||||
(&ErrorMismatchOutputType(ref a), &ErrorMismatchOutputType(ref b)) => a == b,
|
(&ErrorMismatchOutputType(ref a), &ErrorMismatchOutputType(ref b)) => a == b,
|
||||||
(&ErrorCantOpenScriptFile(ref a), &ErrorCantOpenScriptFile(ref b)) => a == b,
|
(&ErrorCantOpenScriptFile(ref a), &ErrorCantOpenScriptFile(ref b)) => a == b,
|
||||||
(&InternalErrorMalformedDotExpression, &InternalErrorMalformedDotExpression) => true,
|
(&ErrorMalformedDotExpression, &ErrorMalformedDotExpression) => true,
|
||||||
(&LoopBreak, &LoopBreak) => true,
|
(&LoopBreak, &LoopBreak) => true,
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
@ -72,6 +74,7 @@ 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 => "Array access expects integer index",
|
EvalAltResult::ErrorIndexMismatch => "Array access expects integer index",
|
||||||
@ -85,15 +88,13 @@ impl Error for EvalAltResult {
|
|||||||
EvalAltResult::ErrorForMismatch => "For loops expect array",
|
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)",
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,6 +109,7 @@ impl fmt::Display for EvalAltResult {
|
|||||||
write!(f, "{}: {}", self.description(), s)
|
write!(f, "{}: {}", self.description(), s)
|
||||||
} else {
|
} else {
|
||||||
match self {
|
match self {
|
||||||
|
EvalAltResult::ErrorParseError(ref p) => write!(f, "Syntax error: {}", p),
|
||||||
EvalAltResult::ErrorArrayOutOfBounds(_, index) if *index < 0 => {
|
EvalAltResult::ErrorArrayOutOfBounds(_, index) if *index < 0 => {
|
||||||
write!(f, "{}: {} < 0", self.description(), index)
|
write!(f, "{}: {} < 0", self.description(), index)
|
||||||
}
|
}
|
||||||
@ -224,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),
|
||||||
@ -308,7 +314,7 @@ 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: Array = args
|
let mut args: Array = args
|
||||||
.iter()
|
.iter()
|
||||||
.map(|arg| self.eval_expr(scope, arg))
|
.map(|arg| self.eval_expr(scope, arg))
|
||||||
@ -351,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),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -429,7 +435,7 @@ impl Engine {
|
|||||||
|
|
||||||
value
|
value
|
||||||
}
|
}
|
||||||
_ => Err(EvalAltResult::InternalErrorMalformedDotExpression),
|
_ => Err(EvalAltResult::ErrorMalformedDotExpression),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -458,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),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -492,16 +498,16 @@ impl Engine {
|
|||||||
|
|
||||||
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 {
|
||||||
@ -575,7 +581,7 @@ 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))
|
||||||
@ -687,7 +693,7 @@ impl Engine {
|
|||||||
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)?;
|
||||||
@ -700,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;
|
||||||
@ -721,52 +755,58 @@ impl Engine {
|
|||||||
/// 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>(
|
||||||
let mut x: Result<Box<dyn Any>, EvalAltResult> = Ok(Box::new(()));
|
&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(()));
|
||||||
|
|
||||||
for f in fns {
|
for f in fns {
|
||||||
let name = f.name.clone();
|
let name = f.name.clone();
|
||||||
let local_f = f.clone();
|
let local_f = f.clone();
|
||||||
|
|
||||||
let spec = FnSpec {
|
let spec = FnSpec {
|
||||||
ident: name,
|
ident: name,
|
||||||
args: None,
|
args: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
self.fns.insert(spec, Arc::new(FnIntExt::Int(local_f)));
|
self.fns.insert(spec, Arc::new(FnIntExt::Int(local_f)));
|
||||||
}
|
}
|
||||||
|
|
||||||
for o in os {
|
for o in os {
|
||||||
x = match self.eval_stmt(scope, o) {
|
x = match self.eval_stmt(scope, o) {
|
||||||
Ok(v) => Ok(v),
|
Ok(v) => Ok(v),
|
||||||
Err(e) => return Err(e),
|
Err(e) => return Err(e),
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let x = x?;
|
|
||||||
|
|
||||||
match x.downcast::<T>() {
|
|
||||||
Ok(out) => Ok(*out),
|
|
||||||
Err(a) => Err(EvalAltResult::ErrorMismatchOutputType((*a).type_name())),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Err(_) => Err(EvalAltResult::ErrorFunctionArgMismatch),
|
}
|
||||||
|
|
||||||
|
let x = x?;
|
||||||
|
|
||||||
|
match x.downcast::<T>() {
|
||||||
|
Ok(out) => Ok(*out),
|
||||||
|
Err(a) => Err(EvalAltResult::ErrorMismatchOutputType((*a).type_name())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -815,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(());
|
||||||
@ -878,7 +918,7 @@ impl Engine {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! reg_func2 {
|
macro_rules! reg_func2x {
|
||||||
($engine:expr, $x:expr, $op:expr, $v:ty, $r:ty, $( $y:ty ),*) => (
|
($engine:expr, $x:expr, $op:expr, $v:ty, $r:ty, $( $y:ty ),*) => (
|
||||||
$(
|
$(
|
||||||
$engine.register_fn($x, $op as fn(x: $v, y: $y)->$r);
|
$engine.register_fn($x, $op as fn(x: $v, y: $y)->$r);
|
||||||
@ -886,7 +926,7 @@ impl Engine {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! reg_func2b {
|
macro_rules! reg_func2y {
|
||||||
($engine:expr, $x:expr, $op:expr, $v:ty, $r:ty, $( $y:ty ),*) => (
|
($engine:expr, $x:expr, $op:expr, $v:ty, $r:ty, $( $y:ty ),*) => (
|
||||||
$(
|
$(
|
||||||
$engine.register_fn($x, $op as fn(y: $y, x: $v)->$r);
|
$engine.register_fn($x, $op as fn(y: $y, x: $v)->$r);
|
||||||
@ -1018,54 +1058,42 @@ impl Engine {
|
|||||||
reg_func1!(engine, "print", print, (), i32, i64, u32, u64);
|
reg_func1!(engine, "print", print, (), i32, i64, u32, u64);
|
||||||
reg_func1!(engine, "print", print, (), f32, f64, bool, String);
|
reg_func1!(engine, "print", print, (), f32, f64, bool, String);
|
||||||
reg_func1!(engine, "print", print_debug, (), Array);
|
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, (), i32, i64, u32, u64);
|
||||||
reg_func1!(engine, "debug", print_debug, (), f32, f64, bool, String);
|
reg_func1!(engine, "debug", print_debug, (), f32, f64, bool, String);
|
||||||
reg_func1!(engine, "debug", print_debug, (), Array);
|
reg_func1!(engine, "debug", print_debug, (), Array, ());
|
||||||
|
|
||||||
// Register array functions
|
// Register array functions
|
||||||
fn push<T: Any + 'static>(list: &mut Array, item: T) {
|
fn push<T: Any + 'static>(list: &mut Array, item: T) {
|
||||||
list.push(Box::new(item));
|
list.push(Box::new(item));
|
||||||
}
|
}
|
||||||
fn pop(list: &mut Array) -> Box<dyn Any> {
|
|
||||||
list.pop().unwrap()
|
reg_func2x!(engine, "push", push, &mut Array, (), i32, i64, u32, u64);
|
||||||
}
|
reg_func2x!(engine, "push", push, &mut Array, (), f32, f64, bool);
|
||||||
fn shift(list: &mut Array) -> Box<dyn Any> {
|
reg_func2x!(engine, "push", push, &mut Array, (), String, Array, ());
|
||||||
list.remove(0)
|
|
||||||
}
|
engine.register_box_fn("pop", |list: &mut Array| list.pop().unwrap());
|
||||||
fn len(list: &mut Array) -> i64 {
|
engine.register_box_fn("shift", |list: &mut Array| list.remove(0));
|
||||||
|
engine.register_fn("len", |list: &mut Array| -> i64 {
|
||||||
list.len().try_into().unwrap()
|
list.len().try_into().unwrap()
|
||||||
}
|
});
|
||||||
|
|
||||||
reg_func2!(engine, "push", push, &mut Array, (), i32, i64, u32, u64);
|
|
||||||
reg_func2!(engine, "push", push, &mut Array, (), f32, f64, bool);
|
|
||||||
reg_func2!(engine, "push", push, &mut Array, (), String, Array);
|
|
||||||
|
|
||||||
engine.register_box_fn("pop", pop);
|
|
||||||
engine.register_box_fn("shift", shift);
|
|
||||||
engine.register_fn("len", len);
|
|
||||||
|
|
||||||
// Register string concatenate functions
|
// Register string concatenate functions
|
||||||
fn prepend<T: Display>(x: T, y: String) -> String {
|
fn prepend<T: Display>(x: T, y: String) -> String {
|
||||||
format!("{}{}", x, y)
|
format!("{}{}", x, y)
|
||||||
}
|
}
|
||||||
fn append<T: Display>(x: &mut String, y: T) -> String {
|
fn append<T: Display>(x: String, y: T) -> String {
|
||||||
format!("{}{}", x, y)
|
format!("{}{}", x, y)
|
||||||
}
|
}
|
||||||
fn prepend_array(x: Array, y: String) -> String {
|
|
||||||
format!("{:?}{}", x, y)
|
|
||||||
}
|
|
||||||
fn append_array(x: &mut String, y: Array) -> String {
|
|
||||||
format!("{}{:?}", x, y)
|
|
||||||
}
|
|
||||||
|
|
||||||
reg_func2!(engine, "+", append, &mut String, String, i32, i64);
|
reg_func2x!(engine, "+", append, String, String, i32, i64, u32, u64, f32, f64, bool);
|
||||||
reg_func2!(engine, "+", append, &mut String, String, u32, u64);
|
engine.register_fn("+", |x: String, y: Array| format!("{}{:?}", x, y));
|
||||||
reg_func2!(engine, "+", append, &mut String, String, f32, f64, bool);
|
engine.register_fn("+", |x: String, _: ()| format!("{}", x));
|
||||||
engine.register_fn("+", append_array);
|
|
||||||
|
|
||||||
reg_func2b!(engine, "+", prepend, String, String, i32, i64, u32, u64, f32, f64, bool);
|
reg_func2y!(engine, "+", prepend, String, String, i32, i64, u32, u64, f32, f64, bool);
|
||||||
engine.register_fn("+", prepend_array);
|
engine.register_fn("+", |x: Array, y: String| format!("{:?}{}", x, y));
|
||||||
|
engine.register_fn("+", |_: (), y: String| format!("{}", y));
|
||||||
|
|
||||||
// Register array iterator
|
// Register array iterator
|
||||||
engine.register_iterator::<Array, _>(|a| {
|
engine.register_iterator::<Array, _>(|a| {
|
||||||
|
@ -48,3 +48,4 @@ 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::{RegisterBoxFn, RegisterFn};
|
pub use fn_register::{RegisterBoxFn, RegisterFn};
|
||||||
|
pub use parser::{ParseError, ParseErrorType, AST};
|
||||||
|
967
src/parser.rs
967
src/parser.rs
File diff suppressed because it is too large
Load Diff
@ -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()
|
||||||
))
|
))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -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()
|
||||||
))
|
))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user