Compare commits

..

No commits in common. "40f759eea8417c6b2c813b491ec402ac9819473b" and "5bba900a3d42a2005db31945201f8b6ccbf15586" have entirely different histories.

12 changed files with 368 additions and 488 deletions

View File

@ -11,8 +11,7 @@ pub use visitor::ASTVisitor;
pub use walker::walk_ast;
use crate::tokenizing::Location;
/// An abstract identifier for an AST node. Note that
/// the u32 index limits the size of an AST to 2^32 nodes.
/// An abstract identifier for an AST node
#[derive(Debug, PartialEq, Eq, Hash, Clone, Default)]
pub struct ItemId {
idx: u32,
@ -32,7 +31,13 @@ impl ItemIdStore {
pub fn new() -> ItemIdStore {
ItemIdStore { last_idx: 0 }
}
/// Always returns an ItemId with internal value zero
#[cfg(test)]
pub fn new_id() -> ItemId {
ItemId { idx: 0 }
}
/// This limits the size of the AST to 2^32 tree elements
pub fn fresh(&mut self) -> ItemId {
let idx = self.last_idx;
self.last_idx += 1;

View File

@ -2,8 +2,10 @@ use std::rc::Rc;
use std::fmt::Write;
use std::io;
//use crate::schala::SymbolTableHandle;
use crate::util::ScopeStack;
use crate::reduced_ast::{BoundVars, ReducedAST, Stmt, Expr, Lit, Func, Alternative, Subpattern};
//use crate::symbol_table::{SymbolSpec, Symbol, SymbolTable, FullyQualifiedSymbolName};
use crate::builtin::Builtin;
mod test;

View File

@ -1,17 +1,23 @@
#![cfg(test)]
use std::cell::RefCell;
use std::rc::Rc;
use crate::symbol_table::SymbolTable;
use crate::scope_resolution::ScopeResolver;
use crate::reduced_ast::reduce;
use crate::eval::State;
fn evaluate_all_outputs(input: &str) -> Vec<Result<String, String>> {
let ast = crate::util::quick_ast(input);
let mut symbol_table = SymbolTable::new();
symbol_table.process_ast(&ast).unwrap();
let reduced = reduce(&ast, &symbol_table);
let mut ast = crate::util::quick_ast(input);
let symbol_table = Rc::new(RefCell::new(SymbolTable::new()));
symbol_table.borrow_mut().add_top_level_symbols(&ast).unwrap();
{
let mut scope_resolver = ScopeResolver::new(symbol_table.clone());
let _ = scope_resolver.resolve(&mut ast);
}
let reduced = reduce(&ast, &symbol_table.borrow());
let mut state = State::new();
let all_output = state.evaluate(reduced, true);
all_output

View File

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

View File

@ -45,7 +45,7 @@ macro_rules! parse_test {
};
}
macro_rules! parse_test_wrap_ast {
($string:expr, $correct:expr) => { parse_test!($string, AST { id: Default::default(), statements: vec![$correct] }) }
($string:expr, $correct:expr) => { parse_test!($string, AST { id: ItemIdStore::new_id(), statements: vec![$correct] }) }
}
macro_rules! parse_error {
($string:expr) => { assert!(parse($string).is_err()) }
@ -57,12 +57,12 @@ macro_rules! qname {
$(
components.push(rc!($component));
)*
QualifiedName { components, id: Default::default() }
QualifiedName { components, id: ItemIdStore::new_id() }
}
};
}
macro_rules! val {
($var:expr) => { Value(QualifiedName { components: vec![Rc::new($var.to_string())], id: Default::default() }) };
($var:expr) => { Value(QualifiedName { components: vec![Rc::new($var.to_string())], id: ItemIdStore::new_id() }) };
}
macro_rules! ty {
($name:expr) => { Singleton(tys!($name)) }
@ -90,8 +90,8 @@ macro_rules! module {
}
macro_rules! ex {
($expr_type:expr) => { Expression::new(Default::default(), $expr_type) };
($expr_type:expr, $type_anno:expr) => { Expression::with_anno(Default::default(), $expr_type, $type_anno) };
($expr_type:expr) => { Expression::new(ItemIdStore::new_id(), $expr_type) };
($expr_type:expr, $type_anno:expr) => { Expression::with_anno(ItemIdStore::new_id(), $expr_type, $type_anno) };
(s $expr_text:expr) => {
{
let mut parser = make_parser($expr_text);
@ -105,14 +105,14 @@ macro_rules! inv {
}
macro_rules! binexp {
($op:expr, $lhs:expr, $rhs:expr) => { BinExp(BinOp::from_sigil($op), bx!(Expression::new(Default::default(), $lhs).into()), bx!(Expression::new(Default::default(), $rhs).into())) }
($op:expr, $lhs:expr, $rhs:expr) => { BinExp(BinOp::from_sigil($op), bx!(Expression::new(ItemIdStore::new_id(), $lhs).into()), bx!(Expression::new(ItemIdStore::new_id(), $rhs).into())) }
}
macro_rules! prefexp {
($op:expr, $lhs:expr) => { PrefixExp(PrefixOp::from_sigil($op), bx!(Expression::new(Default::default(), $lhs).into())) }
($op:expr, $lhs:expr) => { PrefixExp(PrefixOp::from_sigil($op), bx!(Expression::new(ItemIdStore::new_id(), $lhs).into())) }
}
macro_rules! exst {
($expr_type:expr) => { make_statement(StatementKind::Expression(Expression::new(Default::default(), $expr_type).into())) };
($expr_type:expr, $type_anno:expr) => { make_statement(StatementKind::Expression(Expression::with_anno(Default::default(), $expr_type, $type_anno).into())) };
($expr_type:expr) => { make_statement(StatementKind::Expression(Expression::new(ItemIdStore::new_id(), $expr_type).into())) };
($expr_type:expr, $type_anno:expr) => { make_statement(StatementKind::Expression(Expression::with_anno(ItemIdStore::new_id(), $expr_type, $type_anno).into())) };
($op:expr, $lhs:expr, $rhs:expr) => { make_statement(StatementKind::Expression(ex!(binexp!($op, $lhs, $rhs)))) };
(s $statement_text:expr) => {
{
@ -137,7 +137,7 @@ fn parsing_number_literals_and_binexps() {
parse_test! {"3; 4; 4.3",
AST {
id: Default::default(),
id: ItemIdStore::new_id(),
statements: vec![exst!(NatLiteral(3)), exst!(NatLiteral(4)),
exst!(FloatLiteral(4.3))]
}
@ -217,7 +217,7 @@ fn qualified_identifiers() {
parse_test_wrap_ast! {
"let q_q = Yolo::Swaggins",
decl!(Binding { name: rc!(q_q), constant: true, type_anno: None,
expr: Expression::new(Default::default(), Value(qname!(Yolo, Swaggins))),
expr: Expression::new(ItemIdStore::new_id(), Value(qname!(Yolo, Swaggins))),
})
}
@ -583,7 +583,7 @@ fn more_advanced_lambdas() {
r#"fn wahoo() { let a = 10; \(x) { x + a } };
wahoo()(3) "#,
AST {
id: Default::default(),
id: ItemIdStore::new_id(),
statements: vec![
exst!(s r"fn wahoo() { let a = 10; \(x) { x + a } }"),
exst! {
@ -746,7 +746,7 @@ fn imports() {
parse_test_wrap_ast! {
"import harbinger::draughts::Norgleheim",
import!(ImportSpecifier {
id: Default::default(),
id: ItemIdStore::new_id(),
path_components: vec![rc!(harbinger), rc!(draughts), rc!(Norgleheim)],
imported_names: ImportedNames::LastOfPath
})
@ -758,7 +758,7 @@ fn imports_2() {
parse_test_wrap_ast! {
"import harbinger::draughts::{Norgleheim, Xraksenlaigar}",
import!(ImportSpecifier {
id: Default::default(),
id: ItemIdStore::new_id(),
path_components: vec![rc!(harbinger), rc!(draughts)],
imported_names: ImportedNames::List(vec![
rc!(Norgleheim),
@ -773,7 +773,7 @@ fn imports_3() {
parse_test_wrap_ast! {
"import bespouri::{}",
import!(ImportSpecifier {
id: Default::default(),
id: ItemIdStore::new_id(),
path_components: vec![rc!(bespouri)],
imported_names: ImportedNames::List(vec![])
})
@ -786,7 +786,7 @@ fn imports_4() {
parse_test_wrap_ast! {
"import bespouri::*",
import!(ImportSpecifier {
id: Default::default(),
id: ItemIdStore::new_id(),
path_components: vec![rc!(bespouri)],
imported_names: ImportedNames::All
})

View File

@ -17,7 +17,7 @@ use std::str::FromStr;
use std::convert::TryFrom;
use crate::ast::*;
use crate::symbol_table::{Symbol, SymbolSpec, SymbolTable};
use crate::symbol_table::{Symbol, SymbolSpec, SymbolTable, FullyQualifiedSymbolName};
use crate::builtin::Builtin;
use crate::util::deref_optional_box;
@ -129,12 +129,12 @@ impl<'a> Reducer<'a> {
fn statement(&mut self, stmt: &Statement) -> Stmt {
match &stmt.kind {
StatementKind::Expression(expr) => Stmt::Expr(self.expression(expr)),
StatementKind::Declaration(decl) => self.declaration(decl),
StatementKind::Expression(expr) => Stmt::Expr(self.expression(&expr)),
StatementKind::Declaration(decl) => self.declaration(&decl),
StatementKind::Import(_) => Stmt::Noop,
StatementKind::Module(modspec) => {
for statement in modspec.contents.iter() {
self.statement(statement);
self.statement(&statement);
}
Stmt::Noop
}
@ -156,7 +156,7 @@ impl<'a> Reducer<'a> {
fn expression(&mut self, expr: &Expression) -> Expr {
use crate::ast::ExpressionKind::*;
let input = &expr.kind;
let ref input = expr.kind;
match input {
NatLiteral(n) => Expr::Lit(Lit::Nat(*n)),
FloatLiteral(f) => Expr::Lit(Lit::Float(*f)),
@ -178,26 +178,34 @@ impl<'a> Reducer<'a> {
}
fn value(&mut self, qualified_name: &QualifiedName) -> Expr {
let Symbol { local_name, spec, .. } = match self.symbol_table.lookup_symbol(&qualified_name.id) {
let symbol_table = self.symbol_table;
let ref id = qualified_name.id;
let ref sym_name = match symbol_table.get_fqsn_from_id(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 symbol_table.lookup_by_fqsn(&sym_name) {
Some(s) => s,
//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 => {
let name = qualified_name.components.last().unwrap().clone();
return Expr::Sym(name)
}
None => return Expr::Sym(name.clone())
};
match spec {
SymbolSpec::RecordConstructor { .. } => Expr::ReductionError(format!("AST reducer doesn't expect a RecordConstructor here")),
SymbolSpec::DataConstructor { index, type_args, type_name } => Expr::Constructor {
type_name: type_name.clone(),
name: local_name.clone(),
name: name.clone(),
tag: index.clone(),
arity: type_args.len(),
},
SymbolSpec::Func(_) => Expr::Sym(local_name.clone()),
SymbolSpec::Binding => Expr::Sym(local_name.clone()), //TODO not sure if this is right, probably needs to eventually be fqsn
SymbolSpec::Type { .. } => Expr::ReductionError("AST reducer doesnt expect a type here".to_string())
}
}
@ -210,16 +218,18 @@ impl<'a> Reducer<'a> {
}
fn reduce_named_struct(&mut self, name: &QualifiedName, fields: &Vec<(Rc<String>, Expression)>) -> Expr {
let symbol = match self.symbol_table.lookup_symbol(&name.id) {
let symbol_table = self.symbol_table;
let ref sym_name = match symbol_table.get_fqsn_from_id(&name.id) {
Some(fqsn) => fqsn,
None => return Expr::ReductionError(format!("FQSN lookup for name {:?} failed", name)),
};
let (type_name, index, members_from_table) = match &symbol.spec {
SymbolSpec::RecordConstructor { members, type_name, index } => (type_name.clone(), index, members),
let FullyQualifiedSymbolName(ref v) = sym_name;
let ref name = v.last().unwrap().name;
let (type_name, index, members_from_table) = match 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()),
};
let arity = members_from_table.len();
let mut args: Vec<(Rc<String>, Expr)> = fields.iter()
@ -232,7 +242,7 @@ impl<'a> Reducer<'a> {
let args = args.into_iter().map(|(_, expr)| expr).collect();
//TODO make sure this sorting actually works
let f = box Expr::Constructor { type_name, name: symbol.local_name.clone(), tag: *index, arity, };
let f = box Expr::Constructor { type_name, name: name.clone(), tag: *index, arity, };
Expr::Call { f, args }
}
@ -244,6 +254,7 @@ impl<'a> Reducer<'a> {
}
fn reduce_if_expression(&mut self, discriminator: Option<&Expression>, body: &IfExpressionBody) -> Expr {
let symbol_table = self.symbol_table;
let cond = Box::new(match discriminator {
Some(expr) => self.expression(expr),
None => return Expr::ReductionError(format!("blank cond if-expr not supported")),
@ -266,7 +277,7 @@ impl<'a> Reducer<'a> {
};
let alternatives = vec![
pattern.to_alternative(then_clause, self.symbol_table),
pattern.to_alternative(then_clause, symbol_table),
Alternative {
matchable: Subpattern {
tag: None,
@ -292,7 +303,7 @@ impl<'a> Reducer<'a> {
},
Condition::Pattern(ref p) => {
let item = self.block(&arm.body);
let alt = p.to_alternative(item, self.symbol_table);
let alt = p.to_alternative(item, symbol_table);
alternatives.push(alt);
},
Condition::TruncatedOp(_, _) => {
@ -308,7 +319,7 @@ impl<'a> Reducer<'a> {
}
}
fn binop(&mut self, binop: &BinOp, lhs: &Expression, rhs: &Expression) -> Expr {
fn binop(&mut self, binop: &BinOp, lhs: &Box<Expression>, rhs: &Box<Expression>) -> Expr {
let operation = Builtin::from_str(binop.sigil()).ok();
match operation {
Some(Builtin::Assignment) => Expr::Assign {
@ -326,7 +337,7 @@ impl<'a> Reducer<'a> {
}
}
fn prefix(&mut self, prefix: &PrefixOp, arg: &Expression) -> Expr {
fn prefix(&mut self, prefix: &PrefixOp, arg: &Box<Expression>) -> Expr {
let builtin: Option<Builtin> = TryFrom::try_from(prefix).ok();
match builtin {
Some(op) => {
@ -348,7 +359,7 @@ impl<'a> Reducer<'a> {
func: Func::UserDefined {
name: Some(name.clone()),
params: params.iter().map(|param| param.name.clone()).collect(),
body: self.block(statements),
body: self.block(&statements),
}
},
TypeDecl { .. } => Stmt::Noop,
@ -371,12 +382,13 @@ impl<'a> Reducer<'a> {
fn handle_symbol(symbol: Option<&Symbol>, inner_patterns: &Vec<Pattern>, symbol_table: &SymbolTable) -> Subpattern {
use self::Pattern::*;
let tag = symbol.map(|symbol| match symbol.spec {
SymbolSpec::DataConstructor { index, .. } => index,
SymbolSpec::DataConstructor { index, .. } => index.clone(),
_ => panic!("Symbol is not a data constructor - this should've been caught in type-checking"),
});
let bound_vars = inner_patterns.iter().map(|p| match p {
VarOrName(qualified_name) => {
let symbol_exists = symbol_table.lookup_symbol(&qualified_name.id).is_some();
let fqsn = symbol_table.get_fqsn_from_id(&qualified_name.id);
let symbol_exists = fqsn.and_then(|fqsn| symbol_table.lookup_by_fqsn(&fqsn)).is_some();
if symbol_exists {
None
} else {
@ -436,9 +448,12 @@ impl Pattern {
use self::Pattern::*;
match self {
TupleStruct(QualifiedName{ components, id }, inner_patterns) => {
match symbol_table.lookup_symbol(id) {
let fqsn = symbol_table.get_fqsn_from_id(&id);
match fqsn.and_then(|fqsn| symbol_table.lookup_by_fqsn(&fqsn)) {
Some(symbol) => handle_symbol(Some(symbol), inner_patterns, symbol_table),
None => panic!("Symbol {:?} not found", components)
None => {
panic!("Symbol {:?} not found", components);
}
}
},
TuplePattern(inner_patterns) => handle_symbol(None, inner_patterns, symbol_table),
@ -448,12 +463,12 @@ impl Pattern {
Ignored => Subpattern { tag: None, subpatterns: vec![], guard: None, bound_vars: vec![] },
Literal(lit) => lit.to_subpattern(symbol_table),
VarOrName(QualifiedName { components, id }) => {
// if symbol is Some, treat this as a symbol pattern. If it's None, treat it
// if fqsn is Some, treat this as a symbol pattern. If it's None, treat it
// as a variable.
match symbol_table.lookup_symbol(id) {
let fqsn = symbol_table.get_fqsn_from_id(&id);
match fqsn.and_then(|fqsn| symbol_table.lookup_by_fqsn(&fqsn)) {
Some(symbol) => handle_symbol(Some(symbol), &vec![], symbol_table),
None => {
println!("Components: {:?}", components);
let name = if components.len() == 1 {
components[0].clone()
} else {
@ -463,7 +478,7 @@ impl Pattern {
tag: None,
subpatterns: vec![],
guard: None,
bound_vars: vec![Some(name)],
bound_vars: vec![Some(name.clone())],
}
}
}

View File

@ -20,6 +20,7 @@ pub struct Schala {
state: eval::State<'static>,
/// Keeps track of symbols and scopes
symbol_table: SymbolTableHandle,
resolver: crate::scope_resolution::ScopeResolver<'static>,
/// Contains information for type-checking
type_context: typechecking::TypeContext<'static>,
/// Schala Parser
@ -44,6 +45,7 @@ impl Schala {
Schala {
source_reference: SourceReference::new(),
symbol_table: symbols.clone(),
resolver: crate::scope_resolution::ScopeResolver::new(symbols.clone()),
state: eval::State::new(),
type_context: typechecking::TypeContext::new(),
active_parser: parsing::Parser::new()
@ -76,13 +78,17 @@ impl Schala {
//2nd stage - parsing
self.active_parser.add_new_tokens(tokens);
let ast = self.active_parser.parse()
let mut ast = self.active_parser.parse()
.map_err(|err| SchalaError::from_parse_error(err, &self.source_reference))?;
//Perform all symbol table work
self.symbol_table.borrow_mut().process_ast(&ast)
// Symbol table
self.symbol_table.borrow_mut().add_top_level_symbols(&ast)
.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))?;
// Typechecking
// TODO typechecking not working
let _overall_type = self.type_context.typecheck(&ast)

View File

@ -4,83 +4,55 @@ use std::rc::Rc;
use std::fmt;
use std::fmt::Write;
use crate::tokenizing::Location;
use crate::tokenizing::LineNumber;
use crate::ast;
use crate::ast::{ItemId, TypeBody, Variant, TypeSingletonName, Declaration, Statement, StatementKind, ModuleSpecifier};
use crate::ast::{ItemId, TypeBody, TypeSingletonName, Signature, Statement, StatementKind, ModuleSpecifier};
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;
use tables::DeclLocations;
mod symbol_trie;
use symbol_trie::SymbolTrie;
mod test;
/// Fully-qualified symbol name
#[derive(Debug, Clone, Eq, PartialEq, Hash, PartialOrd, Ord)]
pub struct FQSN {
//TODO FQSN's need to be cheaply cloneable
scopes: Vec<Scope>, //TODO rename to ScopeSegment
/// Keeps track of what names were used in a given namespace. Call try_register to add a name to
/// the table, or report an error if a name already exists.
struct DuplicateNameTrackTable {
table: HashMap<Rc<String>, LineNumber>,
}
impl FQSN {
fn from_scope_stack(scopes: &[Scope], new_name: String) -> Self {
let mut v = Vec::new();
for s in scopes {
v.push(s.clone());
}
v.push(Scope::Name(new_name));
FQSN { scopes: v }
}
}
//TODO eventually this should use ItemId's to avoid String-cloning
/// One segment within a scope.
#[derive(Debug, Clone, Eq, PartialEq, Hash, PartialOrd, Ord)]
enum Scope {
Name(String)
}
#[allow(dead_code)]
#[derive(Debug, Clone)]
struct DuplicateName {
prev_name: FQSN,
location: Location
}
#[allow(dead_code)]
#[derive(Debug)]
struct NameSpec<K> {
location: Location,
kind: K
}
#[derive(Debug)]
enum NameKind {
Module,
Function,
Binding,
}
#[derive(Debug)]
struct TypeKind;
/// Keeps track of what names were used in a given namespace.
struct NameTable<K> {
table: HashMap<FQSN, NameSpec<K>>
}
impl<K> NameTable<K> {
fn new() -> Self {
Self { table: HashMap::new() }
impl DuplicateNameTrackTable {
fn new() -> DuplicateNameTrackTable {
DuplicateNameTrackTable { table: HashMap::new() }
}
fn register(&mut self, name: FQSN, spec: NameSpec<K>) -> Result<(), DuplicateName> {
fn try_register(&mut self, name: &Rc<String>, id: &ItemId, decl_locations: &DeclLocations) -> Result<(), LineNumber> {
match self.table.entry(name.clone()) {
Entry::Occupied(o) => {
Err(DuplicateName { prev_name: name, location: o.get().location })
let line_number = o.get();
Err(*line_number)
},
Entry::Vacant(v) => {
v.insert(spec);
let line_number = if let Some(loc) = decl_locations.lookup(id) {
loc.line_num
} else {
0
};
v.insert(line_number);
Ok(())
}
}
@ -119,54 +91,48 @@ impl ScopeSegment {
}
}
//cf. p. 150 or so of Language Implementation Patterns
pub struct SymbolTable {
decl_locations: DeclLocations,
symbol_path_to_symbol: HashMap<FullyQualifiedSymbolName, Symbol>,
/// Used for import resolution.
id_to_fqsn: HashMap<ItemId, FullyQualifiedSymbolName>,
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
types: NameTable<TypeKind>,
/// A map of the `ItemId`s of instances of use of names to their fully-canonicalized FQSN form.
/// Updated by the item id resolver.
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 {
pub fn new() -> SymbolTable {
SymbolTable {
decl_locations: DeclLocations::new(),
symbol_path_to_symbol: HashMap::new(),
symbol_trie: SymbolTrie::new(),
fq_names: NameTable::new(),
types: NameTable::new(),
id_to_fqsn: HashMap::new(),
fqsn_to_symbol: HashMap::new(),
symbol_trie: SymbolTrie::new()
}
}
/// The main entry point into the symbol table. This will traverse the AST in several
/// different ways and populate subtables with information that will be used further in the
/// compilation process.
pub fn process_ast(&mut self, ast: &ast::AST) -> Result<(), String> {
self.populate_name_tables(ast)?;
self.resolve_symbol_ids(ast)?;
Ok(())
pub fn map_id_to_fqsn(&mut self, id: &ItemId, fqsn: FullyQualifiedSymbolName) {
self.id_to_fqsn.insert(id.clone(), fqsn);
}
pub fn lookup_symbol(&self, id: &ItemId) -> Option<&Symbol> {
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) {
let mut vec: Vec<ScopeSegment> = scope_path.clone();
vec.push(ScopeSegment { name: local_name.clone() });
let fully_qualified_name = FullyQualifiedSymbolName(vec);
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_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)
}
}
@ -174,7 +140,7 @@ impl SymbolTable {
#[derive(Debug)]
pub struct Symbol {
pub local_name: Rc<String>,
//fully_qualified_name: FullyQualifiedSymbolName,
fully_qualified_name: FullyQualifiedSymbolName,
pub spec: SymbolSpec,
}
@ -189,8 +155,8 @@ pub enum SymbolSpec {
Func(Vec<TypeName>),
DataConstructor {
index: usize,
type_name: TypeName, //TODO this eventually needs to be some kind of ID
type_args: Vec<Rc<String>>, //TODO this should be a lookup table into type information, it's not the concern of the symbol table
type_name: TypeName,
type_args: Vec<Rc<String>>,
},
RecordConstructor {
index: usize,
@ -198,6 +164,9 @@ pub enum SymbolSpec {
type_name: TypeName,
},
Binding,
Type {
name: TypeName
},
}
impl fmt::Display for SymbolSpec {
@ -208,159 +177,137 @@ impl fmt::Display for SymbolSpec {
DataConstructor { index, type_name, type_args } => write!(f, "DataConstructor(idx: {})({:?} -> {})", index, type_args, type_name),
RecordConstructor { type_name, index, ..} => write!(f, "RecordConstructor(idx: {})(<members> -> {})", index, type_name),
Binding => write!(f, "Binding"),
Type { name } => write!(f, "Type <{}>", name),
}
}
}
impl SymbolTable {
/* note: this adds names for *forward reference* but doesn't actually create any types. solve that problem
* later */
/// 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> {
let mut resolver = resolver::Resolver::new(self);
resolver.resolve(ast)?;
Ok(())
pub fn add_top_level_symbols(&mut self, ast: &ast::AST) -> Result<(), String> {
let mut scope_name_stack = Vec::new();
self.add_symbols_from_scope(&ast.statements, &mut scope_name_stack)
}
/// This function traverses the AST and adds symbol table entries for
/// 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> {
let mut scope_stack = vec![];
self.add_from_scope(ast.statements.as_ref(), &mut scope_stack)
.map_err(|err| format!("{:?}", err))?;
Ok(())
}
fn add_symbols_from_scope<'a>(&'a mut self, statements: &Vec<Statement>, scope_name_stack: &mut Vec<ScopeSegment>) -> Result<(), String> {
use self::ast::Declaration::*;
fn add_symbol(&mut self, fqsn: FQSN, symbol: Symbol) {
self.symbol_trie.insert(&fqsn);
self.fqsn_to_symbol.insert(fqsn, symbol);
let mut seen_identifiers = DuplicateNameTrackTable::new();
let mut seen_modules = DuplicateNameTrackTable::new();
}
for statement in statements.iter() {
match statement {
Statement { kind: StatementKind::Declaration(decl), id, location, } => {
self.decl_locations.add_location(id, *location);
//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> {
for statement in statements {
//TODO I'm not sure if I need to do anything with this ID
let Statement { id: _, kind, location } = statement;
let location = *location;
match kind {
StatementKind::Declaration(Declaration::FuncSig(signature)) => {
let fn_name: String = signature.name.as_str().to_owned();
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.types.register(fq_function.clone(), NameSpec { location, kind: TypeKind } )?;
self.add_symbol(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)) => {
let fn_name: String = signature.name.as_str().to_owned();
let new_scope = Scope::Name(fn_name.clone());
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.types.register(fq_function.clone(), NameSpec { location, kind: TypeKind } )?;
self.add_symbol(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);
let output = self.add_from_scope(body.as_ref(), scope_stack);
scope_stack.pop();
output?
},
StatementKind::Declaration(Declaration::TypeDecl { name, body, mutable }) => {
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 } )?;
if let Err(errors) = self.add_type_members(name, body, mutable, location, scope_stack) {
return Err(errors[0].clone());
match decl {
FuncSig(ref signature) => {
seen_identifiers.try_register(&signature.name, id, &self.decl_locations)
.map_err(|line| format!("Duplicate function definition: {}. It's already defined at {}", signature.name, line))?;
self.add_function_signature(signature, scope_name_stack)?
}
FuncDecl(ref signature, ref body) => {
seen_identifiers.try_register(&signature.name, id, &self.decl_locations)
.map_err(|line| format!("Duplicate function definition: {}. It's already defined at {}", signature.name, line))?;
self.add_function_signature(signature, scope_name_stack)?;
scope_name_stack.push(ScopeSegment{
name: signature.name.clone(),
});
let output = self.add_symbols_from_scope(body, scope_name_stack);
scope_name_stack.pop();
output?
},
TypeDecl { name, body, mutable } => {
seen_identifiers.try_register(&name.name, &id, &self.decl_locations)
.map_err(|line| format!("Duplicate type definition: {}. It's already defined at {}", name.name, line))?;
self.add_type_decl(name, body, mutable, scope_name_stack)?
},
Binding { name, .. } => {
seen_identifiers.try_register(&name, &id, &self.decl_locations)
.map_err(|line| format!("Duplicate variable definition: {}. It's already defined at {}", name, line))?;
self.add_new_symbol(name, scope_name_stack, SymbolSpec::Binding);
}
_ => ()
}
},
StatementKind::Declaration(Declaration::Binding { name, .. }) => {
let fq_binding = FQSN::from_scope_stack(scope_stack.as_ref(), name.as_str().to_owned());
self.fq_names.register(fq_binding.clone(), NameSpec { location, kind: NameKind::Binding })?;
self.add_symbol(fq_binding, Symbol {
local_name: name.clone(),
spec: SymbolSpec::Binding,
});
}
StatementKind::Module(ModuleSpecifier { name, contents }) => {
let mod_name = name.as_str().to_owned();
let fq_module = FQSN::from_scope_stack(scope_stack.as_ref(), mod_name.clone());
let new_scope = Scope::Name(mod_name);
self.fq_names.register(fq_module, NameSpec { location, kind: NameKind::Module })?;
scope_stack.push(new_scope);
let output = self.add_from_scope(contents.as_ref(), scope_stack);
scope_stack.pop();
Statement { kind: StatementKind::Module(ModuleSpecifier { name, contents}), id, location } => {
self.decl_locations.add_location(id, *location);
seen_modules.try_register(name, id, &self.decl_locations)
.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?
},
_ => (),
_ => ()
}
}
Ok(())
}
#[allow(dead_code)]
pub fn debug_symbol_table(&self) -> String {
let mut output = "Symbol table\n".to_string();
let mut sorted_symbols: Vec<(&FullyQualifiedSymbolName, &Symbol)> = self.symbol_path_to_symbol.iter().collect();
sorted_symbols.sort_by(|(fqsn, _), (other_fqsn, _)| fqsn.cmp(other_fqsn));
for (name, sym) in sorted_symbols.iter() {
write!(output, "{} -> {}\n", name, sym).unwrap();
}
output
}
fn add_type_members(&mut self, type_name: &TypeSingletonName, type_body: &TypeBody, _mutable: &bool, location: Location, scope_stack: &mut Vec<Scope>) -> Result<(), Vec<DuplicateName>> {
let mut errors = vec![];
fn add_function_signature(&mut self, signature: &Signature, scope_name_stack: &mut Vec<ScopeSegment>) -> Result<(), String> {
let mut local_type_context = LocalTypeContext::new();
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()
}).collect();
self.add_new_symbol(&signature.name, scope_name_stack, SymbolSpec::Func(types));
Ok(())
}
let mut register = |fqsn: FQSN, spec: SymbolSpec| {
let name_spec = NameSpec { location, kind: TypeKind };
if let Err(err) = self.types.register(fqsn.clone(), name_spec) {
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.add_symbol(fqsn, symbol);
};
//TODO handle type mutability
fn add_type_decl(&mut self, type_name: &TypeSingletonName, body: &TypeBody, _mutable: &bool, scope_name_stack: &mut Vec<ScopeSegment>) -> Result<(), String> {
use crate::ast::{TypeIdentifier, Variant};
let TypeBody(variants) = body;
let ref type_name = type_name.name;
let type_spec = SymbolSpec::Type {
name: type_name.clone(),
};
self.add_new_symbol(type_name, &scope_name_stack, type_spec);
let TypeBody(variants) = type_body;
let new_scope = Scope::Name(type_name.name.as_ref().to_owned());
scope_stack.push(new_scope);
for (index, variant) in variants.iter().enumerate() {
match variant {
Variant::UnitStruct(name) => {
let fq_name = FQSN::from_scope_stack(scope_stack.as_ref(), name.as_ref().to_owned());
scope_name_stack.push(ScopeSegment{
name: type_name.clone(),
});
//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,
type_name: name.clone(),
type_name: type_name.clone(),
type_args: vec![],
};
register(fq_name, spec);
self.add_new_symbol(variant_name, scope_name_stack, spec);
},
Variant::TupleStruct(name, items) => {
let fq_name = FQSN::from_scope_stack(scope_stack.as_ref(), name.as_ref().to_owned());
Variant::TupleStruct(variant_name, tuple_members) => {
//TODO fix the notion of a tuple type
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,
type_name: name.clone(),
type_args: items.iter().map(|_| Rc::new("DUMMY_TYPE_ID".to_string())).collect()
type_name: type_name.clone(),
type_args
};
register(fq_name, spec);
self.add_new_symbol(variant_name, scope_name_stack, spec);
},
Variant::Record { name, members } => {
let fq_name = FQSN::from_scope_stack(scope_stack.as_ref(), name.as_ref().to_owned());
let spec = SymbolSpec::RecordConstructor {
index,
type_name: name.clone(),
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
/*
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()) {
@ -376,28 +323,28 @@ impl SymbolTable {
if duplicate_member_definitions.len() != 0 {
return Err(format!("Duplicate member(s) in definition of type {}: {:?}", type_name, duplicate_member_definitions));
}
*/
}
let spec = SymbolSpec::RecordConstructor { index, type_name: type_name.clone(), members };
self.add_new_symbol(name, scope_name_stack, spec);
},
}
}
scope_stack.pop();
if errors.is_empty() {
Ok(())
} else {
Err(errors)
}
}
#[allow(dead_code)]
pub fn debug_symbol_table(&self) -> String {
let mut output = "Symbol table\n".to_string();
let mut sorted_symbols: Vec<(&FullyQualifiedSymbolName, &Symbol)> = self.symbol_path_to_symbol.iter().collect();
sorted_symbols.sort_by(|(fqsn, _), (other_fqsn, _)| fqsn.cmp(other_fqsn));
for (name, sym) in sorted_symbols.iter() {
write!(output, "{} -> {}\n", name, sym).unwrap();
}
output
scope_name_stack.pop();
Ok(())
}
}
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))
}
}

View File

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

View File

@ -0,0 +1,28 @@
use std::collections::HashMap;
use crate::ast::ItemId;
use crate::tokenizing::Location;
/// Maps top-level declarations to Locations in source code, to detect
/// multiply-defined top level items.
pub struct DeclLocations {
map: HashMap<ItemId, Location>
}
impl DeclLocations {
pub fn new() -> Self {
Self { map: HashMap::new() }
}
pub(crate) fn add_location(&mut self, id: &ItemId, loc: Location) {
self.map.insert(id.clone(), loc);
}
pub(crate) fn lookup(&self, id: &ItemId) -> Option<Location> {
match self.map.get(id) {
Some(loc) => Some(loc.clone()),
None => None
}
}
}

View File

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