More operator stuff

This commit is contained in:
greg 2018-02-26 02:21:21 -08:00
parent 547def990e
commit 63c3e0a4db
3 changed files with 20 additions and 1 deletions

View File

@ -44,6 +44,10 @@ impl PrefixOp {
pub fn is_prefix(op: &str) -> bool { pub fn is_prefix(op: &str) -> bool {
PREFIX_OPS.get(op).is_some() PREFIX_OPS.get(op).is_some()
} }
pub fn get_type(&self) -> TypeResult<Type> {
let s = self.sigil.as_str();
PREFIX_OPS.get(s).map(|x| x.0.clone()).ok_or(format!("Prefix op {} not found", s))
}
} }
lazy_static! { lazy_static! {
static ref PREFIX_OPS: HashMap<&'static str, (Type, ())> = static ref PREFIX_OPS: HashMap<&'static str, (Type, ())> =
@ -51,7 +55,6 @@ lazy_static! {
"+" => (Func(bx!(Const(Int)), bx!(Const(Int))), ()), "+" => (Func(bx!(Const(Int)), bx!(Const(Int))), ()),
"-" => (Func(bx!(Const(Int)), bx!(Const(Int))), ()), "-" => (Func(bx!(Const(Int)), bx!(Const(Int))), ()),
"!" => (Func(bx!(Const(Bool)), bx!(Const(Bool))), ()), "!" => (Func(bx!(Const(Bool)), bx!(Const(Bool))), ()),
"~" => (Func(bx!(Const(Int)), bx!(Const(Int))), ()),
}; };
} }
@ -67,5 +70,8 @@ lazy_static! {
"//" => (Func(bx!(Const(Int)), bx!(Func(bx!(Const(Int)), bx!(Const(Int))))), (), 20), "//" => (Func(bx!(Const(Int)), bx!(Func(bx!(Const(Int)), bx!(Const(Int))))), (), 20),
"%" => (Func(bx!(Const(Int)), bx!(Func(bx!(Const(Int)), bx!(Const(Int))))), (), 20), "%" => (Func(bx!(Const(Int)), bx!(Func(bx!(Const(Int)), bx!(Const(Int))))), (), 20),
"++" => (Func(bx!(Const(StringT)), bx!(Func(bx!(Const(StringT)), bx!(Const(StringT))))), (), 30), "++" => (Func(bx!(Const(StringT)), bx!(Func(bx!(Const(StringT)), bx!(Const(StringT))))), (), 30),
"^" => (Func(bx!(Const(Int)), bx!(Func(bx!(Const(Int)), bx!(Const(Int))))), (), 20),
"&" => (Func(bx!(Const(Int)), bx!(Func(bx!(Const(Int)), bx!(Const(Int))))), (), 20),
"|" => (Func(bx!(Const(Int)), bx!(Func(bx!(Const(Int)), bx!(Const(Int))))), (), 20),
}; };
} }

View File

@ -190,6 +190,9 @@ impl<'a> State<'a> {
UnsignedInt(l / r) UnsignedInt(l / r)
}, },
("%", UnsignedInt(l), UnsignedInt(r)) => UnsignedInt(l % r), ("%", UnsignedInt(l), UnsignedInt(r)) => UnsignedInt(l % r),
("^", UnsignedInt(l), UnsignedInt(r)) => UnsignedInt(l ^ r),
("&", UnsignedInt(l), UnsignedInt(r)) => UnsignedInt(l & r),
("|", UnsignedInt(l), UnsignedInt(r)) => UnsignedInt(l | r),
_ => return Err(format!("Runtime error: not yet implemented")), _ => return Err(format!("Runtime error: not yet implemented")),
}) })
} }
@ -204,6 +207,8 @@ impl<'a> State<'a> {
("!", Bool(false)) => Bool(true), ("!", Bool(false)) => Bool(true),
("-", UnsignedInt(n)) => SignedInt(-1*(n as i64)), ("-", UnsignedInt(n)) => SignedInt(-1*(n as i64)),
("-", SignedInt(n)) => SignedInt(-1*(n as i64)), ("-", SignedInt(n)) => SignedInt(-1*(n as i64)),
("+", SignedInt(n)) => SignedInt(n),
("+", UnsignedInt(n)) => UnsignedInt(n),
_ => return Err(format!("Runtime error: not yet implemented")), _ => return Err(format!("Runtime error: not yet implemented")),
}) })
} }

View File

@ -110,6 +110,14 @@ impl TypeContext {
other => return Err(format!("{:?} is not a binary function type", other)) other => return Err(format!("{:?} is not a binary function type", other))
} }
}, },
&PrefixExp(ref op, ref expr) => match op.get_type()? {
Func(box t1, box t2) => {
let expr_ty = self.infer(expr)?;
self.unify(t1, expr_ty)?;
Ok(t2)
},
other => return Err(format!("{:?} is not a prefix op function type", other))
},
/* /*
PrefixExp(Operation, Box<Expression>), PrefixExp(Operation, Box<Expression>),
TupleLiteral(Vec<Expression>), TupleLiteral(Vec<Expression>),