Skip to content

Commit

Permalink
feat: module table
Browse files Browse the repository at this point in the history
  • Loading branch information
MilkeeyCat committed Oct 30, 2024
1 parent b7be439 commit e20cf20
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
38 changes: 38 additions & 0 deletions src/module_table.rs
Original file line number Diff line number Diff line change
@@ -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<String, Item>);

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)
}
}
3 changes: 3 additions & 0 deletions src/scope.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{
module_table::ModuleTable,
symbol_table::{Symbol, SymbolTable, SymbolTableError},
type_table::{Type, TypeTable},
};
Expand All @@ -8,6 +9,7 @@ pub struct Scope {
parent_id: Option<usize>,
type_table: TypeTable,
symbol_table: SymbolTable,
module_table: ModuleTable,
}

impl Scope {
Expand All @@ -16,6 +18,7 @@ impl Scope {
parent_id,
type_table: TypeTable::new(),
symbol_table: SymbolTable::new(),
module_table: ModuleTable::new(),
}
}
}
Expand Down

0 comments on commit e20cf20

Please sign in to comment.