If statement expression

This commit is contained in:
2026-04-22 14:38:37 +02:00
parent 22cb5aade3
commit bd262f98f8
2 changed files with 69 additions and 21 deletions
+35 -2
View File
@@ -82,8 +82,7 @@ impl LocalScope {
} }
} }
pub fn with_block(self: &Arc<Self>, block: &mut parser::ast::Block) { pub fn with_statement(self: &Arc<Self>, statement: &mut Statement) {
for statement in &mut block.0 {
match statement { match statement {
Statement::Block(b) => self.clone().with_block(b), Statement::Block(b) => self.clone().with_block(b),
Statement::VarDecl { name, init, .. } => { Statement::VarDecl { name, init, .. } => {
@@ -103,9 +102,25 @@ impl LocalScope {
Statement::Expression(e) => { Statement::Expression(e) => {
self.get_type_from_expr(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<Self>, block: &mut parser::ast::Block) {
for statement in &mut block.0 {
self.with_statement(statement);
}
} }
pub fn walk_postfixes( pub fn walk_postfixes(
@@ -137,6 +152,24 @@ impl LocalScope {
} }
_ => unimplemented!(), _ => 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!(), _ => unimplemented!(),
} }
} }
+15
View File
@@ -3,5 +3,20 @@ package main;
import "fmt"; import "fmt";
function main() { function main() {
doSomething();
let a = doSomethingElse();
if (a + 1 == 67 + 1) {
fmt.println(a);
}
}
function doSomething() {
fmt.println("hello"); fmt.println("hello");
} }
function doSomethingElse(): int {
fmt.println("world");
return 67;
}