Add tuple thing
This commit is contained in:
parent
a7cad3b88e
commit
a36be407ca
@ -40,12 +40,19 @@ pub trait ASTVisitor: Sized {
|
|||||||
fn pattern(&mut self, _pat: &Pattern) {}
|
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?
|
pub trait ExpressionVisitor {//TODO maybe this should be an associated type?
|
||||||
type Output;
|
type Output;
|
||||||
fn type_anno(&mut self, _anno: &TypeIdentifier) -> Self::Output;
|
fn type_anno(&mut self, _anno: &TypeIdentifier) -> Self::Output;
|
||||||
fn nat_literal(&mut self, _value: &u64) -> Self::Output;
|
fn nat_literal(&mut self, _value: &u64) -> Self::Output;
|
||||||
fn string_literal(&mut self, _value: &Rc<String>) -> 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 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;
|
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)?;
|
let rhs = dispatch_expression_visitor(rhs, visitor)?;
|
||||||
visitor.binexp(op, lhs, rhs)
|
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!")),
|
_ => return Err(format!("Lol not done yet!")),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -59,6 +59,16 @@ impl ExpressionVisitor for ExprPrinter {
|
|||||||
fn binexp(&mut self, op: &BinOp, lhs_result: String, rhs_result: String) -> String {
|
fn binexp(&mut self, op: &BinOp, lhs_result: String, rhs_result: String) -> String {
|
||||||
format!("{} {} {}", lhs_result, op.sigil().to_string(), rhs_result)
|
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 {
|
fn done(&mut self, kind: String, anno: Option<String>) -> String {
|
||||||
match anno {
|
match anno {
|
||||||
Some(anno) => format!("{}: {}", kind, anno),
|
Some(anno) => format!("{}: {}", kind, anno),
|
||||||
@ -82,9 +92,9 @@ fn make_expr(input: &str) -> Expression {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn new_visitor() {
|
fn new_visitor() {
|
||||||
let expr: Expression = make_expr("7+\"nueces\"");
|
let expr: Expression = make_expr("7+\"nueces\"*(33,32)");
|
||||||
|
|
||||||
let mut printer = ExprPrinter { };
|
let mut printer = ExprPrinter { };
|
||||||
let s = dispatch_expression_visitor(&expr, &mut printer).unwrap();
|
let s = dispatch_expression_visitor(&expr, &mut printer).unwrap();
|
||||||
assert_eq!(s, r#"7 + "nueces""#);
|
assert_eq!(s, r#"7 + "nueces" * (33, 32, )"#);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user