2018-05-20 20:36:57 -07:00
|
|
|
use std::collections::HashMap;
|
2019-03-08 03:57:32 -08:00
|
|
|
use std::collections::hash_map::Entry;
|
2018-05-20 20:36:57 -07:00
|
|
|
use std::rc::Rc;
|
|
|
|
use std::fmt;
|
|
|
|
use std::fmt::Write;
|
|
|
|
|
2019-01-07 13:00:37 -08:00
|
|
|
use crate::ast;
|
2019-03-08 03:57:32 -08:00
|
|
|
use crate::ast::{Meta, TypeBody, TypeSingletonName, Signature, Statement};
|
2019-01-07 13:00:37 -08:00
|
|
|
use crate::typechecking::TypeName;
|
2018-05-20 20:36:57 -07:00
|
|
|
|
2019-03-08 03:57:32 -08:00
|
|
|
type LineNumber = u32;
|
|
|
|
type SymbolTrackTable = HashMap<Rc<String>, LineNumber>;
|
|
|
|
|
2019-08-30 18:41:47 -07:00
|
|
|
#[derive(PartialEq, Eq, Hash, Debug, Clone)]
|
2019-08-30 19:03:52 -07:00
|
|
|
pub struct FullyQualifiedSymbolName(pub Vec<ScopeSegment>);
|
2019-03-11 01:36:11 -07:00
|
|
|
|
2019-08-30 22:55:59 -07:00
|
|
|
impl fmt::Display for FullyQualifiedSymbolName {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
let FullyQualifiedSymbolName(v) = self;
|
|
|
|
for segment in v {
|
|
|
|
write!(f, "::{}", segment)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-30 18:41:47 -07:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
|
|
|
|
pub struct ScopeSegment {
|
2019-08-31 23:39:01 -07:00
|
|
|
pub name: Rc<String>, //TODO maybe this could be a &str, for efficiency?
|
|
|
|
pub kind: ScopeSegmentKind,
|
2019-03-11 01:36:11 -07:00
|
|
|
}
|
|
|
|
|
2019-08-30 22:55:59 -07:00
|
|
|
impl fmt::Display for ScopeSegment {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
use ScopeSegmentKind::*;
|
|
|
|
let kind = match self.kind {
|
|
|
|
Function => "fn",
|
|
|
|
Type => "ty",
|
|
|
|
Terminal => "tr",
|
|
|
|
};
|
|
|
|
write!(f, "{}({})", self.name, kind)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-30 19:03:52 -07:00
|
|
|
impl ScopeSegment {
|
|
|
|
pub fn new(name: Rc<String>, kind: ScopeSegmentKind) -> ScopeSegment {
|
|
|
|
ScopeSegment { name, kind }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-30 18:41:47 -07:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
|
|
|
|
pub enum ScopeSegmentKind {
|
2019-03-11 02:35:42 -07:00
|
|
|
Function,
|
2019-08-30 18:41:47 -07:00
|
|
|
Type,
|
|
|
|
Terminal,
|
|
|
|
}
|
|
|
|
|
2019-08-30 18:56:16 -07:00
|
|
|
#[allow(unused_macros)]
|
2019-08-30 18:41:47 -07:00
|
|
|
macro_rules! fqsn {
|
|
|
|
( $( $name:expr ; $kind:tt),* ) => {
|
|
|
|
{
|
|
|
|
let mut vec = vec![];
|
|
|
|
$(
|
2019-08-30 19:03:52 -07:00
|
|
|
vec.push(ScopeSegment::new(
|
|
|
|
Rc::new($name.to_string()),
|
|
|
|
sym_path_kind!($kind),
|
|
|
|
));
|
2019-08-30 18:41:47 -07:00
|
|
|
)*
|
|
|
|
FullyQualifiedSymbolName(vec)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-08-30 18:56:16 -07:00
|
|
|
#[allow(unused_macros)]
|
2019-08-30 18:41:47 -07:00
|
|
|
macro_rules! sym_path_kind {
|
|
|
|
(fn) => { ScopeSegmentKind::Function };
|
|
|
|
(ty) => { ScopeSegmentKind::Type };
|
|
|
|
(tr) => { ScopeSegmentKind::Terminal };
|
2019-03-07 20:45:12 -08:00
|
|
|
}
|
|
|
|
|
2018-05-20 20:36:57 -07:00
|
|
|
//cf. p. 150 or so of Language Implementation Patterns
|
|
|
|
pub struct SymbolTable {
|
2019-08-30 18:41:47 -07:00
|
|
|
symbol_path_to_symbol: HashMap<FullyQualifiedSymbolName, Symbol>,
|
2018-05-20 20:36:57 -07:00
|
|
|
}
|
|
|
|
|
2019-03-10 17:29:02 -07:00
|
|
|
//TODO add various types of lookups here, maybe multiple hash tables internally?
|
2018-05-20 20:36:57 -07:00
|
|
|
impl SymbolTable {
|
|
|
|
pub fn new() -> SymbolTable {
|
2019-03-07 23:51:31 -08:00
|
|
|
SymbolTable {
|
2019-08-21 10:08:39 -07:00
|
|
|
symbol_path_to_symbol: HashMap::new(),
|
2019-03-07 23:51:31 -08:00
|
|
|
}
|
2018-05-20 20:36:57 -07:00
|
|
|
}
|
2018-08-05 18:19:48 -07:00
|
|
|
|
2019-03-11 02:47:47 -07:00
|
|
|
fn add_new_symbol(&mut self, name: &Rc<String>, scope_path: &Vec<ScopeSegment>, spec: SymbolSpec) {
|
2019-08-30 18:41:47 -07:00
|
|
|
let mut vec: Vec<ScopeSegment> = scope_path.clone();
|
|
|
|
vec.push(ScopeSegment { name: name.clone(), kind: ScopeSegmentKind::Terminal });
|
|
|
|
let fully_qualified_name = FullyQualifiedSymbolName(vec);
|
|
|
|
let symbol = Symbol { name: name.clone(), fully_qualified_name: fully_qualified_name.clone(), spec };
|
|
|
|
self.symbol_path_to_symbol.insert(fully_qualified_name, symbol);
|
2019-03-07 20:45:12 -08:00
|
|
|
}
|
|
|
|
|
2018-08-05 18:19:48 -07:00
|
|
|
pub fn lookup_by_name(&self, name: &Rc<String>) -> Option<&Symbol> {
|
2019-08-30 18:41:47 -07:00
|
|
|
let vec = vec![
|
|
|
|
ScopeSegment {
|
|
|
|
name: name.clone(),
|
|
|
|
kind: ScopeSegmentKind::Terminal,
|
|
|
|
}
|
|
|
|
];
|
|
|
|
let symbol_path = FullyQualifiedSymbolName(vec);
|
2019-08-30 19:05:01 -07:00
|
|
|
self.lookup_by_fqsn(&symbol_path)
|
2019-08-21 10:08:39 -07:00
|
|
|
}
|
|
|
|
|
2019-08-30 19:05:01 -07:00
|
|
|
pub fn lookup_by_fqsn(&self, fully_qualified_path: &FullyQualifiedSymbolName) -> Option<&Symbol> {
|
2019-08-30 18:41:47 -07:00
|
|
|
self.symbol_path_to_symbol.get(fully_qualified_path)
|
2019-03-10 16:04:20 -07:00
|
|
|
}
|
2018-05-20 20:36:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Symbol {
|
2019-03-11 02:47:47 -07:00
|
|
|
pub name: Rc<String>, //TODO does this need to be pub?
|
2019-08-30 18:41:47 -07:00
|
|
|
fully_qualified_name: FullyQualifiedSymbolName,
|
2018-05-20 20:36:57 -07:00
|
|
|
pub spec: SymbolSpec,
|
|
|
|
}
|
|
|
|
|
2018-06-03 23:04:07 -07:00
|
|
|
impl fmt::Display for Symbol {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "<Name: {}, Spec: {}>", self.name, self.spec)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-20 20:36:57 -07:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum SymbolSpec {
|
2018-06-03 02:39:49 -07:00
|
|
|
Func(Vec<TypeName>),
|
2018-05-30 23:54:24 -07:00
|
|
|
DataConstructor {
|
2018-08-05 13:59:13 -07:00
|
|
|
index: usize,
|
2019-08-12 11:18:03 -07:00
|
|
|
type_name: TypeName,
|
2018-05-30 23:54:24 -07:00
|
|
|
type_args: Vec<Rc<String>>,
|
|
|
|
},
|
2019-01-25 00:57:01 -08:00
|
|
|
RecordConstructor {
|
2019-08-12 12:46:18 -07:00
|
|
|
index: usize,
|
2019-08-12 11:18:03 -07:00
|
|
|
members: HashMap<Rc<String>, TypeName>,
|
|
|
|
type_name: TypeName,
|
2019-03-10 17:02:01 -07:00
|
|
|
},
|
|
|
|
Binding
|
2018-05-20 20:36:57 -07:00
|
|
|
}
|
|
|
|
|
2018-06-03 23:04:07 -07:00
|
|
|
impl fmt::Display for SymbolSpec {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
use self::SymbolSpec::*;
|
|
|
|
match self {
|
|
|
|
Func(type_names) => write!(f, "Func({:?})", type_names),
|
2018-10-15 19:37:02 -07:00
|
|
|
DataConstructor { index, type_name, type_args } => write!(f, "DataConstructor(idx: {})({:?} -> {})", index, type_args, type_name),
|
2019-08-12 12:46:18 -07:00
|
|
|
RecordConstructor { type_name, index, ..} => write!(f, "RecordConstructor(idx: {})(<members> -> {})", index, type_name),
|
2019-03-10 17:02:01 -07:00
|
|
|
Binding => write!(f, "Binding"),
|
2018-06-03 23:04:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-20 20:36:57 -07:00
|
|
|
impl SymbolTable {
|
|
|
|
/* note: this adds names for *forward reference* but doesn't actually create any types. solve that problem
|
|
|
|
* later */
|
2019-03-08 03:57:32 -08:00
|
|
|
|
2018-06-04 19:25:40 -07:00
|
|
|
pub fn add_top_level_symbols(&mut self, ast: &ast::AST) -> Result<(), String> {
|
2019-03-10 16:04:20 -07:00
|
|
|
let mut scope_name_stack = Vec::new();
|
2019-03-10 17:02:01 -07:00
|
|
|
self.add_symbols_from_scope(&ast.0, &mut scope_name_stack)
|
2019-03-08 03:57:32 -08:00
|
|
|
}
|
|
|
|
|
2019-03-11 02:47:47 -07:00
|
|
|
fn add_symbols_from_scope<'a>(&'a mut self, statements: &Vec<Meta<Statement>>, scope_name_stack: &mut Vec<ScopeSegment>) -> Result<(), String> {
|
2018-06-04 19:25:40 -07:00
|
|
|
use self::ast::Declaration::*;
|
2019-03-08 03:57:32 -08:00
|
|
|
|
2019-03-10 17:02:01 -07:00
|
|
|
fn insert_and_check_duplicate_symbol(table: &mut SymbolTrackTable, name: &Rc<String>) -> Result<(), String> {
|
2019-03-08 03:57:32 -08:00
|
|
|
match table.entry(name.clone()) {
|
|
|
|
Entry::Occupied(o) => {
|
|
|
|
let line_number = o.get(); //TODO make this actually work
|
|
|
|
Err(format!("Duplicate definition: {}. It's already defined at {}", name, line_number))
|
|
|
|
},
|
|
|
|
Entry::Vacant(v) => {
|
|
|
|
let line_number = 0; //TODO should work
|
|
|
|
v.insert(line_number);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-10 17:02:01 -07:00
|
|
|
let mut seen_identifiers: SymbolTrackTable = HashMap::new();
|
|
|
|
|
2019-03-08 03:57:32 -08:00
|
|
|
for meta in statements.iter() {
|
|
|
|
let statement = meta.node();
|
2018-05-20 20:36:57 -07:00
|
|
|
if let Statement::Declaration(decl) = statement {
|
|
|
|
match decl {
|
2019-03-08 03:57:32 -08:00
|
|
|
FuncSig(ref signature) => {
|
2019-03-10 17:02:01 -07:00
|
|
|
insert_and_check_duplicate_symbol(&mut seen_identifiers, &signature.name)?;
|
2019-03-10 16:04:20 -07:00
|
|
|
self.add_function_signature(signature, scope_name_stack)?
|
2019-03-08 03:57:32 -08:00
|
|
|
}
|
|
|
|
FuncDecl(ref signature, ref body) => {
|
2019-03-10 17:02:01 -07:00
|
|
|
insert_and_check_duplicate_symbol(&mut seen_identifiers, &signature.name)?;
|
2019-03-10 16:04:20 -07:00
|
|
|
self.add_function_signature(signature, scope_name_stack)?;
|
2019-03-11 02:47:47 -07:00
|
|
|
scope_name_stack.push(ScopeSegment{
|
2019-08-30 18:41:47 -07:00
|
|
|
name: signature.name.clone(),
|
|
|
|
kind: ScopeSegmentKind::Function,
|
2019-03-11 02:47:47 -07:00
|
|
|
});
|
2019-03-10 17:02:01 -07:00
|
|
|
let output = self.add_symbols_from_scope(body, scope_name_stack);
|
2019-03-10 16:04:20 -07:00
|
|
|
let _ = scope_name_stack.pop();
|
|
|
|
output?
|
2019-03-08 03:57:32 -08:00
|
|
|
},
|
2019-03-10 17:02:01 -07:00
|
|
|
TypeDecl { name, body, mutable } => {
|
|
|
|
insert_and_check_duplicate_symbol(&mut seen_identifiers, &name.name)?;
|
2019-08-30 18:41:47 -07:00
|
|
|
//TODO add ScopeSegmentKind::Type here
|
2019-03-10 17:02:01 -07:00
|
|
|
self.add_type_decl(name, body, mutable, scope_name_stack)?
|
|
|
|
},
|
|
|
|
Binding { name, .. } => {
|
|
|
|
insert_and_check_duplicate_symbol(&mut seen_identifiers, name)?;
|
2019-03-11 02:47:47 -07:00
|
|
|
self.add_new_symbol(name, scope_name_stack, SymbolSpec::Binding);
|
2019-03-10 17:02:01 -07:00
|
|
|
}
|
2018-05-20 20:36:57 -07:00
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
pub fn debug_symbol_table(&self) -> String {
|
|
|
|
let mut output = format!("Symbol table\n");
|
2019-08-21 10:08:39 -07:00
|
|
|
for (name, sym) in &self.symbol_path_to_symbol {
|
2019-08-30 22:55:59 -07:00
|
|
|
write!(output, "{} -> {}\n", name, sym).unwrap();
|
2018-05-20 20:36:57 -07:00
|
|
|
}
|
|
|
|
output
|
|
|
|
}
|
2019-01-20 00:22:35 -08:00
|
|
|
|
2019-03-11 02:47:47 -07:00
|
|
|
fn add_function_signature(&mut self, signature: &Signature, scope_name_stack: &mut Vec<ScopeSegment>) -> Result<(), String> {
|
2019-01-20 00:22:35 -08:00
|
|
|
let mut local_type_context = LocalTypeContext::new();
|
2019-06-16 14:56:52 -07:00
|
|
|
let types = signature.params.iter().map(|param| match param.anno {
|
|
|
|
Some(ref type_identifier) => Rc::new(format!("{:?}", type_identifier)),
|
|
|
|
None => local_type_context.new_universal_type()
|
2019-01-20 00:22:35 -08:00
|
|
|
}).collect();
|
2019-03-11 02:47:47 -07:00
|
|
|
self.add_new_symbol(&signature.name, scope_name_stack, SymbolSpec::Func(types));
|
2019-01-20 22:13:05 -08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-08-12 11:27:16 -07:00
|
|
|
//TODO handle type mutability
|
2019-03-11 02:47:47 -07:00
|
|
|
fn add_type_decl(&mut self, type_name: &TypeSingletonName, body: &TypeBody, _mutable: &bool, scope_name_stack: &mut Vec<ScopeSegment>) -> Result<(), String> {
|
2019-01-20 22:13:05 -08:00
|
|
|
use crate::ast::{TypeIdentifier, Variant};
|
|
|
|
let TypeBody(variants) = body;
|
2019-08-12 11:18:03 -07:00
|
|
|
let ref type_name = type_name.name;
|
2019-09-10 03:31:23 -07:00
|
|
|
scope_name_stack.push(ScopeSegment{
|
|
|
|
name: type_name.clone(),
|
|
|
|
kind: ScopeSegmentKind::Type,
|
|
|
|
});
|
2019-01-20 22:13:05 -08:00
|
|
|
//TODO figure out why _params isn't being used here
|
|
|
|
for (index, var) in variants.iter().enumerate() {
|
|
|
|
match var {
|
|
|
|
Variant::UnitStruct(variant_name) => {
|
|
|
|
let spec = SymbolSpec::DataConstructor {
|
|
|
|
index,
|
2019-08-12 11:18:03 -07:00
|
|
|
type_name: type_name.clone(),
|
2019-01-20 22:13:05 -08:00
|
|
|
type_args: vec![],
|
|
|
|
};
|
2019-03-11 02:47:47 -07:00
|
|
|
self.add_new_symbol(variant_name, scope_name_stack, spec);
|
2019-01-20 22:13:05 -08:00
|
|
|
},
|
|
|
|
Variant::TupleStruct(variant_name, tuple_members) => {
|
2019-08-12 11:18:03 -07:00
|
|
|
//TODO fix the notion of a tuple type
|
2019-01-20 22:13:05 -08:00
|
|
|
let type_args = tuple_members.iter().map(|type_name| match type_name {
|
|
|
|
TypeIdentifier::Singleton(TypeSingletonName { name, ..}) => name.clone(),
|
|
|
|
TypeIdentifier::Tuple(_) => unimplemented!(),
|
|
|
|
}).collect();
|
|
|
|
let spec = SymbolSpec::DataConstructor {
|
|
|
|
index,
|
2019-08-12 11:18:03 -07:00
|
|
|
type_name: type_name.clone(),
|
2019-01-20 22:13:05 -08:00
|
|
|
type_args
|
|
|
|
};
|
2019-03-11 02:47:47 -07:00
|
|
|
self.add_new_symbol(variant_name, scope_name_stack, spec);
|
2019-01-20 22:13:05 -08:00
|
|
|
},
|
2019-08-12 11:18:03 -07:00
|
|
|
Variant::Record { name, members: defined_members } => {
|
|
|
|
let mut members = HashMap::new();
|
|
|
|
let mut duplicate_member_definitions = Vec::new();
|
|
|
|
for (member_name, member_type) in defined_members {
|
|
|
|
match members.entry(member_name.clone()) {
|
|
|
|
Entry::Occupied(_) => duplicate_member_definitions.push(member_name.clone()),
|
|
|
|
Entry::Vacant(v) => {
|
|
|
|
v.insert(match member_type {
|
|
|
|
TypeIdentifier::Singleton(TypeSingletonName { name, ..}) => name.clone(),
|
|
|
|
TypeIdentifier::Tuple(_) => unimplemented!(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if duplicate_member_definitions.len() != 0 {
|
|
|
|
return Err(format!("Duplicate member(s) in definition of type {}: {:?}", type_name, duplicate_member_definitions));
|
|
|
|
}
|
2019-08-12 12:46:18 -07:00
|
|
|
let spec = SymbolSpec::RecordConstructor { index, type_name: type_name.clone(), members };
|
2019-03-11 02:47:47 -07:00
|
|
|
self.add_new_symbol(name, scope_name_stack, spec);
|
2019-01-25 00:57:01 -08:00
|
|
|
},
|
2019-01-20 22:13:05 -08:00
|
|
|
}
|
|
|
|
}
|
2019-09-10 03:31:23 -07:00
|
|
|
scope_name_stack.pop();
|
2019-01-20 22:13:05 -08:00
|
|
|
Ok(())
|
2019-01-20 00:22:35 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct LocalTypeContext {
|
|
|
|
state: u8
|
|
|
|
}
|
|
|
|
impl LocalTypeContext {
|
|
|
|
fn new() -> LocalTypeContext {
|
|
|
|
LocalTypeContext { state: 0 }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn new_universal_type(&mut self) -> TypeName {
|
|
|
|
let n = self.state;
|
|
|
|
self.state += 1;
|
|
|
|
Rc::new(format!("{}", (('a' as u8) + n) as char))
|
|
|
|
}
|
2018-05-20 20:36:57 -07:00
|
|
|
}
|
2019-03-07 23:51:31 -08:00
|
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod symbol_table_tests {
|
|
|
|
use super::*;
|
2019-03-10 16:04:20 -07:00
|
|
|
use crate::util::quick_ast;
|
2019-03-07 23:51:31 -08:00
|
|
|
|
|
|
|
macro_rules! values_in_table {
|
|
|
|
//TODO multiple values
|
|
|
|
($source:expr, $single_value:expr) => {
|
|
|
|
{
|
|
|
|
let mut symbol_table = SymbolTable::new();
|
2019-03-10 16:04:20 -07:00
|
|
|
let ast = quick_ast($source);
|
2019-03-07 23:51:31 -08:00
|
|
|
symbol_table.add_top_level_symbols(&ast).unwrap();
|
2019-08-30 19:05:01 -07:00
|
|
|
match symbol_table.lookup_by_fqsn($single_value) {
|
2019-03-07 23:51:31 -08:00
|
|
|
Some(_spec) => (),
|
|
|
|
None => panic!(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn basic_symbol_table() {
|
2019-08-30 18:56:16 -07:00
|
|
|
values_in_table! { "let a = 10; fn b() { 20 }", &fqsn!("b"; tr) }
|
2019-03-07 23:51:31 -08:00
|
|
|
}
|
2019-03-08 03:57:32 -08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_duplicates() {
|
|
|
|
let source = r#"
|
|
|
|
fn a() { 1 }
|
|
|
|
fn b() { 2 }
|
|
|
|
fn a() { 3 }
|
|
|
|
"#;
|
|
|
|
let mut symbol_table = SymbolTable::new();
|
2019-03-10 16:04:20 -07:00
|
|
|
let ast = quick_ast(source);
|
|
|
|
let output = symbol_table.add_top_level_symbols(&ast).unwrap_err();
|
|
|
|
assert!(output.contains("Duplicate"))
|
|
|
|
}
|
|
|
|
|
2019-03-10 17:02:01 -07:00
|
|
|
#[test]
|
|
|
|
fn no_duplicates_2() {
|
|
|
|
let source = r#"
|
|
|
|
let a = 20;
|
|
|
|
let q = 39;
|
|
|
|
let a = 30;
|
|
|
|
"#;
|
|
|
|
let mut symbol_table = SymbolTable::new();
|
|
|
|
let ast = quick_ast(source);
|
|
|
|
let output = symbol_table.add_top_level_symbols(&ast).unwrap_err();
|
|
|
|
assert!(output.contains("Duplicate"))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_duplicates_3() {
|
|
|
|
let source = r#"
|
|
|
|
fn a() {
|
|
|
|
let a = 20
|
|
|
|
let b = 40
|
|
|
|
a + b
|
|
|
|
}
|
|
|
|
|
|
|
|
fn q() {
|
|
|
|
let x = 30
|
|
|
|
let x = 33
|
|
|
|
}
|
|
|
|
"#;
|
|
|
|
let mut symbol_table = SymbolTable::new();
|
|
|
|
let ast = quick_ast(source);
|
|
|
|
let output = symbol_table.add_top_level_symbols(&ast).unwrap_err();
|
|
|
|
assert!(output.contains("Duplicate"))
|
|
|
|
}
|
|
|
|
|
2019-03-11 02:47:47 -07:00
|
|
|
#[test]
|
2019-03-10 17:02:01 -07:00
|
|
|
fn dont_falsely_detect_duplicates() {
|
|
|
|
let source = r#"
|
|
|
|
let a = 20;
|
|
|
|
fn some_func() {
|
|
|
|
let a = 40;
|
|
|
|
77
|
|
|
|
}
|
|
|
|
let q = 39;
|
|
|
|
"#;
|
|
|
|
let mut symbol_table = SymbolTable::new();
|
|
|
|
let ast = quick_ast(source);
|
|
|
|
symbol_table.add_top_level_symbols(&ast).unwrap();
|
2019-08-30 19:05:01 -07:00
|
|
|
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());
|
2019-03-10 17:02:01 -07:00
|
|
|
}
|
|
|
|
|
2019-03-10 16:04:20 -07:00
|
|
|
#[test]
|
|
|
|
fn enclosing_scopes() {
|
|
|
|
let source = r#"
|
|
|
|
fn outer_func(x) {
|
|
|
|
fn inner_func(arg) {
|
|
|
|
arg
|
|
|
|
}
|
|
|
|
x + inner_func(x)
|
|
|
|
}"#;
|
|
|
|
let mut symbol_table = SymbolTable::new();
|
|
|
|
let ast = quick_ast(source);
|
|
|
|
symbol_table.add_top_level_symbols(&ast).unwrap();
|
2019-08-30 19:05:01 -07:00
|
|
|
assert!(symbol_table.lookup_by_fqsn(&fqsn!("outer_func"; tr)).is_some());
|
|
|
|
assert!(symbol_table.lookup_by_fqsn(&fqsn!("outer_func"; fn, "inner_func"; tr)).is_some());
|
2019-03-10 16:04:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn enclosing_scopes_2() {
|
|
|
|
let source = r#"
|
|
|
|
fn outer_func(x) {
|
|
|
|
fn inner_func(arg) {
|
|
|
|
arg
|
|
|
|
}
|
|
|
|
|
|
|
|
fn second_inner_func() {
|
|
|
|
fn another_inner_func() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inner_func(x)
|
|
|
|
}"#;
|
|
|
|
let mut symbol_table = SymbolTable::new();
|
|
|
|
let ast = quick_ast(source);
|
|
|
|
symbol_table.add_top_level_symbols(&ast).unwrap();
|
2019-08-30 19:05:01 -07:00
|
|
|
assert!(symbol_table.lookup_by_fqsn(&fqsn!("outer_func"; tr)).is_some());
|
|
|
|
assert!(symbol_table.lookup_by_fqsn(&fqsn!("outer_func"; fn, "inner_func"; tr)).is_some());
|
|
|
|
assert!(symbol_table.lookup_by_fqsn(&fqsn!("outer_func"; fn, "second_inner_func"; tr)).is_some());
|
|
|
|
assert!(symbol_table.lookup_by_fqsn(&fqsn!("outer_func"; fn, "second_inner_func"; fn, "another_inner_func"; tr)).is_some());
|
2019-03-10 16:04:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn enclosing_scopes_3() {
|
|
|
|
let source = r#"
|
|
|
|
fn outer_func(x) {
|
|
|
|
fn inner_func(arg) {
|
|
|
|
arg
|
|
|
|
}
|
|
|
|
|
|
|
|
fn second_inner_func() {
|
|
|
|
fn another_inner_func() {
|
|
|
|
}
|
|
|
|
fn another_inner_func() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inner_func(x)
|
|
|
|
}"#;
|
|
|
|
let mut symbol_table = SymbolTable::new();
|
|
|
|
let ast = quick_ast(source);
|
2019-03-08 03:57:32 -08:00
|
|
|
let output = symbol_table.add_top_level_symbols(&ast).unwrap_err();
|
|
|
|
assert!(output.contains("Duplicate"))
|
|
|
|
}
|
2019-03-07 23:51:31 -08:00
|
|
|
}
|
|
|
|
|