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 c8c1a65 commit 2fc5cc1
Show file tree
Hide file tree
Showing 14 changed files with 189 additions and 119 deletions.
59 changes: 30 additions & 29 deletions .idea/workspace.xml

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

35 changes: 35 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ nom_locate = "4.2.0"
nom-supreme = "0.8.0"
substring = "1.4.5"
derive-getters = "0.3.0"
derive_more = "0.99.17"

[profile.release]
opt-level = 3
Expand Down
4 changes: 3 additions & 1 deletion src/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::fs;
use std::io::ErrorKind;
use std::path::PathBuf;
use crate::root::name_resolver::resolve::resolve;
use crate::root::shared::types::ByteSize;

// #[cfg(target_os = "windows")]
// use crate::root::runner::run;
Expand All @@ -27,8 +28,9 @@ pub mod utils;
pub mod name_resolver;
pub mod builtin;
pub mod shared;
pub mod compiler;

pub const POINTER_SIZE: usize = 8;
pub const POINTER_SIZE: ByteSize = ByteSize(8);

/// Compiler for Whython files (.why)
#[derive(Parser)]
Expand Down
10 changes: 5 additions & 5 deletions src/root/builtin/int.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use unique_type_id::UniqueTypeId;
use crate::root::shared::types::Type;
use crate::root::shared::types::{ByteSize, Type, TypeID};

#[derive(UniqueTypeId)]
#[UniqueTypeIdType = "u16"]
pub struct IntType {}

