Skip to content

Commit

Permalink
Improved name system
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert-M-Lucas committed Jun 3, 2024
1 parent c64fd5f commit c8c1a65
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 13 deletions.
17 changes: 13 additions & 4 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion main.why
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
struct A {
b: int,
c: int
}

fn check() -> A {

}

fn main() -> int {
printb(true);
}
}
20 changes: 17 additions & 3 deletions src/root/name_resolver/name_resolvers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl LocalVariableTable {
}
}

#[derive(Default, Getters)]
#[derive(Getters)]
pub struct GlobalDefinitionTable {
id_counter: isize,
type_definitions: HashMap<isize, Box<dyn Type>>,
Expand All @@ -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<dyn Type>, impl_node: ImplNode) {
let id = t.id();
self.type_definitions.insert(id, t);
Expand All @@ -155,9 +165,13 @@ impl GlobalDefinitionTable {
}

pub fn add_from_function_token(&mut self, ft: &FunctionToken, containing_class: Option<isize>) -> 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 {
Expand Down
8 changes: 7 additions & 1 deletion src/root/name_resolver/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,14 @@ impl AddressedTypeRef {
}

pub fn resolve(ast: Vec<TopLevelTokens>) -> (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)
}
2 changes: 1 addition & 1 deletion src/root/name_resolver/resolve_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ pub fn resolve_names(ast: Vec<TopLevelTokens>, 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 {
Expand Down
22 changes: 19 additions & 3 deletions src/root/name_resolver/resolve_type_sizes.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -18,7 +19,13 @@ impl UnsizedUserType {
}
}

pub fn resolve_type_sizes(unsized_type: UnsizedUserType, final_types: &mut HashMap<isize, UserType>, unsized_types: &mut HashMap<isize, UnsizedUserType>, path: &mut Vec<isize>) -> usize {
pub fn resolve_type_sizes(
unsized_type: UnsizedUserType,
final_types: &mut HashMap<isize, UserType>,
unsized_types: &mut HashMap<isize, UnsizedUserType>,
global_table: &GlobalDefinitionTable,
path: &mut Vec<isize>
) -> usize {
let (id, attributes, location) = unsized_type.dissolve();

if path.contains(&id) {
Expand All @@ -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));
Expand Down

0 comments on commit c8c1a65

Please sign in to comment.