diff --git a/semantic/src/scope.rs b/semantic/src/scope.rs index 78e88dc..a5d7ea5 100644 --- a/semantic/src/scope.rs +++ b/semantic/src/scope.rs @@ -90,9 +90,17 @@ 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, .. } => { + Statement::VarDecl { + name, init, type_, .. + } => { if let Some(init) = init { - let var_type = self.get_type_from_expr(init).unwrap(); + let init_type = self.get_type_from_expr(init); + let var_type = type_ + .as_mut() + .map(|t| self.get_type_from_type_expr(t)) + .or_else(|| Some(init_type)) + .unwrap() + .unwrap(); let var_name = name.clone(); @@ -193,6 +201,22 @@ impl LocalScope { Some(current_type) } + pub fn get_type_from_type_expr( + self: &Arc, + expr: &mut ast::TypeExpr, + ) -> Option> { + match expr { + ast::TypeExpr::Identifier(id) => { + if let Some(v) = self.get_type_reference(id).map(|r| r.var_type.clone()) { + *id = v.get_name(); + Some(v) + } else { + None + } + } + } + } + pub fn get_type_from_expr( self: &Arc, expr: &mut ast::Expression, diff --git a/test/src/main.mist b/test/src/main.mist index 7bc9895..2c08dfc 100644 --- a/test/src/main.mist +++ b/test/src/main.mist @@ -1,5 +1,11 @@ package main; +public struct Idk { + int a, + int b, +} + int test(int a, int b) { - return a + b; + Idk bro = 10; + return a + b + bro.a; }