Compare commits

...

5 Commits

Author SHA1 Message Date
Greg Shuflin
0464d959ec Rename Scope -> ScopeSegment 2021-11-02 01:20:30 -07:00
Greg Shuflin
e2f39dd7b9 Fix bug with loops 2021-11-02 01:16:08 -07:00
Greg Shuflin
d4b00b008b Get rid of id_to_symbol table
Now, an ItemId maps to a DefId, and a DefId maps to
a Symbol in a different table.
2021-11-02 00:57:24 -07:00
Greg Shuflin
ec92e14fcf Remove fqsn_to_id table
The trie already takes care of this
2021-11-01 23:43:03 -07:00
Greg Shuflin
0bf0b3e2e8 Clean up some symbol table code 2021-11-01 21:34:45 -07:00
6 changed files with 110 additions and 85 deletions

View File

@ -1,10 +1,6 @@
# Immediate TODOs / General Code Cleanup # Immediate TODOs / General Code Cleanup
## Evaluator
* Make the evaluator take ReducedIR items by reference
## Testing ## Testing
* Make an automatic (macro-based?) system for numbering compiler errors, this should be every type of error * 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)] #[derive(Debug, Clone, Eq, PartialEq, Hash, PartialOrd, Ord)]
pub struct Fqsn { pub struct Fqsn {
//TODO Fqsn's need to be cheaply cloneable //TODO Fqsn's need to be cheaply cloneable
scopes: Vec<Scope>, //TODO rename to ScopeSegment scopes: Vec<ScopeSegment>,
} }
impl Fqsn { impl Fqsn {
fn from_scope_stack(scopes: &[Scope], new_name: Rc<String>) -> Self { fn from_scope_stack(scopes: &[ScopeSegment], new_name: Rc<String>) -> Self {
let mut v = Vec::new(); let mut v = Vec::new();
for s in scopes { for s in scopes {
v.push(s.clone()); v.push(s.clone());
} }
v.push(Scope::Name(new_name)); v.push(ScopeSegment::Name(new_name));
Fqsn { scopes: v } Fqsn { scopes: v }
} }
fn from_strs(strs: &[&str]) -> Fqsn { fn from_strs(strs: &[&str]) -> Fqsn {
let mut scopes = vec![]; let mut scopes = vec![];
for s in strs { for s in strs {
scopes.push(Scope::Name(Rc::new(s.to_string()))); scopes.push(ScopeSegment::Name(Rc::new(s.to_string())));
} }
Fqsn { scopes } Fqsn { scopes }
} }
fn local_name(&self) -> Rc<String> { fn local_name(&self) -> Rc<String> {
let Scope::Name(name) = self.scopes.last().unwrap(); let ScopeSegment::Name(name) = self.scopes.last().unwrap();
name.clone() name.clone()
} }
} }
@ -72,13 +72,13 @@ impl fmt::Display for Fqsn {
//TODO eventually this should use ItemId's to avoid String-cloning //TODO eventually this should use ItemId's to avoid String-cloning
/// One segment within a scope. /// One segment within a scope.
#[derive(Debug, Clone, Eq, PartialEq, Hash, PartialOrd, Ord)] #[derive(Debug, Clone, Eq, PartialEq, Hash, PartialOrd, Ord)]
enum Scope { enum ScopeSegment {
Name(Rc<String>), Name(Rc<String>),
} }
impl fmt::Display for Scope { impl fmt::Display for ScopeSegment {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let Scope::Name(name) = self; let ScopeSegment::Name(name) = self;
write!(f, "{}", name) write!(f, "{}", name)
} }
} }
@ -141,12 +141,7 @@ 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 fq_names: NameTable<NameKind>, //Note that presence of two tables implies that a type and other binding with the same name can co-exist
types: NameTable<TypeKind>, types: NameTable<TypeKind>,
/// A map of the Fqsn of an AST definition to a Symbol data structure, which contains id_to_def: HashMap<ItemId, DefId>,
/// 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>>, def_to_symbol: HashMap<DefId, Rc<Symbol>>,
} }
@ -158,8 +153,7 @@ impl SymbolTable {
fq_names: NameTable::new(), fq_names: NameTable::new(),
types: NameTable::new(), types: NameTable::new(),
fqsn_to_symbol: HashMap::new(), id_to_def: HashMap::new(),
id_to_symbol: HashMap::new(),
def_to_symbol: HashMap::new(), def_to_symbol: HashMap::new(),
}; };
@ -186,7 +180,8 @@ impl SymbolTable {
} }
pub fn lookup_symbol(&self, id: &ItemId) -> Option<&Symbol> { pub fn lookup_symbol(&self, id: &ItemId) -> Option<&Symbol> {
self.id_to_symbol.get(id).map(|s| s.as_ref()) let def = self.id_to_def.get(id)?;
self.def_to_symbol.get(def).map(|s| s.as_ref())
} }
pub fn lookup_symbol_by_def(&self, def: &DefId) -> Option<&Symbol> { pub fn lookup_symbol_by_def(&self, def: &DefId) -> Option<&Symbol> {
@ -197,8 +192,12 @@ impl SymbolTable {
pub fn debug(&self) { pub fn debug(&self) {
println!("Symbol table:"); println!("Symbol table:");
println!("----------------"); println!("----------------");
for (id, sym) in self.id_to_symbol.iter() { for (id, def) in self.id_to_def.iter() {
println!("{} => {}", id, sym); if let Some(symbol) = self.def_to_symbol.get(def) {
println!("{} => {}: {}", id, def, symbol);
} else {
println!("{} => {} <NO SYMBOL FOUND>", id, def);
}
} }
} }
@ -207,9 +206,8 @@ impl SymbolTable {
fn add_symbol(&mut self, id: &ItemId, fqsn: Fqsn, spec: SymbolSpec) { fn add_symbol(&mut self, id: &ItemId, fqsn: Fqsn, spec: SymbolSpec) {
let def_id = self.def_id_store.fresh(); let def_id = self.def_id_store.fresh();
let symbol = Rc::new(Symbol { fully_qualified_name: fqsn.clone(), spec, def_id }); let symbol = Rc::new(Symbol { fully_qualified_name: fqsn.clone(), spec, def_id });
self.symbol_trie.insert(&fqsn); self.symbol_trie.insert(&fqsn, def_id);
self.fqsn_to_symbol.insert(fqsn, symbol.clone()); self.id_to_def.insert(*id, def_id);
self.id_to_symbol.insert(*id, symbol.clone());
self.def_to_symbol.insert(def_id, symbol); self.def_to_symbol.insert(def_id, symbol);
} }
@ -218,8 +216,7 @@ impl SymbolTable {
let spec = SymbolSpec::Builtin(builtin); let spec = SymbolSpec::Builtin(builtin);
let symbol = Rc::new(Symbol { fully_qualified_name: fqsn.clone(), spec, def_id }); let symbol = Rc::new(Symbol { fully_qualified_name: fqsn.clone(), spec, def_id });
self.symbol_trie.insert(&fqsn); self.symbol_trie.insert(&fqsn, def_id);
self.fqsn_to_symbol.insert(fqsn, symbol.clone());
self.def_to_symbol.insert(def_id, symbol); self.def_to_symbol.insert(def_id, symbol);
} }
@ -331,13 +328,13 @@ impl<'a> SymbolTableRunner<'a> {
fn add_from_scope( fn add_from_scope(
&mut self, &mut self,
statements: &[Statement], statements: &[Statement],
scope_stack: &mut Vec<Scope>, scope_stack: &mut Vec<ScopeSegment>,
function_scope: bool, function_scope: bool,
) -> Vec<SymbolError> { ) -> Vec<SymbolError> {
let mut errors = vec![]; let mut errors = vec![];
for statement in statements { for statement in statements {
let Statement { id, kind, location } = statement; //TODO I'm not sure if I need to do anything with this ID let Statement { id, kind, location } = statement;
let location = *location; let location = *location;
if let Err(err) = self.add_single_statement(id, kind, location, scope_stack, function_scope) { if let Err(err) = self.add_single_statement(id, kind, location, scope_stack, function_scope) {
errors.push(err); errors.push(err);
@ -345,14 +342,14 @@ impl<'a> SymbolTableRunner<'a> {
// If there's an error with a name, don't recurse into subscopes of that name // If there's an error with a name, don't recurse into subscopes of that name
let recursive_errs = match kind { let recursive_errs = match kind {
StatementKind::Declaration(Declaration::FuncDecl(signature, body)) => { StatementKind::Declaration(Declaration::FuncDecl(signature, body)) => {
let new_scope = Scope::Name(signature.name.clone()); let new_scope = ScopeSegment::Name(signature.name.clone());
scope_stack.push(new_scope); scope_stack.push(new_scope);
let output = self.add_from_scope(body.as_ref(), scope_stack, true); let output = self.add_from_scope(body.as_ref(), scope_stack, true);
scope_stack.pop(); scope_stack.pop();
output output
} }
StatementKind::Module(ModuleSpecifier { name, contents }) => { StatementKind::Module(ModuleSpecifier { name, contents }) => {
let new_scope = Scope::Name(name.clone()); let new_scope = ScopeSegment::Name(name.clone());
scope_stack.push(new_scope); scope_stack.push(new_scope);
let output = self.add_from_scope(contents.as_ref(), scope_stack, false); let output = self.add_from_scope(contents.as_ref(), scope_stack, false);
scope_stack.pop(); scope_stack.pop();
@ -374,7 +371,7 @@ impl<'a> SymbolTableRunner<'a> {
id: &ItemId, id: &ItemId,
kind: &StatementKind, kind: &StatementKind,
location: Location, location: Location,
scope_stack: &[Scope], scope_stack: &[ScopeSegment],
function_scope: bool, function_scope: bool,
) -> Result<(), SymbolError> { ) -> Result<(), SymbolError> {
match kind { match kind {
@ -425,7 +422,7 @@ impl<'a> SymbolTableRunner<'a> {
type_body: &TypeBody, type_body: &TypeBody,
_mutable: &bool, _mutable: &bool,
location: Location, location: Location,
scope_stack: &mut Vec<Scope>, scope_stack: &mut Vec<ScopeSegment>,
) -> Vec<SymbolError> { ) -> Vec<SymbolError> {
let (variants, immediate_variant) = match type_body { let (variants, immediate_variant) = match type_body {
TypeBody::Variants(variants) => (variants.clone(), false), TypeBody::Variants(variants) => (variants.clone(), false),
@ -440,7 +437,7 @@ impl<'a> SymbolTableRunner<'a> {
}; };
let type_fqsn = Fqsn::from_scope_stack(scope_stack, type_name.name.clone()); let type_fqsn = Fqsn::from_scope_stack(scope_stack, type_name.name.clone());
let new_scope = Scope::Name(type_name.name.clone()); let new_scope = ScopeSegment::Name(type_name.name.clone());
scope_stack.push(new_scope); scope_stack.push(new_scope);
// Check for duplicates before registering any types with the TypeContext // Check for duplicates before registering any types with the TypeContext

View File

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

View File

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

View File

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

View File

@ -467,3 +467,24 @@ count
"#; "#;
eval_assert(source, "500"); 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");
}