diff --git a/src/analyzer.rs b/src/analyzer.rs index d6a7a3d..fe83fd4 100644 --- a/src/analyzer.rs +++ b/src/analyzer.rs @@ -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) } @@ -248,7 +248,7 @@ mod test { }), ], }; - let mut ctx = SymbolTable::new(); + let mut ctx = SymbolTable::default(); ctx.def_global( "println", Type::Fn(FnType { @@ -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 { @@ -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"), @@ -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); @@ -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); } } diff --git a/src/symbol_table.rs b/src/symbol_table.rs index 1a35f95..4875285 100644 --- a/src/symbol_table.rs +++ b/src/symbol_table.rs @@ -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 @@ -16,16 +16,19 @@ pub struct SymbolTable { } impl SymbolTable { - pub fn new() -> Self { - Self { globals: HashMap::new(), frames: Vec::new() } - } - pub fn def_global>(&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 { + 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()); } @@ -34,11 +37,10 @@ impl SymbolTable { self.frames.pop().unwrap(); } - fn get_global(&self, name: &str) -> Option { - self.globals.get(name).map(|SymbolInfo { idx, typ }| Resolution { - reference: Reference::Global { idx: *idx }, - typ: typ.clone(), - }) + pub fn def_local>(&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 { @@ -54,16 +56,4 @@ impl SymbolTable { .find_map(|depth| self.get_frame(name, depth)) .or_else(|| self.get_global(name)) } - - pub fn def_local>(&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() - } }