-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
143 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use std::{collections::HashMap, rc::Rc}; | ||
|
||
#[derive(Clone, Debug)] | ||
pub enum SymbolScope { | ||
Global, | ||
Local, | ||
Builtin, | ||
Free, | ||
Function, | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct Symbol { | ||
pub name: String, | ||
pub scope: SymbolScope, | ||
pub index: usize, | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct SymbolTable { | ||
pub store: HashMap<String, Rc<Symbol>>, | ||
pub num_definitions: usize, | ||
} | ||
|
||
impl SymbolTable { | ||
pub fn define(&mut self, name: &str) -> Rc<Symbol> { | ||
let symbol = Rc::new( | ||
Symbol { | ||
name: name.to_string(), | ||
scope: SymbolScope::Global, | ||
index: self.num_definitions, | ||
} | ||
); | ||
|
||
self.store.insert(name.to_string(), Rc::clone(&symbol)); | ||
|
||
self.num_definitions += 1; | ||
|
||
symbol | ||
} | ||
|
||
pub fn new() -> Self { | ||
Self { | ||
store: HashMap::new(), | ||
num_definitions: 0, | ||
} | ||
} | ||
|
||
pub fn resolve(&self, name: &str) -> Option<Rc<Symbol>> { | ||
match self.store.get(name) { | ||
Some(symbol) => Some(Rc::clone(symbol)), | ||
None => None, | ||
} | ||
} | ||
} |
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,59 @@ | ||
use std::collections::HashMap; | ||
|
||
use anyhow::Error; | ||
use compiler::symbol_table::{Symbol, SymbolScope, SymbolTable}; | ||
|
||
#[test] | ||
fn test_define() -> Result<(), Error> { | ||
let expected = HashMap::from([ | ||
("a".to_string(), Symbol {name: "a".to_string(), scope: SymbolScope::Global, index: 0}), | ||
("b".to_string(), Symbol {name: "b".to_string(), scope: SymbolScope::Global, index: 1}), | ||
("c".to_string(), Symbol {name: "c".to_string(), scope: SymbolScope::Global, index: 2}), | ||
("d".to_string(), Symbol {name: "d".to_string(), scope: SymbolScope::Global, index: 3}), | ||
]); | ||
|
||
let mut global = SymbolTable::new(); | ||
|
||
let a = global.define("a"); | ||
|
||
if a.name != "a" { | ||
panic!("a.name not 'a'. got={}", a.name); | ||
} | ||
|
||
let b = global.define("b"); | ||
|
||
if b.name != "b" { | ||
panic!("b.name not 'b'. got={}", b.name); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn test_resolve_global() -> Result<(), Error> { | ||
let mut global = SymbolTable::new(); | ||
|
||
global.define("a"); | ||
global.define("b"); | ||
|
||
let expected = vec![ | ||
("a".to_string(), Symbol {name: "a".to_string(), scope: SymbolScope::Global, index: 0}), | ||
("b".to_string(), Symbol {name: "b".to_string(), scope: SymbolScope::Global, index: 1}), | ||
]; | ||
|
||
for (name, expected_symbol) in expected { | ||
let symbol = global.resolve(&name); | ||
|
||
if symbol.is_none() { | ||
panic!("symbol for {} is None", name); | ||
} | ||
|
||
let symbol = symbol.unwrap(); | ||
|
||
if symbol.name != expected_symbol.name { | ||
panic!("symbol.name not {}. got={}", expected_symbol.name, symbol.name); | ||
} | ||
} | ||
|
||
Ok(()) | ||
} |
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