Modules in symbol table

This commit is contained in:
greg 2019-10-24 02:13:07 -07:00
parent cc0ac83709
commit c96644ddce
2 changed files with 19 additions and 10 deletions

View File

@ -7,7 +7,7 @@ use std::fmt::Write;
use crate::schala::SourceMapHandle; use crate::schala::SourceMapHandle;
use crate::source_map::{SourceMap, LineNumber}; use crate::source_map::{SourceMap, LineNumber};
use crate::ast; use crate::ast;
use crate::ast::{ItemId, TypeBody, TypeSingletonName, Signature, Statement, StatementKind}; use crate::ast::{ItemId, TypeBody, TypeSingletonName, Signature, Statement, StatementKind, ModuleSpecifier};
use crate::typechecking::TypeName; use crate::typechecking::TypeName;
@ -191,8 +191,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::*;
//Err(format!("Duplicate definition: {}. It's already defined at {}", name, line_number))
let mut seen_identifiers = DuplicateNameTrackTable::new(); let mut seen_identifiers = DuplicateNameTrackTable::new();
let mut seen_modules = DuplicateNameTrackTable::new(); let mut seen_modules = DuplicateNameTrackTable::new();
@ -213,7 +211,7 @@ impl SymbolTable {
name: signature.name.clone(), name: signature.name.clone(),
}); });
let output = self.add_symbols_from_scope(body, scope_name_stack); let output = self.add_symbols_from_scope(body, scope_name_stack);
let _ = scope_name_stack.pop(); scope_name_stack.pop();
output? output?
}, },
TypeDecl { name, body, mutable } => { TypeDecl { name, body, mutable } => {
@ -229,8 +227,13 @@ impl SymbolTable {
_ => () _ => ()
} }
}, },
Statement { kind: StatementKind::Module(mod_spec), id } => { Statement { kind: StatementKind::Module(ModuleSpecifier { name, contents}), id } => {
() seen_modules.try_register(&name, &id, &self.source_map_handle.borrow())
.map_err(|line| format!("Duplicate module definition: {}. It's already defined at {}", name, line))?;
scope_name_stack.push(ScopeSegment { name: name.clone() });
let output = self.add_symbols_from_scope(contents, scope_name_stack);
scope_name_stack.pop();
output?
}, },
_ => () _ => ()
} }

View File

@ -156,16 +156,22 @@ inner_func(x)
} }
#[test] #[test]
fn modules() { fn duplicate_modules() {
let source = r#" let source = r#"
module q {
module a { fn foo() { 4 }
} }
module a { module a {
fn foo() { 334 }
}
module a {
fn foo() { 256.1 }
} }
"#; "#;
let (_, output) = add_symbols_from_source(source); let (_, output) = add_symbols_from_source(source);
//assert!(output.unwrap_err().contains("Duplicate")) let output = output.unwrap_err();
assert!(output.contains("Duplicate module"));
assert!(output.contains("already defined at 5"));
} }