Starting to refactor binop

This commit is contained in:
greg 2019-08-14 09:26:08 -07:00
parent 44ae10b7ae
commit 44938aa4e6
2 changed files with 7 additions and 10 deletions

View File

@ -106,12 +106,14 @@ impl FromStr for Builtin {
#[derive(Debug, PartialEq, Clone)]
pub struct BinOp {
sigil: Rc<String>
sigil: Rc<String>,
pub builtin: Option<Builtin>,
}
impl BinOp {
pub fn from_sigil(sigil: &str) -> BinOp {
BinOp { sigil: Rc::new(sigil.to_string()) }
let builtin = Builtin::from_str(sigil).ok();
BinOp { sigil: Rc::new(sigil.to_string()), builtin }
}
pub fn sigil(&self) -> &Rc<String> {
&self.sigil
@ -131,11 +133,6 @@ impl BinOp {
Some(BinOp::from_sigil(s))
}
pub fn get_type(&self) -> Result<Type, String> {
let s = self.sigil.as_str();
BINOPS.get(s).map(|x| x.0.clone()).ok_or(format!("Binop {} not found", s))
}
pub fn min_precedence() -> i32 {
i32::min_value()
}

View File

@ -336,9 +336,9 @@ impl<'a> TypeContext<'a> {
}
fn binexp(&mut self, op: &BinOp, lhs: &Expression, rhs: &Expression) -> InferResult<Type> {
let tf = match op.get_type() {
Ok(ty) => ty,
Err(e) => return TypeError::new(e),
let tf = match op.builtin.map(|b| b.get_type()) {
Some(ty) => ty,
None => return TypeError::new("no type found"),
};
let t_lhs = self.expr(lhs)?;