Starting to add some more structure

This commit is contained in:
greg 2018-11-17 18:15:20 -08:00
parent 654e326c40
commit 4ad5739615
1 changed files with 31 additions and 3 deletions

View File

@ -5,17 +5,23 @@ pub fn dispatch<T, V: ASTVisitor<T>>(visitor: &mut V, ast: &AST) -> T {
for statement in ast.0.iter() {
visitor.statement(statement);
match statement {
Statement::ExpressionStatement(expr) => visitor.expression(expr),
Statement::ExpressionStatement(expr) => {
let ref mut expression_visitor = visitor.expression();
dispatch_expression(expression_visitor, 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),
_ => unimplemented!(),
/*
Declaration::TypeDecl { .. } => unimplemented!(),
Declaration::TypeAlias(..) => unimplemented!(),
Declaration::Binding { .. } => unimplemented!(),
Declaration::Impl { .. } => unimplemented!(),
Declaration::Interface { .. } => unimplemented!(),
*/
};
visitor.decl_done();
}
@ -25,6 +31,8 @@ pub fn dispatch<T, V: ASTVisitor<T>>(visitor: &mut V, ast: &AST) -> T {
}
pub trait ASTVisitor<T> {
type EV : ExpressionVisitor<T>;
fn ast(&mut self, ast: &AST) {
}
fn done(&mut self) -> T;
@ -43,9 +51,18 @@ pub trait ASTVisitor<T> {
fn func_declaration(&mut self, sig: &Signature, block: &Vec<Statement>) {
}
fn expression(&mut self, statement: &Expression) {
}
fn expression(&mut self) -> Self::EV;
}
pub fn dispatch_expression<T, V: ExpressionVisitor<T>>(visitor: &mut V, expr: &Expression) -> T {
visitor.expression(expr);
visitor.done()
}
pub trait ExpressionVisitor<T> {
fn expression(&mut self, expression: &Expression) {
}
fn done(&mut self) -> T;
}
/*----*/
@ -53,6 +70,7 @@ pub trait ASTVisitor<T> {
struct NullVisitor { }
impl ASTVisitor<()> for NullVisitor {
type EV = NullVisitor;
fn done(&mut self) -> () {
()
}
@ -60,5 +78,15 @@ impl ASTVisitor<()> for NullVisitor {
fn decl_done(&mut self) -> () {
()
}
fn expression(&mut self) -> Self::EV {
NullVisitor { }
}
}
impl ExpressionVisitor<()> for NullVisitor {
fn done(&mut self) -> () {
()
}
}