From 4ad5739615b53b5c346e208eeec846d05da241e1 Mon Sep 17 00:00:00 2001 From: greg Date: Sat, 17 Nov 2018 18:15:20 -0800 Subject: [PATCH] Starting to add some more structure --- schala-lang/language/src/ast_visitor.rs | 34 ++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/schala-lang/language/src/ast_visitor.rs b/schala-lang/language/src/ast_visitor.rs index 2c2ee17..14be077 100644 --- a/schala-lang/language/src/ast_visitor.rs +++ b/schala-lang/language/src/ast_visitor.rs @@ -5,17 +5,23 @@ pub fn dispatch>(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>(visitor: &mut V, ast: &AST) -> T { } pub trait ASTVisitor { + type EV : ExpressionVisitor; + fn ast(&mut self, ast: &AST) { } fn done(&mut self) -> T; @@ -43,9 +51,18 @@ pub trait ASTVisitor { fn func_declaration(&mut self, sig: &Signature, block: &Vec) { } - fn expression(&mut self, statement: &Expression) { - } + 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 done(&mut self) -> T; } /*----*/ @@ -53,6 +70,7 @@ pub trait ASTVisitor { 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) -> () { + () + } }