Start moving all walking logic out of visitor
This commit is contained in:
parent
89649273d8
commit
f654cd6b50
@ -8,27 +8,17 @@ use crate::ast::walker;
|
|||||||
|
|
||||||
//TODO default implmentations should call walk methods - then I can test printing
|
//TODO default implmentations should call walk methods - then I can test printing
|
||||||
pub trait ASTVisitor: Sized {
|
pub trait ASTVisitor: Sized {
|
||||||
fn visit(&mut self, ast: &AST) {
|
fn ast(&mut self, ast: &AST) {
|
||||||
println!("FUCK");
|
|
||||||
walker::ast(self, ast);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn block(&mut self, statements: &Vec<Statement>) {
|
fn block(&mut self, statements: &Vec<Statement>) {}
|
||||||
println!("oi");
|
|
||||||
walker::block(self, statements);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn statement(&mut self, statement: &Statement) {
|
fn statement(&mut self, statement: &Statement) {}
|
||||||
println!("stmt");
|
|
||||||
walker::statement(self, statement);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn declaration(&mut self, declaration: &Declaration) {
|
fn declaration(&mut self, declaration: &Declaration) {
|
||||||
walker::declaration(self, declaration);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn signature(&mut self, signature: &Signature) {
|
fn signature(&mut self, signature: &Signature) {
|
||||||
walker::signature(self, signature);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn type_declaration(&mut self, name: &TypeSingletonName, body: &TypeBody, mutable: bool) {
|
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) {
|
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>) {
|
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) {
|
fn expression(&mut self, expression: &Expression) {
|
||||||
walker::expression(self, expression);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn expression_kind(&mut self, kind: &ExpressionKind) {
|
fn expression_kind(&mut self, kind: &ExpressionKind) {
|
||||||
|
@ -27,11 +27,15 @@ import gragh
|
|||||||
|
|
||||||
let a = 20 + 84
|
let a = 20 + 84
|
||||||
let b = 28 + 1 + 2 + 2.0
|
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);
|
assert_eq!(tester.float_count, 1);
|
||||||
}
|
}
|
||||||
|
@ -4,20 +4,28 @@ use crate::ast::visitor::ASTVisitor;
|
|||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
|
|
||||||
pub fn ast<V: ASTVisitor>(v: &mut V, ast: &AST) {
|
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>) {
|
pub fn walk_block<V: ASTVisitor>(v: &mut V, block: &Vec<Statement>) {
|
||||||
for statement in block {
|
for s in block {
|
||||||
v.statement(statement);
|
v.statement(s);
|
||||||
|
statement(v, s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn statement<V: ASTVisitor>(v: &mut V, statement: &Statement) {
|
pub fn statement<V: ASTVisitor>(v: &mut V, statement: &Statement) {
|
||||||
use StatementKind::*;
|
use StatementKind::*;
|
||||||
match statement.kind {
|
match statement.kind {
|
||||||
Expression(ref expr) => v.expression(expr),
|
Expression(ref expr) => {
|
||||||
Declaration(ref decl) => v.declaration(decl),
|
v.expression(expr);
|
||||||
|
expression(v, expr);
|
||||||
|
},
|
||||||
|
Declaration(ref decl) => {
|
||||||
|
v.declaration(decl);
|
||||||
|
declaration(v, decl);
|
||||||
|
},
|
||||||
Import(ref import_spec) => v.import(import_spec),
|
Import(ref import_spec) => v.import(import_spec),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -27,15 +35,19 @@ pub fn declaration<V: ASTVisitor>(v: &mut V, decl: &Declaration) {
|
|||||||
match decl {
|
match decl {
|
||||||
FuncSig(sig) => {
|
FuncSig(sig) => {
|
||||||
v.signature(&sig);
|
v.signature(&sig);
|
||||||
|
signature(v, &sig);
|
||||||
},
|
},
|
||||||
FuncDecl(sig, block) => {
|
FuncDecl(sig, block) => {
|
||||||
v.signature(&sig);
|
v.signature(&sig);
|
||||||
v.block(&block);
|
v.block(&block);
|
||||||
|
walk_block(v, block);
|
||||||
},
|
},
|
||||||
TypeDecl { name, body, mutable } => v.type_declaration(name, body, *mutable),
|
TypeDecl { name, body, mutable } => v.type_declaration(name, body, *mutable),
|
||||||
TypeAlias(n, a) => v.type_alias(n, a),
|
TypeAlias(n, a) => v.type_alias(n, a),
|
||||||
Binding { name, constant, type_anno, expr } => {
|
Binding { name, constant, type_anno, expr } => {
|
||||||
v.binding(name, *constant, type_anno.as_ref(), 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 } => {
|
Impl { type_name, interface_name, block } => {
|
||||||
v.implemention(type_name, interface_name.as_ref(), block);
|
v.implemention(type_name, interface_name.as_ref(), block);
|
||||||
|
Loading…
Reference in New Issue
Block a user