Compare commits
No commits in common. "40f759eea8417c6b2c813b491ec402ac9819473b" and "5bba900a3d42a2005db31945201f8b6ccbf15586" have entirely different histories.
40f759eea8
...
5bba900a3d
@ -11,8 +11,7 @@ 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. Note that
|
/// An abstract identifier for an AST node
|
||||||
/// 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,
|
||||||
@ -32,7 +31,13 @@ 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;
|
||||||
|
@ -2,8 +2,10 @@ 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;
|
||||||
|
@ -1,17 +1,23 @@
|
|||||||
#![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 ast = crate::util::quick_ast(input);
|
let mut ast = crate::util::quick_ast(input);
|
||||||
|
let symbol_table = Rc::new(RefCell::new(SymbolTable::new()));
|
||||||
let mut symbol_table = SymbolTable::new();
|
symbol_table.borrow_mut().add_top_level_symbols(&ast).unwrap();
|
||||||
symbol_table.process_ast(&ast).unwrap();
|
{
|
||||||
|
let mut scope_resolver = ScopeResolver::new(symbol_table.clone());
|
||||||
let reduced = reduce(&ast, &symbol_table);
|
let _ = scope_resolver.resolve(&mut ast);
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
@ -25,6 +25,7 @@ mod builtin;
|
|||||||
mod error;
|
mod error;
|
||||||
mod eval;
|
mod eval;
|
||||||
mod reduced_ast;
|
mod reduced_ast;
|
||||||
|
mod scope_resolution;
|
||||||
|
|
||||||
mod schala;
|
mod schala;
|
||||||
|
|
||||||
|
@ -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: Default::default(), statements: vec![$correct] }) }
|
($string:expr, $correct:expr) => { parse_test!($string, AST { id: ItemIdStore::new_id(), 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: Default::default() }
|
QualifiedName { components, id: ItemIdStore::new_id() }
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
macro_rules! val {
|
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 {
|
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(Default::default(), $expr_type) };
|
($expr_type:expr) => { Expression::new(ItemIdStore::new_id(), $expr_type) };
|
||||||
($expr_type:expr, $type_anno:expr) => { Expression::with_anno(Default::default(), $expr_type, $type_anno) };
|
($expr_type:expr, $type_anno:expr) => { Expression::with_anno(ItemIdStore::new_id(), $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(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 {
|
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 {
|
macro_rules! exst {
|
||||||
($expr_type:expr) => { make_statement(StatementKind::Expression(Expression::new(Default::default(), $expr_type).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(Default::default(), $expr_type, $type_anno).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)))) };
|
($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: Default::default(),
|
id: ItemIdStore::new_id(),
|
||||||
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(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 } };
|
r#"fn wahoo() { let a = 10; \(x) { x + a } };
|
||||||
wahoo()(3) "#,
|
wahoo()(3) "#,
|
||||||
AST {
|
AST {
|
||||||
id: Default::default(),
|
id: ItemIdStore::new_id(),
|
||||||
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: Default::default(),
|
id: ItemIdStore::new_id(),
|
||||||
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: Default::default(),
|
id: ItemIdStore::new_id(),
|
||||||
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: Default::default(),
|
id: ItemIdStore::new_id(),
|
||||||
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: Default::default(),
|
id: ItemIdStore::new_id(),
|
||||||
path_components: vec![rc!(bespouri)],
|
path_components: vec![rc!(bespouri)],
|
||||||
imported_names: ImportedNames::All
|
imported_names: ImportedNames::All
|
||||||
})
|
})
|
||||||
|
@ -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};
|
use crate::symbol_table::{Symbol, SymbolSpec, SymbolTable, FullyQualifiedSymbolName};
|
||||||
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 input = &expr.kind;
|
let ref 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,26 +178,34 @@ impl<'a> Reducer<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn value(&mut self, qualified_name: &QualifiedName) -> Expr {
|
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,
|
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 => {
|
None => return Expr::Sym(name.clone())
|
||||||
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: local_name.clone(),
|
name: 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())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -210,16 +218,18 @@ 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 = 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,
|
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 (type_name, index, members_from_table) = match &symbol.spec {
|
let FullyQualifiedSymbolName(ref v) = sym_name;
|
||||||
SymbolSpec::RecordConstructor { members, type_name, index } => (type_name.clone(), index, members),
|
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()),
|
_ => 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()
|
||||||
@ -232,7 +242,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: symbol.local_name.clone(), tag: *index, arity, };
|
let f = box Expr::Constructor { type_name, name: name.clone(), tag: *index, arity, };
|
||||||
Expr::Call { f, args }
|
Expr::Call { f, args }
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -244,6 +254,7 @@ 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")),
|
||||||
@ -266,7 +277,7 @@ impl<'a> Reducer<'a> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let alternatives = vec![
|
let alternatives = vec![
|
||||||
pattern.to_alternative(then_clause, self.symbol_table),
|
pattern.to_alternative(then_clause, symbol_table),
|
||||||
Alternative {
|
Alternative {
|
||||||
matchable: Subpattern {
|
matchable: Subpattern {
|
||||||
tag: None,
|
tag: None,
|
||||||
@ -292,7 +303,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, self.symbol_table);
|
let alt = p.to_alternative(item, symbol_table);
|
||||||
alternatives.push(alt);
|
alternatives.push(alt);
|
||||||
},
|
},
|
||||||
Condition::TruncatedOp(_, _) => {
|
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();
|
let operation = Builtin::from_str(binop.sigil()).ok();
|
||||||
match operation {
|
match operation {
|
||||||
Some(Builtin::Assignment) => Expr::Assign {
|
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();
|
let builtin: Option<Builtin> = TryFrom::try_from(prefix).ok();
|
||||||
match builtin {
|
match builtin {
|
||||||
Some(op) => {
|
Some(op) => {
|
||||||
@ -348,7 +359,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,
|
||||||
@ -371,12 +382,13 @@ 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,
|
SymbolSpec::DataConstructor { index, .. } => index.clone(),
|
||||||
_ => 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 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 {
|
if symbol_exists {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
@ -436,9 +448,12 @@ impl Pattern {
|
|||||||
use self::Pattern::*;
|
use self::Pattern::*;
|
||||||
match self {
|
match self {
|
||||||
TupleStruct(QualifiedName{ components, id }, inner_patterns) => {
|
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),
|
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),
|
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![] },
|
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 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.
|
// 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),
|
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 {
|
||||||
@ -463,7 +478,7 @@ impl Pattern {
|
|||||||
tag: None,
|
tag: None,
|
||||||
subpatterns: vec![],
|
subpatterns: vec![],
|
||||||
guard: None,
|
guard: None,
|
||||||
bound_vars: vec![Some(name)],
|
bound_vars: vec![Some(name.clone())],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,7 @@ 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
|
||||||
@ -44,6 +45,7 @@ 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()
|
||||||
@ -76,13 +78,17 @@ impl Schala {
|
|||||||
|
|
||||||
//2nd stage - parsing
|
//2nd stage - parsing
|
||||||
self.active_parser.add_new_tokens(tokens);
|
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))?;
|
.map_err(|err| SchalaError::from_parse_error(err, &self.source_reference))?;
|
||||||
|
|
||||||
//Perform all symbol table work
|
// Symbol table
|
||||||
self.symbol_table.borrow_mut().process_ast(&ast)
|
self.symbol_table.borrow_mut().add_top_level_symbols(&ast)
|
||||||
.map_err(|err| SchalaError::from_string(err, Stage::Symbols))?;
|
.map_err(|err| SchalaError::from_string(err, Stage::Symbols))?;
|
||||||
|
|
||||||
|
// Scope resolution - requires mutating AST
|
||||||
|
self.resolver.resolve(&mut ast)
|
||||||
|
.map_err(|err| SchalaError::from_string(err, Stage::ScopeResolution))?;
|
||||||
|
|
||||||
// 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)
|
||||||
|
@ -4,83 +4,55 @@ use std::rc::Rc;
|
|||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::fmt::Write;
|
use std::fmt::Write;
|
||||||
|
|
||||||
use crate::tokenizing::Location;
|
use crate::tokenizing::LineNumber;
|
||||||
use crate::ast;
|
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;
|
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;
|
||||||
|
|
||||||
/// Fully-qualified symbol name
|
/// Keeps track of what names were used in a given namespace. Call try_register to add a name to
|
||||||
#[derive(Debug, Clone, Eq, PartialEq, Hash, PartialOrd, Ord)]
|
/// the table, or report an error if a name already exists.
|
||||||
pub struct FQSN {
|
struct DuplicateNameTrackTable {
|
||||||
//TODO FQSN's need to be cheaply cloneable
|
table: HashMap<Rc<String>, LineNumber>,
|
||||||
scopes: Vec<Scope>, //TODO rename to ScopeSegment
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FQSN {
|
impl DuplicateNameTrackTable {
|
||||||
fn from_scope_stack(scopes: &[Scope], new_name: String) -> Self {
|
fn new() -> DuplicateNameTrackTable {
|
||||||
let mut v = Vec::new();
|
DuplicateNameTrackTable { table: HashMap::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() }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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()) {
|
match self.table.entry(name.clone()) {
|
||||||
Entry::Occupied(o) => {
|
Entry::Occupied(o) => {
|
||||||
Err(DuplicateName { prev_name: name, location: o.get().location })
|
let line_number = o.get();
|
||||||
|
Err(*line_number)
|
||||||
},
|
},
|
||||||
Entry::Vacant(v) => {
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -119,54 +91,48 @@ 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(),
|
||||||
fqsn_to_symbol: HashMap::new(),
|
symbol_trie: SymbolTrie::new()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The main entry point into the symbol table. This will traverse the AST in several
|
pub fn map_id_to_fqsn(&mut self, id: &ItemId, fqsn: FullyQualifiedSymbolName) {
|
||||||
/// different ways and populate subtables with information that will be used further in the
|
self.id_to_fqsn.insert(id.clone(), fqsn);
|
||||||
/// 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 lookup_symbol(&self, id: &ItemId) -> Option<&Symbol> {
|
pub fn get_fqsn_from_id(&self, id: &ItemId) -> Option<FullyQualifiedSymbolName> {
|
||||||
let fqsn = self.id_to_fqsn.get(id);
|
self.id_to_fqsn.get(&id).cloned()
|
||||||
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -174,7 +140,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,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -189,8 +155,8 @@ pub enum SymbolSpec {
|
|||||||
Func(Vec<TypeName>),
|
Func(Vec<TypeName>),
|
||||||
DataConstructor {
|
DataConstructor {
|
||||||
index: usize,
|
index: usize,
|
||||||
type_name: TypeName, //TODO this eventually needs to be some kind of ID
|
type_name: TypeName,
|
||||||
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_args: Vec<Rc<String>>,
|
||||||
},
|
},
|
||||||
RecordConstructor {
|
RecordConstructor {
|
||||||
index: usize,
|
index: usize,
|
||||||
@ -198,6 +164,9 @@ pub enum SymbolSpec {
|
|||||||
type_name: TypeName,
|
type_name: TypeName,
|
||||||
},
|
},
|
||||||
Binding,
|
Binding,
|
||||||
|
Type {
|
||||||
|
name: TypeName
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for SymbolSpec {
|
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),
|
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 */
|
||||||
|
|
||||||
/// Walks the AST, matching the ID of an identifier used in some expression to
|
pub fn add_top_level_symbols(&mut self, ast: &ast::AST) -> Result<(), String> {
|
||||||
/// the corresponding Symbol.
|
let mut scope_name_stack = Vec::new();
|
||||||
fn resolve_symbol_ids(&mut self, ast: &ast::AST) -> Result<(), String> {
|
self.add_symbols_from_scope(&ast.statements, &mut scope_name_stack)
|
||||||
let mut resolver = resolver::Resolver::new(self);
|
|
||||||
resolver.resolve(ast)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This function traverses the AST and adds symbol table entries for
|
fn add_symbols_from_scope<'a>(&'a mut self, statements: &Vec<Statement>, scope_name_stack: &mut Vec<ScopeSegment>) -> Result<(), String> {
|
||||||
/// constants, functions, types, and modules defined within. This simultaneously
|
use self::ast::Declaration::*;
|
||||||
/// checks for dupicate definitions (and returns errors if discovered), and sets
|
|
||||||
/// up name tables that will be used by further parts of the compiler
|
let mut seen_identifiers = DuplicateNameTrackTable::new();
|
||||||
fn populate_name_tables(&mut self, ast: &ast::AST) -> Result<(), String> {
|
let mut seen_modules = DuplicateNameTrackTable::new();
|
||||||
let mut scope_stack = vec![];
|
|
||||||
self.add_from_scope(ast.statements.as_ref(), &mut scope_stack)
|
for statement in statements.iter() {
|
||||||
.map_err(|err| format!("{:?}", err))?;
|
match statement {
|
||||||
Ok(())
|
Statement { kind: StatementKind::Declaration(decl), id, location, } => {
|
||||||
|
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) => {
|
||||||
fn add_symbol(&mut self, fqsn: FQSN, symbol: Symbol) {
|
seen_identifiers.try_register(&signature.name, id, &self.decl_locations)
|
||||||
self.symbol_trie.insert(&fqsn);
|
.map_err(|line| format!("Duplicate function definition: {}. It's already defined at {}", signature.name, line))?;
|
||||||
self.fqsn_to_symbol.insert(fqsn, symbol);
|
self.add_function_signature(signature, scope_name_stack)?;
|
||||||
|
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);
|
||||||
StatementKind::Declaration(Declaration::FuncDecl(signature, body)) => {
|
scope_name_stack.pop();
|
||||||
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?
|
||||||
},
|
},
|
||||||
StatementKind::Declaration(Declaration::TypeDecl { name, body, mutable }) => {
|
TypeDecl { name, body, mutable } => {
|
||||||
let fq_type = FQSN::from_scope_stack(scope_stack.as_ref(), name.name.as_ref().to_owned());
|
seen_identifiers.try_register(&name.name, &id, &self.decl_locations)
|
||||||
self.types.register(fq_type, NameSpec { location, kind: TypeKind } )?;
|
.map_err(|line| format!("Duplicate type definition: {}. It's already defined at {}", name.name, line))?;
|
||||||
if let Err(errors) = self.add_type_members(name, body, mutable, location, scope_stack) {
|
self.add_type_decl(name, body, mutable, scope_name_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);
|
||||||
|
}
|
||||||
|
_ => ()
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
StatementKind::Declaration(Declaration::Binding { name, .. }) => {
|
Statement { kind: StatementKind::Module(ModuleSpecifier { name, contents}), id, location } => {
|
||||||
let fq_binding = FQSN::from_scope_stack(scope_stack.as_ref(), name.as_str().to_owned());
|
self.decl_locations.add_location(id, *location);
|
||||||
self.fq_names.register(fq_binding.clone(), NameSpec { location, kind: NameKind::Binding })?;
|
seen_modules.try_register(name, id, &self.decl_locations)
|
||||||
self.add_symbol(fq_binding, Symbol {
|
.map_err(|line| format!("Duplicate module definition: {}. It's already defined at {}", name, line))?;
|
||||||
local_name: name.clone(),
|
scope_name_stack.push(ScopeSegment { name: name.clone() });
|
||||||
spec: SymbolSpec::Binding,
|
let output = self.add_symbols_from_scope(contents, scope_name_stack);
|
||||||
});
|
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_type_members(&mut self, type_name: &TypeSingletonName, type_body: &TypeBody, _mutable: &bool, location: Location, scope_stack: &mut Vec<Scope>) -> Result<(), Vec<DuplicateName>> {
|
fn add_function_signature(&mut self, signature: &Signature, scope_name_stack: &mut Vec<ScopeSegment>) -> Result<(), String> {
|
||||||
let mut errors = vec![];
|
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| {
|
//TODO handle type mutability
|
||||||
let name_spec = NameSpec { location, kind: TypeKind };
|
fn add_type_decl(&mut self, type_name: &TypeSingletonName, body: &TypeBody, _mutable: &bool, scope_name_stack: &mut Vec<ScopeSegment>) -> Result<(), String> {
|
||||||
if let Err(err) = self.types.register(fqsn.clone(), name_spec) {
|
use crate::ast::{TypeIdentifier, Variant};
|
||||||
errors.push(err);
|
let TypeBody(variants) = body;
|
||||||
} else {
|
let ref type_name = type_name.name;
|
||||||
let local_name = match spec {
|
|
||||||
SymbolSpec::DataConstructor { ref type_name, ..} | SymbolSpec::RecordConstructor { ref type_name, .. } => type_name.clone(),
|
|
||||||
_ => panic!("This should never happen"),
|
let type_spec = SymbolSpec::Type {
|
||||||
};
|
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);
|
||||||
|
|
||||||
let TypeBody(variants) = type_body;
|
scope_name_stack.push(ScopeSegment{
|
||||||
let new_scope = Scope::Name(type_name.name.as_ref().to_owned());
|
name: type_name.clone(),
|
||||||
scope_stack.push(new_scope);
|
});
|
||||||
|
//TODO figure out why _params isn't being used here
|
||||||
for (index, variant) in variants.iter().enumerate() {
|
for (index, var) in variants.iter().enumerate() {
|
||||||
match variant {
|
match var {
|
||||||
Variant::UnitStruct(name) => {
|
Variant::UnitStruct(variant_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: name.clone(),
|
type_name: type_name.clone(),
|
||||||
type_args: vec![],
|
type_args: vec![],
|
||||||
};
|
};
|
||||||
register(fq_name, spec);
|
self.add_new_symbol(variant_name, scope_name_stack, spec);
|
||||||
},
|
},
|
||||||
Variant::TupleStruct(name, items) => {
|
Variant::TupleStruct(variant_name, tuple_members) => {
|
||||||
let fq_name = FQSN::from_scope_stack(scope_stack.as_ref(), name.as_ref().to_owned());
|
//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 {
|
let spec = SymbolSpec::DataConstructor {
|
||||||
index,
|
index,
|
||||||
type_name: name.clone(),
|
type_name: type_name.clone(),
|
||||||
type_args: items.iter().map(|_| Rc::new("DUMMY_TYPE_ID".to_string())).collect()
|
type_args
|
||||||
};
|
};
|
||||||
register(fq_name, spec);
|
self.add_new_symbol(variant_name, scope_name_stack, spec);
|
||||||
},
|
},
|
||||||
Variant::Record { name, members } => {
|
Variant::Record { name, members: defined_members } => {
|
||||||
let fq_name = FQSN::from_scope_stack(scope_stack.as_ref(), name.as_ref().to_owned());
|
let mut members = HashMap::new();
|
||||||
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()) {
|
||||||
@ -376,28 +323,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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -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),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,18 +1,18 @@
|
|||||||
use radix_trie::{Trie, TrieCommon, TrieKey};
|
use radix_trie::{Trie, TrieCommon, TrieKey};
|
||||||
use super::{Scope, FQSN};
|
use super::FullyQualifiedSymbolName;
|
||||||
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<FQSN, ()>);
|
pub struct SymbolTrie(Trie<FullyQualifiedSymbolName, ()>);
|
||||||
|
|
||||||
impl TrieKey for FQSN {
|
impl TrieKey for FullyQualifiedSymbolName {
|
||||||
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![];
|
||||||
for segment in self.scopes.iter() {
|
let FullyQualifiedSymbolName(scopes) = self;
|
||||||
let Scope::Name(s) = segment;
|
for segment in scopes.iter() {
|
||||||
s.as_bytes().hash(&mut hasher);
|
segment.name.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,44 +24,28 @@ impl SymbolTrie {
|
|||||||
SymbolTrie(Trie::new())
|
SymbolTrie(Trie::new())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn insert(&mut self, fqsn: &FQSN) {
|
pub fn insert(&mut self, fqsn: &FullyQualifiedSymbolName) {
|
||||||
self.0.insert(fqsn.clone(), ());
|
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) {
|
let subtrie = match self.0.subtrie(fqsn) {
|
||||||
Some(s) => s,
|
Some(s) => s,
|
||||||
None => return vec![]
|
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
|
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(&make_fqsn(&["unrelated","thing"]));
|
trie.insert(&fqsn!("unrelated"; ty, "thing"; tr));
|
||||||
trie.insert(&make_fqsn(&["outer","inner"]));
|
trie.insert(&fqsn!("outer"; ty, "inner"; tr));
|
||||||
trie.insert(&make_fqsn(&["outer","inner", "still_inner"]));
|
trie.insert(&fqsn!("outer"; ty, "inner"; ty, "still_inner"; tr));
|
||||||
|
|
||||||
let children = trie.get_children(&make_fqsn(&["outer", "inner"]));
|
let children = trie.get_children(&fqsn!("outer"; ty, "inner"; tr));
|
||||||
assert_eq!(children.len(), 1);
|
assert_eq!(children.len(), 1);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -2,37 +2,37 @@
|
|||||||
use super::*;
|
use super::*;
|
||||||
use crate::util::quick_ast;
|
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 ast = quick_ast(src);
|
||||||
let mut symbol_table = SymbolTable::new();
|
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)
|
(symbol_table, result)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn make_fqsn(strs: &[&str]) -> FQSN {
|
macro_rules! values_in_table {
|
||||||
let mut scopes = vec![];
|
($source:expr, $single_value:expr) => {
|
||||||
for s in strs {
|
values_in_table!($source => $single_value);
|
||||||
scopes.push(Scope::Name(s.to_string()));
|
};
|
||||||
}
|
($source:expr => $( $value:expr ),* ) => {
|
||||||
FQSN {
|
{
|
||||||
scopes
|
let (symbol_table, _) = add_symbols_from_source($source);
|
||||||
|
$(
|
||||||
|
match symbol_table.lookup_by_fqsn($value) {
|
||||||
|
Some(_spec) => (),
|
||||||
|
None => panic!(),
|
||||||
|
};
|
||||||
|
)*
|
||||||
}
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn basic_symbol_table() {
|
fn basic_symbol_table() {
|
||||||
let src = "let a = 10; fn b() { 20 }";
|
values_in_table! { "let a = 10; fn b() { 20 }", &fqsn!("b"; tr) };
|
||||||
let (symbols, _) = add_symbols(src);
|
values_in_table! { "type Option<T> = Some(T) | None" =>
|
||||||
|
&fqsn!("Option"; tr),
|
||||||
symbols.fq_names.table.get(&make_fqsn(&["b"])).unwrap();
|
&fqsn!("Option"; ty, "Some"; tr),
|
||||||
|
&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,9 +42,8 @@ fn no_function_definition_duplicates() {
|
|||||||
fn b() { 2 }
|
fn b() { 2 }
|
||||||
fn a() { 3 }
|
fn a() { 3 }
|
||||||
"#;
|
"#;
|
||||||
let (_, output) = add_symbols(source);
|
let (_, output) = add_symbols_from_source(source);
|
||||||
//TODO test for right type of error
|
assert!(output.unwrap_err().contains("Duplicate function definition: a"))
|
||||||
output.unwrap_err();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -55,12 +54,10 @@ fn no_variable_definition_duplicates() {
|
|||||||
let q = 39
|
let q = 39
|
||||||
let a = 30
|
let a = 30
|
||||||
"#;
|
"#;
|
||||||
let (_, output) = add_symbols(source);
|
let (_, output) = add_symbols_from_source(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]
|
||||||
@ -78,9 +75,8 @@ fn no_variable_definition_duplicates_in_function() {
|
|||||||
let x = 33
|
let x = 33
|
||||||
}
|
}
|
||||||
"#;
|
"#;
|
||||||
let (_, output) = add_symbols(source);
|
let (_, output) = add_symbols_from_source(source);
|
||||||
let _err = output.unwrap_err();
|
assert!(output.unwrap_err().contains("Duplicate variable definition: x"))
|
||||||
//assert!(output.unwrap_err().contains("Duplicate variable definition: x"))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -93,10 +89,9 @@ fn dont_falsely_detect_duplicates() {
|
|||||||
}
|
}
|
||||||
let q = 39;
|
let q = 39;
|
||||||
"#;
|
"#;
|
||||||
let (symbols, _) = add_symbols(source);
|
let (symbol_table, _) = add_symbols_from_source(source);
|
||||||
|
assert!(symbol_table.lookup_by_fqsn(&fqsn!["a"; tr]).is_some());
|
||||||
assert!(symbols.fq_names.table.get(&make_fqsn(&["a"])).is_some());
|
assert!(symbol_table.lookup_by_fqsn(&fqsn!["some_func"; fn, "a";tr]).is_some());
|
||||||
assert!(symbols.fq_names.table.get(&make_fqsn(&["some_func", "a"])).is_some());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -108,9 +103,9 @@ fn inner_func(arg) {
|
|||||||
}
|
}
|
||||||
x + inner_func(x)
|
x + inner_func(x)
|
||||||
}"#;
|
}"#;
|
||||||
let (symbols, _) = add_symbols(source);
|
let (symbol_table, _) = add_symbols_from_source(source);
|
||||||
assert!(symbols.fq_names.table.get(&make_fqsn(&["outer_func"])).is_some());
|
assert!(symbol_table.lookup_by_fqsn(&fqsn!("outer_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, "inner_func"; tr)).is_some());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -128,11 +123,11 @@ fn second_inner_func() {
|
|||||||
|
|
||||||
inner_func(x)
|
inner_func(x)
|
||||||
}"#;
|
}"#;
|
||||||
let (symbols, _) = add_symbols(source);
|
let (symbol_table, _) = add_symbols_from_source(source);
|
||||||
assert!(symbols.fq_names.table.get(&make_fqsn(&["outer_func"])).is_some());
|
assert!(symbol_table.lookup_by_fqsn(&fqsn!("outer_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, "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"; tr)).is_some());
|
||||||
assert!(symbols.fq_names.table.get(&make_fqsn(&["outer_func", "second_inner_func", "another_inner_func"])).is_some());
|
assert!(symbol_table.lookup_by_fqsn(&fqsn!("outer_func"; fn, "second_inner_func"; fn, "another_inner_func"; tr)).is_some());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -152,8 +147,8 @@ fn second_inner_func() {
|
|||||||
|
|
||||||
inner_func(x)
|
inner_func(x)
|
||||||
}"#;
|
}"#;
|
||||||
let (_, output) = add_symbols(source);
|
let (_, output) = add_symbols_from_source(source);
|
||||||
let _err = output.unwrap_err();
|
assert!(output.unwrap_err().contains("Duplicate"))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -166,11 +161,10 @@ module stuff {
|
|||||||
|
|
||||||
fn item()
|
fn item()
|
||||||
"#;
|
"#;
|
||||||
|
values_in_table! { source =>
|
||||||
let (symbols, _) = add_symbols(source);
|
&fqsn!("item"; tr),
|
||||||
symbols.fq_names.table.get(&make_fqsn(&["stuff"])).unwrap();
|
&fqsn!("stuff"; tr, "item"; tr)
|
||||||
symbols.fq_names.table.get(&make_fqsn(&["item"])).unwrap();
|
};
|
||||||
symbols.fq_names.table.get(&make_fqsn(&["stuff", "item"])).unwrap();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -188,6 +182,8 @@ fn duplicate_modules() {
|
|||||||
fn foo() { 256.1 }
|
fn foo() { 256.1 }
|
||||||
}
|
}
|
||||||
"#;
|
"#;
|
||||||
let (_, output) = add_symbols(source);
|
let (_, output) = add_symbols_from_source(source);
|
||||||
let _output = output.unwrap_err();
|
let output = output.unwrap_err();
|
||||||
|
assert!(output.contains("Duplicate module"));
|
||||||
|
assert!(output.contains("already defined at 5"));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user