diff --git a/semantic/src/lib.rs b/semantic/src/lib.rs index 9af6dfa..340e2ea 100644 --- a/semantic/src/lib.rs +++ b/semantic/src/lib.rs @@ -26,11 +26,6 @@ pub fn walk_ast(top_scope: Arc, tl: &mut Vec) { *name = rf.name.clone(); } - match &*rf.var_type { - hir::TypeRef::Function(f) => walk_param_list(&f.params, params), - _ => unimplemented!(), - } - let scope = LocalScope::new(top_scope.clone()); scope.with_params(params); diff --git a/semantic/src/scope.rs b/semantic/src/scope.rs index f920fff..78e88dc 100644 --- a/semantic/src/scope.rs +++ b/semantic/src/scope.rs @@ -59,15 +59,16 @@ impl LocalScope { } pub fn get_reference(&self, name: &String) -> Option> { - let rf = self - .variables + self.variables .lock() .unwrap() .get(name) .cloned() - .or_else(|| self.parent.get_reference(name)); + .or_else(|| self.parent.get_reference(name)) + } - match rf { + pub fn get_type_reference(&self, name: &String) -> Option> { + match self.get_reference(name) { Some(v) => Some(v), None => { let var_ref = Arc::new(VarRef { @@ -121,6 +122,11 @@ impl LocalScope { self.clone().with_statement(else_branch); } } + Statement::Return(expr) => { + if let Some(expr) = expr { + self.get_type_from_expr(expr); + } + } _ => {} } } @@ -193,19 +199,19 @@ impl LocalScope { ) -> Option> { match expr { ast::Expression::IntLiteral(_) => self - .get_reference(&"int".to_string()) + .get_type_reference(&"int".to_string()) .map(|r| r.var_type.clone()), ast::Expression::FloatLiteral(_) => self - .get_reference(&"float".to_string()) + .get_type_reference(&"float".to_string()) .map(|r| r.var_type.clone()), ast::Expression::BoolLiteral(_) => self - .get_reference(&"bool".to_string()) + .get_type_reference(&"bool".to_string()) .map(|r| r.var_type.clone()), ast::Expression::StringLiteral(_) => self - .get_reference(&"string".to_string()) + .get_type_reference(&"string".to_string()) .map(|r| r.var_type.clone()), ast::Expression::Identifier(id) => { @@ -220,24 +226,32 @@ impl LocalScope { } } - pub fn with_params(self: &Arc, param_list: &ParamList) { - for (param_name, (export, type_expr)) in ¶m_list.0 { - match type_expr { + pub fn with_params(self: &Arc, param_list: &mut ParamList) { + let mut new_param_list = HashMap::new(); + + for (param_name, (export, type_expr)) in param_list.0.drain() { + match &type_expr { parser::ast::TypeExpr::Identifier(id) => { - if let Some(var_type) = - self.parent.get_reference(id).map(|r| r.var_type.clone()) - { - self.variables.lock().unwrap().insert( - param_name.clone(), - Arc::new(VarRef { - export: *export, - var_type, - name: param_name.clone(), - }), - ); - } + let new_name = self.parent.get_name(export); + + self.variables.lock().unwrap().insert( + param_name.clone(), + Arc::new(VarRef { + export: export, + var_type: self + .parent + .get_reference(id) + .map(|r| r.var_type.clone()) + .unwrap(), + name: new_name.clone(), + }), + ); + + new_param_list.insert(new_name, (export, type_expr)); } } } + + param_list.0 = new_param_list; } } diff --git a/test/src/main.mist b/test/src/main.mist index 9a686bb..7bc9895 100644 --- a/test/src/main.mist +++ b/test/src/main.mist @@ -1,8 +1,5 @@ package main; -import "fmt"; - -void main() { - string test = "Hello, World"; - fmt.println(test); +int test(int a, int b) { + return a + b; }