Skip to content

Commit

Permalink
Add module name to symbol table
Browse files Browse the repository at this point in the history
  • Loading branch information
Glyphack committed Dec 10, 2023
1 parent db35159 commit 8632087
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
2 changes: 1 addition & 1 deletion typechecker/src/semantic_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub struct SemanticAnalyzer {
#[allow(unused)]
impl SemanticAnalyzer {
pub fn new(file: EnderpyFile, imports: HashMap<String, ImportResult>) -> Self {
let globals = SymbolTable::global();
let globals = SymbolTable::global(file.module_name());
log::debug!("Creating semantic analyzer for {}", file.module_name());
SemanticAnalyzer {
globals,
Expand Down
3 changes: 2 additions & 1 deletion typechecker/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,14 @@ impl Settings {
}

pub fn test_settings() -> Self {
let file_dir = env::current_dir().unwrap();
Settings {
debug: false,
root: PathBuf::from(""),
follow_imports: FollowImports::All,
import_discovery: ImportDiscovery {
python_executable: None,
typeshed_path: None,
typeshed_path: Some(PathBuf::from(file_dir.parent().unwrap().join("typeshed"))),
},
}
}
Expand Down
7 changes: 5 additions & 2 deletions typechecker/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ pub struct State {

impl State {
pub fn new(file: EnderpyFile) -> Self {
let module_name = file.module_name();
Self {
file,
symbol_table: SymbolTable::global(),
symbol_table: SymbolTable::global(module_name),
diagnostics: Vec::new(),
imports: HashMap::new(),
}
Expand All @@ -36,7 +37,9 @@ impl State {
for stmt in &self.file.body {
sem_anal.visit_stmt(stmt)
}
self.symbol_table = sem_anal.globals
// TODO: Hacky way to add the global scope to all scopes in symbol table after finishing
sem_anal.globals.exit_scope();
self.symbol_table = sem_anal.globals;
}

pub fn get_symbol_table(&self) -> SymbolTable {
Expand Down
20 changes: 14 additions & 6 deletions typechecker/src/symbol_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ pub struct SymbolTable {
/// The distance between the current scope and the scope where the symbol
/// was defined
_locals: HashMap<ast::Expression, u8>,

/// Name of the module that this symbol table is for
pub module_name: String,
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -205,7 +208,7 @@ pub enum SymbolScope {
}

impl SymbolTable {
pub fn global() -> Self {
pub fn global(module_name: String) -> Self {
let mut builtin_scope = SymbolTableScope {
id: get_id(),
symbol_table_type: SymbolTableType::BUILTIN,
Expand Down Expand Up @@ -293,6 +296,7 @@ impl SymbolTable {
scopes: vec![builtin_scope, global_scope],
all_scopes: vec![],
_locals: HashMap::new(),
module_name,
}
}

Expand Down Expand Up @@ -328,6 +332,7 @@ impl SymbolTable {
Some(pos) => {
let mut innermost_scope = self.innermost_scope(pos);
while let Some(scope) = innermost_scope {
log::debug!("lookin in scope: {:?}", scope.name);
if let Some(symbol) = scope.symbols.get(&lookup_request.name) {
return Some(symbol);
}
Expand All @@ -346,7 +351,7 @@ impl SymbolTable {
}
}
None => {
if let Some(symbol) = self.current_scope().symbols.get(&lookup_request.name) {
if let Some(symbol) = self.global_scope().symbols.get(&lookup_request.name) {
return Some(symbol);
}
}
Expand All @@ -367,7 +372,7 @@ impl SymbolTable {
}

pub fn global_scope(&self) -> &SymbolTableScope {
self.scopes
self.all_scopes
.iter()
.filter(|scope| scope.symbol_table_type == SymbolTableType::Module)
.last()
Expand All @@ -376,9 +381,12 @@ impl SymbolTable {

pub fn exit_scope(&mut self) {
let finished_scope = self.scopes.pop();
match finished_scope {
Some(scope) => self.all_scopes.push(scope),
None => panic!("tried to exit non-existent scope"),
if let Some(scope) = finished_scope {
// Also pop the builtin scope if we are exiting the global scope
// if scope.symbol_table_type == SymbolTableType::Module {
// self.all_scopes.push(self.scopes.pop().unwrap());
// }
self.all_scopes.push(scope);
}
}

Expand Down

0 comments on commit 8632087

Please sign in to comment.