Pass SourceMapHandle to SymbolTable

This commit is contained in:
greg 2019-10-23 02:51:34 -07:00
parent 82520aa28d
commit 7495f30e16
4 changed files with 34 additions and 13 deletions

View File

@ -3,13 +3,15 @@
use std::cell::RefCell;
use std::rc::Rc;
use crate::source_map::SourceMap;
use crate::symbol_table::SymbolTable;
use crate::scope_resolution::ScopeResolver;
use crate::reduced_ast::reduce;
use crate::eval::State;
fn evaluate_all_outputs(input: &str) -> Vec<Result<String, String>> {
let symbol_table = Rc::new(RefCell::new(SymbolTable::new()));
let source_map = Rc::new(RefCell::new(SourceMap::new()));
let symbol_table = Rc::new(RefCell::new(SymbolTable::new(source_map)));
let mut state = State::new(symbol_table);
let mut ast = crate::util::quick_ast(input);
state.symbol_table_handle.borrow_mut().add_top_level_symbols(&ast).unwrap();

View File

@ -10,17 +10,19 @@ use schala_repl::{ProgrammingLanguageInterface,
ComputationRequest, ComputationResponse,
LangMetaRequest, LangMetaResponse, GlobalOutputStats,
DebugResponse, DebugAsk};
use crate::{ast, reduced_ast, tokenizing, parsing, eval, typechecking, symbol_table};
use crate::{ast, reduced_ast, tokenizing, parsing, eval, typechecking, symbol_table, source_map};
pub type SymbolTableHandle = Rc<RefCell<symbol_table::SymbolTable>>;
pub type SourceMapHandle = Rc<RefCell<source_map::SourceMap>>;
/// All the state necessary to parse and execute a Schala program are stored in this struct.
/// `state` represents the execution state for the AST-walking interpreter, the other fields
/// should be self-explanatory.
pub struct Schala {
source_reference: SourceReference,
source_map: SourceMapHandle,
state: eval::State<'static>,
symbol_table: Rc<RefCell<symbol_table::SymbolTable>>,
symbol_table: SymbolTableHandle,
resolver: crate::scope_resolution::ScopeResolver<'static>,
type_context: typechecking::TypeContext<'static>,
active_parser: Option<parsing::Parser>,
@ -37,10 +39,13 @@ impl Schala {
impl Schala {
/// Creates a new Schala environment *without* any prelude.
fn new_blank_env() -> Schala {
let symbols = Rc::new(RefCell::new(symbol_table::SymbolTable::new()));
let source_map = Rc::new(RefCell::new(source_map::SourceMap::new()));
let symbols = Rc::new(RefCell::new(symbol_table::SymbolTable::new(source_map.clone())));
Schala {
//TODO maybe these can be the same structure
source_reference: SourceReference::new(),
symbol_table: symbols.clone(),
source_map: source_map.clone(),
resolver: crate::scope_resolution::ScopeResolver::new(symbols.clone()),
state: eval::State::new(symbols),
type_context: typechecking::TypeContext::new(),

View File

@ -4,6 +4,7 @@ use std::rc::Rc;
use std::fmt;
use std::fmt::Write;
use crate::schala::SourceMapHandle;
use crate::ast;
use crate::ast::{ItemId, TypeBody, TypeSingletonName, Signature, Statement, StatementKind};
use crate::typechecking::TypeName;
@ -63,14 +64,16 @@ impl ScopeSegment {
//cf. p. 150 or so of Language Implementation Patterns
pub struct SymbolTable {
source_map_handle: SourceMapHandle,
symbol_path_to_symbol: HashMap<FullyQualifiedSymbolName, Symbol>,
id_to_fqsn: HashMap<ItemId, FullyQualifiedSymbolName>,
symbol_trie: SymbolTrie,
}
impl SymbolTable {
pub fn new() -> SymbolTable {
pub fn new(source_map_handle: SourceMapHandle) -> SymbolTable {
SymbolTable {
source_map_handle,
symbol_path_to_symbol: HashMap::new(),
id_to_fqsn: HashMap::new(),
symbol_trie: SymbolTrie::new()

View File

@ -1,7 +1,10 @@
#![cfg(test)]
use std::cell::RefCell;
use std::rc::Rc;
use super::*;
use crate::util::quick_ast;
use crate::source_map;
macro_rules! values_in_table {
($source:literal, $single_value:expr) => {
@ -9,7 +12,8 @@ macro_rules! values_in_table {
};
($source:literal | $( $value:expr ),* ) => {
{
let mut symbol_table = SymbolTable::new();
let source_map = Rc::new(RefCell::new(source_map::SourceMap::new()));
let mut symbol_table = SymbolTable::new(source_map);
let ast = quick_ast($source);
symbol_table.add_top_level_symbols(&ast).unwrap();
$(
@ -38,7 +42,8 @@ fn no_duplicates() {
fn b() { 2 }
fn a() { 3 }
"#;
let mut symbol_table = SymbolTable::new();
let source_map = Rc::new(RefCell::new(source_map::SourceMap::new()));
let mut symbol_table = SymbolTable::new(source_map);
let ast = quick_ast(source);
let output = symbol_table.add_top_level_symbols(&ast).unwrap_err();
println!("OUTPUT: {}", output);
@ -52,7 +57,8 @@ fn no_duplicates_2() {
let q = 39;
let a = 30;
"#;
let mut symbol_table = SymbolTable::new();
let source_map = Rc::new(RefCell::new(source_map::SourceMap::new()));
let mut symbol_table = SymbolTable::new(source_map);
let ast = quick_ast(source);
let output = symbol_table.add_top_level_symbols(&ast).unwrap_err();
assert!(output.contains("Duplicate"));
@ -73,7 +79,8 @@ fn no_duplicates_3() {
let x = 33
}
"#;
let mut symbol_table = SymbolTable::new();
let source_map = Rc::new(RefCell::new(source_map::SourceMap::new()));
let mut symbol_table = SymbolTable::new(source_map);
let ast = quick_ast(source);
let output = symbol_table.add_top_level_symbols(&ast).unwrap_err();
assert!(output.contains("Duplicate"))
@ -89,7 +96,8 @@ fn dont_falsely_detect_duplicates() {
}
let q = 39;
"#;
let mut symbol_table = SymbolTable::new();
let source_map = Rc::new(RefCell::new(source_map::SourceMap::new()));
let mut symbol_table = SymbolTable::new(source_map);
let ast = quick_ast(source);
symbol_table.add_top_level_symbols(&ast).unwrap();
assert!(symbol_table.lookup_by_fqsn(&fqsn!["a"; tr]).is_some());
@ -105,7 +113,8 @@ fn inner_func(arg) {
}
x + inner_func(x)
}"#;
let mut symbol_table = SymbolTable::new();
let source_map = Rc::new(RefCell::new(source_map::SourceMap::new()));
let mut symbol_table = SymbolTable::new(source_map);
let ast = quick_ast(source);
symbol_table.add_top_level_symbols(&ast).unwrap();
assert!(symbol_table.lookup_by_fqsn(&fqsn!("outer_func"; tr)).is_some());
@ -127,7 +136,8 @@ fn second_inner_func() {
inner_func(x)
}"#;
let mut symbol_table = SymbolTable::new();
let source_map = Rc::new(RefCell::new(source_map::SourceMap::new()));
let mut symbol_table = SymbolTable::new(source_map);
let ast = quick_ast(source);
symbol_table.add_top_level_symbols(&ast).unwrap();
assert!(symbol_table.lookup_by_fqsn(&fqsn!("outer_func"; tr)).is_some());
@ -153,7 +163,8 @@ fn second_inner_func() {
inner_func(x)
}"#;
let mut symbol_table = SymbolTable::new();
let source_map = Rc::new(RefCell::new(source_map::SourceMap::new()));
let mut symbol_table = SymbolTable::new(source_map);
let ast = quick_ast(source);
let output = symbol_table.add_top_level_symbols(&ast).unwrap_err();
assert!(output.contains("Duplicate"))