Starting to add some more structure
This commit is contained in:
parent
654e326c40
commit
4ad5739615
@ -5,17 +5,23 @@ pub fn dispatch<T, V: ASTVisitor<T>>(visitor: &mut V, ast: &AST) -> T {
|
|||||||
for statement in ast.0.iter() {
|
for statement in ast.0.iter() {
|
||||||
visitor.statement(statement);
|
visitor.statement(statement);
|
||||||
match 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) => {
|
Statement::Declaration(decl) => {
|
||||||
visitor.declaration(decl);
|
visitor.declaration(decl);
|
||||||
match decl {
|
match decl {
|
||||||
Declaration::FuncSig(sig) => visitor.func_signature(sig),
|
Declaration::FuncSig(sig) => visitor.func_signature(sig),
|
||||||
Declaration::FuncDecl(sig, block) => visitor.func_declaration(sig, block),
|
Declaration::FuncDecl(sig, block) => visitor.func_declaration(sig, block),
|
||||||
|
_ => unimplemented!(),
|
||||||
|
/*
|
||||||
Declaration::TypeDecl { .. } => unimplemented!(),
|
Declaration::TypeDecl { .. } => unimplemented!(),
|
||||||
Declaration::TypeAlias(..) => unimplemented!(),
|
Declaration::TypeAlias(..) => unimplemented!(),
|
||||||
Declaration::Binding { .. } => unimplemented!(),
|
Declaration::Binding { .. } => unimplemented!(),
|
||||||
Declaration::Impl { .. } => unimplemented!(),
|
Declaration::Impl { .. } => unimplemented!(),
|
||||||
Declaration::Interface { .. } => unimplemented!(),
|
Declaration::Interface { .. } => unimplemented!(),
|
||||||
|
*/
|
||||||
};
|
};
|
||||||
visitor.decl_done();
|
visitor.decl_done();
|
||||||
}
|
}
|
||||||
@ -25,6 +31,8 @@ pub fn dispatch<T, V: ASTVisitor<T>>(visitor: &mut V, ast: &AST) -> T {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub trait ASTVisitor<T> {
|
pub trait ASTVisitor<T> {
|
||||||
|
type EV : ExpressionVisitor<T>;
|
||||||
|
|
||||||
fn ast(&mut self, ast: &AST) {
|
fn ast(&mut self, ast: &AST) {
|
||||||
}
|
}
|
||||||
fn done(&mut self) -> T;
|
fn done(&mut self) -> T;
|
||||||
@ -43,9 +51,18 @@ pub trait ASTVisitor<T> {
|
|||||||
fn func_declaration(&mut self, sig: &Signature, block: &Vec<Statement>) {
|
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 { }
|
struct NullVisitor { }
|
||||||
impl ASTVisitor<()> for NullVisitor {
|
impl ASTVisitor<()> for NullVisitor {
|
||||||
|
type EV = NullVisitor;
|
||||||
fn done(&mut self) -> () {
|
fn done(&mut self) -> () {
|
||||||
()
|
()
|
||||||
}
|
}
|
||||||
@ -60,5 +78,15 @@ impl ASTVisitor<()> for NullVisitor {
|
|||||||
fn decl_done(&mut self) -> () {
|
fn decl_done(&mut self) -> () {
|
||||||
()
|
()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn expression(&mut self) -> Self::EV {
|
||||||
|
NullVisitor { }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ExpressionVisitor<()> for NullVisitor {
|
||||||
|
fn done(&mut self) -> () {
|
||||||
|
()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user