From 44938aa4e64b422a566c487c7e8b6b5b0a5ffc84 Mon Sep 17 00:00:00 2001 From: greg Date: Wed, 14 Aug 2019 09:26:08 -0700 Subject: [PATCH] Starting to refactor binop --- schala-lang/language/src/builtin.rs | 11 ++++------- schala-lang/language/src/typechecking.rs | 6 +++--- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/schala-lang/language/src/builtin.rs b/schala-lang/language/src/builtin.rs index fcbf97d..6093fc9 100644 --- a/schala-lang/language/src/builtin.rs +++ b/schala-lang/language/src/builtin.rs @@ -106,12 +106,14 @@ impl FromStr for Builtin { #[derive(Debug, PartialEq, Clone)] pub struct BinOp { - sigil: Rc + sigil: Rc, + pub builtin: Option, } 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 { &self.sigil @@ -131,11 +133,6 @@ impl BinOp { Some(BinOp::from_sigil(s)) } - pub fn get_type(&self) -> Result { - 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() } diff --git a/schala-lang/language/src/typechecking.rs b/schala-lang/language/src/typechecking.rs index 756276f..7e7e73f 100644 --- a/schala-lang/language/src/typechecking.rs +++ b/schala-lang/language/src/typechecking.rs @@ -336,9 +336,9 @@ impl<'a> TypeContext<'a> { } fn binexp(&mut self, op: &BinOp, lhs: &Expression, rhs: &Expression) -> InferResult { - 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)?;