From a36be407cad0431a37bda007b701a762acbe7980 Mon Sep 17 00:00:00 2001 From: greg Date: Mon, 11 Nov 2019 03:31:49 -0800 Subject: [PATCH] Add tuple thing --- schala-lang/language/src/ast/visitor.rs | 14 ++++++++++++++ schala-lang/language/src/ast/visitor_test.rs | 14 ++++++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/schala-lang/language/src/ast/visitor.rs b/schala-lang/language/src/ast/visitor.rs index c78a78f..6a0c19c 100644 --- a/schala-lang/language/src/ast/visitor.rs +++ b/schala-lang/language/src/ast/visitor.rs @@ -40,12 +40,19 @@ pub trait ASTVisitor: Sized { fn pattern(&mut self, _pat: &Pattern) {} } +pub enum VisitorOutput { + NotImplemented, + Ok(T), + Err(E) +} + 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 tuple_literal(&mut self, _items: Vec) -> Self::Output; fn done(&mut self, kind: Self::Output, anno: Option) -> Self::Output; } @@ -59,6 +66,13 @@ pub fn dispatch_expression_visitor(input: &Expression, visitor: &mut dyn Expr let rhs = dispatch_expression_visitor(rhs, visitor)?; visitor.binexp(op, lhs, rhs) }, + ExpressionKind::TupleLiteral(ref exprs) => { + let mut output = vec![]; + for ex in exprs { + output.push(dispatch_expression_visitor(&ex, visitor)?); + } + visitor.tuple_literal(output) + }, _ => return Err(format!("Lol not done yet!")), }; diff --git a/schala-lang/language/src/ast/visitor_test.rs b/schala-lang/language/src/ast/visitor_test.rs index 680c8da..5a8db20 100644 --- a/schala-lang/language/src/ast/visitor_test.rs +++ b/schala-lang/language/src/ast/visitor_test.rs @@ -59,6 +59,16 @@ impl ExpressionVisitor for ExprPrinter { fn binexp(&mut self, op: &BinOp, lhs_result: String, rhs_result: String) -> String { format!("{} {} {}", lhs_result, op.sigil().to_string(), rhs_result) } + fn tuple_literal(&mut self, items: Vec) -> String { + let mut buf = String::new(); + buf.push('('); + for item in items { + buf.push_str(item.as_str()); + buf.push_str(", "); + } + buf.push(')'); + buf + } fn done(&mut self, kind: String, anno: Option) -> String { match anno { Some(anno) => format!("{}: {}", kind, anno), @@ -82,9 +92,9 @@ fn make_expr(input: &str) -> Expression { #[test] fn new_visitor() { - let expr: Expression = make_expr("7+\"nueces\""); + let expr: Expression = make_expr("7+\"nueces\"*(33,32)"); let mut printer = ExprPrinter { }; let s = dispatch_expression_visitor(&expr, &mut printer).unwrap(); - assert_eq!(s, r#"7 + "nueces""#); + assert_eq!(s, r#"7 + "nueces" * (33, 32, )"#); }