From 736aa8aad29ed3dee0f19a87f05c48dad534c635 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Tue, 19 Oct 2021 16:42:48 -0700 Subject: [PATCH] Remove dead code --- schala-lang/language/src/error.rs | 2 +- schala-lang/language/src/scope_resolution.rs | 119 ------------------- schala-lang/language/src/symbol_table/mod.rs | 62 ++-------- schala-lang/language/src/tokenizing.rs | 11 +- 4 files changed, 15 insertions(+), 179 deletions(-) delete mode 100644 schala-lang/language/src/scope_resolution.rs diff --git a/schala-lang/language/src/error.rs b/schala-lang/language/src/error.rs index 407499d..a3f3ee4 100644 --- a/schala-lang/language/src/error.rs +++ b/schala-lang/language/src/error.rs @@ -85,7 +85,7 @@ fn format_parse_error(error: ParseError, source_reference: &SourceReference) -> let line_num = error.token.location.line_num; let ch = error.token.location.char_num; let line_from_program = source_reference.get_line(line_num as usize); - let location_pointer = format!("{}^", " ".repeat(ch)); + let location_pointer = format!("{}^", " ".repeat(ch.into())); let line_num_digits = format!("{}", line_num).chars().count(); let space_padding = " ".repeat(line_num_digits); diff --git a/schala-lang/language/src/scope_resolution.rs b/schala-lang/language/src/scope_resolution.rs deleted file mode 100644 index 79e4e8e..0000000 --- a/schala-lang/language/src/scope_resolution.rs +++ /dev/null @@ -1,119 +0,0 @@ -use std::rc::Rc; - -use crate::schala::SymbolTableHandle; -use crate::symbol_table::{ScopeSegment, FullyQualifiedSymbolName}; -use crate::ast::*; -use crate::util::ScopeStack; - -type FQSNPrefix = Vec; - -pub struct ScopeResolver<'a> { - symbol_table_handle: SymbolTableHandle, - name_scope_stack: ScopeStack<'a, Rc, FQSNPrefix>, -} - -impl<'a> ASTVisitor for ScopeResolver<'a> { - //TODO need to un-insert these - maybe need to rethink visitor - fn import(&mut self, import_spec: &ImportSpecifier) { - let ref symbol_table = self.symbol_table_handle.borrow(); - let ImportSpecifier { ref path_components, ref imported_names, .. } = &import_spec; - match imported_names { - ImportedNames::All => { - let prefix = FullyQualifiedSymbolName(path_components.iter().map(|c| ScopeSegment { - name: c.clone(), - }).collect()); - let members = symbol_table.lookup_children_of_fqsn(&prefix); - for member in members.into_iter() { - let local_name = member.0.last().unwrap().name.clone(); - self.name_scope_stack.insert(local_name.clone(), member.0); - } - }, - ImportedNames::LastOfPath => { - let name = path_components.last().unwrap(); //TODO handle better - let fqsn_prefix = path_components.iter().map(|c| ScopeSegment { - 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(), - }).collect(); - for name in names.iter() { - self.name_scope_stack.insert(name.clone(), fqsn_prefix.clone()); - } - } - }; - } - - fn qualified_name(&mut self, qualified_name: &QualifiedName) { - let ref mut symbol_table = self.symbol_table_handle.borrow_mut(); - let fqsn = self.lookup_name_in_scope(&qualified_name); - let ref id = qualified_name.id; - symbol_table.map_id_to_fqsn(id, fqsn); - } - - fn named_struct(&mut self, name: &QualifiedName, _fields: &Vec<(Rc, Expression)>) { - let ref mut symbol_table = self.symbol_table_handle.borrow_mut(); - let ref id = name.id; - let fqsn = self.lookup_name_in_scope(&name); - symbol_table.map_id_to_fqsn(id, fqsn); - } - - fn pattern(&mut self, pat: &Pattern) { - use Pattern::*; - match pat { - Ignored => (), - TuplePattern(_) => (), - Literal(_) => (), - TupleStruct(name, _) => self.qualified_name_in_pattern(name), - Record(name, _) => self.qualified_name_in_pattern(name), - VarOrName(name) => self.qualified_name_in_pattern(name), - }; - } -} - -impl<'a> ScopeResolver<'a> { - pub fn new(symbol_table_handle: SymbolTableHandle) -> ScopeResolver<'static> { - let name_scope_stack = ScopeStack::new(None); - ScopeResolver { symbol_table_handle, name_scope_stack } - } - pub fn resolve(&mut self, ast: &mut AST) -> Result<(), String> { - walk_ast(self, ast); - Ok(()) - } - - fn lookup_name_in_scope(&self, sym_name: &QualifiedName) -> FullyQualifiedSymbolName { - let QualifiedName { components, .. } = sym_name; - let first_component = &components[0]; - match self.name_scope_stack.lookup(first_component) { - None => { - FullyQualifiedSymbolName(components.iter().map(|name| ScopeSegment { name: name.clone() }).collect()) - }, - Some(fqsn_prefix) => { - let mut full_name = fqsn_prefix.clone(); - let rest_of_name: FQSNPrefix = components[1..].iter().map(|name| ScopeSegment { name: name.clone() }).collect(); - full_name.extend_from_slice(&rest_of_name); - FullyQualifiedSymbolName(full_name) - } - } - } - - /// this might be a variable or a pattern. if a variable, set to none - fn qualified_name_in_pattern(&mut self, qualified_name: &QualifiedName) { - let ref mut symbol_table = self.symbol_table_handle.borrow_mut(); - let ref id = qualified_name.id; - let fqsn = self.lookup_name_in_scope(qualified_name); - if symbol_table.lookup_by_fqsn(&fqsn).is_some() { - symbol_table.map_id_to_fqsn(&id, fqsn); - } - } -} - -#[cfg(test)] -mod tests { - #[test] - fn basic_scope() { - - } -} diff --git a/schala-lang/language/src/symbol_table/mod.rs b/schala-lang/language/src/symbol_table/mod.rs index 50b331a..85bd054 100644 --- a/schala-lang/language/src/symbol_table/mod.rs +++ b/schala-lang/language/src/symbol_table/mod.rs @@ -2,7 +2,6 @@ use std::collections::HashMap; use std::collections::hash_map::Entry; use std::rc::Rc; use std::fmt; -use std::fmt::Write; use crate::tokenizing::Location; use crate::ast; @@ -87,43 +86,8 @@ impl NameTable { } } -#[derive(PartialEq, Eq, Hash, Debug, Clone, PartialOrd, Ord)] -pub struct FullyQualifiedSymbolName(pub Vec); - -impl fmt::Display for FullyQualifiedSymbolName { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let FullyQualifiedSymbolName(v) = self; - for segment in v { - write!(f, "::{}", segment)?; - } - Ok(()) - } -} - -#[derive(Debug, Clone, Eq, PartialEq, Hash, PartialOrd, Ord)] -pub struct ScopeSegment { - pub name: Rc, //TODO maybe this could be a &str, for efficiency? -} - -impl fmt::Display for ScopeSegment { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let kind = ""; //TODO implement some kind of kind-tracking here - write!(f, "{}{}", self.name, kind) - } -} - -impl ScopeSegment { - #[allow(dead_code)] - pub fn new(name: Rc) -> ScopeSegment { - ScopeSegment { name } - } -} - //cf. p. 150 or so of Language Implementation Patterns pub struct SymbolTable { - symbol_path_to_symbol: HashMap, - - /// Used for import resolution. symbol_trie: SymbolTrie, @@ -144,9 +108,7 @@ pub struct SymbolTable { impl SymbolTable { pub fn new() -> SymbolTable { SymbolTable { - symbol_path_to_symbol: HashMap::new(), symbol_trie: SymbolTrie::new(), - fq_names: NameTable::new(), types: NameTable::new(), id_to_fqsn: HashMap::new(), @@ -217,6 +179,13 @@ impl SymbolTable { /* note: this adds names for *forward reference* but doesn't actually create any types. solve that problem * later */ + /// Register a new mapping of a fully-qualified symbol name (e.g. `Option::Some`) + /// to a Symbol, a descriptor of what that name refers to. + fn add_symbol(&mut self, fqsn: FQSN, symbol: Symbol) { + self.symbol_trie.insert(&fqsn); + self.fqsn_to_symbol.insert(fqsn, symbol); + } + /// Walks the AST, matching the ID of an identifier used in some expression to /// the corresponding Symbol. fn resolve_symbol_ids(&mut self, ast: &ast::AST) -> Result<(), String> { @@ -236,12 +205,6 @@ impl SymbolTable { Ok(()) } - fn add_symbol(&mut self, fqsn: FQSN, symbol: Symbol) { - self.symbol_trie.insert(&fqsn); - self.fqsn_to_symbol.insert(fqsn, symbol); - - } - //TODO this should probably return a vector of duplicate name errors fn add_from_scope<'a>(&'a mut self, statements: &[Statement], scope_stack: &mut Vec) -> Result<(), DuplicateName> { for statement in statements { @@ -389,15 +352,4 @@ impl SymbolTable { Err(errors) } } - - #[allow(dead_code)] - pub fn debug_symbol_table(&self) -> String { - let mut output = "Symbol table\n".to_string(); - let mut sorted_symbols: Vec<(&FullyQualifiedSymbolName, &Symbol)> = self.symbol_path_to_symbol.iter().collect(); - sorted_symbols.sort_by(|(fqsn, _), (other_fqsn, _)| fqsn.cmp(other_fqsn)); - for (name, sym) in sorted_symbols.iter() { - write!(output, "{} -> {}\n", name, sym).unwrap(); - } - output - } } diff --git a/schala-lang/language/src/tokenizing.rs b/schala-lang/language/src/tokenizing.rs index e5571fd..c883456 100644 --- a/schala-lang/language/src/tokenizing.rs +++ b/schala-lang/language/src/tokenizing.rs @@ -2,12 +2,15 @@ use itertools::Itertools; use std::{iter::{Iterator, Peekable}, convert::TryFrom, rc::Rc, fmt}; use std::convert::TryInto; -pub type LineNumber = u32; +/// A location in a particular source file. Note that the +/// sizes of the internal unsigned integer types limit +/// the size of a source file to 2^32 lines of +/// at most 2^16 characters, which should be plenty big. #[derive(Debug, Clone, Copy, PartialEq, Default)] pub struct Location { - pub(crate) line_num: LineNumber, - pub(crate) char_num: usize, + pub(crate) line_num: u32, + pub(crate) char_num: u16, } impl fmt::Display for Location { @@ -184,7 +187,7 @@ pub fn tokenize(input: &str) -> Vec { c if is_operator(&c) => handle_operator(c, &mut input), unknown => Error(format!("Unexpected character: {}", unknown)), }; - let location = Location { line_num: line_num.try_into().unwrap(), char_num }; + let location = Location { line_num: line_num.try_into().unwrap(), char_num: char_num.try_into().unwrap() }; tokens.push(Token { kind: cur_tok_kind, location }); } tokens