From ca5204d4b26780c3f80d9a05cc52c21988968cfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vili=20Sinerv=C3=A4?= Date: Tue, 28 Jan 2025 16:47:11 +0200 Subject: [PATCH] Add tests for future parser features --- src/compiler/ast.rs | 2 +- src/compiler/parser.rs | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/compiler/ast.rs b/src/compiler/ast.rs index d2ffdcf..ac76662 100644 --- a/src/compiler/ast.rs +++ b/src/compiler/ast.rs @@ -3,7 +3,7 @@ pub enum Expression<'source> { IntLiteral(u32), BoolLiteral(bool), - Indentifier(String), + Identifier(&'source str), BinaryOp( Box>, &'source str, diff --git a/src/compiler/parser.rs b/src/compiler/parser.rs index 2e1785c..e092ac7 100644 --- a/src/compiler/parser.rs +++ b/src/compiler/parser.rs @@ -120,4 +120,42 @@ mod tests { BinaryOp(Box::new(IntLiteral(4)), "-", Box::new(IntLiteral(56))) ); } + + #[test] + fn test_binary_op_identifier() { + let result = parse(&vec![new_id("a"), new_id("+"), new_int("1")]); + assert_eq!( + result, + BinaryOp(Box::new(Identifier("a")), "+", Box::new(IntLiteral(1))) + ); + + let result = parse(&vec![new_int("1"), new_id("-"), new_id("a")]); + assert_eq!( + result, + BinaryOp(Box::new(IntLiteral(1)), "-", Box::new(Identifier("a"))) + ); + } + + #[test] + fn test_binary_op_multiple() { + let result = parse(&vec![ + new_int("1"), + new_id("+"), + new_int("2"), + new_id("-"), + new_int("3"), + ]); + assert_eq!( + result, + BinaryOp( + Box::new(IntLiteral(1)), + "+", + Box::new(BinaryOp( + Box::new(IntLiteral(2)), + "-", + Box::new(IntLiteral(3)) + )) + ) + ); + } }