Finish cleaning up visitor logic

This commit is contained in:
greg 2019-09-28 01:58:22 -07:00
parent f654cd6b50
commit 253b5d88f0
2 changed files with 67 additions and 103 deletions

View File

@ -8,93 +8,36 @@ 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 ast(&mut self, ast: &AST) { fn ast(&mut self, ast: &AST) {}
}
fn block(&mut self, statements: &Vec<Statement>) {} fn 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 type_declaration(&mut self, name: &TypeSingletonName, body: &TypeBody, mutable: bool) {}
fn type_alias(&mut self, old_name: &Rc<String>, new_name: &Rc<String>) {}
fn signature(&mut self, signature: &Signature) { fn binding(&mut self, name: &Rc<String>, constant: bool, type_anno: Option<&TypeIdentifier>, expr: &Expression) {}
} fn implemention(&mut self, type_name: &TypeIdentifier, interface_name: Option<&TypeSingletonName>, block: &Vec<Declaration>) {}
fn interface(&mut self, name: &Rc<String>, signatures: &Vec<Signature>) {}
fn type_declaration(&mut self, name: &TypeSingletonName, body: &TypeBody, mutable: bool) { fn expression(&mut self, expression: &Expression) {}
} fn expression_kind(&mut self, kind: &ExpressionKind) {}
fn type_annotation(&mut self, type_anno: Option<&TypeIdentifier>) {}
fn type_alias(&mut self, old_name: &Rc<String>, new_name: &Rc<String>) { fn named_struct(&mut self, name: &QualifiedName, fields: &Vec<(Rc<String>, Expression)>) {}
} fn call(&mut self, f: &Expression, arguments: &Vec<InvocationArgument>) {}
fn index(&mut self, indexee: &Expression, indexers: &Vec<Expression>) {}
fn binding(&mut self, name: &Rc<String>, constant: bool, type_anno: Option<&TypeIdentifier>, expr: &Expression) { 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 implemention(&mut self, type_name: &TypeIdentifier, interface_name: Option<&TypeSingletonName>, block: &Vec<Declaration>) { fn lambda(&mut self, params: &Vec<FormalParam>, type_anno: Option<&TypeIdentifier>, body: &Block) {}
} fn invocation_argument(&mut self, arg: &InvocationArgument) {}
fn formal_param(&mut self, param: &FormalParam) {}
fn interface(&mut self, name: &Rc<String>, signatures: &Vec<Signature>) {
}
fn expression(&mut self, expression: &Expression) {
}
fn expression_kind(&mut self, kind: &ExpressionKind) {
walker::expression_kind(self, kind);
}
fn maybe_type_identifier(&mut self, type_anno: Option<&TypeIdentifier>) {
walker::maybe_type_identifier(self, type_anno);
}
fn named_struct(&mut self, name: &QualifiedName, fields: &Vec<(Rc<String>, Expression)>) {
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 import(&mut self, import: &ImportSpecifier) {}
fn qualified_name(&mut self, name: &QualifiedName) {} fn qualified_name(&mut self, name: &QualifiedName) {}
fn nat_literal(&mut self, n: u64) {} fn nat_literal(&mut self, n: u64) {}
fn float_literal(&mut self, f: f64) {} fn float_literal(&mut self, f: f64) {}
fn string_literal(&mut self, s: &Rc<String>) {} fn string_literal(&mut self, s: &Rc<String>) {}
fn bool_literal(&mut self, b: bool) {} fn bool_literal(&mut self, b: bool) {}
fn binexp(&mut self, op: &BinOp, lhs: &Expression, rhs: &Expression) { fn binexp(&mut self, op: &BinOp, lhs: &Expression, rhs: &Expression) {}
walker::expression(self, lhs); fn prefix_exp(&mut self, op: &PrefixOp, arg: &Expression) {}
walker::expression(self, rhs);
}
fn prefix_exp(&mut self, op: &PrefixOp, arg: &Expression) {
walker::expression(self, arg);
}
} }

View File

