more modules

This commit is contained in:
greg 2018-11-17 18:38:53 -08:00
parent 4ad5739615
commit f79125e9df

View File

@ -7,24 +7,12 @@ pub fn dispatch<T, V: ASTVisitor<T>>(visitor: &mut V, ast: &AST) -> T {
match statement {
Statement::ExpressionStatement(expr) => {
let ref mut expression_visitor = visitor.expression();
dispatch_expression(expression_visitor, expr);
ExpressionVisitor::dispatch(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();
}
let ref mut declaration_visitor = visitor.declaration();
DeclarationVisitor::dispatch(declaration_visitor, decl);
},
}
}
visitor.done()
@ -32,6 +20,7 @@ pub fn dispatch<T, V: ASTVisitor<T>>(visitor: &mut V, ast: &AST) -> T {
pub trait ASTVisitor<T> {
type EV : ExpressionVisitor<T>;
type DV : DeclarationVisitor<T>;
fn ast(&mut self, ast: &AST) {
}
@ -40,10 +29,37 @@ pub trait ASTVisitor<T> {
fn statement(&mut self, statement: &Statement) {
}
fn declaration(&mut self, statement: &Declaration) {
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()
}
fn decl_done(&mut self) -> T;
fn declaration(&mut self, decl: &Declaration) {
}
fn func_signature(&mut self, sig: &Signature) {
}
@ -51,17 +67,9 @@ pub trait ASTVisitor<T> {
fn func_declaration(&mut self, sig: &Signature, block: &Vec<Statement>) {
}
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 type_declaration(&mut self, name: &TypeSingletonName, body: &TypeBody, mutable: &bool) {
}
fn done(&mut self) -> T;
}
@ -71,15 +79,16 @@ pub trait ExpressionVisitor<T> {
struct NullVisitor { }
impl ASTVisitor<()> for NullVisitor {
type EV = NullVisitor;
type DV = NullVisitor;
fn done(&mut self) -> () {
()
}
fn decl_done(&mut self) -> () {
()
fn expression(&mut self) -> Self::EV {
NullVisitor { }
}
fn expression(&mut self) -> Self::EV {
fn declaration(&mut self) -> Self::DV {
NullVisitor { }
}
}
@ -90,3 +99,9 @@ impl ExpressionVisitor<()> for NullVisitor {
}
}
impl DeclarationVisitor<()> for NullVisitor {
fn done(&mut self) -> () {
()
}
}