More work in symbol_table, reduced_ast

This commit is contained in:
Greg Shuflin 2021-10-19 00:07:02 -07:00
parent 1e9a15d01e
commit f5328fac9d
2 changed files with 2 additions and 45 deletions

View File

@ -189,6 +189,7 @@ impl<'a> Reducer<'a> {
let Symbol { local_name, spec, .. } = match self.symbol_table.lookup_by_fqsn(&sym_name) { let Symbol { local_name, spec, .. } = match self.symbol_table.lookup_by_fqsn(&sym_name) {
Some(s) => s, Some(s) => s,
//TODO investigate why this fails
//None => return Expr::ReductionError(format!("Symbol {:?} not found", sym_name)), //None => return Expr::ReductionError(format!("Symbol {:?} not found", sym_name)),
None => return Expr::Sym(name) None => return Expr::Sym(name)
}; };
@ -197,7 +198,7 @@ impl<'a> Reducer<'a> {
SymbolSpec::RecordConstructor { .. } => Expr::ReductionError(format!("AST reducer doesn't expect a RecordConstructor here")), SymbolSpec::RecordConstructor { .. } => Expr::ReductionError(format!("AST reducer doesn't expect a RecordConstructor here")),
SymbolSpec::DataConstructor { index, type_args, type_name } => Expr::Constructor { SymbolSpec::DataConstructor { index, type_args, type_name } => Expr::Constructor {
type_name: type_name.clone(), type_name: type_name.clone(),
name, name: local_name.clone(),
tag: index.clone(), tag: index.clone(),
arity: type_args.len(), arity: type_args.len(),
}, },

View File

@ -107,37 +107,6 @@ impl<K> NameTable<K> {
} }
} }
/// Keeps track of what names were used in a given namespace. Call try_register to add a name to
/// the table, or report an error if a name already exists.
struct DuplicateNameTrackTable {
table: HashMap<Rc<String>, LineNumber>,
}
impl DuplicateNameTrackTable {
fn new() -> DuplicateNameTrackTable {
DuplicateNameTrackTable { table: HashMap::new() }
}
fn try_register(&mut self, name: &Rc<String>, id: &ItemId, decl_locations: &DeclLocations) -> Result<(), LineNumber> {
match self.table.entry(name.clone()) {
Entry::Occupied(o) => {
let line_number = o.get();
Err(*line_number)
},
Entry::Vacant(v) => {
let line_number = if let Some(loc) = decl_locations.lookup(id) {
loc.line_num
} else {
0
};
v.insert(line_number);
Ok(())
}
}
}
}
#[derive(PartialEq, Eq, Hash, Debug, Clone, PartialOrd, Ord)] #[derive(PartialEq, Eq, Hash, Debug, Clone, PartialOrd, Ord)]
pub struct FullyQualifiedSymbolName(pub Vec<ScopeSegment>); pub struct FullyQualifiedSymbolName(pub Vec<ScopeSegment>);
@ -395,9 +364,6 @@ impl SymbolTable {
fn add_symbols_from_scope<'a>(&'a mut self, statements: &Vec<Statement>, scope_name_stack: &mut Vec<ScopeSegment>) -> Result<(), String> { fn add_symbols_from_scope<'a>(&'a mut self, statements: &Vec<Statement>, scope_name_stack: &mut Vec<ScopeSegment>) -> Result<(), String> {
use self::ast::Declaration::*; use self::ast::Declaration::*;
let mut seen_identifiers = DuplicateNameTrackTable::new();
let mut seen_modules = DuplicateNameTrackTable::new();
for statement in statements.iter() { for statement in statements.iter() {
match statement { match statement {
Statement { kind: StatementKind::Declaration(decl), id, location, } => { Statement { kind: StatementKind::Declaration(decl), id, location, } => {
@ -405,13 +371,9 @@ impl SymbolTable {
match decl { match decl {
FuncSig(ref signature) => { FuncSig(ref signature) => {
seen_identifiers.try_register(&signature.name, id, &self.decl_locations)
.map_err(|line| format!("Duplicate function definition: {}. It's already defined at {}", signature.name, line))?;
self.add_function_signature(signature, scope_name_stack)? self.add_function_signature(signature, scope_name_stack)?
} }
FuncDecl(ref signature, ref body) => { FuncDecl(ref signature, ref body) => {
seen_identifiers.try_register(&signature.name, id, &self.decl_locations)
.map_err(|line| format!("Duplicate function definition: {}. It's already defined at {}", signature.name, line))?;
self.add_function_signature(signature, scope_name_stack)?; self.add_function_signature(signature, scope_name_stack)?;
scope_name_stack.push(ScopeSegment{ scope_name_stack.push(ScopeSegment{
name: signature.name.clone(), name: signature.name.clone(),
@ -421,13 +383,9 @@ impl SymbolTable {
output? output?
}, },
TypeDecl { name, body, mutable } => { TypeDecl { name, body, mutable } => {
seen_identifiers.try_register(&name.name, &id, &self.decl_locations)
.map_err(|line| format!("Duplicate type definition: {}. It's already defined at {}", name.name, line))?;
self.add_type_decl(name, body, mutable, scope_name_stack)? self.add_type_decl(name, body, mutable, scope_name_stack)?
}, },
Binding { name, .. } => { Binding { name, .. } => {
seen_identifiers.try_register(&name, &id, &self.decl_locations)
.map_err(|line| format!("Duplicate variable definition: {}. It's already defined at {}", name, line))?;
self.add_new_symbol(name, scope_name_stack, SymbolSpec::Binding); self.add_new_symbol(name, scope_name_stack, SymbolSpec::Binding);
} }
_ => () _ => ()
@ -435,8 +393,6 @@ impl SymbolTable {
}, },
Statement { kind: StatementKind::Module(ModuleSpecifier { name, contents}), id, location } => { Statement { kind: StatementKind::Module(ModuleSpecifier { name, contents}), id, location } => {
self.decl_locations.add_location(id, *location); self.decl_locations.add_location(id, *location);
seen_modules.try_register(name, id, &self.decl_locations)
.map_err(|line| format!("Duplicate module definition: {}. It's already defined at {}", name, line))?;
scope_name_stack.push(ScopeSegment { name: name.clone() }); scope_name_stack.push(ScopeSegment { name: name.clone() });
let output = self.add_symbols_from_scope(contents, scope_name_stack); let output = self.add_symbols_from_scope(contents, scope_name_stack);
scope_name_stack.pop(); scope_name_stack.pop();