@ -15,7 +15,7 @@ pub fn walk_block<V: ASTVisitor>(v: &mut V, block: &Vec<Statement>) {
} }
} }
pub fn statement<V: ASTVisitor>(v: &mut V, statement: &Statement) { fn statement<V: ASTVisitor>(v: &mut V, statement: &Statement) {
use StatementKind::*; use StatementKind::*;
match statement.kind { match statement.kind {
Expression(ref expr) => { Expression(ref expr) => {
@ -30,7 +30,7 @@ pub fn statement<V: ASTVisitor>(v: &mut V, statement: &Statement) {
} }
} }
pub fn declaration<V: ASTVisitor>(v: &mut V, decl: &Declaration) { fn declaration<V: ASTVisitor>(v: &mut V, decl: &Declaration) {
use Declaration::*; use Declaration::*;
match decl { match decl {
FuncSig(sig) => { FuncSig(sig) => {
@ -46,7 +46,7 @@ pub fn declaration<V: ASTVisitor>(v: &mut V, decl: &Declaration) {
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()); v.type_annotation(type_anno.as_ref());
expression(v, &expr); expression(v, &expr);
}, },
Impl { type_name, interface_name, block } => { Impl { type_name, interface_name, block } => {
@ -56,78 +56,99 @@ pub fn declaration<V: ASTVisitor>(v: &mut V, decl: &Declaration) {
} }
} }
pub fn signature<V: ASTVisitor>(v: &mut V, signature: &Signature) { fn signature<V: ASTVisitor>(v: &mut V, signature: &Signature) {
for p in signature.params.iter() { for p in signature.params.iter() {
v.formal_param(p); v.formal_param(p);
} }
v.maybe_type_identifier(signature.type_anno.as_ref()); v.type_annotation(signature.type_anno.as_ref());
for p in signature.params.iter() {
formal_param(v, p);
}
} }
pub fn expression<V: ASTVisitor>(v: &mut V, expression: &Expression) { fn expression<V: ASTVisitor>(v: &mut V, expression: &Expression) {
v.expression_kind(&expression.kind); v.expression_kind(&expression.kind);
v.maybe_type_identifier(expression.type_anno.as_ref()); v.type_annotation(expression.type_anno.as_ref());
expression_kind(v, &expression.kind);
} }
pub fn maybe_type_identifier<V: ASTVisitor>(v: &mut V, maybe_ty_identifier: Option<&TypeIdentifier>) {
} fn call<V: ASTVisitor>(v: &mut V, f: &Expression, args: &Vec<InvocationArgument>) {
pub fn call<V: ASTVisitor>(v: &mut V, f: &Expression, args: &Vec<InvocationArgument>) {
v.expression(f); v.expression(f);
for arg in args.iter() { for arg in args.iter() {
v.invocation_argument(arg); v.invocation_argument(arg);
} }
} }
pub fn index<V: ASTVisitor>(v: &mut V, indexee: &Expression, indexers: &Vec<Expression>) { fn index<V: ASTVisitor>(v: &mut V, indexee: &Expression, indexers: &Vec<Expression>) {
v.expression(indexee); v.expression(indexee);
for i in indexers.iter() { for i in indexers.iter() {
v.expression(i); v.expression(i);
} }
} }
pub fn named_struct<V: ASTVisitor>(v: &mut V, n: &QualifiedName, fields: &Vec<(Rc<String>, Expression)>) { fn named_struct<V: ASTVisitor>(v: &mut V, n: &QualifiedName, fields: &Vec<(Rc<String>, Expression)>) {
v.qualified_name(n); v.qualified_name(n);
for (_, expr) in fields.iter() { for (_, expr) in fields.iter() {
v.expression(expr); v.expression(expr);
} }
} }
pub fn lambda<V: ASTVisitor>(v: &mut V, params: &Vec<FormalParam>, type_anno: Option<&TypeIdentifier>, body: &Block) { fn lambda<V: ASTVisitor>(v: &mut V, params: &Vec<FormalParam>, type_anno: Option<&TypeIdentifier>, body: &Block) {
for param in params { for param in params {
v.formal_param(param); v.formal_param(param);
} }
type_anno.map(|anno| v.type_anno(anno)); v.type_annotation(type_anno);
v.block(body); v.block(body);
walk_block(v, body);
} }
pub fn formal_param<V: ASTVisitor>(v: &mut V, param: &FormalParam) { fn formal_param<V: ASTVisitor>(v: &mut V, param: &FormalParam) {
param.default.as_ref().map(|p| v.expression(p)); param.default.as_ref().map(|p| v.expression(p));
param.anno.as_ref().map(|a| v.type_anno(a)); v.type_annotation(param.anno.as_ref());
} }
pub fn expression_kind<V: ASTVisitor>(v: &mut V, expression_kind: &ExpressionKind) { fn expression_kind<V: ASTVisitor>(v: &mut V, expression_kind: &ExpressionKind) {
use ExpressionKind::*; use ExpressionKind::*;
match expression_kind { match expression_kind {
NatLiteral(n) => v.nat_literal(*n), NatLiteral(n) => v.nat_literal(*n),
FloatLiteral(f) => v.float_literal(*f), FloatLiteral(f) => v.float_literal(*f),
StringLiteral(s) => v.string_literal(s), StringLiteral(s) => v.string_literal(s),
BoolLiteral(b) => v.bool_literal(*b), BoolLiteral(b) => v.bool_literal(*b),
BinExp(op, lhs, rhs) => v.binexp(op, lhs, rhs), BinExp(op, lhs, rhs) => {
PrefixExp(op, arg) => v.prefix_exp(op, arg), v.binexp(op, lhs, rhs);
expression(v, lhs);
expression(v, rhs);
},
PrefixExp(op, arg) => {
v.prefix_exp(op, arg);
expression(v, arg);
}
TupleLiteral(exprs) => { TupleLiteral(exprs) => {
for expr in exprs { for expr in exprs {
v.expression(expr); v.expression(expr);
} }
}, },
Value(name) => v.qualified_name(name), Value(name) => v.qualified_name(name),
NamedStruct { name, fields } => v.named_struct(name, fields), NamedStruct { name, fields } => {
Call { f, arguments } => v.call(f, arguments), v.named_struct(name, fields);
Index { indexee, indexers } => v.index(indexee, indexers), named_struct(v, name, fields);
}
Call { f, arguments } => {
v.call(f, arguments);
call(v, f, arguments);
},
Index { indexee, indexers } => {
v.index(indexee, indexers);
index(v, indexee, indexers);
},
IfExpression { discriminator, body } => v.if_expression(discriminator, body), 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), 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), ForExpression { enumerators, body } => v.for_expression(enumerators, body),
Lambda { params , type_anno, body } => v.lambda(params, type_anno.as_ref(), body), Lambda { params , type_anno, body } => {
v.lambda(params, type_anno.as_ref(), body);
lambda(v, params, type_anno.as_ref(), body);
},
ListLiteral(exprs) => { ListLiteral(exprs) => {
for expr in exprs { for expr in exprs {
v.expression(expr); v.expression(expr);