From a7cad3b88e000dec04af27357619caaab1ef48d4 Mon Sep 17 00:00:00 2001 From: greg Date: Mon, 11 Nov 2019 02:55:00 -0800 Subject: [PATCH] Try associated type --- schala-lang/language/src/ast/visitor.rs | 15 ++++++++------- schala-lang/language/src/ast/visitor_test.rs | 7 ++++--- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/schala-lang/language/src/ast/visitor.rs b/schala-lang/language/src/ast/visitor.rs index dfd9ee2..c78a78f 100644 --- a/schala-lang/language/src/ast/visitor.rs +++ b/schala-lang/language/src/ast/visitor.rs @@ -40,15 +40,16 @@ pub trait ASTVisitor: Sized { fn pattern(&mut self, _pat: &Pattern) {} } -pub trait ExpressionVisitor {//TODO maybe this should be an associated type? - fn type_anno(&mut self, _anno: &TypeIdentifier) -> T; - fn nat_literal(&mut self, _value: &u64) -> T; - fn string_literal(&mut self, _value: &Rc) -> T; - fn binexp(&mut self, _op: &BinOp, _lhs_resul: T, _rhs_result: T) -> T; - fn done(&mut self, kind: T, anno: Option) -> T; +pub trait ExpressionVisitor {//TODO maybe this should be an associated type? + type Output; + fn type_anno(&mut self, _anno: &TypeIdentifier) -> Self::Output; + fn nat_literal(&mut self, _value: &u64) -> Self::Output; + fn string_literal(&mut self, _value: &Rc) -> Self::Output; + fn binexp(&mut self, _op: &BinOp, _lhs_resul: Self::Output, _rhs_result: Self::Output) -> Self::Output; + fn done(&mut self, kind: Self::Output, anno: Option) -> Self::Output; } -pub fn dispatch_expression_visitor(input: &Expression, visitor: &mut dyn ExpressionVisitor) -> Result { +pub fn dispatch_expression_visitor(input: &Expression, visitor: &mut dyn ExpressionVisitor) -> Result { let output = match input.kind { ExpressionKind::NatLiteral(ref n) => visitor.nat_literal(n), diff --git a/schala-lang/language/src/ast/visitor_test.rs b/schala-lang/language/src/ast/visitor_test.rs index d180d30..680c8da 100644 --- a/schala-lang/language/src/ast/visitor_test.rs +++ b/schala-lang/language/src/ast/visitor_test.rs @@ -45,7 +45,8 @@ fn heh() { struct ExprPrinter { } -impl ExpressionVisitor for ExprPrinter { +impl ExpressionVisitor for ExprPrinter { + type Output = String; fn type_anno(&mut self, _anno: &TypeIdentifier) -> String { "Any".to_string() } @@ -81,9 +82,9 @@ fn make_expr(input: &str) -> Expression { #[test] fn new_visitor() { - let expr: Expression = make_expr("7+20"); + let expr: Expression = make_expr("7+\"nueces\""); let mut printer = ExprPrinter { }; let s = dispatch_expression_visitor(&expr, &mut printer).unwrap(); - assert_eq!(s, "7 + 20"); + assert_eq!(s, r#"7 + "nueces""#); }