From 7218898ce0c9a992c63a9debdb1ed0f12a969517 Mon Sep 17 00:00:00 2001 From: Araozu Date: Mon, 6 May 2024 09:53:18 -0500 Subject: [PATCH] Naive typechecking of binary operators --- src/semantic/types/expression.rs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/semantic/types/expression.rs b/src/semantic/types/expression.rs index 1427ce1..89d64a3 100644 --- a/src/semantic/types/expression.rs +++ b/src/semantic/types/expression.rs @@ -26,12 +26,30 @@ impl Typed for Expression<'_> { })) } }; - + Ok(datatype) } Expression::FunctionCall(_) => todo!(), Expression::UnaryOperator(_, _) => todo!(), - Expression::BinaryOperator(_, _, _) => todo!(), + Expression::BinaryOperator(exp1, exp2, operator) => { + let t1 = exp1.get_type(scope)?; + let t2 = exp2.get_type(scope)?; + + // TODO: There's definitely a better way to do this + if *operator == "+" && t1 == "Int" && t2 == "Int" { + return Ok("Int".into()); + } else if *operator == "-" && t1 == "Int" && t2 == "Int" { + return Ok("Int".into()); + } + + return Err(MistiError::Semantic(SemanticError { + error_start: 0, + error_end: 1, + reason: format!( + "Unsupported binary operator or invalid arguments to the operator." + ), + })); + } } } }