Starting to refactor binop
This commit is contained in:
parent
44ae10b7ae
commit
44938aa4e6
@ -106,12 +106,14 @@ impl FromStr for Builtin {
|
|||||||
|
|
||||||
#[derive(Debug, PartialEq, Clone)]
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
pub struct BinOp {
|
pub struct BinOp {
|
||||||
sigil: Rc<String>
|
sigil: Rc<String>,
|
||||||
|
pub builtin: Option<Builtin>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BinOp {
|
impl BinOp {
|
||||||
pub fn from_sigil(sigil: &str) -> 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> {
|
pub fn sigil(&self) -> &Rc<String> {
|
||||||
&self.sigil
|
&self.sigil
|
||||||
@ -131,11 +133,6 @@ impl BinOp {
|
|||||||
Some(BinOp::from_sigil(s))
|
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 {
|
pub fn min_precedence() -> i32 {
|
||||||
i32::min_value()
|
i32::min_value()
|
||||||
}
|
}
|
||||||
|
@ -336,9 +336,9 @@ impl<'a> TypeContext<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn binexp(&mut self, op: &BinOp, lhs: &Expression, rhs: &Expression) -> InferResult<Type> {
|
fn binexp(&mut self, op: &BinOp, lhs: &Expression, rhs: &Expression) -> InferResult<Type> {
|
||||||
let tf = match op.get_type() {
|
let tf = match op.builtin.map(|b| b.get_type()) {
|
||||||
Ok(ty) => ty,
|
Some(ty) => ty,
|
||||||
Err(e) => return TypeError::new(e),
|
None => return TypeError::new("no type found"),
|
||||||
};
|
};
|
||||||
|
|
||||||
let t_lhs = self.expr(lhs)?;
|
let t_lhs = self.expr(lhs)?;
|
||||||
|
Loading…
Reference in New Issue
Block a user