From bd262f98f8b9b5bc616f481c8597e9030da64f52 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Wed, 22 Apr 2026 14:38:37 +0200 Subject: [PATCH] If statement expression --- semantic/src/scope.rs | 75 +++++++++++++++++++++++++++++++------------ test/src/main.ms | 15 +++++++++ 2 files changed, 69 insertions(+), 21 deletions(-) diff --git a/semantic/src/scope.rs b/semantic/src/scope.rs index 4ad0655..f7b638e 100644 --- a/semantic/src/scope.rs +++ b/semantic/src/scope.rs @@ -82,29 +82,44 @@ impl LocalScope { } } + pub fn with_statement(self: &Arc, statement: &mut Statement) { + match statement { + Statement::Block(b) => self.clone().with_block(b), + Statement::VarDecl { name, init, .. } => { + if let Some(init) = init { + let var_type = self.get_type_from_expr(init).unwrap(); + + self.variables.lock().unwrap().insert( + name.clone(), + Arc::new(VarRef { + export: false, + name: name.to_string(), + var_type, + }), + ); + } + } + Statement::Expression(e) => { + self.get_type_from_expr(e); + } + Statement::If { + condition, + then_branch, + else_branch, + } => { + self.get_type_from_expr(condition); + self.clone().with_statement(then_branch); + if let Some(else_branch) = else_branch { + self.clone().with_statement(else_branch); + } + } + _ => {} + } + } + pub fn with_block(self: &Arc, block: &mut parser::ast::Block) { for statement in &mut block.0 { - match statement { - Statement::Block(b) => self.clone().with_block(b), - Statement::VarDecl { name, init, .. } => { - if let Some(init) = init { - let var_type = self.get_type_from_expr(init).unwrap(); - - self.variables.lock().unwrap().insert( - name.clone(), - Arc::new(VarRef { - export: false, - name: name.to_string(), - var_type, - }), - ); - } - } - Statement::Expression(e) => { - self.get_type_from_expr(e); - } - _ => {} - } + self.with_statement(statement); } } @@ -137,6 +152,24 @@ impl LocalScope { } _ => unimplemented!(), }, + Postfix::Binary(op, right) => match op { + parser::ast::BinaryOp::Equal + | parser::ast::BinaryOp::NotEqual + | parser::ast::BinaryOp::GreaterThan + | parser::ast::BinaryOp::LessThan + | parser::ast::BinaryOp::GreaterThanOrEqual + | parser::ast::BinaryOp::LessThanOrEqual => { + self.get_type_from_expr(right)?; + current_type = Arc::new(TypeRef::Name("bool".to_string())); + } + parser::ast::BinaryOp::Plus + | parser::ast::BinaryOp::Minus + | parser::ast::BinaryOp::Multiply + | parser::ast::BinaryOp::Divide + | parser::ast::BinaryOp::Modulo => { + self.get_type_from_expr(right)?; + } + }, _ => unimplemented!(), } } diff --git a/test/src/main.ms b/test/src/main.ms index d994a44..0fc1fcc 100644 --- a/test/src/main.ms +++ b/test/src/main.ms @@ -3,5 +3,20 @@ package main; import "fmt"; function main() { + doSomething(); + let a = doSomethingElse(); + + if (a + 1 == 67 + 1) { + fmt.println(a); + } +} + +function doSomething() { fmt.println("hello"); } + +function doSomethingElse(): int { + fmt.println("world"); + + return 67; +}