From e20cf20f571f16224ff22917565109bd6d2e37bc Mon Sep 17 00:00:00 2001 From: MilkeeyCat Date: Wed, 30 Oct 2024 21:42:25 +0200 Subject: [PATCH] feat: module table --- src/lib.rs | 1 + src/module_table.rs | 38 ++++++++++++++++++++++++++++++++++++++ src/scope.rs | 3 +++ 3 files changed, 42 insertions(+) create mode 100644 src/module_table.rs diff --git a/src/lib.rs b/src/lib.rs index 8286058..92ef16e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,7 @@ pub mod codegen; pub mod compile; pub mod lexer; pub mod macros; +pub mod module_table; pub mod parser; pub mod passes; pub mod register; diff --git a/src/module_table.rs b/src/module_table.rs new file mode 100644 index 0000000..2c54e6d --- /dev/null +++ b/src/module_table.rs @@ -0,0 +1,38 @@ +use crate::symbol_table::Symbol; +use std::collections::HashMap; +use thiserror::Error; + +#[derive(Error, Debug, PartialEq)] +pub enum ModuleTableError { + #[error("Redeclaration of '{0}'")] + Redeclaration(String), +} + +#[derive(Debug, Clone, PartialEq)] +pub enum Item { + Symbol(Symbol), + Module(ModuleTable), +} + +#[derive(Debug, Clone, PartialEq)] +pub struct ModuleTable(HashMap); + +impl ModuleTable { + pub fn new() -> Self { + Self(HashMap::new()) + } + + pub fn push(&mut self, name: String, item: Item) -> Result<(), ModuleTableError> { + if self.0.contains_key(&name) { + Err(ModuleTableError::Redeclaration(name)) + } else { + self.0.insert(name, item); + + Ok(()) + } + } + + pub fn get(&self, name: &str) -> Option<&Item> { + self.0.get(name) + } +} diff --git a/src/scope.rs b/src/scope.rs index 9d0c1a6..acc0e86 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -1,4 +1,5 @@ use crate::{ + module_table::ModuleTable, symbol_table::{Symbol, SymbolTable, SymbolTableError}, type_table::{Type, TypeTable}, }; @@ -8,6 +9,7 @@ pub struct Scope { parent_id: Option, type_table: TypeTable, symbol_table: SymbolTable, + module_table: ModuleTable, } impl Scope { @@ -16,6 +18,7 @@ impl Scope { parent_id, type_table: TypeTable::new(), symbol_table: SymbolTable::new(), + module_table: ModuleTable::new(), } } }