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