Skip to content

Commit

Permalink
Started implementation of name resolution. Created a basic symbol tab…
Browse files Browse the repository at this point in the history
…le for holding identifier information
  • Loading branch information
Caleb committed Jul 7, 2023
1 parent 8913a01 commit 663fe00
Show file tree
Hide file tree
Showing 5 changed files with 147 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),
}
12 changes: 12 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ use frontend::{
source::{SourceFile, SourceReporter},
token::Token,
};
use name_resolution::name_resolver::Resolver;
use pir::ir::{PIRModule, PIRModulePass};

// use crate::tools::pfmt::Pfmt;

mod analysis_a;
mod frontend;
mod name_resolution;
mod pastel;
mod pir;
mod tools;
Expand All @@ -38,20 +40,24 @@ commands:
";

#[allow(dead_code)]
#[derive(Clone, Copy)]
enum Backend {
PIR, // will go to PVM
CPP, // will go to C++
}

#[allow(dead_code)]
#[derive(Clone, Copy)]
enum Stage {
Lexer,
Parser,
PfmtFile,
DependencyResolvr,
NameResolution,
}

#[allow(dead_code)]
#[derive(Clone, Copy)]
enum Command {
Compile,
}
Expand Down Expand Up @@ -111,6 +117,7 @@ fn create_config(args: Vec<String>) -> ProtoConfig {
"parse" => max_stage = Stage::Parser,
"fmt" => max_stage = Stage::PfmtFile,
"dep" => max_stage = Stage::DependencyResolvr,
"name" => max_stage = Stage::NameResolution,
"dbg" => dbg_info = true,
"help" => show_help = true,
_ => {}
Expand Down Expand Up @@ -230,4 +237,9 @@ fn main() {
Err(_) => todo!(),
}
}

if let Stage::NameResolution = config.max_stage {
let mut name_resolver = Resolver::new(&ir_mod);
let _res = name_resolver.process();
}
}
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 663fe00

Please sign in to comment.