From b0dd1a6527dea3658e9d0286ef8c07eace5b6743 Mon Sep 17 00:00:00 2001 From: greg Date: Fri, 12 Jul 2019 01:51:51 -0700 Subject: [PATCH] More visitor stuff --- schala-lang/language/src/reduced_ast.rs | 2 +- schala-lang/language/src/visitor.rs | 54 ++++++++++++++++++++++++- 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/schala-lang/language/src/reduced_ast.rs b/schala-lang/language/src/reduced_ast.rs index 5737587..2531c74 100644 --- a/schala-lang/language/src/reduced_ast.rs +++ b/schala-lang/language/src/reduced_ast.rs @@ -115,7 +115,7 @@ impl ASTVisitor for Reducer { pub fn perform_reduction(mut input: AST, _symbol_table: &SymbolTable) -> Result { let mut reducer = Reducer::new(); - input.visit(&mut reducer); + let _ = input.visit(&mut reducer).map_err(|e| e.to_string())?; reducer.output() } diff --git a/schala-lang/language/src/visitor.rs b/schala-lang/language/src/visitor.rs index 4f93143..ddda82a 100644 --- a/schala-lang/language/src/visitor.rs +++ b/schala-lang/language/src/visitor.rs @@ -4,7 +4,10 @@ use crate::ast::*; pub type VResult = Result<(), Box>; 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 Visitable for Option { + 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(()) }