Fill out support for operators
This commit is contained in:
263
src/engine.rs
263
src/engine.rs
@@ -6,14 +6,16 @@ use std::fmt;
|
||||
|
||||
use parser::{lex, parse, Expr, Stmt };
|
||||
use fn_register::FnRegister;
|
||||
use ops::{add_boxes, sub_boxes, mult_boxes, div_boxes};
|
||||
//use ops::{add_boxes, sub_boxes, mult_boxes, div_boxes};
|
||||
|
||||
use std::ops::{Add, Sub, Mul, Div};
|
||||
use std::cmp::{Ord, Eq};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum EvalError {
|
||||
FunctionNotFound,
|
||||
FunctionArgMismatch,
|
||||
FunctionCallNotSupported,
|
||||
TypeMismatchForOperands,
|
||||
IfGuardMismatch,
|
||||
VariableNotFound
|
||||
}
|
||||
@@ -23,8 +25,7 @@ impl Error for EvalError {
|
||||
match *self {
|
||||
EvalError::FunctionNotFound => "Function not found",
|
||||
EvalError::FunctionArgMismatch => "Function argument types do not match",
|
||||
EvalError::FunctionCallNotSupported => "Function call with > 1 argument not supported",
|
||||
EvalError::TypeMismatchForOperands => "Operand types incompatible with operator",
|
||||
EvalError::FunctionCallNotSupported => "Function call with > 2 argument not supported",
|
||||
EvalError::IfGuardMismatch => "If guards expect boolean expression",
|
||||
EvalError::VariableNotFound => "Variable not found",
|
||||
}
|
||||
@@ -41,7 +42,56 @@ impl fmt::Display for EvalError {
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
pub struct Engine {
|
||||
pub fns_arity_2: HashMap<String, Vec<Box<Fn(&mut Box<Any>, &mut Box<Any>)->Result<Box<Any>, EvalError>>>>,
|
||||
pub fns_arity_1: HashMap<String, Vec<Box<Fn(&mut Box<Any>)->Result<Box<Any>, EvalError>>>>,
|
||||
pub fns_arity_0: HashMap<String, Vec<Box<Fn()->Result<Box<Any>, EvalError>>>>,
|
||||
pub scope: Vec<(String, Box<Any>)>
|
||||
@@ -80,6 +130,22 @@ impl Engine {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn call_fn_2_arg(&self, name: &str, arg1: &mut Box<Any>, arg2: &mut Box<Any>) -> Result<Box<Any>, EvalError> {
|
||||
match self.fns_arity_2.get(name) {
|
||||
Some(vf) => {
|
||||
for f in vf {
|
||||
let invoke = f(arg1, arg2);
|
||||
match invoke {
|
||||
Ok(v) => return Ok(v),
|
||||
_ => ()
|
||||
}
|
||||
};
|
||||
Err(EvalError::FunctionArgMismatch)
|
||||
}
|
||||
None => Err(EvalError::FunctionNotFound)
|
||||
}
|
||||
}
|
||||
|
||||
fn register_type<T: Clone+Any>(&mut self) {
|
||||
fn clone_helper<T: Clone>(t:T)->T { t.clone() };
|
||||
|
||||
@@ -139,30 +205,16 @@ impl Engine {
|
||||
|
||||
self.call_fn_1_arg(&fn_name, &mut arg)
|
||||
}
|
||||
else if args.len() == 2 {
|
||||
let mut arg1 = try!(self.eval_expr(&args[0]));
|
||||
let mut arg2 = try!(self.eval_expr(&args[1]));
|
||||
|
||||
self.call_fn_2_arg(&fn_name, &mut arg1, &mut arg2)
|
||||
}
|
||||
else {
|
||||
Err(EvalError::FunctionCallNotSupported)
|
||||
}
|
||||
}
|
||||
Expr::Plus(ref a, ref b) => {
|
||||
let arg1 = try!(self.eval_expr(a));
|
||||
let arg2 = try!(self.eval_expr(b));
|
||||
add_boxes(arg1, arg2)
|
||||
}
|
||||
Expr::Minus(ref a, ref b) => {
|
||||
let arg1 = try!(self.eval_expr(a));
|
||||
let arg2 = try!(self.eval_expr(b));
|
||||
sub_boxes(arg1, arg2)
|
||||
}
|
||||
Expr::Multiply(ref a, ref b) => {
|
||||
let arg1 = try!(self.eval_expr(a));
|
||||
let arg2 = try!(self.eval_expr(b));
|
||||
mult_boxes(arg1, arg2)
|
||||
}
|
||||
Expr::Divide(ref a, ref b) => {
|
||||
let arg1 = try!(self.eval_expr(a));
|
||||
let arg2 = try!(self.eval_expr(b));
|
||||
div_boxes(arg1, arg2)
|
||||
}
|
||||
Expr::True => {
|
||||
Ok(Box::new(true))
|
||||
}
|
||||
@@ -254,20 +306,161 @@ impl Engine {
|
||||
}
|
||||
}
|
||||
|
||||
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 ),*) => (
|
||||
$(
|
||||
($op as fn(x: $y, y: $y)->$y).register($engine, $x);
|
||||
)*
|
||||
)
|
||||
}
|
||||
|
||||
macro_rules! reg_cmp {
|
||||
($engine:expr, $x:expr, $op:expr, $( $y:ty ),*) => (
|
||||
$(
|
||||
($op as fn(x: $y, y: $y)->bool).register($engine, $x);
|
||||
)*
|
||||
)
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
reg_cmp!(engine, "<", lt, i32, i64, u32, u64);
|
||||
reg_cmp!(engine, "<=", lte, i32, i64, u32, u64);
|
||||
reg_cmp!(engine, ">", gt, i32, i64, u32, u64);
|
||||
reg_cmp!(engine, ">=", gte, i32, i64, u32, u64);
|
||||
reg_cmp!(engine, "==", eq, i32, i64, u32, u64);
|
||||
reg_cmp!(engine, "!=", ne, i32, i64, u32, u64);
|
||||
|
||||
reg_op!(engine, "||", or, bool);
|
||||
reg_op!(engine, "&&", and, bool);
|
||||
}
|
||||
|
||||
pub fn new() -> Engine {
|
||||
let mut output = Engine { fns_arity_0: HashMap::new(), fns_arity_1: HashMap::new(), scope: Vec::new() };
|
||||
let mut engine = Engine {
|
||||
fns_arity_0: HashMap::new(),
|
||||
fns_arity_1: HashMap::new(),
|
||||
fns_arity_2: HashMap::new(),
|
||||
scope: Vec::new()
|
||||
};
|
||||
|
||||
output.register_type::<i32>();
|
||||
output.register_type::<u32>();
|
||||
output.register_type::<i64>();
|
||||
output.register_type::<u64>();
|
||||
output.register_type::<f32>();
|
||||
output.register_type::<f64>();
|
||||
output.register_type::<String>();
|
||||
output.register_type::<char>();
|
||||
output.register_type::<bool>();
|
||||
Engine::register_default_lib(&mut engine);
|
||||
|
||||
output
|
||||
engine
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn test_number_literal() {
|
||||
let mut engine = Engine::new();
|
||||
|
||||
if let Ok(result) = engine.eval("65".to_string()).unwrap().downcast::<i32>() {
|
||||
assert_eq!(*result, 65);
|
||||
}
|
||||
else {
|
||||
assert!(false);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_addition() {
|
||||
let mut engine = Engine::new();
|
||||
|
||||
if let Ok(result) = engine.eval("60 + 5".to_string()).unwrap().downcast::<i32>() {
|
||||
assert_eq!(*result, 65);
|
||||
}
|
||||
else {
|
||||
assert!(false);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bool_op() {
|
||||
let mut engine = Engine::new();
|
||||
|
||||
if let Ok(result) = engine.eval("true && (false || true)".to_string()).unwrap().downcast::<bool>() {
|
||||
assert_eq!(*result, true);
|
||||
}
|
||||
else {
|
||||
assert!(false);
|
||||
}
|
||||
|
||||
if let Ok(result) = engine.eval("false && (false || true)".to_string()).unwrap().downcast::<bool>() {
|
||||
assert_eq!(*result, false);
|
||||
}
|
||||
else {
|
||||
assert!(false);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_op_prec() {
|
||||
let mut engine = Engine::new();
|
||||
|
||||
if let Ok(result) = engine.eval("var x = 0; if x == 10 || true { x = 1} x".to_string()).unwrap().downcast::<i32>() {
|
||||
assert_eq!(*result, 1);
|
||||
}
|
||||
else {
|
||||
assert!(false);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_if() {
|
||||
let mut engine = Engine::new();
|
||||
|
||||
if let Ok(result) = engine.eval("if true { 55 }".to_string()).unwrap().downcast::<i32>() {
|
||||
assert_eq!(*result, 55);
|
||||
}
|
||||
else {
|
||||
assert!(false);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_var_scope() {
|
||||
let mut engine = Engine::new();
|
||||
|
||||
if let Ok(_) = engine.eval("var x = 4 + 5".to_string()) { } else { assert!(false); }
|
||||
|
||||
if let Ok(result) = engine.eval("x".to_string()).unwrap().downcast::<i32>() {
|
||||
assert_eq!(*result, 9);
|
||||
}
|
||||
else {
|
||||
assert!(false);
|
||||
}
|
||||
|
||||
if let Ok(_) = engine.eval("x = x + 1; x = x + 2;".to_string()) { } else { assert!(false); }
|
||||
|
||||
if let Ok(result) = engine.eval("x".to_string()).unwrap().downcast::<i32>() {
|
||||
assert_eq!(*result, 12);
|
||||
}
|
||||
else {
|
||||
assert!(false);
|
||||
}
|
||||
|
||||
if let Ok(_) = engine.eval("{var x = 3}".to_string()) { } else { assert!(false); }
|
||||
|
||||
if let Ok(result) = engine.eval("x".to_string()).unwrap().downcast::<i32>() {
|
||||
assert_eq!(*result, 12);
|
||||
}
|
||||
else {
|
||||
assert!(false);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user