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::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;
mod test;

View File

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

View File

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

View File

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