From a187d43fc1071c4d44ff19acc5a1b84d3575c72e Mon Sep 17 00:00:00 2001 From: greg Date: Thu, 11 Jul 2019 18:57:54 -0700 Subject: [PATCH] Starting to implement ASTVisitor --- schala-lang/language/src/ast.rs | 4 ++++ schala-lang/language/src/visitor.rs | 28 +++++++++++++++++++++++++--- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/schala-lang/language/src/ast.rs b/schala-lang/language/src/ast.rs index 0b9e07b..c53cbaa 100644 --- a/schala-lang/language/src/ast.rs +++ b/schala-lang/language/src/ast.rs @@ -19,6 +19,10 @@ impl Meta { pub fn node(&self) -> &T { &self.n } + + pub fn node_mut(&mut self) -> &mut T { + &mut self.n + } } //TODO this PartialEq is here to make tests work - find a way to make it not necessary diff --git a/schala-lang/language/src/visitor.rs b/schala-lang/language/src/visitor.rs index 6fc14c0..4f93143 100644 --- a/schala-lang/language/src/visitor.rs +++ b/schala-lang/language/src/visitor.rs @@ -1,7 +1,29 @@ use std::error::Error; +use crate::ast::*; -pub type VResult = Result>; - -pub trait Visitor { +pub type VResult = Result<(), Box>; +pub trait ASTVisitor { + fn visit_expression(&mut self, _: &mut Expression) -> VResult { + Ok(()) + } +} + +pub trait Visitable { + fn visit(&mut self, v: &mut dyn ASTVisitor) -> VResult; +} + +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(()) + } +} + +impl Visitable for Statement { + fn visit(&mut self, _v: &mut dyn ASTVisitor) -> VResult { + Ok(()) + } }