From d2f7c0c69b091172c86544783de228033a8f9e8a Mon Sep 17 00:00:00 2001 From: Selimaj Dev Date: Fri, 17 Apr 2026 12:35:20 +0200 Subject: [PATCH] field naming --- semantic/src/hir.rs | 12 +++++++++++- semantic/src/lib.rs | 38 +++++++++++++++++++++++++++++++++----- test/src/main.ms | 6 +----- 3 files changed, 45 insertions(+), 11 deletions(-) diff --git a/semantic/src/hir.rs b/semantic/src/hir.rs index 3e678e8..b9b879f 100644 --- a/semantic/src/hir.rs +++ b/semantic/src/hir.rs @@ -163,10 +163,20 @@ impl TopLevelHirScope { } 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 { 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(), + } + } +} diff --git a/semantic/src/lib.rs b/semantic/src/lib.rs index 0d300f7..14851bc 100644 --- a/semantic/src/lib.rs +++ b/semantic/src/lib.rs @@ -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 scope; @@ -14,7 +19,13 @@ pub fn walk_ast(top_scope: Arc, tl: &mut Vec) { parser::ast::TopLevel::FunctionDecl { 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()); @@ -23,9 +34,26 @@ pub fn walk_ast(top_scope: Arc, tl: &mut Vec) { scope.with_block(body); } - parser::ast::TopLevel::StructDecl { name, .. } => { - *name = top_scope.get_reference(name).unwrap().name.clone(); + parser::ast::TopLevel::StructDecl { name, fields, .. } => { + 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>, 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()), + )); + } +} diff --git a/test/src/main.ms b/test/src/main.ms index fe84848..f84ddbd 100644 --- a/test/src/main.ms +++ b/test/src/main.ms @@ -2,8 +2,4 @@ export struct MyType { x: int, } -export function test(): MyType { } - -function add() { - let output = test().x; -} +export function test(a: MyType) { }