2018-11-17 02:09:10 -08:00
|
|
|
use ast::*;
|
2018-11-17 17:41:11 -08:00
|
|
|
|
|
|
|
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 {
|
2018-11-17 18:15:20 -08:00
|
|
|
Statement::ExpressionStatement(expr) => {
|
|
|
|
let ref mut expression_visitor = visitor.expression();
|
2018-11-17 18:38:53 -08:00
|
|
|
ExpressionVisitor::dispatch(expression_visitor, expr);
|
2018-11-17 18:15:20 -08:00
|
|
|
},
|
2018-11-17 17:41:11 -08:00
|
|
|
Statement::Declaration(decl) => {
|
2018-11-17 18:38:53 -08:00
|
|
|
let ref mut declaration_visitor = visitor.declaration();
|
|
|
|
DeclarationVisitor::dispatch(declaration_visitor, decl);
|
|
|
|
},
|
2018-11-17 17:41:11 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
visitor.done()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait ASTVisitor<T> {
|
2018-11-17 18:15:20 -08:00
|
|
|
type EV : ExpressionVisitor<T>;
|
2018-11-17 18:38:53 -08:00
|
|
|
type DV : DeclarationVisitor<T>;
|
2018-11-17 18:15:20 -08:00
|
|
|
|
2018-11-17 17:41:11 -08:00
|
|
|
fn ast(&mut self, ast: &AST) {
|
|
|
|
}
|
|
|
|
fn done(&mut self) -> T;
|
|
|
|
|
|
|
|
fn statement(&mut self, statement: &Statement) {
|
|
|
|
}
|
|
|
|
|
2018-11-17 18:38:53 -08:00
|
|
|
fn expression(&mut self) -> Self::EV;
|
|
|
|
fn declaration(&mut self) -> Self::DV;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait ExpressionVisitor<T> {
|
|
|
|
fn dispatch(visitor: &mut Self, expr: &Expression) -> T {
|
|
|
|
visitor.expression(expr);
|
|
|
|
visitor.done()
|
|
|
|
}
|
|
|
|
fn expression(&mut self, expression: &Expression) {
|
|
|
|
}
|
|
|
|
fn done(&mut self) -> T;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait DeclarationVisitor<T> {
|
|
|
|
fn dispatch(visitor: &mut Self, decl: &Declaration) -> T {
|
|
|
|
use self::Declaration::*;
|
|
|
|
match decl {
|
|
|
|
FuncSig(sig) => visitor.func_signature(sig),
|
|
|
|
FuncDecl(sig, block) => visitor.func_declaration(sig, block),
|
|
|
|
TypeDecl { name, body, mutable } => visitor.type_declaration(name, body, mutable),
|
|
|
|
TypeAlias(..) => unimplemented!(),
|
|
|
|
Binding { .. } => unimplemented!(),
|
|
|
|
Impl { .. } => unimplemented!(),
|
|
|
|
Interface { .. } => unimplemented!(),
|
|
|
|
};
|
|
|
|
visitor.done()
|
2018-11-17 17:41:11 -08:00
|
|
|
}
|
|
|
|
|
2018-11-17 18:38:53 -08:00
|
|
|
fn declaration(&mut self, decl: &Declaration) {
|
|
|
|
}
|
2018-11-17 17:41:11 -08:00
|
|
|
|
|
|
|
fn func_signature(&mut self, sig: &Signature) {
|
|
|
|
}
|
|
|
|
|
|
|
|
fn func_declaration(&mut self, sig: &Signature, block: &Vec<Statement>) {
|
|
|
|
}
|
|
|
|
|
2018-11-17 18:38:53 -08:00
|
|
|
fn type_declaration(&mut self, name: &TypeSingletonName, body: &TypeBody, mutable: &bool) {
|
2018-11-17 18:15:20 -08:00
|
|
|
}
|
2018-11-17 18:38:53 -08:00
|
|
|
|
2018-11-17 18:15:20 -08:00
|
|
|
fn done(&mut self) -> T;
|
2018-11-17 17:41:11 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*----*/
|
|
|
|
|
|
|
|
|
|
|
|
struct NullVisitor { }
|
|
|
|
impl ASTVisitor<()> for NullVisitor {
|
2018-11-17 18:15:20 -08:00
|
|
|
type EV = NullVisitor;
|
2018-11-17 18:38:53 -08:00
|
|
|
type DV = NullVisitor;
|
2018-11-17 17:41:11 -08:00
|
|
|
fn done(&mut self) -> () {
|
|
|
|
()
|
|
|
|
}
|
|
|
|
|
2018-11-17 18:38:53 -08:00
|
|
|
fn expression(&mut self) -> Self::EV {
|
|
|
|
NullVisitor { }
|
2018-11-17 17:41:11 -08:00
|
|
|
}
|
2018-11-17 18:15:20 -08:00
|
|
|
|
2018-11-17 18:38:53 -08:00
|
|
|
fn declaration(&mut self) -> Self::DV {
|
2018-11-17 18:15:20 -08:00
|
|
|
NullVisitor { }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ExpressionVisitor<()> for NullVisitor {
|
|
|
|
fn done(&mut self) -> () {
|
|
|
|
()
|
|
|
|
}
|
2018-11-17 17:41:11 -08:00
|
|
|
}
|
|
|
|
|
2018-11-17 18:38:53 -08:00
|
|
|
impl DeclarationVisitor<()> for NullVisitor {
|
|
|
|
fn done(&mut self) -> () {
|
|
|
|
()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|