Fix all warnings

This commit is contained in:
Greg Shuflin 2021-10-19 14:19:26 -07:00
parent d1d3a70339
commit 40f759eea8
6 changed files with 6 additions and 193 deletions

View File

@ -2,10 +2,8 @@ use std::rc::Rc;
use std::fmt::Write; use std::fmt::Write;
use std::io; use std::io;
//use crate::schala::SymbolTableHandle;
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, FullyQualifiedSymbolName};
use crate::builtin::Builtin; use crate::builtin::Builtin;
mod test; mod test;

View File

@ -1,20 +1,17 @@
#![cfg(test)] #![cfg(test)]
use std::cell::RefCell;
use std::rc::Rc;
use crate::symbol_table::SymbolTable; use crate::symbol_table::SymbolTable;
use crate::reduced_ast::reduce; use crate::reduced_ast::reduce;
use crate::eval::State; use crate::eval::State;
fn evaluate_all_outputs(input: &str) -> Vec<Result<String, String>> { fn evaluate_all_outputs(input: &str) -> Vec<Result<String, String>> {
let ast = crate::util::quick_ast(input); let ast = crate::util::quick_ast(input);
let symbol_table = Rc::new(RefCell::new(SymbolTable::new()));
symbol_table.borrow_mut().add_top_level_symbols(&ast).unwrap();
symbol_table.borrow_mut().process_ast(&ast).unwrap(); let mut symbol_table = SymbolTable::new();
symbol_table.process_ast(&ast).unwrap();
let reduced = reduce(&ast, &symbol_table);
let reduced = reduce(&ast, &symbol_table.borrow());
let mut state = State::new(); let mut state = State::new();
let all_output = state.evaluate(reduced, true); let all_output = state.evaluate(reduced, true);
all_output all_output

View File

@ -198,7 +198,6 @@ impl<'a> Reducer<'a> {
}, },
SymbolSpec::Func(_) => Expr::Sym(local_name.clone()), SymbolSpec::Func(_) => Expr::Sym(local_name.clone()),
SymbolSpec::Binding => Expr::Sym(local_name.clone()), //TODO not sure if this is right, probably needs to eventually be fqsn SymbolSpec::Binding => Expr::Sym(local_name.clone()), //TODO not sure if this is right, probably needs to eventually be fqsn
SymbolSpec::Type { .. } => Expr::ReductionError("AST reducer doesnt expect a type here".to_string())
} }
} }

View File

