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();
}
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);
+37 -23
View File
@@ -59,15 +59,16 @@ impl LocalScope {
}
pub fn get_reference(&self, name: &String) -> Option<Arc<VarRef>> {
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<Arc<VarRef>> {
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<Arc<TypeRef>> {
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<Self>, param_list: &ParamList) {
for (param_name, (export, type_expr)) in &param_list.0 {
match type_expr {
pub fn with_params(self: &Arc<Self>, 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;
}
}