Modify Symbol struct

This commit is contained in:
Greg Shuflin 2021-10-25 13:03:31 -07:00
parent fb0bf29826
commit 97117827c6
4 changed files with 35 additions and 18 deletions

View File

@ -1,5 +1,5 @@
use crate::ast; use crate::ast;
use crate::symbol_table::{DefId, Symbol, SymbolSpec, SymbolTable}; use crate::symbol_table::{DefId, SymbolSpec, SymbolTable};
use crate::builtin::Builtin; use crate::builtin::Builtin;
use std::str::FromStr; use std::str::FromStr;
@ -46,7 +46,8 @@ impl<'a> Reducer<'a> {
}, },
ast::StatementKind::Declaration(ast::Declaration::Binding { name: _, constant, expr, ..}) => { ast::StatementKind::Declaration(ast::Declaration::Binding { name: _, constant, expr, ..}) => {
let symbol = self.symbol_table.lookup_symbol(item_id).unwrap(); 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, ..} => { ast::Declaration::Binding { constant, expr, ..} => {
let symbol = self.symbol_table.lookup_symbol(item_id).unwrap(); 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 _ => None
@ -104,7 +106,7 @@ impl<'a> Reducer<'a> {
fn insert_function_definition(&mut self, item_id: &ast::ItemId, statements: &ast::Block) { fn insert_function_definition(&mut self, item_id: &ast::ItemId, statements: &ast::Block) {
let symbol = self.symbol_table.lookup_symbol(item_id).unwrap(); 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 { let function_def = FunctionDefinition {
body: self.function(statements) body: self.function(statements)
}; };
@ -185,8 +187,8 @@ impl<'a> Reducer<'a> {
Some(Builtin::Assignment) => { Some(Builtin::Assignment) => {
let lval = match &lhs.kind { let lval = match &lhs.kind {
ast::ExpressionKind::Value(qualified_name) => { ast::ExpressionKind::Value(qualified_name) => {
if let Some(Symbol { def_id, .. }) = self.symbol_table.lookup_symbol(&qualified_name.id) { if let Some(symbol) = self.symbol_table.lookup_symbol(&qualified_name.id) {
def_id.clone() symbol.def_id().unwrap()
} else { } else {
return ReductionError(format!("Couldn't look up name: {:?}", qualified_name)); return ReductionError(format!("Couldn't look up name: {:?}", qualified_name));
} }
@ -222,12 +224,14 @@ impl<'a> Reducer<'a> {
Some(s) => s, Some(s) => s,
None => return Expression::ReductionError(format!("No symbol found for name: {:?}", qualified_name)) None => return Expression::ReductionError(format!("No symbol found for name: {:?}", qualified_name))
}; };
let Symbol { def_id, spec, .. } = symbol;
match spec { let def_id = symbol.def_id();
Func(_) => Expression::Lookup { id: def_id.clone(), kind: Lookup::Function },
GlobalBinding => Expression::Lookup { id: def_id.clone(), kind: Lookup::GlobalVar }, match symbol.spec() {
LocalVariable => Expression::Lookup { id: def_id.clone(), kind: Lookup::LocalVar }, Func(_) => Expression::Lookup { id: def_id.unwrap(), kind: Lookup::Function },
FunctionParam(n) => Expression::Lookup { id: def_id.clone(), kind: Lookup::Param(*n) }, 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 { .. } => { DataConstructor { .. } => {
Expression::ReductionError("DataConstructor not supported".to_string()) Expression::ReductionError("DataConstructor not supported".to_string())
}, },

View File

@ -23,7 +23,7 @@ impl ReducedIR {
println!("Functions:"); println!("Functions:");
println!("-----------"); println!("-----------");
for (id, callable) in self.functions.iter() { 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!("{}({}) -> {:?}", id, name, callable);
} }
println!(""); println!("");

View File

@ -226,15 +226,28 @@ impl SymbolTable {
#[allow(dead_code)] #[allow(dead_code)]
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Symbol { pub struct Symbol {
pub local_name: Rc<String>, //TODO get rid of this, it can be computed from fqsn
fully_qualified_name: Fqsn, fully_qualified_name: Fqsn,
pub spec: SymbolSpec, spec: SymbolSpec,
pub def_id: DefId, def_id: DefId,
}
impl Symbol {
pub fn local_name(&self) -> Rc<String> {
self.fully_qualified_name.local_name()
}
pub fn def_id(&self) -> Option<DefId> {
Some(self.def_id.clone())
}
pub fn spec(&self) -> SymbolSpec {
self.spec.clone()
}
} }
impl fmt::Display for Symbol { impl fmt::Display for Symbol {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "<Local name: {}, Spec: {}>", self.local_name, self.spec) write!(f, "<Local name: {}, Spec: {}>", self.local_name(), self.spec)
} }
} }
@ -305,7 +318,6 @@ impl SymbolTable {
fn add_symbol(&mut self, id: &ItemId, fqsn: Fqsn, spec: SymbolSpec) { fn add_symbol(&mut self, id: &ItemId, fqsn: Fqsn, spec: SymbolSpec) {
let def_id = self.def_id_store.fresh(); let def_id = self.def_id_store.fresh();
let symbol = Rc::new(Symbol { let symbol = Rc::new(Symbol {
local_name: fqsn.local_name(),
fully_qualified_name: fqsn.clone(), fully_qualified_name: fqsn.clone(),
spec, spec,
def_id, def_id,

View File

@ -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 { pub fn lookup_with_scope(&self, key: &T) -> Option<(&V, Option<&N>)> where T: Hash + Eq {
match (self.values.get(key), self.parent) { match (self.values.get(key), self.parent) {
(None, None) => None, (None, None) => None,