More visitor stuff

This commit is contained in:
greg 2019-09-27 09:54:24 -07:00
parent c8804eeefb
commit 9fa4e3797c
2 changed files with 77 additions and 35 deletions

View File

@ -49,12 +49,39 @@ pub trait ASTVisitor: Sized {
}
fn named_struct(&mut self, name: &QualifiedName, fields: &Vec<(Rc<String>, Expression)>) {
self.qualified_name(name);
for (_, expr) in fields.iter() {
walker::expression(self, expr);
}
walker::named_struct(self, name, fields);
}
fn call(&mut self, f: &Expression, arguments: &Vec<InvocationArgument>) {
walker::call(self, f, arguments);
}
fn index(&mut self, indexee: &Expression, indexers: &Vec<Expression>) {
walker::index(self, indexee, indexers);
}
fn if_expression(&mut self, discrim: &Discriminator, body: &IfExpressionBody) {
}
fn while_expression(&mut self, condition: Option<&Expression>, body: &Block) {
}
fn for_expression(&mut self, enumerators: &Vec<Enumerator>, body: &ForBody) {
}
fn lambda(&mut self, params: &Vec<FormalParam>, type_anno: Option<&TypeIdentifier>, body: &Block) {
walker::lambda(self, params, type_anno, body);
}
fn invocation_argument(&mut self, arg: &InvocationArgument) {
}
fn formal_param(&mut self, param: &FormalParam) {
walker::formal_param(self, param);
}
fn type_anno(&mut self, anno: &TypeIdentifier) { }
fn import(&mut self, import: &ImportSpecifier) {}
fn qualified_name(&mut self, name: &QualifiedName) {}
fn nat_literal(&mut self, n: u64) {}

View File

@ -1,6 +1,7 @@
use std::rc::Rc;
use crate::ast::*;
use crate::ast::visitor::ASTVisitor;
use std::ops::Deref;
pub fn ast<V: ASTVisitor>(v: &mut V, ast: &AST) {
v.block(&ast.statements);
@ -60,6 +61,40 @@ pub fn maybe_type_identifier<V: ASTVisitor>(v: &mut V, maybe_ty_identifier: Opti
}
pub fn call<V: ASTVisitor>(v: &mut V, f: &Expression, args: &Vec<InvocationArgument>) {
v.expression(f);
for arg in args.iter() {
v.invocation_argument(arg);
}
}
pub fn index<V: ASTVisitor>(v: &mut V, indexee: &Expression, indexers: &Vec<Expression>) {
v.expression(indexee);
for i in indexers.iter() {
v.expression(i);
}
}
pub fn named_struct<V: ASTVisitor>(v: &mut V, n: &QualifiedName, fields: &Vec<(Rc<String>, Expression)>) {
v.qualified_name(n);
for (_, expr) in fields.iter() {
v.expression(expr);
}
}
pub fn lambda<V: ASTVisitor>(v: &mut V, params: &Vec<FormalParam>, type_anno: Option<&TypeIdentifier>, body: &Block) {
for param in params {
v.formal_param(param);
}
type_anno.map(|anno| v.type_anno(anno));
v.block(body);
}
pub fn formal_param<V: ASTVisitor>(v: &mut V, param: &FormalParam) {
param.default.as_ref().map(|p| v.expression(p));
param.anno.as_ref().map(|a| v.type_anno(a));
}
pub fn expression_kind<V: ASTVisitor>(v: &mut V, expression_kind: &ExpressionKind) {
use ExpressionKind::*;
match expression_kind {
@ -76,37 +111,17 @@ pub fn expression_kind<V: ASTVisitor>(v: &mut V, expression_kind: &ExpressionKin
},
Value(name) => v.qualified_name(name),
NamedStruct { name, fields } => v.named_struct(name, fields),
_ => (),
Call { f, arguments } => v.call(f, arguments),
Index { indexee, indexers } => v.index(indexee, indexers),
IfExpression { discriminator, body } => v.if_expression(discriminator, body),
WhileExpression { condition, body } => v.while_expression(condition.as_ref().map(|b: &Box<Expression>| Deref::deref(b)), body),
ForExpression { enumerators, body } => v.for_expression(enumerators, body),
Lambda { params , type_anno, body } => v.lambda(params, type_anno.as_ref(), body),
ListLiteral(exprs) => {
for expr in exprs {
v.expression(expr);
}
},
}
/*
Call {
f: Box<Expression>,
arguments: Vec<InvocationArgument>,
},
Index {
indexee: Box<Expression>,
indexers: Vec<Expression>,
},
IfExpression {
discriminator: Box<Discriminator>,
body: Box<IfExpressionBody>,
},
WhileExpression {
condition: Option<Box<Expression>>,
body: Block,
},
ForExpression {
enumerators: Vec<Enumerator>,
body: Box<ForBody>,
},
Lambda {
params: Vec<FormalParam>,
type_anno: Option<TypeIdentifier>,
body: Block,
},
ListLiteral(Vec<Expression>),
}
*/
}