From 9fa4e3797c316d890e932d34898299fefe2e7276 Mon Sep 17 00:00:00 2001 From: greg Date: Fri, 27 Sep 2019 09:54:24 -0700 Subject: [PATCH] More visitor stuff --- schala-lang/language/src/ast/visitor.rs | 35 +++++++++-- schala-lang/language/src/ast/walker.rs | 77 +++++++++++++++---------- 2 files changed, 77 insertions(+), 35 deletions(-) diff --git a/schala-lang/language/src/ast/visitor.rs b/schala-lang/language/src/ast/visitor.rs index 889e9d8..1abd227 100644 --- a/schala-lang/language/src/ast/visitor.rs +++ b/schala-lang/language/src/ast/visitor.rs @@ -49,12 +49,39 @@ pub trait ASTVisitor: Sized { } fn named_struct(&mut self, name: &QualifiedName, fields: &Vec<(Rc, 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) { + 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 import(&mut self, import: &ImportSpecifier) {} fn qualified_name(&mut self, name: &QualifiedName) {} fn nat_literal(&mut self, n: u64) {} diff --git a/schala-lang/language/src/ast/walker.rs b/schala-lang/language/src/ast/walker.rs index 74fca5b..edf27a5 100644 --- a/schala-lang/language/src/ast/walker.rs +++ b/schala-lang/language/src/ast/walker.rs @@ -1,6 +1,7 @@ use std::rc::Rc; use crate::ast::*; use crate::ast::visitor::ASTVisitor; +use std::ops::Deref; pub fn ast(v: &mut V, ast: &AST) { v.block(&ast.statements); @@ -60,6 +61,40 @@ pub fn maybe_type_identifier(v: &mut V, maybe_ty_identifier: Opti } +pub 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) { + v.expression(indexee); + for i in indexers.iter() { + v.expression(i); + } +} + +pub 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) { + for param in params { + v.formal_param(param); + } + type_anno.map(|anno| v.type_anno(anno)); + v.block(body); +} + +pub 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)); +} + pub fn expression_kind(v: &mut V, expression_kind: &ExpressionKind) { use ExpressionKind::*; match expression_kind { @@ -76,37 +111,17 @@ pub fn expression_kind(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| 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, - arguments: Vec, - }, - Index { - indexee: Box, - indexers: Vec, - }, - IfExpression { - discriminator: Box, - body: Box, - }, - WhileExpression { - condition: Option>, - body: Block, - }, - ForExpression { - enumerators: Vec, - body: Box, - }, - Lambda { - params: Vec, - type_anno: Option, - body: Block, - }, - ListLiteral(Vec), - - } - */ }