Compare commits

..

12 Commits

Author SHA1 Message Date
Greg Shuflin
40f759eea8 Fix all warnings 2021-10-19 14:19:26 -07:00
Greg Shuflin
d1d3a70339 Fix last test 2021-10-19 14:12:57 -07:00
Greg Shuflin
3060afd752 Fix warnings 2021-10-19 13:54:32 -07:00
Greg Shuflin
8b724cf0ff Big refactor of symbol table 2021-10-19 13:48:00 -07:00
Greg Shuflin
769ef448e8 Mark out weird oddity with value() in reduced_ast 2021-10-19 00:37:44 -07:00
Greg Shuflin
f5328fac9d More work in symbol_table, reduced_ast 2021-10-19 00:07:02 -07:00
Greg Shuflin
1e9a15d01e Some cipppy lints in reduced ast 2021-10-18 23:41:29 -07:00
Greg Shuflin
2609dd404a Tighten up reduced_ast code a bit 2021-10-18 23:10:50 -07:00
Greg Shuflin
845461e2b3 Modify symbol table tests 2021-10-18 23:04:23 -07:00
Greg Shuflin
9d89440a6d Reduce number of tables in symbol table 2021-10-18 22:32:08 -07:00
Greg Shuflin
db6c9bb162 Start adding new SymbolTable infrastructure 2021-10-18 21:56:48 -07:00
Greg Shuflin
c697c929a4 Use default for ItemId 2021-10-18 17:39:20 -07:00
12 changed files with 487 additions and 367 deletions

View File

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

View File

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

View File

@ -1,23 +1,17 @@
#![cfg(test)] #![cfg(test)]
use std::cell::RefCell;
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()));
symbol_table.borrow_mut().add_top_level_symbols(&ast).unwrap(); let mut symbol_table = SymbolTable::new();
{ symbol_table.process_ast(&ast).unwrap();
let mut scope_resolver = ScopeResolver::new(symbol_table.clone());
let _ = scope_resolver.resolve(&mut ast); let reduced = reduce(&ast, &symbol_table);
}
let reduced = reduce(&ast, &symbol_table.borrow());
let mut state = State::new(); let mut state = State::new();
let all_output = state.evaluate(reduced, true); let all_output = state.evaluate(reduced, true);
all_output all_output

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

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

View File

