Add block enter/exit methods
This commit is contained in:
parent
5bbbcfa676
commit
e0df4bda86
@ -6,7 +6,8 @@ use crate::ast::*;
|
|||||||
|
|
||||||
pub trait ASTVisitor: Sized {
|
pub trait ASTVisitor: Sized {
|
||||||
fn ast(&mut self, _ast: &AST) {}
|
fn ast(&mut self, _ast: &AST) {}
|
||||||
fn block(&mut self, _statements: &Vec<Statement>) {}
|
fn enter_block(&mut self, _statements: &Vec<Statement>) {}
|
||||||
|
fn exit_block(&mut self, _statements: &Vec<Statement>) {}
|
||||||
fn statement(&mut self, _statement: &Statement) {}
|
fn statement(&mut self, _statement: &Statement) {}
|
||||||
fn declaration(&mut self, _declaration: &Declaration) {}
|
fn declaration(&mut self, _declaration: &Declaration) {}
|
||||||
fn signature(&mut self, _signature: &Signature) {}
|
fn signature(&mut self, _signature: &Signature) {}
|
||||||
@ -55,9 +56,17 @@ pub trait ExpressionVisitor {
|
|||||||
fn string_literal(&mut self, _value: &Rc<String>) -> Self::Output;
|
fn string_literal(&mut self, _value: &Rc<String>) -> Self::Output;
|
||||||
fn binexp(&mut self, _op: &BinOp, _lhs_resul: Self::Output, _rhs_result: Self::Output) -> Self::Output;
|
fn binexp(&mut self, _op: &BinOp, _lhs_resul: Self::Output, _rhs_result: Self::Output) -> Self::Output;
|
||||||
fn tuple_literal(&mut self, _items: Vec<Self::Output>) -> Self::Output;
|
fn tuple_literal(&mut self, _items: Vec<Self::Output>) -> Self::Output;
|
||||||
|
fn visit_statement(&mut self) -> StatementVisitor<Output=Self::Output>;
|
||||||
fn done(&mut self, kind: Self::Output, anno: Option<Self::Output>) -> Self::Output;
|
fn done(&mut self, kind: Self::Output, anno: Option<Self::Output>) -> Self::Output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub trait StatementVisitor {
|
||||||
|
type Output;
|
||||||
|
|
||||||
|
fn expression(&mut self) -> Self::Output;
|
||||||
|
fn declaration(&mut self, &Declaration) -> Self::Output;
|
||||||
|
}
|
||||||
|
|
||||||
pub fn dispatch_expression<T>(input: &Expression, visitor: &mut dyn ExpressionVisitor<Output=T>) -> Result<T, String> {
|
pub fn dispatch_expression<T>(input: &Expression, visitor: &mut dyn ExpressionVisitor<Output=T>) -> Result<T, String> {
|
||||||
|
|
||||||
let output = match input.kind {
|
let output = match input.kind {
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
#![allow(dead_code)]
|
#![allow(dead_code)]
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use crate::ast::*;
|
use crate::ast::*;
|
||||||
use crate::ast::visitor::ASTVisitor;
|
use crate::ast::visitor::{ASTVisitor, BlockEntry};
|
||||||
use crate::util::deref_optional_box;
|
use crate::util::deref_optional_box;
|
||||||
|
|
||||||
pub fn walk_ast<V: ASTVisitor>(v: &mut V, ast: &AST) {
|
pub fn walk_ast<V: ASTVisitor>(v: &mut V, ast: &AST) {
|
||||||
@ -10,10 +10,12 @@ pub fn walk_ast<V: ASTVisitor>(v: &mut V, ast: &AST) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn walk_block<V: ASTVisitor>(v: &mut V, block: &Vec<Statement>) {
|
fn walk_block<V: ASTVisitor>(v: &mut V, block: &Vec<Statement>) {
|
||||||
|
v.enter_block(&block);
|
||||||
for s in block {
|
for s in block {
|
||||||
v.statement(s);
|
v.statement(s);
|
||||||
statement(v, s);
|
statement(v, s);
|
||||||
}
|
}
|
||||||
|
v.exit_block(&block);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn statement<V: ASTVisitor>(v: &mut V, statement: &Statement) {
|
fn statement<V: ASTVisitor>(v: &mut V, statement: &Statement) {
|
||||||
@ -44,7 +46,6 @@ fn declaration<V: ASTVisitor>(v: &mut V, decl: &Declaration) {
|
|||||||
},
|
},
|
||||||
FuncDecl(sig, block) => {
|
FuncDecl(sig, block) => {
|
||||||
v.signature(&sig);
|
v.signature(&sig);
|
||||||
v.block(&block);
|
|
||||||
walk_block(v, 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),
|
||||||
@ -123,7 +124,6 @@ fn lambda<V: ASTVisitor>(v: &mut V, params: &Vec<FormalParam>, type_anno: Option
|
|||||||
formal_param(v, param);
|
formal_param(v, param);
|
||||||
}
|
}
|
||||||
v.type_annotation(type_anno);
|
v.type_annotation(type_anno);
|
||||||
v.block(body);
|
|
||||||
walk_block(v, body);
|
walk_block(v, body);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -234,7 +234,6 @@ fn condition_arm<V: ASTVisitor>(v: &mut V, arm: &ConditionArm) {
|
|||||||
v.expression(guard);
|
v.expression(guard);
|
||||||
expression(v, guard);
|
expression(v, guard);
|
||||||
});
|
});
|
||||||
v.block(&arm.body);
|
|
||||||
walk_block(v, &arm.body);
|
walk_block(v, &arm.body);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user