Start moving all walking logic out of visitor

This commit is contained in:
greg 2019-09-28 01:01:56 -07:00
parent 89649273d8
commit f654cd6b50
3 changed files with 27 additions and 24 deletions

View File

@ -8,27 +8,17 @@ use crate::ast::walker;
//TODO default implmentations should call walk methods - then I can test printing
pub trait ASTVisitor: Sized {
fn visit(&mut self, ast: &AST) {
println!("FUCK");
walker::ast(self, ast);
fn ast(&mut self, ast: &AST) {
}
fn block(&mut self, statements: &Vec<Statement>) {
println!("oi");
walker::block(self, statements);
}
fn block(&mut self, statements: &Vec<Statement>) {}
fn statement(&mut self, statement: &Statement) {
println!("stmt");
walker::statement(self, statement);
}
fn statement(&mut self, statement: &Statement) {}
fn declaration(&mut self, declaration: &Declaration) {
walker::declaration(self, declaration);
}
fn signature(&mut self, signature: &Signature) {
walker::signature(self, signature);
}
fn type_declaration(&mut self, name: &TypeSingletonName, body: &TypeBody, mutable: bool) {
@ -38,8 +28,6 @@ pub trait ASTVisitor: Sized {
}
fn binding(&mut self, name: &Rc<String>, constant: bool, type_anno: Option<&TypeIdentifier>, expr: &Expression) {
walker::maybe_type_identifier(self, type_anno);
walker::expression(self, expr);
}
fn implemention(&mut self, type_name: &TypeIdentifier, interface_name: Option<&TypeSingletonName>, block: &Vec<Declaration>) {
@ -49,7 +37,6 @@ pub trait ASTVisitor: Sized {
}
fn expression(&mut self, expression: &Expression) {
walker::expression(self, expression);
}
fn expression_kind(&mut self, kind: &ExpressionKind) {

View File

@ -27,11 +27,15 @@ import gragh
let a = 20 + 84
let b = 28 + 1 + 2 + 2.0
fn heh() {
let m = 9
}
"#);
tester.visit(&ast);
walker::ast(&mut tester, &ast);
assert_eq!(tester.count, 5);
assert_eq!(tester.count, 6);
assert_eq!(tester.float_count, 1);
}

View File

@ -4,20 +4,28 @@ use crate::ast::visitor::ASTVisitor;
use std::ops::Deref;
pub fn ast<V: ASTVisitor>(v: &mut V, ast: &AST) {
v.block(&ast.statements);
v.ast(ast);
walk_block(v, &ast.statements);
}
pub fn block<V: ASTVisitor>(v: &mut V, block: &Vec<Statement>) {
for statement in block {
v.statement(statement);
pub fn walk_block<V: ASTVisitor>(v: &mut V, block: &Vec<Statement>) {
for s in block {
v.statement(s);
statement(v, s);
}
}
pub fn statement<V: ASTVisitor>(v: &mut V, statement: &Statement) {
use StatementKind::*;
match statement.kind {
Expression(ref expr) => v.expression(expr),
Declaration(ref decl) => v.declaration(decl),
Expression(ref expr) => {
v.expression(expr);
expression(v, expr);
},
Declaration(ref decl) => {
v.declaration(decl);
declaration(v, decl);
},
Import(ref import_spec) => v.import(import_spec),
}
}
@ -27,15 +35,19 @@ pub fn declaration<V: ASTVisitor>(v: &mut V, decl: &Declaration) {
match decl {
FuncSig(sig) => {
v.signature(&sig);
signature(v, &sig);
},
FuncDecl(sig, block) => {
v.signature(&sig);
v.block(&block);
walk_block(v, block);
},
TypeDecl { name, body, mutable } => v.type_declaration(name, body, *mutable),
TypeAlias(n, a) => v.type_alias(n, a),
Binding { name, constant, type_anno, expr } => {
v.binding(name, *constant, type_anno.as_ref(), expr);
maybe_type_identifier(v, type_anno.as_ref());
expression(v, &expr);
},
Impl { type_name, interface_name, block } => {
v.implemention(type_name, interface_name.as_ref(), block);