From 97117827c6a75b8f08bd85181477387cf3f70e5f Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Mon, 25 Oct 2021 13:03:31 -0700 Subject: [PATCH] Modify Symbol struct --- schala-lang/language/src/reduced_ir/mod.rs | 28 +++++++++++--------- schala-lang/language/src/reduced_ir/types.rs | 2 +- schala-lang/language/src/symbol_table/mod.rs | 22 +++++++++++---- schala-lang/language/src/util.rs | 1 + 4 files changed, 35 insertions(+), 18 deletions(-) diff --git a/schala-lang/language/src/reduced_ir/mod.rs b/schala-lang/language/src/reduced_ir/mod.rs index a5454b3..e5cf494 100644 --- a/schala-lang/language/src/reduced_ir/mod.rs +++ b/schala-lang/language/src/reduced_ir/mod.rs @@ -1,5 +1,5 @@ use crate::ast; -use crate::symbol_table::{DefId, Symbol, SymbolSpec, SymbolTable}; +use crate::symbol_table::{DefId, SymbolSpec, SymbolTable}; use crate::builtin::Builtin; use std::str::FromStr; @@ -46,7 +46,8 @@ impl<'a> Reducer<'a> { }, ast::StatementKind::Declaration(ast::Declaration::Binding { name: _, constant, expr, ..}) => { let symbol = self.symbol_table.lookup_symbol(item_id).unwrap(); - entrypoint.push(Statement::Binding { id: symbol.def_id.clone(), constant: *constant, expr: self.expression(&expr) }); + let def_id = symbol.def_id().unwrap(); + entrypoint.push(Statement::Binding { id: def_id, constant: *constant, expr: self.expression(&expr) }); }, _ => () } @@ -93,7 +94,8 @@ impl<'a> Reducer<'a> { }, ast::Declaration::Binding { constant, expr, ..} => { let symbol = self.symbol_table.lookup_symbol(item_id).unwrap(); - Some(Statement::Binding { id: symbol.def_id.clone(), constant: *constant, expr: self.expression(&expr) }) + let def_id = symbol.def_id().unwrap(); + Some(Statement::Binding { id: def_id, constant: *constant, expr: self.expression(&expr) }) }, _ => None @@ -104,7 +106,7 @@ impl<'a> Reducer<'a> { fn insert_function_definition(&mut self, item_id: &ast::ItemId, statements: &ast::Block) { let symbol = self.symbol_table.lookup_symbol(item_id).unwrap(); - let def_id = symbol.def_id.clone(); + let def_id = symbol.def_id().unwrap(); let function_def = FunctionDefinition { body: self.function(statements) }; @@ -185,8 +187,8 @@ impl<'a> Reducer<'a> { Some(Builtin::Assignment) => { let lval = match &lhs.kind { ast::ExpressionKind::Value(qualified_name) => { - if let Some(Symbol { def_id, .. }) = self.symbol_table.lookup_symbol(&qualified_name.id) { - def_id.clone() + if let Some(symbol) = self.symbol_table.lookup_symbol(&qualified_name.id) { + symbol.def_id().unwrap() } else { return ReductionError(format!("Couldn't look up name: {:?}", qualified_name)); } @@ -222,12 +224,14 @@ impl<'a> Reducer<'a> { Some(s) => s, None => return Expression::ReductionError(format!("No symbol found for name: {:?}", qualified_name)) }; - let Symbol { def_id, spec, .. } = symbol; - match spec { - Func(_) => Expression::Lookup { id: def_id.clone(), kind: Lookup::Function }, - GlobalBinding => Expression::Lookup { id: def_id.clone(), kind: Lookup::GlobalVar }, - LocalVariable => Expression::Lookup { id: def_id.clone(), kind: Lookup::LocalVar }, - FunctionParam(n) => Expression::Lookup { id: def_id.clone(), kind: Lookup::Param(*n) }, + + let def_id = symbol.def_id(); + + match symbol.spec() { + Func(_) => Expression::Lookup { id: def_id.unwrap(), kind: Lookup::Function }, + GlobalBinding => Expression::Lookup { id: def_id.unwrap(), kind: Lookup::GlobalVar }, + LocalVariable => Expression::Lookup { id: def_id.unwrap(), kind: Lookup::LocalVar }, + FunctionParam(n) => Expression::Lookup { id: def_id.unwrap(), kind: Lookup::Param(n) }, DataConstructor { .. } => { Expression::ReductionError("DataConstructor not supported".to_string()) }, diff --git a/schala-lang/language/src/reduced_ir/types.rs b/schala-lang/language/src/reduced_ir/types.rs index d937278..0fb33b5 100644 --- a/schala-lang/language/src/reduced_ir/types.rs +++ b/schala-lang/language/src/reduced_ir/types.rs @@ -23,7 +23,7 @@ impl ReducedIR { println!("Functions:"); println!("-----------"); for (id, callable) in self.functions.iter() { - let name = &symbol_table.lookup_symbol_by_def(id).unwrap().local_name; + let name = &symbol_table.lookup_symbol_by_def(id).unwrap().local_name(); println!("{}({}) -> {:?}", id, name, callable); } println!(""); diff --git a/schala-lang/language/src/symbol_table/mod.rs b/schala-lang/language/src/symbol_table/mod.rs index 8f54f54..f6ea403 100644 --- a/schala-lang/language/src/symbol_table/mod.rs +++ b/schala-lang/language/src/symbol_table/mod.rs @@ -226,15 +226,28 @@ impl SymbolTable { #[allow(dead_code)] #[derive(Debug, Clone)] pub struct Symbol { - pub local_name: Rc, //TODO get rid of this, it can be computed from fqsn fully_qualified_name: Fqsn, - pub spec: SymbolSpec, - pub def_id: DefId, + spec: SymbolSpec, + def_id: DefId, +} + +impl Symbol { + pub fn local_name(&self) -> Rc { + self.fully_qualified_name.local_name() + } + + pub fn def_id(&self) -> Option { + Some(self.def_id.clone()) + } + + pub fn spec(&self) -> SymbolSpec { + self.spec.clone() + } } impl fmt::Display for Symbol { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "", self.local_name, self.spec) + write!(f, "", self.local_name(), self.spec) } } @@ -305,7 +318,6 @@ impl SymbolTable { fn add_symbol(&mut self, id: &ItemId, fqsn: Fqsn, spec: SymbolSpec) { let def_id = self.def_id_store.fresh(); let symbol = Rc::new(Symbol { - local_name: fqsn.local_name(), fully_qualified_name: fqsn.clone(), spec, def_id, diff --git a/schala-lang/language/src/util.rs b/schala-lang/language/src/util.rs index 999e20f..ab4b99a 100644 --- a/schala-lang/language/src/util.rs +++ b/schala-lang/language/src/util.rs @@ -41,6 +41,7 @@ impl<'a, T, V, N> ScopeStack<'a, T, V, N> where T: Hash + Eq { } } + #[allow(dead_code)] pub fn lookup_with_scope(&self, key: &T) -> Option<(&V, Option<&N>)> where T: Hash + Eq { match (self.values.get(key), self.parent) { (None, None) => None,