Compare commits

..

No commits in common. "0464d959ec214f21e31c866c912319d01a65ccec" and "acc99fa0efb567f95152d6c28428b75be343a4ee" have entirely different histories.

6 changed files with 85 additions and 110 deletions

View File

@ -1,6 +1,10 @@
# Immediate TODOs / General Code Cleanup
## Evaluator
* Make the evaluator take ReducedIR items by reference
## Testing
* Make an automatic (macro-based?) system for numbering compiler errors, this should be every type of error

View File

@ -30,29 +30,29 @@ pub type DefId = Id<DefItem>;
#[derive(Debug, Clone, Eq, PartialEq, Hash, PartialOrd, Ord)]
pub struct Fqsn {
//TODO Fqsn's need to be cheaply cloneable
scopes: Vec<ScopeSegment>,
scopes: Vec<Scope>, //TODO rename to ScopeSegment
}
impl Fqsn {
fn from_scope_stack(scopes: &[ScopeSegment], new_name: Rc<String>) -> Self {
fn from_scope_stack(scopes: &[Scope], new_name: Rc<String>) -> Self {
let mut v = Vec::new();
for s in scopes {
v.push(s.clone());
}
v.push(ScopeSegment::Name(new_name));
v.push(Scope::Name(new_name));
Fqsn { scopes: v }
}
fn from_strs(strs: &[&str]) -> Fqsn {
let mut scopes = vec![];
for s in strs {
scopes.push(ScopeSegment::Name(Rc::new(s.to_string())));
scopes.push(Scope::Name(Rc::new(s.to_string())));
}
Fqsn { scopes }
}
fn local_name(&self) -> Rc<String> {
let ScopeSegment::Name(name) = self.scopes.last().unwrap();
let Scope::Name(name) = self.scopes.last().unwrap();
name.clone()
}
}
@ -72,13 +72,13 @@ impl fmt::Display for Fqsn {
//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 ScopeSegment {
enum Scope {
Name(Rc<String>),
}
impl fmt::Display for ScopeSegment {
impl fmt::Display for Scope {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let ScopeSegment::Name(name) = self;
let Scope::Name(name) = self;
write!(f, "{}", name)
}
}
@ -141,7 +141,12 @@ pub struct SymbolTable {
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>,
id_to_def: HashMap<ItemId, DefId>,
/// 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, Rc<Symbol>>,
id_to_symbol: HashMap<ItemId, Rc<Symbol>>,
def_to_symbol: HashMap<DefId, Rc<Symbol>>,
}
@ -153,7 +158,8 @@ impl SymbolTable {
fq_names: NameTable::new(),
types: NameTable::new(),
id_to_def: HashMap::new(),
fqsn_to_symbol: HashMap::new(),
id_to_symbol: HashMap::new(),
def_to_symbol: HashMap::new(),
};
@ -180,8 +186,7 @@ impl SymbolTable {
}
pub fn lookup_symbol(&self, id: &ItemId) -> Option<&Symbol> {
let def = self.id_to_def.get(id)?;
self.def_to_symbol.get(def).map(|s| s.as_ref())
self.id_to_symbol.get(id).map(|s| s.as_ref())
}
pub fn lookup_symbol_by_def(&self, def: &DefId) -> Option<&Symbol> {
@ -192,12 +197,8 @@ impl SymbolTable {
pub fn debug(&self) {
println!("Symbol table:");
println!("----------------");
for (id, def) in self.id_to_def.iter() {
if let Some(symbol) = self.def_to_symbol.get(def) {
println!("{} => {}: {}", id, def, symbol);
} else {
println!("{} => {} <NO SYMBOL FOUND>", id, def);
}
for (id, sym) in self.id_to_symbol.iter() {
println!("{} => {}", id, sym);
}
}
@ -206,8 +207,9 @@ impl SymbolTable {
fn add_symbol(&mut self, id: &ItemId, fqsn: Fqsn, spec: SymbolSpec) {
let def_id = self.def_id_store.fresh();
let symbol = Rc::new(Symbol { fully_qualified_name: fqsn.clone(), spec, def_id });
self.symbol_trie.insert(&fqsn, def_id);
self.id_to_def.insert(*id, def_id);
self.symbol_trie.insert(&fqsn);
self.fqsn_to_symbol.insert(fqsn, symbol.clone());
self.id_to_symbol.insert(*id, symbol.clone());
self.def_to_symbol.insert(def_id, symbol);
}
@ -216,7 +218,8 @@ impl SymbolTable {
let spec = SymbolSpec::Builtin(builtin);
let symbol = Rc::new(Symbol { fully_qualified_name: fqsn.clone(), spec, def_id });
self.symbol_trie.insert(&fqsn, def_id);
self.symbol_trie.insert(&fqsn);
self.fqsn_to_symbol.insert(fqsn, symbol.clone());
self.def_to_symbol.insert(def_id, symbol);
}
@ -328,13 +331,13 @@ impl<'a> SymbolTableRunner<'a> {
fn add_from_scope(
&mut self,
statements: &[Statement],
scope_stack: &mut Vec<ScopeSegment>,
scope_stack: &mut Vec<Scope>,
function_scope: bool,
) -> Vec<SymbolError> {
let mut errors = vec![];
for statement in statements {
let Statement { id, kind, location } = statement;
let Statement { id, kind, location } = statement; //TODO I'm not sure if I need to do anything with this ID
let location = *location;
if let Err(err) = self.add_single_statement(id, kind, location, scope_stack, function_scope) {
errors.push(err);
@ -342,14 +345,14 @@ impl<'a> SymbolTableRunner<'a> {
// If there's an error with a name, don't recurse into subscopes of that name
let recursive_errs = match kind {
StatementKind::Declaration(Declaration::FuncDecl(signature, body)) => {
let new_scope = ScopeSegment::Name(signature.name.clone());
let new_scope = Scope::Name(signature.name.clone());
scope_stack.push(new_scope);
let output = self.add_from_scope(body.as_ref(), scope_stack, true);
scope_stack.pop();
output
}
StatementKind::Module(ModuleSpecifier { name, contents }) => {
let new_scope = ScopeSegment::Name(name.clone());
let new_scope = Scope::Name(name.clone());
scope_stack.push(new_scope);
let output = self.add_from_scope(contents.as_ref(), scope_stack, false);
scope_stack.pop();
@ -371,7 +374,7 @@ impl<'a> SymbolTableRunner<'a> {
id: &ItemId,
kind: &StatementKind,
location: Location,
scope_stack: &[ScopeSegment],
scope_stack: &[Scope],
function_scope: bool,
) -> Result<(), SymbolError> {
match kind {
@ -422,7 +425,7 @@ impl<'a> SymbolTableRunner<'a> {
type_body: &TypeBody,
_mutable: &bool,
location: Location,
scope_stack: &mut Vec<ScopeSegment>,
scope_stack: &mut Vec<Scope>,
) -> Vec<SymbolError> {
let (variants, immediate_variant) = match type_body {
TypeBody::Variants(variants) => (variants.clone(), false),
@ -437,7 +440,7 @@ impl<'a> SymbolTableRunner<'a> {
};
let type_fqsn = Fqsn::from_scope_stack(scope_stack, type_name.name.clone());
let new_scope = ScopeSegment::Name(type_name.name.clone());
let new_scope = Scope::Name(type_name.name.clone());
scope_stack.push(new_scope);
// Check for duplicates before registering any types with the TypeContext

View File

@ -2,7 +2,7 @@ use std::rc::Rc;
use crate::{
ast::*,
symbol_table::{Fqsn, ScopeSegment, SymbolSpec, SymbolTable},
symbol_table::{Fqsn, Scope, SymbolSpec, SymbolTable},
util::ScopeStack,
};
@ -39,49 +39,51 @@ impl<'a> ScopeResolver<'a> {
walk_ast(self, ast);
}
/// This method correctly modifies the id_to_def table (ItemId) to have the appropriate
/// This method correctly modifies the id_to_symbol table (ItemId) to have the appropriate
/// mappings.
fn lookup_name_in_scope(&mut self, name: &QualifiedName) {
let QualifiedName { id, components } = name;
let local_name = components.first().unwrap().clone();
let name_type = self.lexical_scopes.lookup(&local_name);
let fqsn = Fqsn { scopes: components.iter().map(|name| ScopeSegment::Name(name.clone())).collect() };
let def_id = self.symbol_table.symbol_trie.lookup(&fqsn);
//TODO handle a "partial" qualified name, and also handle it down in the pattern-matching
//section
//TODO some of these if lets that look into the fqsn_to_symbol table should probaby fail
//with an error
if components.len() == 1 {
let local_name: Rc<String> = components[0].clone();
let name_type = self.lexical_scopes.lookup(&local_name);
match name_type {
Some(NameType::Import(fqsn)) => {
let def_id = self.symbol_table.symbol_trie.lookup(&fqsn);
if let Some(def_id) = def_id {
self.symbol_table.id_to_def.insert(*id, def_id);
let symbol = self.symbol_table.fqsn_to_symbol.get(fqsn);
if let Some(symbol) = symbol {
self.symbol_table.id_to_symbol.insert(*id, symbol.clone());
}
}
Some(NameType::Param(n)) => {
let spec = SymbolSpec::FunctionParam(*n);
//TODO need to come up with a better solution for local variable FQSNs
let lscope = ScopeSegment::Name(Rc::new("<local-param>".to_string()));
let fqsn = Fqsn { scopes: vec![lscope, ScopeSegment::Name(local_name.clone())] };
let lscope = Scope::Name(Rc::new("<local-param>".to_string()));
let fqsn = Fqsn { scopes: vec![lscope, Scope::Name(local_name.clone())] };
self.symbol_table.add_symbol(id, fqsn, spec);
}
Some(NameType::LocalVariable(item_id)) => {
let def_id = self.symbol_table.id_to_def.get(item_id);
if let Some(def_id) = def_id {
let def_id = def_id.clone();
self.symbol_table.id_to_def.insert(*id, def_id);
let symbol = self.symbol_table.id_to_symbol.get(item_id).cloned();
if let Some(symbol) = symbol {
self.symbol_table.id_to_symbol.insert(*id, symbol);
}
}
None => {
//TODO see if I can reduce this duplicate code
let fqsn = Fqsn { scopes: vec![Scope::Name(local_name.clone())] };
let symbol = self.symbol_table.fqsn_to_symbol.get(&fqsn);
if let Some(symbol) = symbol {
self.symbol_table.id_to_symbol.insert(*id, symbol.clone());
}
}
None =>
if let Some(def_id) = def_id {
self.symbol_table.id_to_def.insert(*id, def_id);
},
}
} else {
if let Some(def_id) = def_id {
self.symbol_table.id_to_def.insert(*id, def_id);
let fqsn = Fqsn { scopes: components.iter().map(|name| Scope::Name(name.clone())).collect() };
let symbol = self.symbol_table.fqsn_to_symbol.get(&fqsn);
if let Some(symbol) = symbol {
self.symbol_table.id_to_symbol.insert(*id, symbol.clone());
}
}
}
@ -96,23 +98,22 @@ impl<'a> ASTVisitor for ScopeResolver<'a> {
match imported_names {
ImportedNames::All => {
let prefix =
Fqsn { scopes: path_components.iter().map(|c| ScopeSegment::Name(c.clone())).collect() };
Fqsn { scopes: path_components.iter().map(|c| Scope::Name(c.clone())).collect() };
let members = self.symbol_table.symbol_trie.get_children(&prefix);
for fqsn in members.into_iter() {
self.lexical_scopes.insert(fqsn.local_name(), NameType::Import(fqsn));
}
}
ImportedNames::LastOfPath => {
let fqsn =
Fqsn { scopes: path_components.iter().map(|c| ScopeSegment::Name(c.clone())).collect() };
let fqsn = Fqsn { scopes: path_components.iter().map(|c| Scope::Name(c.clone())).collect() };
self.lexical_scopes.insert(fqsn.local_name(), NameType::Import(fqsn));
}
ImportedNames::List(ref names) => {
let fqsn_prefix: Vec<ScopeSegment> =
path_components.iter().map(|c| ScopeSegment::Name(c.clone())).collect();
let fqsn_prefix: Vec<Scope> =
path_components.iter().map(|c| Scope::Name(c.clone())).collect();
for name in names.iter() {
let mut scopes = fqsn_prefix.clone();
scopes.push(ScopeSegment::Name(name.clone()));
scopes.push(Scope::Name(name.clone()));
let fqsn = Fqsn { scopes };
self.lexical_scopes.insert(fqsn.local_name(), NameType::Import(fqsn));
}
@ -147,8 +148,7 @@ impl<'a> ASTVisitor for ScopeResolver<'a> {
Declaration::Binding { name, .. } => {
if let Some(fn_name) = cur_function_name {
// We are within a function scope
let fqsn =
Fqsn { scopes: vec![ScopeSegment::Name(fn_name), ScopeSegment::Name(name.clone())] };
let fqsn = Fqsn { scopes: vec![Scope::Name(fn_name), Scope::Name(name.clone())] };
self.symbol_table.add_symbol(id, fqsn, SymbolSpec::LocalVariable);
self.lexical_scopes.insert(name.clone(), NameType::LocalVariable(*id));
}
@ -247,18 +247,17 @@ impl<'a> ASTVisitor for ScopeResolver<'a> {
if components.len() == 1 {
//TODO need a better way to construct a FQSN from a QualifiedName
let local_name: Rc<String> = components[0].clone();
let lscope = ScopeSegment::Name(Rc::new("<local-case-match>".to_string()));
let fqsn = Fqsn { scopes: vec![lscope, ScopeSegment::Name(local_name.clone())] };
let lscope = Scope::Name(Rc::new("<local-case-match>".to_string()));
let fqsn = Fqsn { scopes: vec![lscope, Scope::Name(local_name.clone())] };
//let local_name = fqsn.local_name();
self.symbol_table.add_symbol(id, fqsn, SymbolSpec::LocalVariable);
self.lexical_scopes.insert(local_name, NameType::LocalVariable(*id));
} else {
let fqsn = Fqsn {
scopes: components.iter().map(|name| ScopeSegment::Name(name.clone())).collect(),
};
let def_id = self.symbol_table.symbol_trie.lookup(&fqsn);
if let Some(def_id) = def_id {
self.symbol_table.id_to_def.insert(*id, def_id);
let fqsn =
Fqsn { scopes: components.iter().map(|name| Scope::Name(name.clone())).collect() };
let symbol = self.symbol_table.fqsn_to_symbol.get(&fqsn);
if let Some(symbol) = symbol {
self.symbol_table.id_to_symbol.insert(*id, symbol.clone());
}
}
}

View File

@ -5,17 +5,17 @@ use std::{
use radix_trie::{Trie, TrieCommon, TrieKey};
use super::{DefId, Fqsn, ScopeSegment};
use super::{Fqsn, Scope};
#[derive(Debug)]
pub struct SymbolTrie(Trie<Fqsn, DefId>);
pub struct SymbolTrie(Trie<Fqsn, ()>);
impl TrieKey for Fqsn {
fn encode_bytes(&self) -> Vec<u8> {
let mut hasher = DefaultHasher::new();
let mut output = vec![];
for segment in self.scopes.iter() {
let ScopeSegment::Name(s) = segment;
let Scope::Name(s) = segment;
s.as_bytes().hash(&mut hasher);
output.extend_from_slice(&hasher.finish().to_be_bytes());
}
@ -28,12 +28,8 @@ impl SymbolTrie {
SymbolTrie(Trie::new())
}
pub fn insert(&mut self, fqsn: &Fqsn, def_id: DefId) {
self.0.insert(fqsn.clone(), def_id);
}
pub fn lookup(&self, fqsn: &Fqsn) -> Option<DefId> {
self.0.get(fqsn).cloned()
pub fn insert(&mut self, fqsn: &Fqsn) {
self.0.insert(fqsn.clone(), ());
}
pub fn get_children(&self, fqsn: &Fqsn) -> Vec<Fqsn> {
@ -57,12 +53,11 @@ mod test {
#[test]
fn test_trie_insertion() {
let id = DefId::default();
let mut trie = SymbolTrie::new();
trie.insert(&make_fqsn(&["unrelated", "thing"]), id);
trie.insert(&make_fqsn(&["outer", "inner"]), id);
trie.insert(&make_fqsn(&["outer", "inner", "still_inner"]), id);
trie.insert(&make_fqsn(&["unrelated", "thing"]));
trie.insert(&make_fqsn(&["outer", "inner"]));
trie.insert(&make_fqsn(&["outer", "inner", "still_inner"]));
let children = trie.get_children(&make_fqsn(&["outer", "inner"]));
assert_eq!(children.len(), 1);

View File

@ -56,6 +56,7 @@ impl<'a, 'b> Evaluator<'a, 'b> {
}
fn block(&mut self, statements: Vec<Statement>) -> EvalResult<Primitive> {
//TODO need to handle breaks, returns, etc.
let mut retval = None;
for stmt in statements.into_iter() {
match self.statement(stmt)? {
@ -68,7 +69,6 @@ impl<'a, 'b> Evaluator<'a, 'b> {
break;
}
if let Some(_) = self.loop_control {
println!("We here?");
break;
}
}
@ -142,7 +142,6 @@ impl<'a, 'b> Evaluator<'a, 'b> {
Expression::Assign { ref lval, box rval } => {
let mem = lval.into();
let evaluated = self.expression(rval)?;
println!("Inserting {:?} into {:?}", evaluated, mem);
self.state.environments.insert(mem, MemoryValue::Primitive(evaluated));
Primitive::unit()
}
@ -207,7 +206,6 @@ impl<'a, 'b> Evaluator<'a, 'b> {
) -> EvalResult<Primitive> {
loop {
let cond = self.expression(cond.clone())?;
println!("COND: {:?}", cond);
match cond {
Primitive::Literal(Literal::Bool(true)) => (),
Primitive::Literal(Literal::Bool(false)) => break,
@ -216,10 +214,7 @@ impl<'a, 'b> Evaluator<'a, 'b> {
//TODO eventually loops shoudl be able to return something
let _output = self.block(statements.clone())?;
match self.loop_control {
None => (),
Some(LoopControlFlow::Continue) => {
self.loop_control = None;
}
None | Some(LoopControlFlow::Continue) => (),
Some(LoopControlFlow::Break) => {
break;
}

View File

@ -467,24 +467,3 @@ count
"#;
eval_assert(source, "500");
}
#[test]
fn loops_2() {
let source = r#"
let mut a = 0
let mut acc = 0
while a < 10 {
acc = acc + 1
a = a + 1
// Without this continue, the output would be 20
if a == 5 then {
continue
}
acc = acc + 1
}
acc"#;
eval_assert(source, "19");
}