Rename Scope -> ScopeSegment

This commit is contained in:
Greg Shuflin 2021-11-02 01:20:30 -07:00
parent e2f39dd7b9
commit 0464d959ec
4 changed files with 33 additions and 34 deletions

View File

@ -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

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<Scope>, //TODO rename to ScopeSegment
scopes: Vec<ScopeSegment>,
}
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();
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<String> {
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<String>),
}
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>,
scope_stack: &mut Vec<ScopeSegment>,
function_scope: bool,
) -> Vec<SymbolError> {
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>,
scope_stack: &mut Vec<ScopeSegment>,
) -> Vec<SymbolError> {
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

View File

@ -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("<local-param>".to_string()));
let fqsn = Fqsn { scopes: vec![lscope, Scope::Name(local_name.clone())] };
let lscope = ScopeSegment::Name(Rc::new("<local-param>".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<Scope> =
path_components.iter().map(|c| Scope::Name(c.clone())).collect();
let fqsn_prefix: Vec<ScopeSegment> =
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<String> = components[0].clone();
let lscope = Scope::Name(Rc::new("<local-case-match>".to_string()));
let fqsn = Fqsn { scopes: vec![lscope, Scope::Name(local_name.clone())] };
let lscope = ScopeSegment::Name(Rc::new("<local-case-match>".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 {

View File

@ -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<Fqsn, DefId>);
@ -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());
}