Big refactor of symbol table

This commit is contained in:
Greg Shuflin 2021-10-19 13:48:00 -07:00
parent 769ef448e8
commit 8b724cf0ff
8 changed files with 266 additions and 119 deletions

View File

@ -4,18 +4,15 @@ use std::cell::RefCell;
use std::rc::Rc; use std::rc::Rc;
use crate::symbol_table::SymbolTable; use crate::symbol_table::SymbolTable;
use crate::scope_resolution::ScopeResolver;
use crate::reduced_ast::reduce; use crate::reduced_ast::reduce;
use crate::eval::State; use crate::eval::State;
fn evaluate_all_outputs(input: &str) -> Vec<Result<String, String>> { fn evaluate_all_outputs(input: &str) -> Vec<Result<String, String>> {
let mut ast = crate::util::quick_ast(input); let ast = crate::util::quick_ast(input);
let symbol_table = Rc::new(RefCell::new(SymbolTable::new())); let symbol_table = Rc::new(RefCell::new(SymbolTable::new()));
symbol_table.borrow_mut().add_top_level_symbols(&ast).unwrap(); symbol_table.borrow_mut().add_top_level_symbols(&ast).unwrap();
{
let mut scope_resolver = ScopeResolver::new(symbol_table.clone()); symbol_table.borrow_mut().process_ast(&ast).unwrap();
let _ = scope_resolver.resolve(&mut ast);
}
let reduced = reduce(&ast, &symbol_table.borrow()); let reduced = reduce(&ast, &symbol_table.borrow());
let mut state = State::new(); let mut state = State::new();

View File

@ -25,7 +25,6 @@ mod builtin;
mod error; mod error;
mod eval; mod eval;
mod reduced_ast; mod reduced_ast;
mod scope_resolution;
mod schala; mod schala;

View File

@ -178,16 +178,7 @@ impl<'a> Reducer<'a> {
} }
fn value(&mut self, qualified_name: &QualifiedName) -> Expr { fn value(&mut self, qualified_name: &QualifiedName) -> Expr {
let sym_name = match self.symbol_table.get_fqsn_from_id(&qualified_name.id) { let Symbol { local_name, spec, .. } = match self.symbol_table.lookup_symbol(&qualified_name.id) {
Some(fqsn) => fqsn,
None => return Expr::ReductionError(format!("FQSN lookup for Value {:?} failed", qualified_name)),
};
//TODO this probably needs to change
let FullyQualifiedSymbolName(ref v) = sym_name;
//let name = v.last().unwrap().name.clone();
let Symbol { local_name, spec, .. } = match self.symbol_table.lookup_by_fqsn(&sym_name) {
Some(s) => s, Some(s) => s,
//TODO this causes several evaluation tests to fail, figure out what's going on here //TODO this causes several evaluation tests to fail, figure out what's going on here
//None => return Expr::ReductionError(format!("Symbol {:?} not found", sym_name)), //None => return Expr::ReductionError(format!("Symbol {:?} not found", sym_name)),
@ -220,17 +211,16 @@ impl<'a> Reducer<'a> {
} }
fn reduce_named_struct(&mut self, name: &QualifiedName, fields: &Vec<(Rc<String>, Expression)>) -> Expr { fn reduce_named_struct(&mut self, name: &QualifiedName, fields: &Vec<(Rc<String>, Expression)>) -> Expr {
let ref sym_name = match self.symbol_table.get_fqsn_from_id(&name.id) { let symbol = match self.symbol_table.lookup_symbol(&name.id) {
Some(fqsn) => fqsn, Some(fqsn) => fqsn,
None => return Expr::ReductionError(format!("FQSN lookup for name {:?} failed", name)), None => return Expr::ReductionError(format!("FQSN lookup for name {:?} failed", name)),
}; };
let FullyQualifiedSymbolName(ref v) = sym_name; let (type_name, index, members_from_table) = match &symbol.spec {
let ref name = v.last().unwrap().name; SymbolSpec::RecordConstructor { members, type_name, index } => (type_name.clone(), index, members),
let (type_name, index, members_from_table) = match self.symbol_table.lookup_by_fqsn(&sym_name) {
Some(Symbol { spec: SymbolSpec::RecordConstructor { members, type_name, index }, .. }) => (type_name.clone(), index, members),
_ => return Expr::ReductionError("Not a record constructor".to_string()), _ => return Expr::ReductionError("Not a record constructor".to_string()),
}; };
let arity = members_from_table.len(); let arity = members_from_table.len();
let mut args: Vec<(Rc<String>, Expr)> = fields.iter() let mut args: Vec<(Rc<String>, Expr)> = fields.iter()
@ -243,7 +233,7 @@ impl<'a> Reducer<'a> {
let args = args.into_iter().map(|(_, expr)| expr).collect(); let args = args.into_iter().map(|(_, expr)| expr).collect();
//TODO make sure this sorting actually works //TODO make sure this sorting actually works
let f = box Expr::Constructor { type_name, name: name.clone(), tag: *index, arity, }; let f = box Expr::Constructor { type_name, name: symbol.local_name.clone(), tag: *index, arity, };
Expr::Call { f, args } Expr::Call { f, args }
} }
@ -387,8 +377,7 @@ fn handle_symbol(symbol: Option<&Symbol>, inner_patterns: &Vec<Pattern>, symbol_
}); });
let bound_vars = inner_patterns.iter().map(|p| match p { let bound_vars = inner_patterns.iter().map(|p| match p {
VarOrName(qualified_name) => { VarOrName(qualified_name) => {
let fqsn = symbol_table.get_fqsn_from_id(&qualified_name.id); let symbol_exists = symbol_table.lookup_symbol(&qualified_name.id).is_some();
let symbol_exists = fqsn.and_then(|fqsn| symbol_table.lookup_by_fqsn(&fqsn)).is_some();
if symbol_exists { if symbol_exists {
None None
} else { } else {
@ -448,12 +437,9 @@ impl Pattern {
use self::Pattern::*; use self::Pattern::*;
match self { match self {
TupleStruct(QualifiedName{ components, id }, inner_patterns) => { TupleStruct(QualifiedName{ components, id }, inner_patterns) => {
let fqsn = symbol_table.get_fqsn_from_id(id); match symbol_table.lookup_symbol(id) {
match fqsn.and_then(|fqsn| symbol_table.lookup_by_fqsn(&fqsn)) {
Some(symbol) => handle_symbol(Some(symbol), inner_patterns, symbol_table), Some(symbol) => handle_symbol(Some(symbol), inner_patterns, symbol_table),
None => { None => panic!("Symbol {:?} not found", components)
panic!("Symbol {:?} not found", components);
}
} }
}, },
TuplePattern(inner_patterns) => handle_symbol(None, inner_patterns, symbol_table), TuplePattern(inner_patterns) => handle_symbol(None, inner_patterns, symbol_table),
@ -463,12 +449,12 @@ impl Pattern {
Ignored => Subpattern { tag: None, subpatterns: vec![], guard: None, bound_vars: vec![] }, Ignored => Subpattern { tag: None, subpatterns: vec![], guard: None, bound_vars: vec![] },
Literal(lit) => lit.to_subpattern(symbol_table), Literal(lit) => lit.to_subpattern(symbol_table),
VarOrName(QualifiedName { components, id }) => { VarOrName(QualifiedName { components, id }) => {
// if fqsn is Some, treat this as a symbol pattern. If it's None, treat it // if symbol is Some, treat this as a symbol pattern. If it's None, treat it
// as a variable. // as a variable.
let fqsn = symbol_table.get_fqsn_from_id(id); match symbol_table.lookup_symbol(id) {
match fqsn.as_ref().and_then(|fqsn| symbol_table.lookup_by_fqsn(fqsn)) {
Some(symbol) => handle_symbol(Some(symbol), &vec![], symbol_table), Some(symbol) => handle_symbol(Some(symbol), &vec![], symbol_table),
None => { None => {
println!("Components: {:?}", components);
let name = if components.len() == 1 { let name = if components.len() == 1 {
components[0].clone() components[0].clone()
} else { } else {

View File

@ -20,7 +20,6 @@ pub struct Schala {
state: eval::State<'static>, state: eval::State<'static>,
/// Keeps track of symbols and scopes /// Keeps track of symbols and scopes
symbol_table: SymbolTableHandle, symbol_table: SymbolTableHandle,
resolver: crate::scope_resolution::ScopeResolver<'static>,
/// Contains information for type-checking /// Contains information for type-checking
type_context: typechecking::TypeContext<'static>, type_context: typechecking::TypeContext<'static>,
/// Schala Parser /// Schala Parser
@ -45,7 +44,6 @@ impl Schala {
Schala { Schala {
source_reference: SourceReference::new(), source_reference: SourceReference::new(),
symbol_table: symbols.clone(), symbol_table: symbols.clone(),
resolver: crate::scope_resolution::ScopeResolver::new(symbols.clone()),
state: eval::State::new(), state: eval::State::new(),
type_context: typechecking::TypeContext::new(), type_context: typechecking::TypeContext::new(),
active_parser: parsing::Parser::new() active_parser: parsing::Parser::new()
@ -78,18 +76,13 @@ impl Schala {
//2nd stage - parsing //2nd stage - parsing
self.active_parser.add_new_tokens(tokens); self.active_parser.add_new_tokens(tokens);
let mut ast = self.active_parser.parse() let ast = self.active_parser.parse()
.map_err(|err| SchalaError::from_parse_error(err, &self.source_reference))?; .map_err(|err| SchalaError::from_parse_error(err, &self.source_reference))?;
// Symbol table // Symbol table
self.symbol_table.borrow_mut().add_top_level_symbols(&ast) self.symbol_table.borrow_mut().add_top_level_symbols(&ast)
.map_err(|err| SchalaError::from_string(err, Stage::Symbols))?; .map_err(|err| SchalaError::from_string(err, Stage::Symbols))?;
// Scope resolution - requires mutating AST
self.resolver.resolve(&mut ast)
.map_err(|err| SchalaError::from_string(err, Stage::ScopeResolution))?;
//WIP - new symbol table //WIP - new symbol table
self.symbol_table.borrow_mut().process_ast(&ast) self.symbol_table.borrow_mut().process_ast(&ast)
.map_err(|err| SchalaError::from_string(err, Stage::Symbols))?; .map_err(|err| SchalaError::from_string(err, Stage::Symbols))?;

View File

@ -9,32 +9,18 @@ use crate::ast;
use crate::ast::{ItemId, TypeBody, Variant, TypeSingletonName, Signature, Declaration, Statement, StatementKind, ModuleSpecifier}; use crate::ast::{ItemId, TypeBody, Variant, TypeSingletonName, Signature, Declaration, Statement, StatementKind, ModuleSpecifier};
use crate::typechecking::TypeName; use crate::typechecking::TypeName;
mod resolver;
#[allow(unused_macros)]
macro_rules! fqsn {
( $( $name:expr ; $kind:tt),* ) => {
{
let mut vec = vec![];
$(
vec.push(crate::symbol_table::ScopeSegment::new(std::rc::Rc::new($name.to_string())));
)*
FullyQualifiedSymbolName(vec)
}
};
}
mod tables; mod tables;
use tables::DeclLocations; use tables::DeclLocations;
mod symbol_trie; mod symbol_trie;
use symbol_trie::SymbolTrie; use symbol_trie::SymbolTrie;
mod test; mod test;
/// Fully-qualified symbol name /// Fully-qualified symbol name
#[derive(Debug, Clone, Eq, PartialEq, Hash, PartialOrd, Ord)] #[derive(Debug, Clone, Eq, PartialEq, Hash, PartialOrd, Ord)]
struct FQSN { pub struct FQSN {
scopes: Vec<Scope>, //TODO FQSN's need to be cheaply cloneable
scopes: Vec<Scope>, //TODO rename to ScopeSegment
} }
impl FQSN { impl FQSN {
@ -58,7 +44,6 @@ impl FQSN {
/// One segment within a scope. /// One segment within a scope.
#[derive(Debug, Clone, Eq, PartialEq, Hash, PartialOrd, Ord)] #[derive(Debug, Clone, Eq, PartialEq, Hash, PartialOrd, Ord)]
enum Scope { enum Scope {
Top,
Name(String) Name(String)
} }
@ -141,12 +126,21 @@ impl ScopeSegment {
//cf. p. 150 or so of Language Implementation Patterns //cf. p. 150 or so of Language Implementation Patterns
pub struct SymbolTable { pub struct SymbolTable {
decl_locations: DeclLocations, decl_locations: DeclLocations, //TODO delete this
symbol_path_to_symbol: HashMap<FullyQualifiedSymbolName, Symbol>, symbol_path_to_symbol: HashMap<FullyQualifiedSymbolName, Symbol>,
id_to_fqsn: HashMap<ItemId, FullyQualifiedSymbolName>,
symbol_trie: SymbolTrie, symbol_trie: SymbolTrie,
/// These tables are responsible for preventing duplicate names.
fq_names: NameTable<NameKind>, //Note that presence of two tables implies that a type and other binding with the same name can co-exist 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>,
/// A map of the `ItemId`s of instances of use of names to their fully-canonicalized FQSN form.
id_to_fqsn: HashMap<ItemId, FQSN>,
/// A map of the FQSN of an AST definition to a Symbol data structure, which contains
/// some basic information about what that symbol is and (ideally) references to other tables
/// (e.g. typechecking tables) with more information about that symbol.
fqsn_to_symbol: HashMap<FQSN, Symbol>,
} }
impl SymbolTable { impl SymbolTable {
@ -154,44 +148,35 @@ impl SymbolTable {
SymbolTable { SymbolTable {
decl_locations: DeclLocations::new(), decl_locations: DeclLocations::new(),
symbol_path_to_symbol: HashMap::new(), symbol_path_to_symbol: HashMap::new(),
id_to_fqsn: HashMap::new(),
symbol_trie: SymbolTrie::new(), symbol_trie: SymbolTrie::new(),
fq_names: NameTable::new(), fq_names: NameTable::new(),
types: NameTable::new(), types: NameTable::new(),
id_to_fqsn: HashMap::new(),
fqsn_to_symbol: HashMap::new(),
} }
} }
pub fn map_id_to_fqsn(&mut self, id: &ItemId, fqsn: FullyQualifiedSymbolName) { pub fn lookup_symbol(&self, id: &ItemId) -> Option<&Symbol> {
self.id_to_fqsn.insert(id.clone(), fqsn); let fqsn = self.id_to_fqsn.get(id);
} fqsn.and_then(|fqsn| self.fqsn_to_symbol.get(fqsn))
pub fn get_fqsn_from_id(&self, id: &ItemId) -> Option<FullyQualifiedSymbolName> {
self.id_to_fqsn.get(&id).cloned()
} }
fn add_new_symbol(&mut self, local_name: &Rc<String>, scope_path: &Vec<ScopeSegment>, spec: SymbolSpec) { fn add_new_symbol(&mut self, local_name: &Rc<String>, scope_path: &Vec<ScopeSegment>, spec: SymbolSpec) {
let mut vec: Vec<ScopeSegment> = scope_path.clone(); let mut vec: Vec<ScopeSegment> = scope_path.clone();
vec.push(ScopeSegment { name: local_name.clone() }); vec.push(ScopeSegment { name: local_name.clone() });
let fully_qualified_name = FullyQualifiedSymbolName(vec); let fully_qualified_name = FullyQualifiedSymbolName(vec);
let symbol = Symbol { local_name: local_name.clone(), fully_qualified_name: fully_qualified_name.clone(), spec }; let symbol = Symbol { local_name: local_name.clone(), /*fully_qualified_name: fully_qualified_name.clone(),*/ spec };
self.symbol_trie.insert(&fully_qualified_name); //self.symbol_trie.insert(&fully_qualified_name);
self.symbol_path_to_symbol.insert(fully_qualified_name, symbol); self.symbol_path_to_symbol.insert(fully_qualified_name, symbol);
} }
pub fn lookup_by_fqsn(&self, fully_qualified_path: &FullyQualifiedSymbolName) -> Option<&Symbol> {
self.symbol_path_to_symbol.get(fully_qualified_path)
}
pub fn lookup_children_of_fqsn(&self, path: &FullyQualifiedSymbolName) -> Vec<FullyQualifiedSymbolName> {
self.symbol_trie.get_children(path)
}
} }
#[allow(dead_code)] #[allow(dead_code)]
#[derive(Debug)] #[derive(Debug)]
pub struct Symbol { pub struct Symbol {
pub local_name: Rc<String>, pub local_name: Rc<String>,
fully_qualified_name: FullyQualifiedSymbolName, //fully_qualified_name: FullyQualifiedSymbolName,
pub spec: SymbolSpec, pub spec: SymbolSpec,
} }
@ -206,8 +191,8 @@ pub enum SymbolSpec {
Func(Vec<TypeName>), Func(Vec<TypeName>),
DataConstructor { DataConstructor {
index: usize, index: usize,
type_name: TypeName, type_name: TypeName, //TODO this eventually needs to be some kind of ID
type_args: Vec<Rc<String>>, type_args: Vec<Rc<String>>, //TODO this should be a lookup table into type information, it's not the concern of the symbol table
}, },
RecordConstructor { RecordConstructor {
index: usize, index: usize,
@ -249,8 +234,11 @@ impl SymbolTable {
Ok(()) Ok(())
} }
//TODO does the same thing scope resolution... but maybe I don't need that /// Walks the AST, matching the ID of an identifier used in some expression to
/// the corresponding Symbol.
fn resolve_symbol_ids(&mut self, ast: &ast::AST) -> Result<(), String> { fn resolve_symbol_ids(&mut self, ast: &ast::AST) -> Result<(), String> {
let mut resolver = resolver::Resolver::new(self);
resolver.resolve(ast)?;
Ok(()) Ok(())
} }
@ -259,7 +247,7 @@ impl SymbolTable {
/// checks for dupicate definitions (and returns errors if discovered), and sets /// checks for dupicate definitions (and returns errors if discovered), and sets
/// up name tables that will be used by further parts of the compiler /// 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) -> Result<(), String> {
let mut scope_stack = vec![Scope::Top]; let mut scope_stack = vec![];
self.add_from_scope(ast.statements.as_ref(), &mut scope_stack) self.add_from_scope(ast.statements.as_ref(), &mut scope_stack)
.map_err(|err| format!("{:?}", err))?; .map_err(|err| format!("{:?}", err))?;
Ok(()) Ok(())
@ -268,21 +256,32 @@ impl SymbolTable {
//TODO this should probably return a vector of duplicate name errors //TODO this should probably return a vector of duplicate name errors
fn add_from_scope<'a>(&'a mut self, statements: &[Statement], scope_stack: &mut Vec<Scope>) -> Result<(), DuplicateName> { fn add_from_scope<'a>(&'a mut self, statements: &[Statement], scope_stack: &mut Vec<Scope>) -> Result<(), DuplicateName> {
for statement in statements { for statement in statements {
let Statement { id: _, kind, location } = statement; let Statement { id, kind, location } = statement;
let location = *location; let location = *location;
match kind { match kind {
StatementKind::Declaration(Declaration::FuncSig(signature)) => { StatementKind::Declaration(Declaration::FuncSig(signature)) => {
let fn_name: String = signature.name.as_str().to_owned(); let fn_name: String = signature.name.as_str().to_owned();
let fq_function = FQSN::from_scope_stack(scope_stack.as_ref(), fn_name); let fq_function = FQSN::from_scope_stack(scope_stack.as_ref(), fn_name);
self.fq_names.register(fq_function.clone(), NameSpec { location, kind: NameKind::Function })?; self.fq_names.register(fq_function.clone(), NameSpec { location, kind: NameKind::Function })?;
self.types.register(fq_function, NameSpec { location, kind: TypeKind } )?; self.types.register(fq_function.clone(), NameSpec { location, kind: TypeKind } )?;
self.fqsn_to_symbol.insert(fq_function, Symbol {
local_name: signature.name.clone(),
spec: SymbolSpec::Func(vec![]), //TODO does this inner vec need to exist at all?
});
} }
StatementKind::Declaration(Declaration::FuncDecl(signature, body)) => { StatementKind::Declaration(Declaration::FuncDecl(signature, body)) => {
let fn_name: String = signature.name.as_str().to_owned(); let fn_name: String = signature.name.as_str().to_owned();
let new_scope = Scope::Name(fn_name.clone()); let new_scope = Scope::Name(fn_name.clone());
let fq_function = FQSN::from_scope_stack(scope_stack.as_ref(), fn_name); let fq_function = FQSN::from_scope_stack(scope_stack.as_ref(), fn_name);
self.fq_names.register(fq_function.clone(), NameSpec { location, kind: NameKind::Function })?; self.fq_names.register(fq_function.clone(), NameSpec { location, kind: NameKind::Function })?;
self.types.register(fq_function, NameSpec { location, kind: TypeKind } )?; self.types.register(fq_function.clone(), NameSpec { location, kind: TypeKind } )?;
self.fqsn_to_symbol.insert(fq_function, Symbol {
local_name: signature.name.clone(),
spec: SymbolSpec::Func(vec![]), //TODO does this inner vec need to exist at all?
});
scope_stack.push(new_scope); scope_stack.push(new_scope);
let output = self.add_from_scope(body.as_ref(), scope_stack); let output = self.add_from_scope(body.as_ref(), scope_stack);
scope_stack.pop(); scope_stack.pop();
@ -291,13 +290,17 @@ impl SymbolTable {
StatementKind::Declaration(Declaration::TypeDecl { name, body, mutable }) => { StatementKind::Declaration(Declaration::TypeDecl { name, body, mutable }) => {
let fq_type = FQSN::from_scope_stack(scope_stack.as_ref(), name.name.as_ref().to_owned()); let fq_type = FQSN::from_scope_stack(scope_stack.as_ref(), name.name.as_ref().to_owned());
self.types.register(fq_type, NameSpec { location, kind: TypeKind } )?; self.types.register(fq_type, NameSpec { location, kind: TypeKind } )?;
if let Err(errors) = self.add_type_members(name, body, mutable, location, scope_stack) { if let Err(errors) = self.add_type_members(id, name, body, mutable, location, scope_stack) {
return Err(errors[0].clone()); return Err(errors[0].clone());
} }
}, },
StatementKind::Declaration(Declaration::Binding { name, .. }) => { StatementKind::Declaration(Declaration::Binding { name, .. }) => {
let fq_binding = FQSN::from_scope_stack(scope_stack.as_ref(), name.as_str().to_owned()); let fq_binding = FQSN::from_scope_stack(scope_stack.as_ref(), name.as_str().to_owned());
self.fq_names.register(fq_binding, NameSpec { location, kind: NameKind::Binding })?; self.fq_names.register(fq_binding.clone(), NameSpec { location, kind: NameKind::Binding })?;
self.fqsn_to_symbol.insert(fq_binding, Symbol {
local_name: name.clone(),
spec: SymbolSpec::Binding,
});
} }
StatementKind::Module(ModuleSpecifier { name, contents }) => { StatementKind::Module(ModuleSpecifier { name, contents }) => {
let mod_name = name.as_str().to_owned(); let mod_name = name.as_str().to_owned();
@ -315,35 +318,77 @@ impl SymbolTable {
Ok(()) 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, id: &ItemId, type_name: &TypeSingletonName, type_body: &TypeBody, _mutable: &bool, location: Location, scope_stack: &mut Vec<Scope>) -> Result<(), Vec<DuplicateName>> {
let mut errors = vec![]; let mut errors = vec![];
let mut register = |fqsn: FQSN| { let mut register = |fqsn: FQSN, spec: SymbolSpec| {
let spec = NameSpec { location, kind: TypeKind }; let name_spec = NameSpec { location, kind: TypeKind };
if let Err(err) = self.types.register(fqsn, spec) { if let Err(err) = self.types.register(fqsn.clone(), name_spec) {
errors.push(err); errors.push(err);
} } else {
let local_name = match spec {
SymbolSpec::DataConstructor { ref type_name, ..} | SymbolSpec::RecordConstructor { ref type_name, .. } => type_name.clone(),
_ => panic!("This should never happen"),
};
let symbol = Symbol { local_name, spec };
self.fqsn_to_symbol.insert(fqsn, symbol);
};
}; };
let TypeBody(variants) = type_body; let TypeBody(variants) = type_body;
let new_scope = Scope::Name(type_name.name.as_ref().to_owned()); let new_scope = Scope::Name(type_name.name.as_ref().to_owned());
scope_stack.push(new_scope); scope_stack.push(new_scope);
for variant in variants { for (index, variant) in variants.iter().enumerate() {
match variant { match variant {
Variant::UnitStruct(name) | Variant::TupleStruct(name, _) => { Variant::UnitStruct(name) => {
let fq_name = FQSN::from_scope_stack(scope_stack.as_ref(), name.as_ref().to_owned()); let fq_name = FQSN::from_scope_stack(scope_stack.as_ref(), name.as_ref().to_owned());
register(fq_name); let spec = SymbolSpec::DataConstructor {
index,
type_name: name.clone(),
type_args: vec![],
};
register(fq_name, spec);
},
Variant::TupleStruct(name, items) => {
let fq_name = FQSN::from_scope_stack(scope_stack.as_ref(), name.as_ref().to_owned());
let spec = SymbolSpec::DataConstructor {
index,
type_name: name.clone(),
type_args: items.iter().map(|_| Rc::new("DUMMY_TYPE_ID".to_string())).collect()
};
register(fq_name, spec);
}, },
Variant::Record { name, members } => { Variant::Record { name, members } => {
let fq_name = FQSN::from_scope_stack(scope_stack.as_ref(), name.as_ref().to_owned()); let fq_name = FQSN::from_scope_stack(scope_stack.as_ref(), name.as_ref().to_owned());
register(fq_name.clone()); let spec = SymbolSpec::RecordConstructor {
for (field_name, _) in members { index,
let fq_field_name = fq_name.extend(field_name.as_str().to_owned()); type_name: name.clone(),
register(fq_field_name); members: members.iter()
.map(|(_, _)| (Rc::new("DUMMY_FIELD".to_string()), Rc::new("DUMMY_TYPE_ID".to_string()))).collect()
};
register(fq_name, spec);
//TODO check for duplicates among struct member definitions
/*
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));
}
*/
}
}
} }
scope_stack.pop(); scope_stack.pop();
@ -355,7 +400,6 @@ impl SymbolTable {
} }
} }
pub fn add_top_level_symbols(&mut self, ast: &ast::AST) -> Result<(), String> { pub fn add_top_level_symbols(&mut self, ast: &ast::AST) -> Result<(), String> {
let mut scope_name_stack = Vec::new(); let mut scope_name_stack = Vec::new();
self.add_symbols_from_scope(&ast.statements, &mut scope_name_stack) self.add_symbols_from_scope(&ast.statements, &mut scope_name_stack)

View File

@ -0,0 +1,112 @@
use std::rc::Rc;
use crate::symbol_table::{SymbolTable, ScopeSegment, FullyQualifiedSymbolName, FQSN, Scope};
use crate::ast::*;
use crate::util::ScopeStack;
type FQSNPrefix = Vec<Scope>;
pub struct Resolver<'a> {
symbol_table: &'a mut super::SymbolTable,
name_scope_stack: ScopeStack<'a, Rc<String>, FQSNPrefix>,
}
impl<'a> Resolver<'a> {
pub fn new(symbol_table: &'a mut SymbolTable) -> Self {
let name_scope_stack: ScopeStack<'a, Rc<String>, FQSNPrefix> = ScopeStack::new(None);
Self { symbol_table, name_scope_stack }
}
pub fn resolve(&mut self, ast: &AST) -> Result<(), String> {
walk_ast(self, ast);
Ok(())
}
fn lookup_name_in_scope(&self, sym_name: &QualifiedName) -> FQSN {
let QualifiedName { components, .. } = sym_name;
let first_component = &components[0];
match self.name_scope_stack.lookup(first_component) {
None => {
FQSN {
scopes: components.iter()
.map(|name| Scope::Name(name.as_ref().to_owned()))
.collect()
}
},
Some(fqsn_prefix) => {
let mut full_name = fqsn_prefix.clone();
let rest_of_name: FQSNPrefix = components[1..].iter().map(|name| Scope::Name(name.as_ref().to_owned())).collect();
full_name.extend_from_slice(&rest_of_name);
FQSN {
scopes: full_name
}
}
}
}
// This might be a variable or a pattern, depending on whether this symbol
// already exists in the table.
fn qualified_name_in_pattern(&mut self, qualified_name: &QualifiedName) {
let fqsn = self.lookup_name_in_scope(qualified_name);
if self.symbol_table.fqsn_to_symbol.get(&fqsn).is_some() {
self.symbol_table.id_to_fqsn.insert(qualified_name.id.clone(), fqsn); //TODO maybe set this to an explicit value if none?
}
}
}
impl<'a> ASTVisitor for Resolver<'a> {
//TODO need to un-insert these - maybe need to rethink visitor
fn import(&mut self, import_spec: &ImportSpecifier) {
let ImportSpecifier { ref path_components, ref imported_names, .. } = &import_spec;
match imported_names {
ImportedNames::All => {
let prefix = FQSN {
scopes: path_components.iter().map(|c| Scope::Name(c.as_ref().to_owned())).collect()
};
let members = self.symbol_table.symbol_trie.get_children(&prefix);
for member in members.into_iter() {
let local_name = match member.scopes.last().unwrap() {
Scope::Name(n) => Rc::new(n.clone()),
_ => panic!("LOL bad"),
};
self.name_scope_stack.insert(local_name.clone(), member.scopes);
}
},
ImportedNames::LastOfPath => {
let name = path_components.last().unwrap(); //TODO handle better
let fqsn_prefix = path_components.iter()
.map(|c| Scope::Name(c.as_ref().to_owned()))
.collect();
self.name_scope_stack.insert(name.clone(), fqsn_prefix);
}
ImportedNames::List(ref names) => {
let fqsn_prefix: FQSNPrefix = path_components.iter()
.map(|c| Scope::Name(c.as_ref().to_owned()))
.collect();
for name in names.iter() {
self.name_scope_stack.insert(name.clone(), fqsn_prefix.clone());
}
}
};
}
fn qualified_name(&mut self, qualified_name: &QualifiedName) {
let fqsn = self.lookup_name_in_scope(&qualified_name);
self.symbol_table.id_to_fqsn.insert(qualified_name.id.clone(), fqsn);
}
fn named_struct(&mut self, qualified_name: &QualifiedName, _fields: &Vec<(Rc<String>, Expression)>) {
let fqsn = self.lookup_name_in_scope(&qualified_name);
self.symbol_table.id_to_fqsn.insert(qualified_name.id.clone(), fqsn);
}
fn pattern(&mut self, pat: &Pattern) {
use Pattern::*;
match pat {
//TODO I think not handling TuplePattern is an oversight
TuplePattern(_) => (),
Literal(_) | Ignored => (),
TupleStruct(name, _) | Record(name, _) | VarOrName(name) => self.qualified_name_in_pattern(name),
};
}
}

View File

@ -1,18 +1,19 @@
use radix_trie::{Trie, TrieCommon, TrieKey}; use radix_trie::{Trie, TrieCommon, TrieKey};
use super::FullyQualifiedSymbolName; use super::{Scope, FQSN};
use std::hash::{Hasher, Hash}; use std::hash::{Hasher, Hash};
use std::collections::hash_map::DefaultHasher; use std::collections::hash_map::DefaultHasher;
#[derive(Debug)] #[derive(Debug)]
pub struct SymbolTrie(Trie<FullyQualifiedSymbolName, ()>); pub struct SymbolTrie(Trie<FQSN, ()>);
impl TrieKey for FullyQualifiedSymbolName { impl TrieKey for FQSN {
fn encode_bytes(&self) -> Vec<u8> { fn encode_bytes(&self) -> Vec<u8> {
let mut hasher = DefaultHasher::new(); let mut hasher = DefaultHasher::new();
let mut output = vec![]; let mut output = vec![];
let FullyQualifiedSymbolName(scopes) = self; for segment in self.scopes.iter() {
for segment in scopes.iter() { if let Scope::Name(s) = segment {
segment.name.as_bytes().hash(&mut hasher); s.as_bytes().hash(&mut hasher);
}
output.extend_from_slice(&hasher.finish().to_be_bytes()); output.extend_from_slice(&hasher.finish().to_be_bytes());
} }
output output
@ -24,28 +25,44 @@ impl SymbolTrie {
SymbolTrie(Trie::new()) SymbolTrie(Trie::new())
} }
pub fn insert(&mut self, fqsn: &FullyQualifiedSymbolName) { pub fn insert(&mut self, fqsn: &FQSN) {
self.0.insert(fqsn.clone(), ()); self.0.insert(fqsn.clone(), ());
} }
pub fn get_children(&self, fqsn: &FullyQualifiedSymbolName) -> Vec<FullyQualifiedSymbolName> { pub fn get_children(&self, fqsn: &FQSN) -> Vec<FQSN> {
let subtrie = match self.0.subtrie(fqsn) { let subtrie = match self.0.subtrie(fqsn) {
Some(s) => s, Some(s) => s,
None => return vec![] None => return vec![]
}; };
let output: Vec<FullyQualifiedSymbolName> = subtrie.keys().filter(|cur_key| **cur_key != *fqsn).map(|fqsn| fqsn.clone()).collect(); let output: Vec<FQSN> = subtrie.keys().filter(|cur_key| **cur_key != *fqsn).map(|fqsn| fqsn.clone()).collect();
output output
} }
} }
#[cfg(test)]
mod test {
use super::*;
use crate::symbol_table::{Scope, FQSN};
fn make_fqsn(strs: &[&str]) -> FQSN {
let mut scopes = vec![];
for s in strs {
scopes.push(Scope::Name(s.to_string()));
}
FQSN {
scopes
}
}
#[test] #[test]
fn test_trie_insertion() { fn test_trie_insertion() {
let mut trie = SymbolTrie::new(); let mut trie = SymbolTrie::new();
trie.insert(&fqsn!("unrelated"; ty, "thing"; tr)); trie.insert(&make_fqsn(&["unrelated","thing"]));
trie.insert(&fqsn!("outer"; ty, "inner"; tr)); trie.insert(&make_fqsn(&["outer","inner"]));
trie.insert(&fqsn!("outer"; ty, "inner"; ty, "still_inner"; tr)); trie.insert(&make_fqsn(&["outer","inner", "still_inner"]));
let children = trie.get_children(&fqsn!("outer"; ty, "inner"; tr)); let children = trie.get_children(&make_fqsn(&["outer", "inner"]));
assert_eq!(children.len(), 1); assert_eq!(children.len(), 1);
}
} }

View File

@ -11,7 +11,6 @@ fn add_symbols(src: &str) -> (SymbolTable, Result<(), String>) {
fn make_fqsn(strs: &[&str]) -> FQSN { fn make_fqsn(strs: &[&str]) -> FQSN {
let mut scopes = vec![]; let mut scopes = vec![];
scopes.push(Scope::Top);
for s in strs { for s in strs {
scopes.push(Scope::Name(s.to_string())); scopes.push(Scope::Name(s.to_string()));
} }