Fix params

This commit is contained in:
2026-04-22 21:47:11 +02:00
parent aedf80822c
commit b4edb33cbc
3 changed files with 39 additions and 33 deletions
-5
View File
@@ -26,11 +26,6 @@ pub fn walk_ast(top_scope: Arc<Scope>, tl: &mut Vec<parser::ast::TopLevel>) {
*name = rf.name.clone(); *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()); let scope = LocalScope::new(top_scope.clone());
scope.with_params(params); scope.with_params(params);
+37 -23
View File
@@ -59,15 +59,16 @@ impl LocalScope {
} }
pub fn get_reference(&self, name: &String) -> Option<Arc<VarRef>> { pub fn get_reference(&self, name: &String) -> Option<Arc<VarRef>> {
let rf = self self.variables
.variables
.lock() .lock()
.unwrap() .unwrap()
.get(name) .get(name)
.cloned() .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<Arc<VarRef>> {
match self.get_reference(name) {
Some(v) => Some(v), Some(v) => Some(v),
None => { None => {
let var_ref = Arc::new(VarRef { let var_ref = Arc::new(VarRef {
@@ -121,6 +122,11 @@ impl LocalScope {
self.clone().with_statement(else_branch); 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<Arc<TypeRef>> { ) -> Option<Arc<TypeRef>> {
match expr { match expr {
ast::Expression::IntLiteral(_) => self ast::Expression::IntLiteral(_) => self
.get_reference(&"int".to_string()) .get_type_reference(&"int".to_string())
.map(|r| r.var_type.clone()), .map(|r| r.var_type.clone()),
ast::Expression::FloatLiteral(_) => self ast::Expression::FloatLiteral(_) => self
.get_reference(&"float".to_string()) .get_type_reference(&"float".to_string())
.map(|r| r.var_type.clone()), .map(|r| r.var_type.clone()),
ast::Expression::BoolLiteral(_) => self ast::Expression::BoolLiteral(_) => self
.get_reference(&"bool".to_string()) .get_type_reference(&"bool".to_string())
.map(|r| r.var_type.clone()), .map(|r| r.var_type.clone()),
ast::Expression::StringLiteral(_) => self ast::Expression::StringLiteral(_) => self
.get_reference(&"string".to_string()) .get_type_reference(&"string".to_string())
.map(|r| r.var_type.clone()), .map(|r| r.var_type.clone()),
ast::Expression::Identifier(id) => { ast::Expression::Identifier(id) => {
@@ -220,24 +226,32 @@ impl LocalScope {
} }
} }
pub fn with_params(self: &Arc<Self>, param_list: &ParamList) { pub fn with_params(self: &Arc<Self>, param_list: &mut ParamList) {
for (param_name, (export, type_expr)) in &param_list.0 { let mut new_param_list = HashMap::new();
match type_expr {
for (param_name, (export, type_expr)) in param_list.0.drain() {
match &type_expr {
parser::ast::TypeExpr::Identifier(id) => { parser::ast::TypeExpr::Identifier(id) => {
if let Some(var_type) = let new_name = self.parent.get_name(export);
self.parent.get_reference(id).map(|r| r.var_type.clone())
{ self.variables.lock().unwrap().insert(
self.variables.lock().unwrap().insert( param_name.clone(),
param_name.clone(), Arc::new(VarRef {
Arc::new(VarRef { export: export,
export: *export, var_type: self
var_type, .parent
name: param_name.clone(), .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;
} }
} }
+2 -5
View File
@@ -1,8 +1,5 @@
package main; package main;
import "fmt"; int test(int a, int b) {
return a + b;
void main() {
string test = "Hello, World";
fmt.println(test);
} }