From c8c1a65bf5e791b228d2f0c0de5b33f45179fba0 Mon Sep 17 00:00:00 2001 From: Robert-M-Lucas Date: Mon, 3 Jun 2024 17:41:55 +0100 Subject: [PATCH] Improved name system --- .idea/workspace.xml | 17 +++++++++++---- main.why | 11 +++++++++- src/root/name_resolver/name_resolvers.rs | 20 +++++++++++++++--- src/root/name_resolver/resolve.rs | 8 ++++++- src/root/name_resolver/resolve_names.rs | 2 +- src/root/name_resolver/resolve_type_sizes.rs | 22 +++++++++++++++++--- 6 files changed, 67 insertions(+), 13 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 9ecbf58..0bd73f0 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -9,10 +9,11 @@ - + - + + diff --git a/main.why b/main.why index 42faafe..6d970aa 100644 --- a/main.why +++ b/main.why @@ -1,3 +1,12 @@ +struct A { + b: int, + c: int +} + +fn check() -> A { + +} + fn main() -> int { printb(true); -} \ No newline at end of file +} diff --git a/src/root/name_resolver/name_resolvers.rs b/src/root/name_resolver/name_resolvers.rs index 007a977..98af483 100644 --- a/src/root/name_resolver/name_resolvers.rs +++ b/src/root/name_resolver/name_resolvers.rs @@ -108,7 +108,7 @@ impl LocalVariableTable { } } -#[derive(Default, Getters)] +#[derive(Getters)] pub struct GlobalDefinitionTable { id_counter: isize, type_definitions: HashMap>, @@ -133,6 +133,16 @@ pub enum NameResultId { } impl GlobalDefinitionTable { + pub fn new() -> GlobalDefinitionTable { + GlobalDefinitionTable { + id_counter: 1, + type_definitions: Default::default(), + function_signatures: Default::default(), + name_table: Default::default(), + builtin_type_name_table: Default::default(), + builtin_function_name_table: Default::default(), + } + } pub fn register_builtin_type(&mut self, name: String, t: Box, impl_node: ImplNode) { let id = t.id(); self.type_definitions.insert(id, t); @@ -155,9 +165,13 @@ impl GlobalDefinitionTable { } pub fn add_from_function_token(&mut self, ft: &FunctionToken, containing_class: Option) -> isize { + let id = if ft.name() == "main" { + 0 + } else { + self.id_counter += 1; + self.id_counter - 1 + }; - self.id_counter += 1; - let id = self.id_counter - 1; if let Some(containing_class) = containing_class { for (_, file_level_tree) in &mut self.name_table.table { diff --git a/src/root/name_resolver/resolve.rs b/src/root/name_resolver/resolve.rs index 31705cf..f84b7e9 100644 --- a/src/root/name_resolver/resolve.rs +++ b/src/root/name_resolver/resolve.rs @@ -30,8 +30,14 @@ impl AddressedTypeRef { } pub fn resolve(ast: Vec) -> (GlobalDefinitionTable, Vec<(isize, FunctionToken)>) { - let mut global_table = GlobalDefinitionTable::default(); + let mut global_table = GlobalDefinitionTable::new(); register_builtin(&mut global_table); let unprocessed_functions = resolve_names(ast, &mut global_table); + + if !global_table.function_signatures().contains_key(&0) { + // NO MAIN! + todo!() + } + (global_table, unprocessed_functions) } \ No newline at end of file diff --git a/src/root/name_resolver/resolve_names.rs b/src/root/name_resolver/resolve_names.rs index 5f46e7c..f47a749 100644 --- a/src/root/name_resolver/resolve_names.rs +++ b/src/root/name_resolver/resolve_names.rs @@ -152,7 +152,7 @@ pub fn resolve_names(ast: Vec, global_table: &mut GlobalDefiniti while !unsized_final_types.is_empty() { let next_type_id = *unsized_final_types.keys().next().unwrap(); let unsized_type = unsized_final_types.remove(&next_type_id).unwrap(); - resolve_type_sizes(unsized_type, &mut final_types, &mut unsized_final_types, &mut Vec::new()); + resolve_type_sizes(unsized_type, &mut final_types, &mut unsized_final_types, global_table, &mut Vec::new()); } for (id, user_type) in final_types { diff --git a/src/root/name_resolver/resolve_type_sizes.rs b/src/root/name_resolver/resolve_type_sizes.rs index 6fde14c..373a053 100644 --- a/src/root/name_resolver/resolve_type_sizes.rs +++ b/src/root/name_resolver/resolve_type_sizes.rs @@ -1,5 +1,6 @@ use std::collections::HashMap; use derive_getters::{Dissolve, Getters}; +use crate::root::name_resolver::name_resolvers::GlobalDefinitionTable; use crate::root::name_resolver::resolve::TypeRef; use crate::root::name_resolver::resolve_names::UserType; use crate::root::parser::parse::Location; @@ -18,7 +19,13 @@ impl UnsizedUserType { } } -pub fn resolve_type_sizes(unsized_type: UnsizedUserType, final_types: &mut HashMap, unsized_types: &mut HashMap, path: &mut Vec) -> usize { +pub fn resolve_type_sizes( + unsized_type: UnsizedUserType, + final_types: &mut HashMap, + unsized_types: &mut HashMap, + global_table: &GlobalDefinitionTable, + path: &mut Vec +) -> usize { let (id, attributes, location) = unsized_type.dissolve(); if path.contains(&id) { @@ -40,8 +47,17 @@ pub fn resolve_type_sizes(unsized_type: UnsizedUserType, final_types: &mut HashM size += sized_type.size(); } else { - let unsized_type = unsized_types.remove(&attribute_type.type_id()).unwrap(); - size += resolve_type_sizes(unsized_type, final_types, unsized_types, path); + if let Some(unsized_type) = unsized_types.remove(&attribute_type.type_id()) { + size += resolve_type_sizes(unsized_type, final_types, unsized_types, global_table, path); + } + else { + if let Some(builtin_type) = global_table.type_definitions().get(&attribute_type.type_id()) { + size += builtin_type.size(); + } + else { + todo!() + } + } } processed_attributes.push((offset, attribute_name, attribute_type));