Skip to content

Commit

Permalink
Merge branch 'name_resolution' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
pepplejoshua authored Jul 20, 2023
2 parents 9f6188a + 663fe00 commit ad46097
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/frontend/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,10 @@ pub enum ParseError {
UnknownCompilerDirective(SourceRef),
TooManyErrors(SourceRef),
}

#[allow(dead_code)]
#[derive(serde::Deserialize, serde::Serialize, Debug, Clone)]
pub enum NameResolutionError {
SymbolAlreadyExists(SourceRef),
UndefinedSymbol(SourceRef),
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use compilation::pipeline::Workspace;
mod analysis_a;
mod compilation;
mod frontend;
mod name_resolution;
mod pastel;
mod pir;
mod tools;
Expand Down
2 changes: 2 additions & 0 deletions src/name_resolution/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod name_resolver;
pub mod resolution_table;
77 changes: 77 additions & 0 deletions src/name_resolution/name_resolver.rs
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
}
}
49 changes: 49 additions & 0 deletions src/name_resolution/resolution_table.rs
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
}
}

0 comments on commit ad46097

Please sign in to comment.