diff --git a/src/parser/parser.rs b/src/parser/parser.rs index fef1ca8..50c6edd 100644 --- a/src/parser/parser.rs +++ b/src/parser/parser.rs @@ -153,11 +153,24 @@ impl Parser { let token = self.cur_token.clone(); self.next_token(); - Expr::Binary(ExprBinary::new( - BinOp::from(&token), - Box::new(left), - Box::new(self.expr(token.precedence())), - )) + let left = Box::new(left); + let right = Box::new(self.expr(token.precedence())); + let op = BinOp::from(&token); + + if op == BinOp::Assign { + if !left + .type_(&self.symtable) + .assignable(&right.type_(&self.symtable)) + { + panic!( + "Cant assign {:?} to {:?}", + right.type_(&self.symtable), + left.type_(&self.symtable) + ); + } + } + + Expr::Binary(ExprBinary::new(op, left, right)) } fn unary_expr(&mut self) -> Expr { diff --git a/src/parser/type_.rs b/src/parser/type_.rs index a2d2d5a..692c9c9 100644 --- a/src/parser/type_.rs +++ b/src/parser/type_.rs @@ -52,4 +52,18 @@ impl Type { right } + + pub fn assignable(&self, type_: &Self) -> bool { + if self.int() && type_.int() { + if (self.signed() && !type_.signed()) || (!self.signed() && type_.signed()) { + return false; + } + + if self >= type_ { + return true; + } + } + + false + } }