From 86dc5eca02fdb1b76717ef7dc302b4af33813990 Mon Sep 17 00:00:00 2001 From: greg Date: Fri, 18 Oct 2019 18:24:57 -0700 Subject: [PATCH] Get rid of symbol segment kind I don't think I need this after all --- schala-lang/language/src/eval.rs | 2 +- schala-lang/language/src/scope_resolution.rs | 21 ++++------- schala-lang/language/src/symbol_table.rs | 37 +++---------------- .../language/src/symbol_table/symbol_trie.rs | 2 +- 4 files changed, 14 insertions(+), 48 deletions(-) diff --git a/schala-lang/language/src/eval.rs b/schala-lang/language/src/eval.rs index 8b1f1d8..403f23d 100644 --- a/schala-lang/language/src/eval.rs +++ b/schala-lang/language/src/eval.rs @@ -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; diff --git a/schala-lang/language/src/scope_resolution.rs b/schala-lang/language/src/scope_resolution.rs index 372f766..a9b3a64 100644 --- a/schala-lang/language/src/scope_resolution.rs +++ b/schala-lang/language/src/scope_resolution.rs @@ -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 = 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 = vec.iter().map(|name| { + ScopeSegment { name: name.clone() } }).collect(); FullyQualifiedSymbolName(new_vec) } diff --git a/schala-lang/language/src/symbol_table.rs b/schala-lang/language/src/symbol_table.rs index 7fe153a..edf8794 100644 --- a/schala-lang/language/src/symbol_table.rs +++ b/schala-lang/language/src/symbol_table.rs @@ -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, //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, kind: ScopeSegmentKind) -> ScopeSegment { - ScopeSegment { name, kind } + pub fn new(name: Rc) -> 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, scope_path: &Vec, spec: SymbolSpec) { let mut vec: Vec = 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() { diff --git a/schala-lang/language/src/symbol_table/symbol_trie.rs b/schala-lang/language/src/symbol_table/symbol_trie.rs index 47a7611..904a7b5 100644 --- a/schala-lang/language/src/symbol_table/symbol_trie.rs +++ b/schala-lang/language/src/symbol_table/symbol_trie.rs @@ -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;