field naming

This commit is contained in:
2026-04-17 12:35:20 +02:00
parent 7996a3ffaa
commit d2f7c0c69b
3 changed files with 45 additions and 11 deletions
+11 -1
View File
@@ -163,10 +163,20 @@ impl TopLevelHirScope {
} }
pub fn next_var_idx(&self) -> usize { pub fn next_var_idx(&self) -> usize {
self.var_idx.fetch_add(1, Ordering::Relaxed) + 1 self.var_idx.fetch_add(1, Ordering::Relaxed)
} }
pub fn get_name(&self, export: bool) -> String { pub fn get_name(&self, export: bool) -> String {
format!("{}{}", if export { 'V' } else { 'v' }, self.next_var_idx()) format!("{}{}", if export { 'V' } else { 'v' }, self.next_var_idx())
} }
} }
impl TypeRef {
pub fn get_name(&self) -> String {
match self {
TypeRef::Function(f) => f.name.clone(),
TypeRef::Struct(s) => s.name.clone(),
TypeRef::Int => "int".to_string(),
}
}
}
+33 -5
View File
@@ -1,6 +1,11 @@
use std::sync::Arc; use std::{collections::HashMap, sync::Arc};
use crate::scope::{LocalScope, Scope}; use parser::ast::{ParamList, TypeExpr};
use crate::{
hir::VarRef,
scope::{LocalScope, Scope},
};
pub mod hir; pub mod hir;
pub mod scope; pub mod scope;
@@ -14,7 +19,13 @@ pub fn walk_ast(top_scope: Arc<Scope>, tl: &mut Vec<parser::ast::TopLevel>) {
parser::ast::TopLevel::FunctionDecl { parser::ast::TopLevel::FunctionDecl {
params, body, name, .. params, body, name, ..
} => { } => {
*name = top_scope.get_reference(name).unwrap().name.clone(); let rf = top_scope.get_reference(name).unwrap();
*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());
@@ -23,9 +34,26 @@ pub fn walk_ast(top_scope: Arc<Scope>, tl: &mut Vec<parser::ast::TopLevel>) {
scope.with_block(body); scope.with_block(body);
} }
parser::ast::TopLevel::StructDecl { name, .. } => { parser::ast::TopLevel::StructDecl { name, fields, .. } => {
*name = top_scope.get_reference(name).unwrap().name.clone(); let rf = top_scope.get_reference(name).unwrap();
*name = rf.name.clone();
match &*rf.var_type {
hir::TypeRef::Struct(s) => walk_param_list(&s.fields, fields),
_ => unimplemented!(),
} }
} }
} }
} }
}
pub fn walk_param_list(fields: &HashMap<String, Arc<VarRef>>, param_list: &mut ParamList) {
param_list.0.clear();
for (_, param) in fields {
param_list.0.push((
param.name.clone(),
TypeExpr::Identifier(param.var_type.get_name()),
));
}
}
+1 -5
View File
@@ -2,8 +2,4 @@ export struct MyType {
x: int, x: int,
} }
export function test(): MyType { } export function test(a: MyType) { }
function add() {
let output = test().x;
}