Some work
This commit is contained in:
parent
e00948cad9
commit
654e326c40
@ -1,2 +1,64 @@
|
||||
|
||||
use ast::*;
|
||||
|
||||
pub fn dispatch<T, V: ASTVisitor<T>>(visitor: &mut V, ast: &AST) -> T {
|
||||
visitor.ast(ast);
|
||||
for statement in ast.0.iter() {
|
||||
visitor.statement(statement);
|
||||
match statement {
|
||||
Statement::ExpressionStatement(expr) => visitor.expression(expr),
|
||||
Statement::Declaration(decl) => {
|
||||
visitor.declaration(decl);
|
||||
match decl {
|
||||
Declaration::FuncSig(sig) => visitor.func_signature(sig),
|
||||
Declaration::FuncDecl(sig, block) => visitor.func_declaration(sig, block),
|
||||
Declaration::TypeDecl { .. } => unimplemented!(),
|
||||
Declaration::TypeAlias(..) => unimplemented!(),
|
||||
Declaration::Binding { .. } => unimplemented!(),
|
||||
Declaration::Impl { .. } => unimplemented!(),
|
||||
Declaration::Interface { .. } => unimplemented!(),
|
||||
};
|
||||
visitor.decl_done();
|
||||
}
|
||||
}
|
||||
}
|
||||
visitor.done()
|
||||
}
|
||||
|
||||
pub trait ASTVisitor<T> {
|
||||
fn ast(&mut self, ast: &AST) {
|
||||
}
|
||||
fn done(&mut self) -> T;
|
||||
|
||||
fn statement(&mut self, statement: &Statement) {
|
||||
}
|
||||
|
||||
fn declaration(&mut self, statement: &Declaration) {
|
||||
}
|
||||
|
||||
fn decl_done(&mut self) -> T;
|
||||
|
||||
fn func_signature(&mut self, sig: &Signature) {
|
||||
}
|
||||
|
||||
fn func_declaration(&mut self, sig: &Signature, block: &Vec<Statement>) {
|
||||
}
|
||||
|
||||
fn expression(&mut self, statement: &Expression) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/*----*/
|
||||
|
||||
|
||||
struct NullVisitor { }
|
||||
impl ASTVisitor<()> for NullVisitor {
|
||||
fn done(&mut self) -> () {
|
||||
()
|
||||
}
|
||||
|
||||
fn decl_done(&mut self) -> () {
|
||||
()
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user