Get rid of symbol segment kind

I don't think I need this after all
This commit is contained in:
greg 2019-10-18 18:24:57 -07:00
parent e75958c2a2
commit 86dc5eca02
4 changed files with 14 additions and 48 deletions

View File

@ -8,7 +8,7 @@ use itertools::Itertools;
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, ScopeSegment, ScopeSegmentKind, FullyQualifiedSymbolName}; use crate::symbol_table::{SymbolSpec, Symbol, SymbolTable, ScopeSegment, FullyQualifiedSymbolName};
use crate::builtin::Builtin; use crate::builtin::Builtin;
mod test; mod test;

View File

@ -1,6 +1,6 @@
use std::rc::Rc; use std::rc::Rc;
use crate::symbol_table::{SymbolTable, ScopeSegment, ScopeSegmentKind, FullyQualifiedSymbolName}; use crate::symbol_table::{SymbolTable, ScopeSegment, FullyQualifiedSymbolName};
use crate::ast::*; use crate::ast::*;
use crate::util::ScopeStack; use crate::util::ScopeStack;
@ -17,9 +17,8 @@ impl<'a> ASTVisitor for ScopeResolver<'a> {
let ImportSpecifier { ref path_components, ref imported_names, .. } = &import_spec; let ImportSpecifier { ref path_components, ref imported_names, .. } = &import_spec;
match imported_names { match imported_names {
ImportedNames::All => { ImportedNames::All => {
let len = path_components.len(); let prefix = FullyQualifiedSymbolName(path_components.iter().map(|c| ScopeSegment {
let prefix = FullyQualifiedSymbolName(path_components.iter().enumerate().map(|(i, c)| ScopeSegment { name: c.clone(),
name: c.clone(), kind: if i == len-1 { ScopeSegmentKind::Terminal } else { ScopeSegmentKind::Type }
}).collect()); }).collect());
let members = self.symbol_table.lookup_children_of_fqsn(&prefix); let members = self.symbol_table.lookup_children_of_fqsn(&prefix);
for member in members.into_iter() { for member in members.into_iter() {
@ -31,13 +30,13 @@ impl<'a> ASTVisitor for ScopeResolver<'a> {
ImportedNames::LastOfPath => { ImportedNames::LastOfPath => {
let name = path_components.last().unwrap(); //TODO handle better let name = path_components.last().unwrap(); //TODO handle better
let fqsn_prefix = path_components.iter().map(|c| ScopeSegment { let fqsn_prefix = path_components.iter().map(|c| ScopeSegment {
name: c.clone(), kind: ScopeSegmentKind::Type name: c.clone(),
}).collect(); }).collect();
self.name_scope_stack.insert(name.clone(), fqsn_prefix); self.name_scope_stack.insert(name.clone(), fqsn_prefix);
} }
ImportedNames::List(ref names) => { ImportedNames::List(ref names) => {
let fqsn_prefix: FQSNPrefix = path_components.iter().map(|c| ScopeSegment { let fqsn_prefix: FQSNPrefix = path_components.iter().map(|c| ScopeSegment {
name: c.clone(), kind: ScopeSegmentKind::Type name: c.clone(),
}).collect(); }).collect();
for name in names.iter() { for name in names.iter() {
self.name_scope_stack.insert(name.clone(), fqsn_prefix.clone()); self.name_scope_stack.insert(name.clone(), fqsn_prefix.clone());
@ -84,14 +83,8 @@ impl<'a> ScopeResolver<'a> {
//TODO this is incomplete //TODO this is incomplete
fn lookup_name_in_scope(&self, sym_name: &QualifiedName) -> FullyQualifiedSymbolName { fn lookup_name_in_scope(&self, sym_name: &QualifiedName) -> FullyQualifiedSymbolName {
let QualifiedName { components: vec, .. } = sym_name; let QualifiedName { components: vec, .. } = sym_name;
let len = vec.len(); let new_vec: Vec<ScopeSegment> = vec.iter().map(|name| {
let new_vec: Vec<ScopeSegment> = vec.iter().enumerate().map(|(i, name)| { ScopeSegment { name: name.clone() }
let kind = if i == (len - 1) {
ScopeSegmentKind::Terminal
} else {
ScopeSegmentKind::Type
};
ScopeSegment { name: name.clone(), kind }
}).collect(); }).collect();
FullyQualifiedSymbolName(new_vec) FullyQualifiedSymbolName(new_vec)
} }

View File

@ -9,23 +9,13 @@ use crate::ast::{ItemId, TypeBody, TypeSingletonName, Signature, Statement, Stat
use crate::typechecking::TypeName; use crate::typechecking::TypeName;
#[allow(unused_macros)]
macro_rules! sym_path_kind {
(fn) => { ScopeSegmentKind::Function };
(ty) => { ScopeSegmentKind::Type };
(tr) => { ScopeSegmentKind::Terminal };
}
#[allow(unused_macros)] #[allow(unused_macros)]
macro_rules! fqsn { macro_rules! fqsn {
( $( $name:expr ; $kind:tt),* ) => { ( $( $name:expr ; $kind:tt),* ) => {
{ {
let mut vec = vec![]; let mut vec = vec![];
$( $(
vec.push(ScopeSegment::new( vec.push(ScopeSegment::new(Rc::new($name.to_string())));
Rc::new($name.to_string()),
sym_path_kind!($kind),
));
)* )*
FullyQualifiedSymbolName(vec) FullyQualifiedSymbolName(vec)
} }
@ -55,35 +45,21 @@ impl fmt::Display for FullyQualifiedSymbolName {
#[derive(Debug, Clone, Eq, PartialEq, Hash, PartialOrd, Ord)] #[derive(Debug, Clone, Eq, PartialEq, Hash, PartialOrd, Ord)]
pub struct ScopeSegment { pub struct ScopeSegment {
pub name: Rc<String>, //TODO maybe this could be a &str, for efficiency? pub name: Rc<String>, //TODO maybe this could be a &str, for efficiency?
pub kind: ScopeSegmentKind,
} }
impl fmt::Display for ScopeSegment { impl fmt::Display for ScopeSegment {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use colored::*; let kind = ""; //TODO implement some kind of kind-tracking here
use ScopeSegmentKind::*;
let kind = match self.kind {
Function => "[fn]".green(),
Type => "[ty]".red(),
Terminal => "[tr]".blue(),
};
write!(f, "{}{}", self.name, kind) write!(f, "{}{}", self.name, kind)
} }
} }
impl ScopeSegment { impl ScopeSegment {
pub fn new(name: Rc<String>, kind: ScopeSegmentKind) -> ScopeSegment { pub fn new(name: Rc<String>) -> ScopeSegment {
ScopeSegment { name, kind } ScopeSegment { name }
} }
} }
#[derive(Copy, Debug, Clone, Eq, PartialEq, Hash, PartialOrd, Ord)]
pub enum ScopeSegmentKind {
Function,
Type,
Terminal,
//Module
}
//cf. p. 150 or so of Language Implementation Patterns //cf. p. 150 or so of Language Implementation Patterns
pub struct SymbolTable { pub struct SymbolTable {
@ -112,7 +88,7 @@ impl SymbolTable {
fn add_new_symbol(&mut self, local_name: &Rc<String>, scope_path: &Vec<ScopeSegment>, spec: SymbolSpec) { fn add_new_symbol(&mut self, local_name: &Rc<String>, scope_path: &Vec<ScopeSegment>, spec: SymbolSpec) {
let mut vec: Vec<ScopeSegment> = scope_path.clone(); let mut vec: Vec<ScopeSegment> = scope_path.clone();
vec.push(ScopeSegment { name: local_name.clone(), kind: ScopeSegmentKind::Terminal }); vec.push(ScopeSegment { name: local_name.clone() });
let fully_qualified_name = FullyQualifiedSymbolName(vec); let fully_qualified_name = FullyQualifiedSymbolName(vec);
let symbol = Symbol { local_name: local_name.clone(), fully_qualified_name: fully_qualified_name.clone(), spec }; 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_trie.insert(&fully_qualified_name);
@ -213,7 +189,6 @@ impl SymbolTable {
self.add_function_signature(signature, scope_name_stack)?; self.add_function_signature(signature, scope_name_stack)?;
scope_name_stack.push(ScopeSegment{ scope_name_stack.push(ScopeSegment{
name: signature.name.clone(), name: signature.name.clone(),
kind: ScopeSegmentKind::Function,
}); });
let output = self.add_symbols_from_scope(body, scope_name_stack); let output = self.add_symbols_from_scope(body, scope_name_stack);
let _ = scope_name_stack.pop(); let _ = scope_name_stack.pop();
@ -221,7 +196,6 @@ impl SymbolTable {
}, },
TypeDecl { name, body, mutable } => { TypeDecl { name, body, mutable } => {
insert_and_check_duplicate_symbol(&mut seen_identifiers, &name.name)?; insert_and_check_duplicate_symbol(&mut seen_identifiers, &name.name)?;
//TODO add ScopeSegmentKind::Type here
self.add_type_decl(name, body, mutable, scope_name_stack)? self.add_type_decl(name, body, mutable, scope_name_stack)?
}, },
Binding { name, .. } => { Binding { name, .. } => {
@ -268,7 +242,6 @@ impl SymbolTable {
scope_name_stack.push(ScopeSegment{ scope_name_stack.push(ScopeSegment{
name: type_name.clone(), name: type_name.clone(),
kind: ScopeSegmentKind::Type,
}); });
//TODO figure out why _params isn't being used here //TODO figure out why _params isn't being used here
for (index, var) in variants.iter().enumerate() { for (index, var) in variants.iter().enumerate() {

View File

@ -1,6 +1,6 @@
use std::rc::Rc; use std::rc::Rc;
use radix_trie::{Trie, TrieCommon, TrieKey}; use radix_trie::{Trie, TrieCommon, TrieKey};
use super::{ScopeSegmentKind, ScopeSegment, FullyQualifiedSymbolName}; use super::{ScopeSegment, FullyQualifiedSymbolName};
use std::hash::{Hasher, Hash}; use std::hash::{Hasher, Hash};
use std::collections::hash_map::DefaultHasher; use std::collections::hash_map::DefaultHasher;