@ -79,11 +79,7 @@ impl Schala {
let ast = self.active_parser.parse() let ast = self.active_parser.parse()
.map_err(|err| SchalaError::from_parse_error(err, &self.source_reference))?; .map_err(|err| SchalaError::from_parse_error(err, &self.source_reference))?;
// Symbol table //Perform all symbol table work
self.symbol_table.borrow_mut().add_top_level_symbols(&ast)
.map_err(|err| SchalaError::from_string(err, Stage::Symbols))?;
//WIP - new symbol table
self.symbol_table.borrow_mut().process_ast(&ast) self.symbol_table.borrow_mut().process_ast(&ast)
.map_err(|err| SchalaError::from_string(err, Stage::Symbols))?; .map_err(|err| SchalaError::from_string(err, Stage::Symbols))?;

View File

@ -6,12 +6,11 @@ use std::fmt::Write;
use crate::tokenizing::Location; use crate::tokenizing::Location;
use crate::ast; use crate::ast;
use crate::ast::{ItemId, TypeBody, Variant, TypeSingletonName, Signature, Declaration, Statement, StatementKind, ModuleSpecifier}; use crate::ast::{ItemId, TypeBody, Variant, TypeSingletonName, Declaration, Statement, StatementKind, ModuleSpecifier};
use crate::typechecking::TypeName; use crate::typechecking::TypeName;
mod resolver; mod resolver;
mod tables; mod tables;
use tables::DeclLocations;
mod symbol_trie; mod symbol_trie;
use symbol_trie::SymbolTrie; use symbol_trie::SymbolTrie;
mod test; mod test;
@ -122,7 +121,6 @@ impl ScopeSegment {
//cf. p. 150 or so of Language Implementation Patterns //cf. p. 150 or so of Language Implementation Patterns
pub struct SymbolTable { pub struct SymbolTable {
decl_locations: DeclLocations, //TODO delete this
symbol_path_to_symbol: HashMap<FullyQualifiedSymbolName, Symbol>, symbol_path_to_symbol: HashMap<FullyQualifiedSymbolName, Symbol>,
@ -146,7 +144,6 @@ pub struct SymbolTable {
impl SymbolTable { impl SymbolTable {
pub fn new() -> SymbolTable { pub fn new() -> SymbolTable {
SymbolTable { SymbolTable {
decl_locations: DeclLocations::new(),
symbol_path_to_symbol: HashMap::new(), symbol_path_to_symbol: HashMap::new(),
symbol_trie: SymbolTrie::new(), symbol_trie: SymbolTrie::new(),
@ -171,15 +168,6 @@ impl SymbolTable {
let fqsn = self.id_to_fqsn.get(id); let fqsn = self.id_to_fqsn.get(id);
fqsn.and_then(|fqsn| self.fqsn_to_symbol.get(fqsn)) fqsn.and_then(|fqsn| self.fqsn_to_symbol.get(fqsn))
} }
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() });
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);
self.symbol_path_to_symbol.insert(fully_qualified_name, symbol);
}
} }
#[allow(dead_code)] #[allow(dead_code)]
@ -210,9 +198,6 @@ pub enum SymbolSpec {
type_name: TypeName, type_name: TypeName,
}, },
Binding, Binding,
Type {
name: TypeName
},
} }
impl fmt::Display for SymbolSpec { impl fmt::Display for SymbolSpec {
@ -223,7 +208,6 @@ impl fmt::Display for SymbolSpec {
DataConstructor { index, type_name, type_args } => write!(f, "DataConstructor(idx: {})({:?} -> {})", index, type_args, type_name), DataConstructor { index, type_name, type_args } => write!(f, "DataConstructor(idx: {})({:?} -> {})", index, type_args, type_name),
RecordConstructor { type_name, index, ..} => write!(f, "RecordConstructor(idx: {})(<members> -> {})", index, type_name), RecordConstructor { type_name, index, ..} => write!(f, "RecordConstructor(idx: {})(<members> -> {})", index, type_name),
Binding => write!(f, "Binding"), Binding => write!(f, "Binding"),
Type { name } => write!(f, "Type <{}>", name),
} }
} }
} }
@ -233,8 +217,6 @@ impl SymbolTable {
/* note: this adds names for *forward reference* but doesn't actually create any types. solve that problem /* note: this adds names for *forward reference* but doesn't actually create any types. solve that problem
* later */ * later */
/// Walks the AST, matching the ID of an identifier used in some expression to /// Walks the AST, matching the ID of an identifier used in some expression to
/// the corresponding Symbol. /// the corresponding Symbol.
fn resolve_symbol_ids(&mut self, ast: &ast::AST) -> Result<(), String> { fn resolve_symbol_ids(&mut self, ast: &ast::AST) -> Result<(), String> {
@ -408,53 +390,6 @@ impl SymbolTable {
} }
} }
pub fn add_top_level_symbols(&mut self, ast: &ast::AST) -> Result<(), String> {
let mut scope_name_stack = Vec::new();
self.add_symbols_from_scope(&ast.statements, &mut scope_name_stack)
}
fn add_symbols_from_scope<'a>(&'a mut self, statements: &Vec<Statement>, scope_name_stack: &mut Vec<ScopeSegment>) -> Result<(), String> {
use self::ast::Declaration::*;
for statement in statements.iter() {
match statement {
Statement { kind: StatementKind::Declaration(decl), id, location, } => {
self.decl_locations.add_location(id, *location);
match decl {
FuncSig(ref signature) => {
self.add_function_signature(signature, scope_name_stack)?
}
FuncDecl(ref signature, ref body) => {
self.add_function_signature(signature, scope_name_stack)?;
scope_name_stack.push(ScopeSegment{
name: signature.name.clone(),
});
let output = self.add_symbols_from_scope(body, scope_name_stack);
scope_name_stack.pop();
output?
},
TypeDecl { name, body, mutable } => {
self.add_type_decl(name, body, mutable, scope_name_stack)?
},
Binding { name, .. } => {
self.add_new_symbol(name, scope_name_stack, SymbolSpec::Binding);
}
_ => ()
}
},
Statement { kind: StatementKind::Module(ModuleSpecifier { name, contents}), id, location } => {
self.decl_locations.add_location(id, *location);
scope_name_stack.push(ScopeSegment { name: name.clone() });
let output = self.add_symbols_from_scope(contents, scope_name_stack);
scope_name_stack.pop();
output?
},
_ => ()
}
}
Ok(())
}
#[allow(dead_code)] #[allow(dead_code)]
pub fn debug_symbol_table(&self) -> String { pub fn debug_symbol_table(&self) -> String {
let mut output = "Symbol table\n".to_string(); let mut output = "Symbol table\n".to_string();
@ -465,95 +400,4 @@ impl SymbolTable {
} }
output output
} }
fn add_function_signature(&mut self, signature: &Signature, scope_name_stack: &mut Vec<ScopeSegment>) -> Result<(), String> {
let mut local_type_context = LocalTypeContext::new();
let types = signature.params.iter().map(|param| match param.anno {
Some(ref type_identifier) => Rc::new(format!("{:?}", type_identifier)),
None => local_type_context.new_universal_type()
}).collect();
self.add_new_symbol(&signature.name, scope_name_stack, SymbolSpec::Func(types));
Ok(())
} }
//TODO handle type mutability
fn add_type_decl(&mut self, type_name: &TypeSingletonName, body: &TypeBody, _mutable: &bool, scope_name_stack: &mut Vec<ScopeSegment>) -> Result<(), String> {
use crate::ast::{TypeIdentifier};
let TypeBody(variants) = body;
let ref type_name = type_name.name;
let type_spec = SymbolSpec::Type {
name: type_name.clone(),
};
self.add_new_symbol(type_name, &scope_name_stack, type_spec);
scope_name_stack.push(ScopeSegment{
name: type_name.clone(),
});
//TODO figure out why _params isn't being used here
for (index, var) in variants.iter().enumerate() {
match var {
Variant::UnitStruct(variant_name) => {
let spec = SymbolSpec::DataConstructor {
index,
type_name: type_name.clone(),
type_args: vec![],
};
self.add_new_symbol(variant_name, scope_name_stack, spec);
},
Variant::TupleStruct(variant_name, tuple_members) => {
//TODO fix the notion of a tuple type
let type_args = tuple_members.iter().map(|type_name| match type_name {
TypeIdentifier::Singleton(TypeSingletonName { name, ..}) => name.clone(),
TypeIdentifier::Tuple(_) => unimplemented!(),
}).collect();
let spec = SymbolSpec::DataConstructor {
index,
type_name: type_name.clone(),
type_args
};
self.add_new_symbol(variant_name, scope_name_stack, spec);
},
Variant::Record { name, members: defined_members } => {
let mut members = HashMap::new();
let mut duplicate_member_definitions = Vec::new();
for (member_name, member_type) in defined_members {
match members.entry(member_name.clone()) {
Entry::Occupied(_) => duplicate_member_definitions.push(member_name.clone()),
Entry::Vacant(v) => {
v.insert(match member_type {
TypeIdentifier::Singleton(TypeSingletonName { name, ..}) => name.clone(),
TypeIdentifier::Tuple(_) => unimplemented!(),
});
}
}
}
if duplicate_member_definitions.len() != 0 {
return Err(format!("Duplicate member(s) in definition of type {}: {:?}", type_name, duplicate_member_definitions));
}
let spec = SymbolSpec::RecordConstructor { index, type_name: type_name.clone(), members };
self.add_new_symbol(name, scope_name_stack, spec);
},
}
}
scope_name_stack.pop();
Ok(())
}
}
struct LocalTypeContext {
state: u8
}
impl LocalTypeContext {
fn new() -> LocalTypeContext {
LocalTypeContext { state: 0 }
}
fn new_universal_type(&mut self) -> TypeName {
let n = self.state;
self.state += 1;
Rc::new(format!("{}", (('a' as u8) + n) as char))
}
}

View File

@ -1,21 +0,0 @@
use std::collections::HashMap;
use crate::ast::ItemId;
use crate::tokenizing::Location;
/// Maps top-level declarations to Locations in source code, to detect
/// multiply-defined top level items.
pub struct DeclLocations {
map: HashMap<ItemId, Location>
}
impl DeclLocations {
pub fn new() -> Self {
Self { map: HashMap::new() }
}
pub(crate) fn add_location(&mut self, id: &ItemId, loc: Location) {
self.map.insert(id.clone(), loc);
}
}