@ -17,7 +17,7 @@ use std::str::FromStr;
use std::convert::TryFrom; use std::convert::TryFrom;
use crate::ast::*; use crate::ast::*;
use crate::symbol_table::{Symbol, SymbolSpec, SymbolTable, FullyQualifiedSymbolName}; use crate::symbol_table::{Symbol, SymbolSpec, SymbolTable};
use crate::builtin::Builtin; use crate::builtin::Builtin;
use crate::util::deref_optional_box; use crate::util::deref_optional_box;
@ -129,12 +129,12 @@ impl<'a> Reducer<'a> {
fn statement(&mut self, stmt: &Statement) -> Stmt { fn statement(&mut self, stmt: &Statement) -> Stmt {
match &stmt.kind { match &stmt.kind {
StatementKind::Expression(expr) => Stmt::Expr(self.expression(&expr)), StatementKind::Expression(expr) => Stmt::Expr(self.expression(expr)),
StatementKind::Declaration(decl) => self.declaration(&decl), StatementKind::Declaration(decl) => self.declaration(decl),
StatementKind::Import(_) => Stmt::Noop, StatementKind::Import(_) => Stmt::Noop,
StatementKind::Module(modspec) => { StatementKind::Module(modspec) => {
for statement in modspec.contents.iter() { for statement in modspec.contents.iter() {
self.statement(&statement); self.statement(statement);
} }
Stmt::Noop Stmt::Noop
} }
@ -156,7 +156,7 @@ impl<'a> Reducer<'a> {
fn expression(&mut self, expr: &Expression) -> Expr { fn expression(&mut self, expr: &Expression) -> Expr {
use crate::ast::ExpressionKind::*; use crate::ast::ExpressionKind::*;
let ref input = expr.kind; let input = &expr.kind;
match input { match input {
NatLiteral(n) => Expr::Lit(Lit::Nat(*n)), NatLiteral(n) => Expr::Lit(Lit::Nat(*n)),
FloatLiteral(f) => Expr::Lit(Lit::Float(*f)), FloatLiteral(f) => Expr::Lit(Lit::Float(*f)),
@ -178,34 +178,26 @@ impl<'a> Reducer<'a> {
} }
fn value(&mut self, qualified_name: &QualifiedName) -> Expr { fn value(&mut self, qualified_name: &QualifiedName) -> Expr {
let symbol_table = self.symbol_table; let Symbol { local_name, spec, .. } = match self.symbol_table.lookup_symbol(&qualified_name.id) {
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, 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 => return Expr::ReductionError(format!("Symbol {:?} not found", sym_name)),
None => return Expr::Sym(name.clone()) None => {
let name = qualified_name.components.last().unwrap().clone();
return Expr::Sym(name)
}
}; };
match spec { match spec {
SymbolSpec::RecordConstructor { .. } => Expr::ReductionError(format!("AST reducer doesn't expect a RecordConstructor here")), SymbolSpec::RecordConstructor { .. } => Expr::ReductionError(format!("AST reducer doesn't expect a RecordConstructor here")),
SymbolSpec::DataConstructor { index, type_args, type_name } => Expr::Constructor { SymbolSpec::DataConstructor { index, type_args, type_name } => Expr::Constructor {
type_name: type_name.clone(), type_name: type_name.clone(),
name: name.clone(), name: local_name.clone(),
tag: index.clone(), tag: index.clone(),
arity: type_args.len(), arity: type_args.len(),
}, },
SymbolSpec::Func(_) => Expr::Sym(local_name.clone()), 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::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())
} }
} }
@ -218,18 +210,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 symbol_table = self.symbol_table; let symbol = match self.symbol_table.lookup_symbol(&name.id) {
let ref sym_name = match symbol_table.get_fqsn_from_id(&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 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()
@ -242,7 +232,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 }
} }
@ -254,7 +244,6 @@ impl<'a> Reducer<'a> {
} }
fn reduce_if_expression(&mut self, discriminator: Option<&Expression>, body: &IfExpressionBody) -> Expr { fn reduce_if_expression(&mut self, discriminator: Option<&Expression>, body: &IfExpressionBody) -> Expr {
let symbol_table = self.symbol_table;
let cond = Box::new(match discriminator { let cond = Box::new(match discriminator {
Some(expr) => self.expression(expr), Some(expr) => self.expression(expr),
None => return Expr::ReductionError(format!("blank cond if-expr not supported")), None => return Expr::ReductionError(format!("blank cond if-expr not supported")),
@ -277,7 +266,7 @@ impl<'a> Reducer<'a> {
}; };
let alternatives = vec![ let alternatives = vec![
pattern.to_alternative(then_clause, symbol_table), pattern.to_alternative(then_clause, self.symbol_table),
Alternative { Alternative {
matchable: Subpattern { matchable: Subpattern {
tag: None, tag: None,
@ -303,7 +292,7 @@ impl<'a> Reducer<'a> {
}, },
Condition::Pattern(ref p) => { Condition::Pattern(ref p) => {
let item = self.block(&arm.body); let item = self.block(&arm.body);
let alt = p.to_alternative(item, symbol_table); let alt = p.to_alternative(item, self.symbol_table);
alternatives.push(alt); alternatives.push(alt);
}, },
Condition::TruncatedOp(_, _) => { Condition::TruncatedOp(_, _) => {
@ -319,7 +308,7 @@ impl<'a> Reducer<'a> {
} }
} }
fn binop(&mut self, binop: &BinOp, lhs: &Box<Expression>, rhs: &Box<Expression>) -> Expr { fn binop(&mut self, binop: &BinOp, lhs: &Expression, rhs: &Expression) -> Expr {
let operation = Builtin::from_str(binop.sigil()).ok(); let operation = Builtin::from_str(binop.sigil()).ok();
match operation { match operation {
Some(Builtin::Assignment) => Expr::Assign { Some(Builtin::Assignment) => Expr::Assign {
@ -337,7 +326,7 @@ impl<'a> Reducer<'a> {
} }
} }
fn prefix(&mut self, prefix: &PrefixOp, arg: &Box<Expression>) -> Expr { fn prefix(&mut self, prefix: &PrefixOp, arg: &Expression) -> Expr {
let builtin: Option<Builtin> = TryFrom::try_from(prefix).ok(); let builtin: Option<Builtin> = TryFrom::try_from(prefix).ok();
match builtin { match builtin {
Some(op) => { Some(op) => {
@ -359,7 +348,7 @@ impl<'a> Reducer<'a> {
func: Func::UserDefined { func: Func::UserDefined {
name: Some(name.clone()), name: Some(name.clone()),
params: params.iter().map(|param| param.name.clone()).collect(), params: params.iter().map(|param| param.name.clone()).collect(),
body: self.block(&statements), body: self.block(statements),
} }
}, },
TypeDecl { .. } => Stmt::Noop, TypeDecl { .. } => Stmt::Noop,
@ -382,13 +371,12 @@ impl<'a> Reducer<'a> {
fn handle_symbol(symbol: Option<&Symbol>, inner_patterns: &Vec<Pattern>, symbol_table: &SymbolTable) -> Subpattern { fn handle_symbol(symbol: Option<&Symbol>, inner_patterns: &Vec<Pattern>, symbol_table: &SymbolTable) -> Subpattern {
use self::Pattern::*; use self::Pattern::*;
let tag = symbol.map(|symbol| match symbol.spec { let tag = symbol.map(|symbol| match symbol.spec {
SymbolSpec::DataConstructor { index, .. } => index.clone(), SymbolSpec::DataConstructor { index, .. } => index,
_ => panic!("Symbol is not a data constructor - this should've been caught in type-checking"), _ => 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 { 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 +436,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 +448,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.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 {
@ -478,7 +463,7 @@ impl Pattern {
tag: None, tag: None,
subpatterns: vec![], subpatterns: vec![],
guard: None, guard: None,
bound_vars: vec![Some(name.clone())], bound_vars: vec![Some(name)],
} }
} }
} }

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,17 +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 //Perform all symbol table work
self.symbol_table.borrow_mut().add_top_level_symbols(&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))?;
// Scope resolution - requires mutating AST
self.resolver.resolve(&mut ast)
.map_err(|err| SchalaError::from_string(err, Stage::ScopeResolution))?;
// Typechecking // Typechecking
// TODO typechecking not working // TODO typechecking not working
let _overall_type = self.type_context.typecheck(&ast) let _overall_type = self.type_context.typecheck(&ast)

View File

@ -4,55 +4,83 @@ use std::rc::Rc;
use std::fmt; use std::fmt;
use std::fmt::Write; use std::fmt::Write;
use crate::tokenizing::LineNumber; use crate::tokenizing::Location;
use crate::ast; use crate::ast;
use crate::ast::{ItemId, TypeBody, TypeSingletonName, Signature, Statement, StatementKind, ModuleSpecifier}; use crate::ast::{ItemId, TypeBody, Variant, TypeSingletonName, 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;
mod symbol_trie; mod symbol_trie;
use symbol_trie::SymbolTrie; use symbol_trie::SymbolTrie;
mod test; mod test;
/// Keeps track of what names were used in a given namespace. Call try_register to add a name to /// Fully-qualified symbol name
/// the table, or report an error if a name already exists. #[derive(Debug, Clone, Eq, PartialEq, Hash, PartialOrd, Ord)]
struct DuplicateNameTrackTable { pub struct FQSN {
table: HashMap<Rc<String>, LineNumber>, //TODO FQSN's need to be cheaply cloneable
scopes: Vec<Scope>, //TODO rename to ScopeSegment
} }
impl DuplicateNameTrackTable { impl FQSN {
fn new() -> DuplicateNameTrackTable { fn from_scope_stack(scopes: &[Scope], new_name: String) -> Self {
DuplicateNameTrackTable { table: HashMap::new() } let mut v = Vec::new();
for s in scopes {
v.push(s.clone());
}
v.push(Scope::Name(new_name));
FQSN { scopes: v }
}
} }
fn try_register(&mut self, name: &Rc<String>, id: &ItemId, decl_locations: &DeclLocations) -> Result<(), LineNumber> { //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() }
}
fn register(&mut self, name: FQSN, spec: NameSpec<K>) -> Result<(), DuplicateName> {
match self.table.entry(name.clone()) { match self.table.entry(name.clone()) {
Entry::Occupied(o) => { Entry::Occupied(o) => {
let line_number = o.get(); Err(DuplicateName { prev_name: name, location: o.get().location })
Err(*line_number)
}, },
Entry::Vacant(v) => { Entry::Vacant(v) => {
let line_number = if let Some(loc) = decl_locations.lookup(id) { v.insert(spec);
loc.line_num
} else {
0
};
v.insert(line_number);
Ok(()) Ok(())
} }
} }
@ -91,48 +119,54 @@ 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,
symbol_path_to_symbol: HashMap<FullyQualifiedSymbolName, Symbol>, symbol_path_to_symbol: HashMap<FullyQualifiedSymbolName, Symbol>,
id_to_fqsn: HashMap<ItemId, FullyQualifiedSymbolName>,
/// Used for import resolution.
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
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 { impl SymbolTable {
pub fn new() -> SymbolTable { pub fn new() -> SymbolTable {
SymbolTable { SymbolTable {
decl_locations: DeclLocations::new(),
symbol_path_to_symbol: HashMap::new(), symbol_path_to_symbol: HashMap::new(),
symbol_trie: SymbolTrie::new(),
fq_names: NameTable::new(),
types: NameTable::new(),
id_to_fqsn: HashMap::new(), id_to_fqsn: HashMap::new(),
symbol_trie: SymbolTrie::new() fqsn_to_symbol: HashMap::new(),
} }
} }
pub fn map_id_to_fqsn(&mut self, id: &ItemId, fqsn: FullyQualifiedSymbolName) { /// The main entry point into the symbol table. This will traverse the AST in several
self.id_to_fqsn.insert(id.clone(), fqsn); /// 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 get_fqsn_from_id(&self, id: &ItemId) -> Option<FullyQualifiedSymbolName> { pub fn lookup_symbol(&self, id: &ItemId) -> Option<&Symbol> {
self.id_to_fqsn.get(&id).cloned() let fqsn = self.id_to_fqsn.get(id);
} fqsn.and_then(|fqsn| self.fqsn_to_symbol.get(fqsn))
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)
} }
} }
@ -140,7 +174,7 @@ impl SymbolTable {
#[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,
} }
@ -155,8 +189,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,
@ -164,9 +198,6 @@ pub enum SymbolSpec {
type_name: TypeName, type_name: TypeName,
}, },
Binding, Binding,
Type {
name: TypeName
},
} }
impl fmt::Display for SymbolSpec { impl fmt::Display for SymbolSpec {
@ -177,137 +208,159 @@ impl fmt::Display for SymbolSpec {
DataConstructor { index, type_name, type_args } => write!(f, "DataConstructor(idx: {})({:?} -> {})", index, type_args, type_name), 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), RecordConstructor { type_name, index, ..} => write!(f, "RecordConstructor(idx: {})(<members> -> {})", index, type_name),
Binding => write!(f, "Binding"), Binding => write!(f, "Binding"),
Type { name } => write!(f, "Type <{}>", name),
} }
} }
} }
impl SymbolTable { impl SymbolTable {
/* note: this adds names for *forward reference* but doesn't actually create any types. solve that problem /* note: this adds names for *forward reference* but doesn't actually create any types. solve that problem
* later */ * later */
pub fn add_top_level_symbols(&mut self, ast: &ast::AST) -> Result<(), String> { /// Walks the AST, matching the ID of an identifier used in some expression to
let mut scope_name_stack = Vec::new(); /// the corresponding Symbol.
self.add_symbols_from_scope(&ast.statements, &mut scope_name_stack) fn resolve_symbol_ids(&mut self, ast: &ast::AST) -> Result<(), String> {
let mut resolver = resolver::Resolver::new(self);
resolver.resolve(ast)?;
Ok(())
} }
fn add_symbols_from_scope<'a>(&'a mut self, statements: &Vec<Statement>, scope_name_stack: &mut Vec<ScopeSegment>) -> Result<(), String> { /// This function traverses the AST and adds symbol table entries for
use self::ast::Declaration::*; /// constants, functions, types, and modules defined within. This simultaneously
/// checks for dupicate definitions (and returns errors if discovered), and sets
let mut seen_identifiers = DuplicateNameTrackTable::new(); /// up name tables that will be used by further parts of the compiler
let mut seen_modules = DuplicateNameTrackTable::new(); fn populate_name_tables(&mut self, ast: &ast::AST) -> Result<(), String> {
let mut scope_stack = vec![];
for statement in statements.iter() { self.add_from_scope(ast.statements.as_ref(), &mut scope_stack)
match statement { .map_err(|err| format!("{:?}", err))?;
Statement { kind: StatementKind::Declaration(decl), id, location, } => { Ok(())
self.decl_locations.add_location(id, *location);
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) fn add_symbol(&mut self, fqsn: FQSN, symbol: Symbol) {
.map_err(|line| format!("Duplicate function definition: {}. It's already defined at {}", signature.name, line))?; self.symbol_trie.insert(&fqsn);
self.add_function_signature(signature, scope_name_stack)?; self.fqsn_to_symbol.insert(fqsn, symbol);
scope_name_stack.push(ScopeSegment{
name: signature.name.clone(), }
//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?
}); });
let output = self.add_symbols_from_scope(body, scope_name_stack); }
scope_name_stack.pop(); 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? output?
}, },
TypeDecl { name, body, mutable } => { StatementKind::Declaration(Declaration::TypeDecl { name, body, mutable }) => {
seen_identifiers.try_register(&name.name, &id, &self.decl_locations) let fq_type = FQSN::from_scope_stack(scope_stack.as_ref(), name.name.as_ref().to_owned());
.map_err(|line| format!("Duplicate type definition: {}. It's already defined at {}", name.name, line))?; self.types.register(fq_type, NameSpec { location, kind: TypeKind } )?;
self.add_type_decl(name, body, mutable, scope_name_stack)? if let Err(errors) = self.add_type_members(name, body, mutable, location, scope_stack) {
}, return Err(errors[0].clone());
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);
}
_ => ()
} }
}, },
Statement { kind: StatementKind::Module(ModuleSpecifier { name, contents}), id, location } => { StatementKind::Declaration(Declaration::Binding { name, .. }) => {
self.decl_locations.add_location(id, *location); let fq_binding = FQSN::from_scope_stack(scope_stack.as_ref(), name.as_str().to_owned());
seen_modules.try_register(name, id, &self.decl_locations) self.fq_names.register(fq_binding.clone(), NameSpec { location, kind: NameKind::Binding })?;
.map_err(|line| format!("Duplicate module definition: {}. It's already defined at {}", name, line))?; self.add_symbol(fq_binding, Symbol {
scope_name_stack.push(ScopeSegment { name: name.clone() }); local_name: name.clone(),
let output = self.add_symbols_from_scope(contents, scope_name_stack); spec: SymbolSpec::Binding,
scope_name_stack.pop(); });
}
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();
output? output?
}, },
_ => () _ => (),
} }
} }
Ok(()) 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_function_signature(&mut self, signature: &Signature, scope_name_stack: &mut Vec<ScopeSegment>) -> Result<(), String> { 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 local_type_context = LocalTypeContext::new(); let mut errors = vec![];
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(())
}
//TODO handle type mutability let mut register = |fqsn: FQSN, spec: SymbolSpec| {
fn add_type_decl(&mut self, type_name: &TypeSingletonName, body: &TypeBody, _mutable: &bool, scope_name_stack: &mut Vec<ScopeSegment>) -> Result<(), String> { let name_spec = NameSpec { location, kind: TypeKind };
use crate::ast::{TypeIdentifier, Variant}; if let Err(err) = self.types.register(fqsn.clone(), name_spec) {
let TypeBody(variants) = body; errors.push(err);
let ref type_name = type_name.name; } else {
let local_name = match spec {
SymbolSpec::DataConstructor { ref type_name, ..} | SymbolSpec::RecordConstructor { ref type_name, .. } => type_name.clone(),
let type_spec = SymbolSpec::Type { _ => panic!("This should never happen"),
name: type_name.clone(), };
let symbol = Symbol { local_name, spec };
self.add_symbol(fqsn, symbol);
};
}; };
self.add_new_symbol(type_name, &scope_name_stack, type_spec);
scope_name_stack.push(ScopeSegment{ let TypeBody(variants) = type_body;
name: type_name.clone(), let new_scope = Scope::Name(type_name.name.as_ref().to_owned());
}); scope_stack.push(new_scope);
//TODO figure out why _params isn't being used here
for (index, var) in variants.iter().enumerate() { for (index, variant) in variants.iter().enumerate() {
match var { match variant {
Variant::UnitStruct(variant_name) => { Variant::UnitStruct(name) => {
let fq_name = FQSN::from_scope_stack(scope_stack.as_ref(), name.as_ref().to_owned());
let spec = SymbolSpec::DataConstructor { let spec = SymbolSpec::DataConstructor {
index, index,
type_name: type_name.clone(), type_name: name.clone(),
type_args: vec![], type_args: vec![],
}; };
self.add_new_symbol(variant_name, scope_name_stack, spec); register(fq_name, spec);
}, },
Variant::TupleStruct(variant_name, tuple_members) => { Variant::TupleStruct(name, items) => {
//TODO fix the notion of a tuple type let fq_name = FQSN::from_scope_stack(scope_stack.as_ref(), name.as_ref().to_owned());
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 { let spec = SymbolSpec::DataConstructor {
index, index,
type_name: type_name.clone(), type_name: name.clone(),
type_args type_args: items.iter().map(|_| Rc::new("DUMMY_TYPE_ID".to_string())).collect()
}; };
self.add_new_symbol(variant_name, scope_name_stack, spec); register(fq_name, spec);
}, },
Variant::Record { name, members: defined_members } => { Variant::Record { name, members } => {
let mut members = HashMap::new(); 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
/*
let mut duplicate_member_definitions = Vec::new(); let mut duplicate_member_definitions = Vec::new();
for (member_name, member_type) in defined_members { for (member_name, member_type) in defined_members {
match members.entry(member_name.clone()) { match members.entry(member_name.clone()) {
@ -323,28 +376,28 @@ impl SymbolTable {
if duplicate_member_definitions.len() != 0 { if duplicate_member_definitions.len() != 0 {
return Err(format!("Duplicate member(s) in definition of type {}: {:?}", type_name, duplicate_member_definitions)); 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_name_stack.pop(); }
scope_stack.pop();
if errors.is_empty() {
Ok(()) Ok(())
} else {
Err(errors)
} }
} }
struct LocalTypeContext { #[allow(dead_code)]
state: u8 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();
} }
impl LocalTypeContext { output
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

@ -0,0 +1,110 @@
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 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() { 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 +24,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

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