impl Type for IntType {
fn id(&self) -> isize {
-(IntType::unique_type_id().0 as isize) - 1
fn id(&self) -> TypeID {
TypeID(-(IntType::unique_type_id().0 as isize) - 1)
}

fn size(&self) -> usize {
8
fn size(&self) -> ByteSize {
ByteSize(8)
}
}
Empty file added src/root/compiler/mod.rs
Empty file.
49 changes: 24 additions & 25 deletions src/root/name_resolver/name_resolvers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@ use std::collections::HashMap;
use std::path::PathBuf;
use std::rc::Rc;
use derive_getters::Getters;
use crate::root::name_resolver::resolve::{AddressedTypeRef, TypeRef};
use crate::root::name_resolver::resolve_function_signatures::FunctionSignature;
use crate::root::shared::types::Type;
use crate::root::shared::types::{AddressedTypeRef, FunctionID, Type, TypeID, TypeRef};
use crate::root::parser::parse_function::FunctionToken;
use crate::root::parser::parse_name::{NameConnectors, UnresolvedNameToken};
use crate::root::parser::parse_name::UnresolvedNameToken;
use crate::root::parser::parse_struct::StructToken;

#[derive(Default)]
pub struct ImplNode {
functions: HashMap<String, isize>
functions: HashMap<String, FunctionID>
}

/// Contents of a `DefinitionTable`
enum FileLevelTreeNode {
Function(isize),
Type(isize, ImplNode),
Function(FunctionID),
Type(TypeID, ImplNode),
}

/// Recursive tree containing all named objects/functions/types
Expand All @@ -27,12 +26,12 @@ struct FileLevelTree {
}

impl FileLevelTree {
pub fn add_type(&mut self, name: String, id: isize) {
pub fn add_type(&mut self, name: String, id: TypeID) {
// TODO: Handle collision
self.table.insert(name, FileLevelTreeNode::Type(id, ImplNode::default()));
}

pub fn add_function_impl(&mut self, name: String, id: isize, containing_class: isize) -> bool {
pub fn add_function_impl(&mut self, name: String, id: FunctionID, containing_class: TypeID) -> bool {
for (_, n) in &mut self.table {
match n {
FileLevelTreeNode::Function(_) => {}
Expand All @@ -51,7 +50,7 @@ impl FileLevelTree {
return false;
}

pub fn add_function(&mut self, name: String, id: isize) {
pub fn add_function(&mut self, name: String, id: FunctionID) {
self.table.insert(name, FileLevelTreeNode::Function(id));
}
}
Expand Down Expand Up @@ -95,7 +94,7 @@ impl LocalVariableTable {
}
}

pub fn get_ref_and_type<'a>(&self, name: &str, type_defs: &'a HashMap<isize, Box<dyn Type>>) -> Option<(AddressedTypeRef, &'a dyn Type)> {
pub fn get_ref_and_type<'a>(&self, name: &str, type_defs: &'a HashMap<TypeID, Box<dyn Type>>) -> Option<(AddressedTypeRef, &'a dyn Type)> {
if let Some(r) = self.table.get(name) {
if let Some(t) = type_defs.get(r.type_ref().type_id()) {
return Some((r.clone(), t.as_ref()));
Expand All @@ -111,11 +110,11 @@ impl LocalVariableTable {
#[derive(Getters)]
pub struct GlobalDefinitionTable {
id_counter: isize,
type_definitions: HashMap<isize, Box<dyn Type>>,
function_signatures: HashMap<isize, FunctionSignature>,
type_definitions: HashMap<TypeID, Box<dyn Type>>,
function_signatures: HashMap<FunctionID, FunctionSignature>,
name_table: TopLevelNameTree,
builtin_type_name_table: HashMap<String, (isize, ImplNode)>,
builtin_function_name_table: HashMap<String, isize>
builtin_type_name_table: HashMap<String, (TypeID, ImplNode)>,
builtin_function_name_table: HashMap<String, FunctionID>
}


Expand All @@ -127,7 +126,7 @@ pub enum NameResult<'a> {
}

pub enum NameResultId {
Function(isize),
Function(FunctionID),
Type(TypeRef),
NotFound
}
Expand All @@ -149,27 +148,27 @@ impl GlobalDefinitionTable {
self.builtin_type_name_table.insert(name, (id, impl_node));
}

pub fn register_builtin_function(&mut self, name: String, t: FunctionSignature, id: isize) {
pub fn register_builtin_function(&mut self, name: String, t: FunctionSignature, id: FunctionID) {
self.function_signatures.insert(id, t);
self.builtin_function_name_table.insert(name, id);
}

pub fn add_from_struct_token(&mut self, st: &StructToken) -> isize {
pub fn add_from_struct_token(&mut self, st: &StructToken) -> TypeID {
let file_level_tree = self.name_table.get_path_tree(st.location().path());
self.id_counter += 1;
let id = self.id_counter - 1;
let id = TypeID(self.id_counter - 1);

file_level_tree.add_type(st.name().clone(), id);

id
}

pub fn add_from_function_token(&mut self, ft: &FunctionToken, containing_class: Option<isize>) -> isize {
pub fn add_from_function_token(&mut self, ft: &FunctionToken, containing_class: Option<TypeID>) -> FunctionID {
let id = if ft.name() == "main" {
0
FunctionID(0)
} else {
self.id_counter += 1;
self.id_counter - 1
FunctionID(self.id_counter - 1)
};


Expand All @@ -189,11 +188,11 @@ impl GlobalDefinitionTable {
id
}

pub fn add_function_signature(&mut self, given_id: isize, function_signature: FunctionSignature) {
pub fn add_function_signature(&mut self, given_id: FunctionID, function_signature: FunctionSignature) {
self.function_signatures.insert(given_id, function_signature);
}

pub fn add_type(&mut self, given_id: isize, definition: Box<dyn Type>) {
pub fn add_type(&mut self, given_id: TypeID, definition: Box<dyn Type>) {
// TODO: handle collisions
self.type_definitions.insert(given_id, definition);
}
Expand Down Expand Up @@ -237,7 +236,7 @@ impl GlobalDefinitionTable {

match base {
FileLevelTreeNode::Function(fid) => {
if name_iter.next().is_some() || *name.indirection() > 0 {
if name_iter.next().is_some() || name.indirection().has_indirection() {
// TODO
return Err(());
}
Expand Down Expand Up @@ -320,7 +319,7 @@ impl GlobalDefinitionTable {

if let Some(id) = self.builtin_function_name_table.get(name.base()) {
// TODO
if !name.names().is_empty() || *name.indirection() != 0 {
if !name.names().is_empty() || name.indirection().has_indirection() {
return Err(());
}

Expand Down
Loading

0 comments on commit 2fc5cc1

Please sign in to comment.