Some cipppy lints in reduced ast

This commit is contained in:
Greg Shuflin 2021-10-18 23:41:29 -07:00
parent 2609dd404a
commit 1e9a15d01e
2 changed files with 31 additions and 17 deletions

View File

@ -129,12 +129,12 @@ impl<'a> Reducer<'a> {
fn statement(&mut self, stmt: &Statement) -> Stmt { fn statement(&mut self, stmt: &Statement) -> Stmt {
match &stmt.kind { match &stmt.kind {
StatementKind::Expression(expr) => Stmt::Expr(self.expression(&expr)), StatementKind::Expression(expr) => Stmt::Expr(self.expression(expr)),
StatementKind::Declaration(decl) => self.declaration(&decl), StatementKind::Declaration(decl) => self.declaration(decl),
StatementKind::Import(_) => Stmt::Noop, StatementKind::Import(_) => Stmt::Noop,
StatementKind::Module(modspec) => { StatementKind::Module(modspec) => {
for statement in modspec.contents.iter() { for statement in modspec.contents.iter() {
self.statement(&statement); self.statement(statement);
} }
Stmt::Noop Stmt::Noop
} }
@ -156,7 +156,7 @@ impl<'a> Reducer<'a> {
fn expression(&mut self, expr: &Expression) -> Expr { fn expression(&mut self, expr: &Expression) -> Expr {
use crate::ast::ExpressionKind::*; use crate::ast::ExpressionKind::*;
let ref input = expr.kind; let input = &expr.kind;
match input { match input {
NatLiteral(n) => Expr::Lit(Lit::Nat(*n)), NatLiteral(n) => Expr::Lit(Lit::Nat(*n)),
FloatLiteral(f) => Expr::Lit(Lit::Float(*f)), FloatLiteral(f) => Expr::Lit(Lit::Float(*f)),
@ -178,8 +178,7 @@ impl<'a> Reducer<'a> {
} }
fn value(&mut self, qualified_name: &QualifiedName) -> Expr { fn value(&mut self, qualified_name: &QualifiedName) -> Expr {
let ref id = qualified_name.id; let sym_name = match self.symbol_table.get_fqsn_from_id(&qualified_name.id) {
let ref sym_name = match self.symbol_table.get_fqsn_from_id(id) {
Some(fqsn) => fqsn, Some(fqsn) => fqsn,
None => return Expr::ReductionError(format!("FQSN lookup for Value {:?} failed", qualified_name)), None => return Expr::ReductionError(format!("FQSN lookup for Value {:?} failed", qualified_name)),
}; };
@ -191,14 +190,14 @@ impl<'a> Reducer<'a> {
let Symbol { local_name, spec, .. } = match self.symbol_table.lookup_by_fqsn(&sym_name) { let Symbol { local_name, spec, .. } = match self.symbol_table.lookup_by_fqsn(&sym_name) {
Some(s) => s, Some(s) => s,
//None => return Expr::ReductionError(format!("Symbol {:?} not found", sym_name)), //None => return Expr::ReductionError(format!("Symbol {:?} not found", sym_name)),
None => return Expr::Sym(name.clone()) None => return Expr::Sym(name)
}; };
match spec { match spec {
SymbolSpec::RecordConstructor { .. } => Expr::ReductionError(format!("AST reducer doesn't expect a RecordConstructor here")), SymbolSpec::RecordConstructor { .. } => Expr::ReductionError(format!("AST reducer doesn't expect a RecordConstructor here")),
SymbolSpec::DataConstructor { index, type_args, type_name } => Expr::Constructor { SymbolSpec::DataConstructor { index, type_args, type_name } => Expr::Constructor {
type_name: type_name.clone(), type_name: type_name.clone(),
name: name.clone(), name,
tag: index.clone(), tag: index.clone(),
arity: type_args.len(), arity: type_args.len(),
}, },
@ -316,7 +315,7 @@ impl<'a> Reducer<'a> {
} }
} }
fn binop(&mut self, binop: &BinOp, lhs: &Box<Expression>, rhs: &Box<Expression>) -> Expr { fn binop(&mut self, binop: &BinOp, lhs: &Expression, rhs: &Expression) -> Expr {
let operation = Builtin::from_str(binop.sigil()).ok(); let operation = Builtin::from_str(binop.sigil()).ok();
match operation { match operation {
Some(Builtin::Assignment) => Expr::Assign { Some(Builtin::Assignment) => Expr::Assign {
@ -334,7 +333,7 @@ impl<'a> Reducer<'a> {
} }
} }
fn prefix(&mut self, prefix: &PrefixOp, arg: &Box<Expression>) -> Expr { fn prefix(&mut self, prefix: &PrefixOp, arg: &Expression) -> Expr {
let builtin: Option<Builtin> = TryFrom::try_from(prefix).ok(); let builtin: Option<Builtin> = TryFrom::try_from(prefix).ok();
match builtin { match builtin {
Some(op) => { Some(op) => {
@ -356,7 +355,7 @@ impl<'a> Reducer<'a> {
func: Func::UserDefined { func: Func::UserDefined {
name: Some(name.clone()), name: Some(name.clone()),
params: params.iter().map(|param| param.name.clone()).collect(), params: params.iter().map(|param| param.name.clone()).collect(),
body: self.block(&statements), body: self.block(statements),
} }
}, },
TypeDecl { .. } => Stmt::Noop, TypeDecl { .. } => Stmt::Noop,
@ -379,7 +378,7 @@ impl<'a> Reducer<'a> {
fn handle_symbol(symbol: Option<&Symbol>, inner_patterns: &Vec<Pattern>, symbol_table: &SymbolTable) -> Subpattern { fn handle_symbol(symbol: Option<&Symbol>, inner_patterns: &Vec<Pattern>, symbol_table: &SymbolTable) -> Subpattern {
use self::Pattern::*; use self::Pattern::*;
let tag = symbol.map(|symbol| match symbol.spec { let tag = symbol.map(|symbol| match symbol.spec {
SymbolSpec::DataConstructor { index, .. } => index.clone(), SymbolSpec::DataConstructor { index, .. } => index,
_ => panic!("Symbol is not a data constructor - this should've been caught in type-checking"), _ => panic!("Symbol is not a data constructor - this should've been caught in type-checking"),
}); });
let bound_vars = inner_patterns.iter().map(|p| match p { let bound_vars = inner_patterns.iter().map(|p| match p {
@ -445,7 +444,7 @@ impl Pattern {
use self::Pattern::*; use self::Pattern::*;
match self { match self {
TupleStruct(QualifiedName{ components, id }, inner_patterns) => { TupleStruct(QualifiedName{ components, id }, inner_patterns) => {
let fqsn = symbol_table.get_fqsn_from_id(&id); let fqsn = symbol_table.get_fqsn_from_id(id);
match fqsn.and_then(|fqsn| symbol_table.lookup_by_fqsn(&fqsn)) { match fqsn.and_then(|fqsn| symbol_table.lookup_by_fqsn(&fqsn)) {
Some(symbol) => handle_symbol(Some(symbol), inner_patterns, symbol_table), Some(symbol) => handle_symbol(Some(symbol), inner_patterns, symbol_table),
None => { None => {
@ -462,8 +461,8 @@ impl Pattern {
VarOrName(QualifiedName { components, id }) => { VarOrName(QualifiedName { components, id }) => {
// if fqsn is Some, treat this as a symbol pattern. If it's None, treat it // if fqsn is Some, treat this as a symbol pattern. If it's None, treat it
// as a variable. // as a variable.
let fqsn = symbol_table.get_fqsn_from_id(&id); let fqsn = symbol_table.get_fqsn_from_id(id);
match fqsn.and_then(|fqsn| symbol_table.lookup_by_fqsn(&fqsn)) { match fqsn.as_ref().and_then(|fqsn| symbol_table.lookup_by_fqsn(fqsn)) {
Some(symbol) => handle_symbol(Some(symbol), &vec![], symbol_table), Some(symbol) => handle_symbol(Some(symbol), &vec![], symbol_table),
None => { None => {
let name = if components.len() == 1 { let name = if components.len() == 1 {
@ -475,7 +474,7 @@ impl Pattern {
tag: None, tag: None,
subpatterns: vec![], subpatterns: vec![],
guard: None, guard: None,
bound_vars: vec![Some(name.clone())], bound_vars: vec![Some(name)],
} }
} }
} }

View File

@ -270,11 +270,26 @@ impl SymbolTable {
* later */ * later */
/// The main entry point into the symbol table. This will traverse the AST in several
/// different ways and populate subtables with information that will be used further in the
/// compilation process.
pub fn process_ast(&mut self, ast: &ast::AST) -> Result<(), String> {
self.populate_name_tables(ast)?;
self.resolve_symbol_ids(ast)?;
Ok(())
}
//TODO does the same thing scope resolution... but maybe I don't need that
fn resolve_symbol_ids(&mut self, ast: &ast::AST) -> Result<(), String> {
Ok(())
}
/// This function traverses the AST and adds symbol table entries for /// This function traverses the AST and adds symbol table entries for
/// constants, functions, types, and modules defined within. This simultaneously /// constants, functions, types, and modules defined within. This simultaneously
/// checks for dupicate definitions (and returns errors if discovered), and sets /// checks for dupicate definitions (and returns errors if discovered), and sets
/// up name tables that will be used by further parts of the compiler /// up name tables that will be used by further parts of the compiler
pub fn process_ast(&mut self, ast: &ast::AST) -> Result<(), String> { fn populate_name_tables(&mut self, ast: &ast::AST) -> Result<(), String> {
let mut scope_stack = vec![Scope::Top]; let mut scope_stack = vec![Scope::Top];
self.add_from_scope(ast.statements.as_ref(), &mut scope_stack) self.add_from_scope(ast.statements.as_ref(), &mut scope_stack)
.map_err(|err| format!("{:?}", err))?; .map_err(|err| format!("{:?}", err))?;