Add printing support for assignment operators
parent
21e1c315ad
commit
2036704732
28
src/lib.rs
28
src/lib.rs
|
@ -136,8 +136,8 @@ impl ExprVisitor<String> for ASTPrinter {
|
||||||
name.lexeme().to_owned()
|
name.lexeme().to_owned()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_assign(&mut self, _name: &lex::Token, _value: &Expr) -> String {
|
fn visit_assign(&mut self, name: &lex::Token, value: &Expr) -> String {
|
||||||
todo!()
|
self.parenthesize(&format!("{}=", name.lexeme()), &[value])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -167,6 +167,30 @@ mod tests {
|
||||||
assert_eq!("(+ 123 456)", result);
|
assert_eq!("(+ 123 456)", result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_simple_add_assignment() {
|
||||||
|
let result = ASTPrinter.visit_expr(&Expr::Assign {
|
||||||
|
name: Token::new(
|
||||||
|
TokenKind::Identifier("a".to_string().into()),
|
||||||
|
"a".to_string(),
|
||||||
|
10,
|
||||||
|
),
|
||||||
|
value: Box::new(Expr::Binary {
|
||||||
|
left: Box::new(Expr::Literal {
|
||||||
|
value: ast::LiteralValue::Number(123_f64),
|
||||||
|
token: Token::new(TokenKind::Number(456_f64), "456".to_string(), 10),
|
||||||
|
}),
|
||||||
|
operator: Token::new(TokenKind::Plus, "+".to_string(), 1),
|
||||||
|
right: Box::new(Expr::Literal {
|
||||||
|
value: ast::LiteralValue::Number(456_f64),
|
||||||
|
token: Token::new(TokenKind::Number(456_f64), "456".to_string(), 10),
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
assert_eq!("(a= (+ 123 456))", result);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_complicated_arithmetic() {
|
fn test_complicated_arithmetic() {
|
||||||
let result = ASTPrinter.visit_expr(&Expr::Binary {
|
let result = ASTPrinter.visit_expr(&Expr::Binary {
|
||||||
|
|
Loading…
Reference in New Issue