From 654e326c408363cf5c1371e416a4ac78bbdb31cf Mon Sep 17 00:00:00 2001 From: greg Date: Sat, 17 Nov 2018 17:41:11 -0800 Subject: [PATCH] Some work --- schala-lang/language/src/ast_visitor.rs | 64 ++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/schala-lang/language/src/ast_visitor.rs b/schala-lang/language/src/ast_visitor.rs index 3a0f335..2c2ee17 100644 --- a/schala-lang/language/src/ast_visitor.rs +++ b/schala-lang/language/src/ast_visitor.rs @@ -1,2 +1,64 @@ - use ast::*; + +pub fn dispatch>(visitor: &mut V, ast: &AST) -> T { + visitor.ast(ast); + for statement in ast.0.iter() { + visitor.statement(statement); + match statement { + Statement::ExpressionStatement(expr) => visitor.expression(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), + Declaration::TypeDecl { .. } => unimplemented!(), + Declaration::TypeAlias(..) => unimplemented!(), + Declaration::Binding { .. } => unimplemented!(), + Declaration::Impl { .. } => unimplemented!(), + Declaration::Interface { .. } => unimplemented!(), + }; + visitor.decl_done(); + } + } + } + visitor.done() +} + +pub trait ASTVisitor { + fn ast(&mut self, ast: &AST) { + } + fn done(&mut self) -> T; + + fn statement(&mut self, statement: &Statement) { + } + + fn declaration(&mut self, statement: &Declaration) { + } + + fn decl_done(&mut self) -> T; + + fn func_signature(&mut self, sig: &Signature) { + } + + fn func_declaration(&mut self, sig: &Signature, block: &Vec) { + } + + fn expression(&mut self, statement: &Expression) { + } + +} + +/*----*/ + + +struct NullVisitor { } +impl ASTVisitor<()> for NullVisitor { + fn done(&mut self) -> () { + () + } + + fn decl_done(&mut self) -> () { + () + } +} +