From 0464d959ec214f21e31c866c912319d01a65ccec Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Tue, 2 Nov 2021 01:20:30 -0700 Subject: [PATCH] Rename Scope -> ScopeSegment --- TODO.md | 4 --- schala-lang/language/src/symbol_table/mod.rs | 28 ++++++++--------- .../language/src/symbol_table/resolver.rs | 31 ++++++++++--------- .../language/src/symbol_table/symbol_trie.rs | 4 +-- 4 files changed, 33 insertions(+), 34 deletions(-) diff --git a/TODO.md b/TODO.md index f03e5f1..a721ed3 100644 --- a/TODO.md +++ b/TODO.md @@ -1,10 +1,6 @@ # 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 diff --git a/schala-lang/language/src/symbol_table/mod.rs b/schala-lang/language/src/symbol_table/mod.rs index 79b65f9..e818ae2 100644 --- a/schala-lang/language/src/symbol_table/mod.rs +++ b/schala-lang/language/src/symbol_table/mod.rs @@ -30,29 +30,29 @@ pub type DefId = Id; #[derive(Debug, Clone, Eq, PartialEq, Hash, PartialOrd, Ord)] pub struct Fqsn { //TODO Fqsn's need to be cheaply cloneable - scopes: Vec, //TODO rename to ScopeSegment + scopes: Vec, } impl Fqsn { - fn from_scope_stack(scopes: &[Scope], new_name: Rc) -> Self { + fn from_scope_stack(scopes: &[ScopeSegment], new_name: Rc) -> Self { let mut v = Vec::new(); for s in scopes { v.push(s.clone()); } - v.push(Scope::Name(new_name)); + v.push(ScopeSegment::Name(new_name)); Fqsn { scopes: v } } fn from_strs(strs: &[&str]) -> Fqsn { let mut scopes = vec![]; for s in strs { - scopes.push(Scope::Name(Rc::new(s.to_string()))); + scopes.push(ScopeSegment::Name(Rc::new(s.to_string()))); } Fqsn { scopes } } fn local_name(&self) -> Rc { - let Scope::Name(name) = self.scopes.last().unwrap(); + let ScopeSegment::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 Scope { +enum ScopeSegment { Name(Rc), } -impl fmt::Display for Scope { +impl fmt::Display for ScopeSegment { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let Scope::Name(name) = self; + let ScopeSegment::Name(name) = self; write!(f, "{}", name) } } @@ -328,7 +328,7 @@ impl<'a> SymbolTableRunner<'a> { fn add_from_scope( &mut self, statements: &[Statement], - scope_stack: &mut Vec, + scope_stack: &mut Vec, function_scope: bool, ) -> Vec { let mut errors = vec![]; @@ -342,14 +342,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 = Scope::Name(signature.name.clone()); + let new_scope = ScopeSegment::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 = Scope::Name(name.clone()); + let new_scope = ScopeSegment::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 +371,7 @@ impl<'a> SymbolTableRunner<'a> { id: &ItemId, kind: &StatementKind, location: Location, - scope_stack: &[Scope], + scope_stack: &[ScopeSegment], function_scope: bool, ) -> Result<(), SymbolError> { match kind { @@ -422,7 +422,7 @@ impl<'a> SymbolTableRunner<'a> { type_body: &TypeBody, _mutable: &bool, location: Location, - scope_stack: &mut Vec, + scope_stack: &mut Vec, ) -> Vec { let (variants, immediate_variant) = match type_body { TypeBody::Variants(variants) => (variants.clone(), false), @@ -437,7 +437,7 @@ impl<'a> SymbolTableRunner<'a> { }; 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); // Check for duplicates before registering any types with the TypeContext diff --git a/schala-lang/language/src/symbol_table/resolver.rs b/schala-lang/language/src/symbol_table/resolver.rs index 6556e59..b4ad4ca 100644 --- a/schala-lang/language/src/symbol_table/resolver.rs +++ b/schala-lang/language/src/symbol_table/resolver.rs @@ -2,7 +2,7 @@ use std::rc::Rc; use crate::{ ast::*, - symbol_table::{Fqsn, Scope, SymbolSpec, SymbolTable}, + symbol_table::{Fqsn, ScopeSegment, SymbolSpec, SymbolTable}, util::ScopeStack, }; @@ -46,7 +46,7 @@ impl<'a> ScopeResolver<'a> { let local_name = components.first().unwrap().clone(); let name_type = self.lexical_scopes.lookup(&local_name); - let fqsn = Fqsn { scopes: components.iter().map(|name| Scope::Name(name.clone())).collect() }; + 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 @@ -63,8 +63,8 @@ impl<'a> ScopeResolver<'a> { Some(NameType::Param(n)) => { let spec = SymbolSpec::FunctionParam(*n); //TODO need to come up with a better solution for local variable FQSNs - let lscope = Scope::Name(Rc::new("".to_string())); - let fqsn = Fqsn { scopes: vec![lscope, Scope::Name(local_name.clone())] }; + let lscope = ScopeSegment::Name(Rc::new("".to_string())); + let fqsn = Fqsn { scopes: vec![lscope, ScopeSegment::Name(local_name.clone())] }; self.symbol_table.add_symbol(id, fqsn, spec); } Some(NameType::LocalVariable(item_id)) => { @@ -96,22 +96,23 @@ impl<'a> ASTVisitor for ScopeResolver<'a> { match imported_names { ImportedNames::All => { 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); 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| 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)); } ImportedNames::List(ref names) => { - let fqsn_prefix: Vec = - path_components.iter().map(|c| Scope::Name(c.clone())).collect(); + let fqsn_prefix: Vec = + path_components.iter().map(|c| ScopeSegment::Name(c.clone())).collect(); for name in names.iter() { let mut scopes = fqsn_prefix.clone(); - scopes.push(Scope::Name(name.clone())); + scopes.push(ScopeSegment::Name(name.clone())); let fqsn = Fqsn { scopes }; self.lexical_scopes.insert(fqsn.local_name(), NameType::Import(fqsn)); } @@ -146,7 +147,8 @@ 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![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.lexical_scopes.insert(name.clone(), NameType::LocalVariable(*id)); } @@ -245,13 +247,14 @@ 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 = components[0].clone(); - let lscope = Scope::Name(Rc::new("".to_string())); - let fqsn = Fqsn { scopes: vec![lscope, Scope::Name(local_name.clone())] }; + let lscope = ScopeSegment::Name(Rc::new("".to_string())); + let fqsn = Fqsn { scopes: vec![lscope, ScopeSegment::Name(local_name.clone())] }; 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| Scope::Name(name.clone())).collect() }; + 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 { diff --git a/schala-lang/language/src/symbol_table/symbol_trie.rs b/schala-lang/language/src/symbol_table/symbol_trie.rs index 9ba2085..3f42cdc 100644 --- a/schala-lang/language/src/symbol_table/symbol_trie.rs +++ b/schala-lang/language/src/symbol_table/symbol_trie.rs @@ -5,7 +5,7 @@ use std::{ use radix_trie::{Trie, TrieCommon, TrieKey}; -use super::{DefId, Fqsn, Scope}; +use super::{DefId, Fqsn, ScopeSegment}; #[derive(Debug)] pub struct SymbolTrie(Trie); @@ -15,7 +15,7 @@ impl TrieKey for Fqsn { let mut hasher = DefaultHasher::new(); let mut output = vec![]; for segment in self.scopes.iter() { - let Scope::Name(s) = segment; + let ScopeSegment::Name(s) = segment; s.as_bytes().hash(&mut hasher); output.extend_from_slice(&hasher.finish().to_be_bytes()); }