More visitor stuff

This commit is contained in:
greg 2019-07-12 01:51:51 -07:00
parent 3dad077a09
commit b0dd1a6527
2 changed files with 53 additions and 3 deletions

View File

@ -115,7 +115,7 @@ impl ASTVisitor for Reducer {
pub fn perform_reduction(mut input: AST, _symbol_table: &SymbolTable) -> Result<ReducedAST, String> {
let mut reducer = Reducer::new();
input.visit(&mut reducer);
let _ = input.visit(&mut reducer).map_err(|e| e.to_string())?;
reducer.output()
}

View File

@ -4,7 +4,10 @@ use crate::ast::*;
pub type VResult = Result<(), Box<dyn Error>>;
pub trait ASTVisitor {
fn visit_expression(&mut self, _: &mut Expression) -> VResult {
fn post_ast(&mut self, _: &mut AST) -> VResult {
Ok(())
}
fn post_expression(&mut self, _: &mut Expression) -> VResult {
Ok(())
}
}
@ -13,16 +16,63 @@ pub trait Visitable {
fn visit(&mut self, v: &mut dyn ASTVisitor) -> VResult;
}
impl<T: Visitable> Visitable for Option<T> {
fn visit(&mut self, v: &mut dyn ASTVisitor) -> VResult {
match self {
Some(t) => t.visit(v),
None => Ok(())
}
}
}
impl Visitable for AST {
fn visit(&mut self, v: &mut dyn ASTVisitor) -> VResult {
for s in self.0.iter_mut() {
s.node_mut().visit(v)?
}
Ok(())
v.post_ast(self)
}
}
impl Visitable for Statement {
fn visit(&mut self, v: &mut dyn ASTVisitor) -> VResult {
match self {
Statement::ExpressionStatement(meta) => meta.node_mut().visit(v),
Statement::Declaration(decl) => decl.visit(v),
}
}
}
impl Visitable for Expression {
fn visit(&mut self, v: &mut dyn ASTVisitor) -> VResult {
let _ = self.kind.visit(v)?;
let _ = self.type_anno.visit(v)?;
v.post_expression(self)
}
}
impl Visitable for Declaration {
fn visit(&mut self, _v: &mut dyn ASTVisitor) -> VResult {
use Declaration::*;
Ok(match self {
FuncSig(sig) => (),
FuncDecl(sig, block) => (),
TypeDecl { .. } => (),
TypeAlias(_, _) => (),
Binding { .. } => (),
Impl { .. } => (),
Interface { .. } => (),
})
}
}
impl Visitable for ExpressionKind {
fn visit(&mut self, _v: &mut dyn ASTVisitor) -> VResult {
Ok(())
}
}
impl Visitable for TypeIdentifier {
fn visit(&mut self, _v: &mut dyn ASTVisitor) -> VResult {
Ok(())
}