Skip to content

Commit

Permalink
apparently you can derive default
Browse files Browse the repository at this point in the history
  • Loading branch information
dhconnelly committed Nov 15, 2023
1 parent 5c568d2 commit 8538b66
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 28 deletions.
12 changes: 6 additions & 6 deletions src/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ struct Analyzer {

impl Default for Analyzer {
fn default() -> Analyzer {
let mut ctx = SymbolTable::new();
let mut ctx = SymbolTable::default();
builtins::install(&mut ctx);
Analyzer::with_context(ctx)
}
Expand Down Expand Up @@ -248,7 +248,7 @@ mod test {
}),
],
};
let mut ctx = SymbolTable::new();
let mut ctx = SymbolTable::default();
ctx.def_global(
"println",
Type::Fn(FnType {
Expand All @@ -262,7 +262,7 @@ mod test {

#[test]
fn test_func() {
let mut ctx = SymbolTable::new();
let mut ctx = SymbolTable::default();
ctx.def_global(
"itoa",
Type::Fn(FnType {
Expand Down Expand Up @@ -541,7 +541,7 @@ mod test {

#[test]
fn test_call() {
let mut ctx = SymbolTable::new();
let mut ctx = SymbolTable::default();
ctx.push_frame();
ctx.def_local(
String::from("println"),
Expand All @@ -566,7 +566,7 @@ mod test {

#[test]
fn test_ident() {
let mut ctx = SymbolTable::new();
let mut ctx = SymbolTable::default();
ctx.push_frame();
ctx.def_local(String::from("foo"), Type::Int);
ctx.def_local(String::from("bar"), Type::Str);
Expand All @@ -584,7 +584,7 @@ mod test {
fn test_literal() {
let inputs: &[&[u8]] = &[b"27", b"\"hello, world\""];
let expected = vec![Ok(Type::Int), Ok(Type::Str)];
let actual = analyze_exprs(inputs, SymbolTable::new());
let actual = analyze_exprs(inputs, SymbolTable::default());
assert_eq!(expected, actual);
}
}
34 changes: 12 additions & 22 deletions src/symbol_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ struct SymbolInfo {
typ: Type,
}

#[derive(Debug, Clone)]
#[derive(Debug, Default, Clone)]
pub struct SymbolTable {
// TODO: supporting forward references will require supporting empty values
// in the globals table
Expand All @@ -16,16 +16,19 @@ pub struct SymbolTable {
}

impl SymbolTable {
pub fn new() -> Self {
Self { globals: HashMap::new(), frames: Vec::new() }
}

pub fn def_global<S: Into<String>>(&mut self, name: S, typ: Type) {
let idx = self.globals.len();
let name = name.into();
self.globals.insert(name, SymbolInfo { idx, typ });
}

fn get_global(&self, name: &str) -> Option<Resolution> {
self.globals.get(name).map(|SymbolInfo { idx, typ }| Resolution {
reference: Reference::Global { idx: *idx },
typ: typ.clone(),
})
}

pub fn push_frame(&mut self) {
self.frames.push(HashMap::new());
}
Expand All @@ -34,11 +37,10 @@ impl SymbolTable {
self.frames.pop().unwrap();
}

fn get_global(&self, name: &str) -> Option<Resolution> {
self.globals.get(name).map(|SymbolInfo { idx, typ }| Resolution {
reference: Reference::Global { idx: *idx },
typ: typ.clone(),
})
pub fn def_local<S: Into<String>>(&mut self, name: S, typ: Type) {
let frame = self.frames.last_mut().unwrap();
let idx = frame.len();
frame.insert(name.into(), SymbolInfo { idx, typ });
}

fn get_frame(&self, name: &str, depth: usize) -> Option<Resolution> {
Expand All @@ -54,16 +56,4 @@ impl SymbolTable {
.find_map(|depth| self.get_frame(name, depth))
.or_else(|| self.get_global(name))
}

pub fn def_local<S: Into<String>>(&mut self, name: S, typ: Type) {
let frame = self.frames.last_mut().unwrap();
let idx = frame.len();
frame.insert(name.into(), SymbolInfo { idx, typ });
}
}

impl Default for SymbolTable {
fn default() -> Self {
Self::new()
}
}

0 comments on commit 8538b66

Please sign in to comment.