From f79125e9df8335a9367251156702bd2ea2754c9e Mon Sep 17 00:00:00 2001 From: greg Date: Sat, 17 Nov 2018 18:38:53 -0800 Subject: [PATCH] more modules --- schala-lang/language/src/ast_visitor.rs | 77 +++++++++++++++---------- 1 file changed, 46 insertions(+), 31 deletions(-) diff --git a/schala-lang/language/src/ast_visitor.rs b/schala-lang/language/src/ast_visitor.rs index 14be077..d3c09f8 100644 --- a/schala-lang/language/src/ast_visitor.rs +++ b/schala-lang/language/src/ast_visitor.rs @@ -7,24 +7,12 @@ pub fn dispatch>(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>(visitor: &mut V, ast: &AST) -> T { pub trait ASTVisitor { type EV : ExpressionVisitor; + type DV : DeclarationVisitor; fn ast(&mut self, ast: &AST) { } @@ -40,10 +29,37 @@ pub trait ASTVisitor { 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 { + 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 { + 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 { fn func_declaration(&mut self, sig: &Signature, block: &Vec) { } - fn expression(&mut self) -> Self::EV; -} - -pub fn dispatch_expression>(visitor: &mut V, expr: &Expression) -> T { - visitor.expression(expr); - visitor.done() -} - -pub trait ExpressionVisitor { - 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 { 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) -> () { + () + } +} +