Modify symbol table tests

This commit is contained in:
Greg Shuflin 2021-10-18 22:51:36 -07:00
parent 9d89440a6d
commit 845461e2b3
2 changed files with 55 additions and 50 deletions

View File

@ -176,7 +176,7 @@ pub struct SymbolTable {
symbol_path_to_symbol: HashMap<FullyQualifiedSymbolName, Symbol>, symbol_path_to_symbol: HashMap<FullyQualifiedSymbolName, Symbol>,
id_to_fqsn: HashMap<ItemId, FullyQualifiedSymbolName>, id_to_fqsn: HashMap<ItemId, FullyQualifiedSymbolName>,
symbol_trie: SymbolTrie, symbol_trie: SymbolTrie,
fq_names: NameTable<NameKind>, fq_names: NameTable<NameKind>, //Note that presence of two tables implies that a type and other binding with the same name can co-exist
types: NameTable<TypeKind>, types: NameTable<TypeKind>,
} }

View File

@ -2,37 +2,38 @@
use super::*; use super::*;
use crate::util::quick_ast; use crate::util::quick_ast;
fn add_symbols_from_source(src: &str) -> (SymbolTable, Result<(), String>) { fn add_symbols(src: &str) -> (SymbolTable, Result<(), String>) {
let ast = quick_ast(src); let ast = quick_ast(src);
let mut symbol_table = SymbolTable::new(); let mut symbol_table = SymbolTable::new();
let result = symbol_table.add_top_level_symbols(&ast); let result = symbol_table.process_ast(&ast);
(symbol_table, result) (symbol_table, result)
} }
macro_rules! values_in_table { fn make_fqsn(strs: &[&str]) -> FQSN {
($source:expr, $single_value:expr) => { let mut scopes = vec![];
values_in_table!($source => $single_value); scopes.push(Scope::Top);
}; for s in strs {
($source:expr => $( $value:expr ),* ) => { scopes.push(Scope::Name(s.to_string()));
{ }
let (symbol_table, _) = add_symbols_from_source($source); FQSN {
$( scopes
match symbol_table.lookup_by_fqsn($value) { }
Some(_spec) => (),
None => panic!(),
};
)*
}
};
} }
#[test] #[test]
fn basic_symbol_table() { fn basic_symbol_table() {
values_in_table! { "let a = 10; fn b() { 20 }", &fqsn!("b"; tr) }; let src = "let a = 10; fn b() { 20 }";
values_in_table! { "type Option<T> = Some(T) | None" => let (symbols, _) = add_symbols(src);
&fqsn!("Option"; tr),
&fqsn!("Option"; ty, "Some"; tr), symbols.fq_names.table.get(&make_fqsn(&["b"])).unwrap();
&fqsn!("Option"; ty, "None"; tr) };
let src = "type Option<T> = Some(T) | None";
let (symbols, _) = add_symbols(src);
symbols.types.table.get(&make_fqsn(&["Option"])).unwrap();
symbols.types.table.get(&make_fqsn(&["Option", "Some"])).unwrap();
symbols.types.table.get(&make_fqsn(&["Option", "None"])).unwrap();
} }
#[test] #[test]
@ -42,8 +43,9 @@ fn no_function_definition_duplicates() {
fn b() { 2 } fn b() { 2 }
fn a() { 3 } fn a() { 3 }
"#; "#;
let (_, output) = add_symbols_from_source(source); let (_, output) = add_symbols(source);
assert!(output.unwrap_err().contains("Duplicate function definition: a")) //TODO test for right type of error
output.unwrap_err();
} }
#[test] #[test]
@ -54,10 +56,12 @@ fn no_variable_definition_duplicates() {
let q = 39 let q = 39
let a = 30 let a = 30
"#; "#;
let (_, output) = add_symbols_from_source(source); let (_, output) = add_symbols(source);
let output = output.unwrap_err(); let _output = output.unwrap_err();
/*
assert!(output.contains("Duplicate variable definition: a")); assert!(output.contains("Duplicate variable definition: a"));
assert!(output.contains("already defined at 2")); assert!(output.contains("already defined at 2"));
*/
} }
#[test] #[test]
@ -75,8 +79,9 @@ fn no_variable_definition_duplicates_in_function() {
let x = 33 let x = 33
} }
"#; "#;
let (_, output) = add_symbols_from_source(source); let (_, output) = add_symbols(source);
assert!(output.unwrap_err().contains("Duplicate variable definition: x")) let _err = output.unwrap_err();
//assert!(output.unwrap_err().contains("Duplicate variable definition: x"))
} }
#[test] #[test]
@ -89,9 +94,10 @@ fn dont_falsely_detect_duplicates() {
} }
let q = 39; let q = 39;
"#; "#;
let (symbol_table, _) = add_symbols_from_source(source); let (symbols, _) = add_symbols(source);
assert!(symbol_table.lookup_by_fqsn(&fqsn!["a"; tr]).is_some());
assert!(symbol_table.lookup_by_fqsn(&fqsn!["some_func"; fn, "a";tr]).is_some()); assert!(symbols.fq_names.table.get(&make_fqsn(&["a"])).is_some());
assert!(symbols.fq_names.table.get(&make_fqsn(&["some_func", "a"])).is_some());
} }
#[test] #[test]
@ -103,9 +109,9 @@ fn inner_func(arg) {
} }
x + inner_func(x) x + inner_func(x)
}"#; }"#;
let (symbol_table, _) = add_symbols_from_source(source); let (symbols, _) = add_symbols(source);
assert!(symbol_table.lookup_by_fqsn(&fqsn!("outer_func"; tr)).is_some()); assert!(symbols.fq_names.table.get(&make_fqsn(&["outer_func"])).is_some());
assert!(symbol_table.lookup_by_fqsn(&fqsn!("outer_func"; fn, "inner_func"; tr)).is_some()); assert!(symbols.fq_names.table.get(&make_fqsn(&["outer_func", "inner_func"])).is_some());
} }
#[test] #[test]
@ -123,11 +129,11 @@ fn second_inner_func() {
inner_func(x) inner_func(x)
}"#; }"#;
let (symbol_table, _) = add_symbols_from_source(source); let (symbols, _) = add_symbols(source);
assert!(symbol_table.lookup_by_fqsn(&fqsn!("outer_func"; tr)).is_some()); assert!(symbols.fq_names.table.get(&make_fqsn(&["outer_func"])).is_some());
assert!(symbol_table.lookup_by_fqsn(&fqsn!("outer_func"; fn, "inner_func"; tr)).is_some()); assert!(symbols.fq_names.table.get(&make_fqsn(&["outer_func", "inner_func"])).is_some());
assert!(symbol_table.lookup_by_fqsn(&fqsn!("outer_func"; fn, "second_inner_func"; tr)).is_some()); assert!(symbols.fq_names.table.get(&make_fqsn(&["outer_func", "second_inner_func"])).is_some());
assert!(symbol_table.lookup_by_fqsn(&fqsn!("outer_func"; fn, "second_inner_func"; fn, "another_inner_func"; tr)).is_some()); assert!(symbols.fq_names.table.get(&make_fqsn(&["outer_func", "second_inner_func", "another_inner_func"])).is_some());
} }
#[test] #[test]
@ -147,8 +153,8 @@ fn second_inner_func() {
inner_func(x) inner_func(x)
}"#; }"#;
let (_, output) = add_symbols_from_source(source); let (_, output) = add_symbols(source);
assert!(output.unwrap_err().contains("Duplicate")) let _err = output.unwrap_err();
} }
#[test] #[test]
@ -161,10 +167,11 @@ module stuff {
fn item() fn item()
"#; "#;
values_in_table! { source =>
&fqsn!("item"; tr), let (symbols, _) = add_symbols(source);
&fqsn!("stuff"; tr, "item"; tr) symbols.fq_names.table.get(&make_fqsn(&["stuff"])).unwrap();
}; symbols.fq_names.table.get(&make_fqsn(&["item"])).unwrap();
symbols.fq_names.table.get(&make_fqsn(&["stuff", "item"])).unwrap();
} }
#[test] #[test]
@ -182,8 +189,6 @@ fn duplicate_modules() {
fn foo() { 256.1 } fn foo() { 256.1 }
} }
"#; "#;
let (_, output) = add_symbols_from_source(source); let (_, output) = add_symbols(source);
let output = output.unwrap_err(); let _output = output.unwrap_err();
assert!(output.contains("Duplicate module"));
assert!(output.contains("already defined at 5"));
} }