From 253b5d88f0aa143d67c81d2b4ef22310bb4cf19b Mon Sep 17 00:00:00 2001 From: greg Date: Sat, 28 Sep 2019 01:58:22 -0700 Subject: [PATCH] Finish cleaning up visitor logic --- schala-lang/language/src/ast/visitor.rs | 101 ++++++------------------ schala-lang/language/src/ast/walker.rs | 69 ++++++++++------ 2 files changed, 67 insertions(+), 103 deletions(-) diff --git a/schala-lang/language/src/ast/visitor.rs b/schala-lang/language/src/ast/visitor.rs index 628832a..c1dc9f2 100644 --- a/schala-lang/language/src/ast/visitor.rs +++ b/schala-lang/language/src/ast/visitor.rs @@ -8,93 +8,36 @@ use crate::ast::walker; //TODO default implmentations should call walk methods - then I can test printing pub trait ASTVisitor: Sized { - fn ast(&mut self, ast: &AST) { - } - + fn ast(&mut self, ast: &AST) {} fn block(&mut self, statements: &Vec) {} - fn statement(&mut self, statement: &Statement) {} - - 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, new_name: &Rc) { - } - - fn binding(&mut self, name: &Rc, constant: bool, type_anno: Option<&TypeIdentifier>, expr: &Expression) { - } - - fn implemention(&mut self, type_name: &TypeIdentifier, interface_name: Option<&TypeSingletonName>, block: &Vec) { - } - - fn interface(&mut self, name: &Rc, signatures: &Vec) { - } - - 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, Expression)>) { - walker::named_struct(self, name, fields); - } - - fn call(&mut self, f: &Expression, arguments: &Vec) { - walker::call(self, f, arguments); - } - - fn index(&mut self, indexee: &Expression, indexers: &Vec) { - 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, body: &ForBody) { - - } - - fn lambda(&mut self, params: &Vec, 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 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, new_name: &Rc) {} + fn binding(&mut self, name: &Rc, constant: bool, type_anno: Option<&TypeIdentifier>, expr: &Expression) {} + fn implemention(&mut self, type_name: &TypeIdentifier, interface_name: Option<&TypeSingletonName>, block: &Vec) {} + fn interface(&mut self, name: &Rc, signatures: &Vec) {} + fn expression(&mut self, expression: &Expression) {} + fn expression_kind(&mut self, kind: &ExpressionKind) {} + fn type_annotation(&mut self, type_anno: Option<&TypeIdentifier>) {} + fn named_struct(&mut self, name: &QualifiedName, fields: &Vec<(Rc, Expression)>) {} + fn call(&mut self, f: &Expression, arguments: &Vec) {} + fn index(&mut self, indexee: &Expression, indexers: &Vec) {} + 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, body: &ForBody) {} + fn lambda(&mut self, params: &Vec, type_anno: Option<&TypeIdentifier>, body: &Block) {} + fn invocation_argument(&mut self, arg: &InvocationArgument) {} + fn formal_param(&mut self, param: &FormalParam) {} fn import(&mut self, import: &ImportSpecifier) {} fn qualified_name(&mut self, name: &QualifiedName) {} fn nat_literal(&mut self, n: u64) {} fn float_literal(&mut self, f: f64) {} fn string_literal(&mut self, s: &Rc) {} fn bool_literal(&mut self, b: bool) {} - fn binexp(&mut self, op: &BinOp, lhs: &Expression, rhs: &Expression) { - walker::expression(self, lhs); - walker::expression(self, rhs); - } - - fn prefix_exp(&mut self, op: &PrefixOp, arg: &Expression) { - walker::expression(self, arg); - } + fn binexp(&mut self, op: &BinOp, lhs: &Expression, rhs: &Expression) {} + fn prefix_exp(&mut self, op: &PrefixOp, arg: &Expression) {} } diff --git a/schala-lang/language/src/ast/walker.rs b/schala-lang/language/src/ast/walker.rs index 527736c..7aef9f3 100644 --- a/schala-lang/language/src/ast/walker.rs +++ b/schala-lang/language/src/ast/walker.rs @@ -15,7 +15,7 @@ pub fn walk_block(v: &mut V, block: &Vec) { } } -pub fn statement(v: &mut V, statement: &Statement) { +fn statement(v: &mut V, statement: &Statement) { use StatementKind::*; match statement.kind { Expression(ref expr) => { @@ -30,7 +30,7 @@ pub fn statement(v: &mut V, statement: &Statement) { } } -pub fn declaration(v: &mut V, decl: &Declaration) { +fn declaration(v: &mut V, decl: &Declaration) { use Declaration::*; match decl { FuncSig(sig) => { @@ -46,7 +46,7 @@ pub fn declaration(v: &mut V, decl: &Declaration) { 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()); + v.type_annotation(type_anno.as_ref()); expression(v, &expr); }, Impl { type_name, interface_name, block } => { @@ -56,78 +56,99 @@ pub fn declaration(v: &mut V, decl: &Declaration) { } } -pub fn signature(v: &mut V, signature: &Signature) { +fn signature(v: &mut V, signature: &Signature) { for p in signature.params.iter() { 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: &mut V, expression: &Expression) { +fn expression(v: &mut V, expression: &Expression) { 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: &mut V, maybe_ty_identifier: Option<&TypeIdentifier>) { -} - -pub fn call(v: &mut V, f: &Expression, args: &Vec) { +fn call(v: &mut V, f: &Expression, args: &Vec) { v.expression(f); for arg in args.iter() { v.invocation_argument(arg); } } -pub fn index(v: &mut V, indexee: &Expression, indexers: &Vec) { +fn index(v: &mut V, indexee: &Expression, indexers: &Vec) { v.expression(indexee); for i in indexers.iter() { v.expression(i); } } -pub fn named_struct(v: &mut V, n: &QualifiedName, fields: &Vec<(Rc, Expression)>) { +fn named_struct(v: &mut V, n: &QualifiedName, fields: &Vec<(Rc, Expression)>) { v.qualified_name(n); for (_, expr) in fields.iter() { v.expression(expr); } } -pub fn lambda(v: &mut V, params: &Vec, type_anno: Option<&TypeIdentifier>, body: &Block) { +fn lambda(v: &mut V, params: &Vec, type_anno: Option<&TypeIdentifier>, body: &Block) { for param in params { v.formal_param(param); } - type_anno.map(|anno| v.type_anno(anno)); + v.type_annotation(type_anno); v.block(body); + walk_block(v, body); } -pub fn formal_param(v: &mut V, param: &FormalParam) { +fn formal_param(v: &mut V, param: &FormalParam) { 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: &mut V, expression_kind: &ExpressionKind) { +fn expression_kind(v: &mut V, expression_kind: &ExpressionKind) { use ExpressionKind::*; match expression_kind { NatLiteral(n) => v.nat_literal(*n), FloatLiteral(f) => v.float_literal(*f), StringLiteral(s) => v.string_literal(s), BoolLiteral(b) => v.bool_literal(*b), - BinExp(op, lhs, rhs) => v.binexp(op, lhs, rhs), - PrefixExp(op, arg) => v.prefix_exp(op, arg), + BinExp(op, lhs, rhs) => { + v.binexp(op, lhs, rhs); + expression(v, lhs); + expression(v, rhs); + }, + PrefixExp(op, arg) => { + v.prefix_exp(op, arg); + expression(v, arg); + } TupleLiteral(exprs) => { for expr in exprs { v.expression(expr); } }, 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), + NamedStruct { name, fields } => { + v.named_struct(name, fields); + 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), WhileExpression { condition, body } => v.while_expression(condition.as_ref().map(|b: &Box| 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), + Lambda { params , type_anno, body } => { + v.lambda(params, type_anno.as_ref(), body); + lambda(v, params, type_anno.as_ref(), body); + }, ListLiteral(exprs) => { for expr in exprs { v.expression(expr);