Use vec of duplicate errors

This commit is contained in:
Greg Shuflin 2021-10-19 18:22:34 -07:00
parent d3378c3210
commit 9640a5b05b

View File

@ -134,7 +134,10 @@ impl SymbolTable {
/// compilation process.
pub fn process_ast(&mut self, ast: &ast::AST) -> Result<(), String> {
self.populate_name_tables(ast)?;
let errs = self.populate_name_tables(ast);
if !errs.is_empty() {
return Err(format!("{:?}", errs));
}
self.resolve_symbol_ids(ast)?;
Ok(())
}
@ -211,14 +214,12 @@ impl SymbolTable {
/// constants, functions, types, and modules defined within. This simultaneously
/// checks for dupicate definitions (and returns errors if discovered), and sets
/// up name tables that will be used by further parts of the compiler
fn populate_name_tables(&mut self, ast: &ast::AST) -> Result<(), String> {
fn populate_name_tables(&mut self, ast: &ast::AST) -> Vec<DuplicateName> {
let mut scope_stack = vec![];
self.add_from_scope(ast.statements.as_ref(), &mut scope_stack)
.map_err(|err| format!("{:?}", err))?;
Ok(())
}
fn add_from_scope<'a>(&'a mut self, statements: &[Statement], scope_stack: &mut Vec<Scope>) -> Result<(), Vec<DuplicateName>> {
fn add_from_scope<'a>(&'a mut self, statements: &[Statement], scope_stack: &mut Vec<Scope>) -> Vec<DuplicateName> {
let mut errors = vec![];
for statement in statements {
@ -245,19 +246,12 @@ impl SymbolTable {
StatementKind::Declaration(Declaration::TypeDecl { name, body, mutable }) => {
self.add_type_members(name, body, mutable, location, scope_stack)
}
_ => Ok(())
_ => vec![]
};
if let Err(errs) = recursive_errs {
errors.extend(errs.into_iter());
}
errors.extend(recursive_errs.into_iter());
}
if errors.is_empty() {
Ok(())
} else {
Err(errors)
}
errors
}
fn add_single_statement(&mut self, kind: &StatementKind, location: Location, scope_stack: &Vec<Scope>) -> Result<(), DuplicateName> {
@ -304,7 +298,7 @@ impl SymbolTable {
Ok(())
}
fn add_type_members(&mut self, type_name: &TypeSingletonName, type_body: &TypeBody, _mutable: &bool, location: Location, scope_stack: &mut Vec<Scope>) -> Result<(), Vec<DuplicateName>> {
fn add_type_members(&mut self, type_name: &TypeSingletonName, type_body: &TypeBody, _mutable: &bool, location: Location, scope_stack: &mut Vec<Scope>) -> Vec<DuplicateName> {
let mut errors = vec![];
let mut register = |fqsn: FQSN, spec: SymbolSpec| {
@ -378,11 +372,6 @@ impl SymbolTable {
}
scope_stack.pop();
if errors.is_empty() {
Ok(())
} else {
Err(errors)
}
errors
}
}