From 0246e510ca23443e7507b22cb17a652684e82832 Mon Sep 17 00:00:00 2001 From: greg Date: Mon, 19 Nov 2018 00:21:50 -0800 Subject: [PATCH] More rejiggering - tests still fail --- schala-lang/language/src/ast_visitor.rs | 63 ++++++++++++++----------- 1 file changed, 36 insertions(+), 27 deletions(-) diff --git a/schala-lang/language/src/ast_visitor.rs b/schala-lang/language/src/ast_visitor.rs index 654efd7..ebf9d76 100644 --- a/schala-lang/language/src/ast_visitor.rs +++ b/schala-lang/language/src/ast_visitor.rs @@ -7,30 +7,7 @@ pub fn dispatch(visitor: &mut V, ast: &AST) { for statement in ast.0.iter() { match statement { Statement::ExpressionStatement(e) => { - match e { - Expression(expr, maybe_anno) => { - match expr { - ExpressionType::NatLiteral(n) => visitor.nat_literal(n), - ExpressionType::FloatLiteral(f) => visitor.float_literal(f), - ExpressionType::StringLiteral(s) => visitor.string_literal(s), - ExpressionType::BoolLiteral(b) => visitor.bool_literal(b), - ExpressionType::BinExp(binop, lhs, rhs) => visitor.binop(binop, lhs, rhs), - ExpressionType::PrefixExp(prefix, expr) => visitor.prefixop(prefix, expr), - ExpressionType::TupleLiteral(v) => visitor.tuple_literal(v), - ExpressionType::Value(v) => visitor.value(v), - ExpressionType::NamedStruct { name, fields } => visitor.named_struct(name, fields), - ExpressionType::Call { f, arguments } => visitor.call(f, arguments), - ExpressionType::Index { indexee, indexers } => visitor.index(indexee, indexers), - ExpressionType::IfExpression { discriminator, body } => visitor.if_expression(discriminator, body), - ExpressionType::WhileExpression { condition, body } => visitor.while_expresssion(condition, body), - ExpressionType::ForExpression { enumerators, body } => visitor.for_expression(enumerators, body), - ExpressionType::Lambda { params, type_anno, body } => visitor.lambda_expression(params, type_anno, body), - ExpressionType::ListLiteral(items) => visitor.list_literal(items), - } - visitor.anno_expr(maybe_anno); - visitor.expr_kind(expr); - } - } + dispatch_expression(visitor, e); visitor.expression(e); }, Statement::Declaration(decl) => { @@ -51,6 +28,34 @@ pub fn dispatch(visitor: &mut V, ast: &AST) { visitor.ast(ast) } +fn dispatch_expression(visitor: &mut V, expression: &Expression) { + match expression { + Expression(expr, maybe_anno) => { + match expr { + ExpressionType::NatLiteral(n) => visitor.nat_literal(n), + ExpressionType::FloatLiteral(f) => visitor.float_literal(f), + ExpressionType::StringLiteral(s) => visitor.string_literal(s), + ExpressionType::BoolLiteral(b) => visitor.bool_literal(b), + ExpressionType::BinExp(binop, lhs, rhs) => visitor.binop(binop, lhs, rhs), + ExpressionType::PrefixExp(prefix, expr) => visitor.prefixop(prefix, expr), + ExpressionType::TupleLiteral(v) => visitor.tuple_literal(v), + ExpressionType::Value(v) => visitor.value(v), + ExpressionType::NamedStruct { name, fields } => visitor.named_struct(name, fields), + ExpressionType::Call { f, arguments } => visitor.call(f, arguments), + ExpressionType::Index { indexee, indexers } => visitor.index(indexee, indexers), + ExpressionType::IfExpression { discriminator, body } => visitor.if_expression(discriminator, body), + ExpressionType::WhileExpression { condition, body } => visitor.while_expresssion(condition, body), + ExpressionType::ForExpression { enumerators, body } => visitor.for_expression(enumerators, body), + ExpressionType::Lambda { params, type_anno, body } => visitor.lambda_expression(params, type_anno, body), + ExpressionType::ListLiteral(items) => visitor.list_literal(items), + } + visitor.anno_expr(maybe_anno); + visitor.expr_kind(expr); + } + } +} + + pub trait ASTVisitor { fn ast(&mut self, _ast: &AST) { } fn statement(&mut self, _stmt: &Statement) { } @@ -111,8 +116,12 @@ impl ASTVisitor for SchalaPrinter { fn expression(&mut self, _: &Expression) { self.s.push_str("some_expr"); } - fn declaration(&mut self, _: &Declaration) { - self.s.push_str("some_decl"); + + fn binding(&mut self, name: &Rc, constant: &bool, _expr: &Expression) { + self.s.push_str(&format!("let{} {} = {}", + if *constant { "" } else { " mut" }, + name, + "some_expr")); } } @@ -136,7 +145,7 @@ mod visitor_tests { let mut pp = SchalaPrinter::new(); dispatch(&mut pp, &ast); let result = pp.done(); - assert_eq!(result, r#"Pretty-printed AST + assert_eq!(result, r#"Schala source code: let a = 1 + 2 let b = 2 + 44 foo()