More operator stuff
This commit is contained in:
parent
a564ffa1ce
commit
d9e96398a4
@ -44,6 +44,10 @@ impl PrefixOp {
|
||||
pub fn is_prefix(op: &str) -> bool {
|
||||
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! {
|
||||
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(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(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),
|
||||
};
|
||||
}
|
||||
|
@ -190,6 +190,9 @@ impl<'a> State<'a> {
|
||||
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")),
|
||||
})
|
||||
}
|
||||
@ -204,6 +207,8 @@ impl<'a> State<'a> {
|
||||
("!", Bool(false)) => Bool(true),
|
||||
("-", UnsignedInt(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")),
|
||||
})
|
||||
}
|
||||
|
@ -110,6 +110,14 @@ impl TypeContext {
|
||||
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>),
|
||||
TupleLiteral(Vec<Expression>),
|
||||
|
Loading…
Reference in New Issue
Block a user