-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'name_resolution' into main
- Loading branch information
Showing
5 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod name_resolver; | ||
pub mod resolution_table; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use std::rc::Rc; | ||
|
||
use crate::pir::ir::{ | ||
ExprRef, InsRef, KeyValueBindings, PIRExpr, PIRIns, PIRModule, PIRModulePass, | ||
}; | ||
|
||
use super::resolution_table::{ResolutionTable, SymbolKind}; | ||
|
||
pub struct Resolver<'a> { | ||
module: &'a PIRModule, | ||
table: Rc<ResolutionTable>, | ||
} | ||
|
||
impl<'a> Resolver<'a> { | ||
fn add_symbol(&mut self, identifier: String, kind: SymbolKind) -> Result<(), String> { | ||
Rc::get_mut(&mut self.table) | ||
.expect("Could not borrow table as mutable") | ||
.add_symbol(identifier, kind) | ||
} | ||
} | ||
|
||
impl<'a> PIRModulePass<'a, (), (), (), Rc<ResolutionTable>, String> for Resolver<'a> { | ||
fn new(module: &'a PIRModule) -> Self { | ||
Self { | ||
module, | ||
table: Rc::new(ResolutionTable::new()), | ||
} | ||
} | ||
|
||
fn process_ins(&mut self, ins: &InsRef) -> Result<(), String> { | ||
let ins_node = self.module.ins_pool.get(&ins); | ||
|
||
match ins_node { | ||
PIRIns::VariableDecl(var_name, _, init_expr, _) => { | ||
let var_name = var_name.as_str(); | ||
if let Some(init_expr) = init_expr { | ||
self.process_expr(&init_expr)?; | ||
} | ||
|
||
self.add_symbol(var_name, SymbolKind::Binding)?; | ||
} | ||
PIRIns::AssignmentIns(lhs, rhs) => { | ||
self.process_expr(&lhs)?; | ||
self.process_expr(&rhs)?; | ||
} | ||
PIRIns::ExpressionIns(expr, _) => { | ||
self.process_expr(&expr)?; | ||
} | ||
n => todo!("Inst: {n:?}"), | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn process_expr(&mut self, expr: &ExprRef) -> Result<(), String> { | ||
let expr_node = self.module.expr_pool.get(&expr); | ||
|
||
match expr_node { | ||
PIRExpr::Id(token, _) => {} | ||
n => todo!("Expr: {n:?}"), | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn process_pairs(&mut self, kv: &KeyValueBindings) -> Result<(), String> { | ||
todo!() | ||
} | ||
|
||
fn process(&mut self) -> Result<Rc<ResolutionTable>, String> { | ||
Ok(Rc::clone(&self.table)) | ||
} | ||
|
||
fn get_module(&mut self) -> &'a PIRModule { | ||
self.module | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use std::fmt::format; | ||
|
||
use crate::frontend::errors::NameResolutionError; | ||
|
||
#[derive(Clone, PartialEq, Eq)] | ||
pub enum SymbolKind { | ||
Binding, | ||
Struct { field_names: Vec<String> }, | ||
Function, | ||
} | ||
|
||
pub struct Symbol { | ||
identifier: String, | ||
kind: SymbolKind, | ||
} | ||
|
||
pub struct ResolutionTable { | ||
symbols: Vec<Symbol>, | ||
} | ||
|
||
impl ResolutionTable { | ||
pub fn new() -> Self { | ||
Self { | ||
symbols: Vec::new(), | ||
} | ||
} | ||
|
||
pub fn add_symbol(&mut self, identifier: String, kind: SymbolKind) -> Result<(), String> { | ||
if self.has_symbol(&identifier, &kind) { | ||
return Err(format!( | ||
"Could not add symbol '{identifier}' as it was already defined" | ||
)); | ||
} | ||
|
||
self.symbols.push(Symbol { identifier, kind }); | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn has_symbol(&self, identifier: &String, kind: &SymbolKind) -> bool { | ||
for symbol in &self.symbols { | ||
if symbol.kind == *kind && symbol.identifier == *identifier { | ||
return true; | ||
} | ||
} | ||
|
||
false | ||
} | ||
} |