Fill out support for operators

This commit is contained in:
jonathandturner
2016-03-01 09:40:48 -05:00
parent 02935fd8cf
commit 419781cfb6
5 changed files with 331 additions and 439 deletions

View File

@@ -7,7 +7,47 @@ pub trait FnRegister {
fn register(self, engine: &mut Engine, name: &str);
}
impl<T: Any, U: Any> FnRegister for fn(&mut T)->U {
impl<T: Any+Clone, U: Any+Clone, V: Any+Clone> FnRegister for fn(&mut T, U)->V {
fn register(self, engine: &mut Engine, name: &str) {
let wrapped : Box<Fn(&mut Box<Any>, &mut Box<Any>)->Result<Box<Any>, EvalError>> =
Box::new(
move |x: &mut Box<Any>, y: &mut Box<Any>| {
let inside1 = (*x).downcast_mut() as Option<&mut T>;
let inside2 = (*y).downcast_mut() as Option<&mut U>;
match (inside1, inside2) {
(Some(b), Some(c)) => Ok(Box::new(self(b, c.clone())) as Box<Any>),
_ => Err(EvalError::FunctionArgMismatch)
}
}
);
let ent = engine.fns_arity_2.entry(name.to_string()).or_insert(Vec::new());
(*ent).push(wrapped);
}
}
impl<T: Any+Clone, U: Any+Clone, V: Any+Clone> FnRegister for fn(T, U)->V {
fn register(self, engine: &mut Engine, name: &str) {
let wrapped : Box<Fn(&mut Box<Any>, &mut Box<Any>)->Result<Box<Any>, EvalError>> =
Box::new(
move |x: &mut Box<Any>, y: &mut Box<Any>| {
let inside1 = (*x).downcast_mut() as Option<&mut T>;
let inside2 = (*y).downcast_mut() as Option<&mut U>;
match (inside1, inside2) {
(Some(b), Some(c)) => Ok(Box::new(self(b.clone(), c.clone())) as Box<Any>),
_ => Err(EvalError::FunctionArgMismatch)
}
}
);
let ent = engine.fns_arity_2.entry(name.to_string()).or_insert(Vec::new());
(*ent).push(wrapped);
}
}
impl<T: Any+Clone, U: Any+Clone> FnRegister for fn(&mut T)->U {
fn register(self, engine: &mut Engine, name: &str) {
let wrapped : Box<Fn(&mut Box<Any>)->Result<Box<Any>, EvalError>> =
Box::new(
@@ -26,7 +66,7 @@ impl<T: Any, U: Any> FnRegister for fn(&mut T)->U {
}
}
impl<T: Any+Clone, U: Any> FnRegister for fn(T)->U {
impl<T: Any+Clone, U: Any+Clone> FnRegister for fn(T)->U {
fn register(self, engine: &mut Engine, name: &str) {
let wrapped : Box<Fn(&mut Box<Any>)->Result<Box<Any>, EvalError>> =
Box::new(
@@ -44,7 +84,7 @@ impl<T: Any+Clone, U: Any> FnRegister for fn(T)->U {
}
}
impl<T: Any> FnRegister for fn()->T {
impl<T: Any+Clone> FnRegister for fn()->T {
fn register(self, engine: &mut Engine, name: &str) {
let wrapped : Box<Fn()->Result<Box<Any>, EvalError>> =
Box::new(