Skip to content

Commit

Permalink
feat(ast): add trait for Visitor pattern.
Browse files Browse the repository at this point in the history
  • Loading branch information
rzvxa committed Mar 10, 2024
1 parent ebca65f commit 28d5c9a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions crates/fuse-ast/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod ast;
mod ast_factory;
mod precedence;
mod visit;

pub use ast::*;
pub use ast_factory::*;
Expand Down
27 changes: 27 additions & 0 deletions crates/fuse-ast/src/visit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// based on https://rust-unofficial.github.io/patterns/patterns/behavioural/visitor.html

use crate::ast::*;

macro_rules! visit_list {
($visitor:ident, $method:ident, $list:expr) => {
todo!()
};
}

pub trait Visitor<'ast>: Sized {
fn visit_chunk(&mut self, chunk: &'ast Chunk) {
walk_block(self, &chunk.body)
}

fn visit_statement(&mut self, statement: &'ast Statement) {
walk_statement(self, &statement)
}
}

pub fn walk_block<'ast, V: Visitor<'ast>>(visitor: &mut V, block: &'ast Block) {
visit_list!(visitor, visit_statement, block.statements)
}

pub fn walk_statement<'ast, V: Visitor<'ast>>(visitor: &mut V, statement: &'ast Statement) {
todo!()
}

0 comments on commit 28d5c9a

Please sign in to comment.