Starting to implement ASTVisitor

This commit is contained in:
greg 2019-07-11 18:57:54 -07:00
parent d043036c61
commit a187d43fc1
2 changed files with 29 additions and 3 deletions

View File

@ -19,6 +19,10 @@ impl<T> Meta<T> {
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

View File

@ -1,7 +1,29 @@
use std::error::Error;
use crate::ast::*;
pub type VResult<T> = Result<T, Box<dyn Error>>;
pub trait Visitor {
pub type VResult = Result<(), Box<dyn Error>>;
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(())
}
}