Add tuple thing

This commit is contained in:
greg 2019-11-11 03:31:49 -08:00
parent a7cad3b88e
commit a36be407ca
2 changed files with 26 additions and 2 deletions

View File

@ -40,12 +40,19 @@ pub trait ASTVisitor: Sized {
fn pattern(&mut self, _pat: &Pattern) {}
}
pub enum VisitorOutput<T, E> {
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<String>) -> 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>) -> Self::Output;
fn done(&mut self, kind: Self::Output, anno: Option<Self::Output>) -> Self::Output;
}
@ -59,6 +66,13 @@ pub fn dispatch_expression_visitor<T>(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!")),
};

View File

@ -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>) -> 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>) -> 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, )"#);
}