From 97ae0e5ce6f014a5f1b1d3bd349889c260ed4435 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vili=20Sinerv=C3=A4?= Date: Fri, 31 Jan 2025 16:50:51 +0200 Subject: [PATCH] Add tests for variable declaration --- src/compiler/ast.rs | 1 + src/compiler/parser/tests.rs | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/compiler/ast.rs b/src/compiler/ast.rs index 9a412c5..1bb204b 100644 --- a/src/compiler/ast.rs +++ b/src/compiler/ast.rs @@ -5,6 +5,7 @@ pub enum Expression<'source> { BoolLiteral(bool), Identifier(&'source str), UnaryOp(&'source str, Box>), + VarDeclaration(&'source str, Box>), BinaryOp( Box>, &'source str, diff --git a/src/compiler/parser/tests.rs b/src/compiler/parser/tests.rs index d49de67..cb31834 100644 --- a/src/compiler/parser/tests.rs +++ b/src/compiler/parser/tests.rs @@ -401,3 +401,31 @@ fn test_block_unmatched() { fn test_block_missing_semicolon() { parse(&tokenize("{ a = 1\nb }")); } + +#[test] +fn test_var_basic() { + let result = parse(&tokenize("var x = 1")); + assert_eq!(result, VarDeclaration("x", int_ast!(1))); + + let result = parse(&tokenize("{ var x = 1; x = 2; }")); + assert_eq!( + result, + Block(vec![ + VarDeclaration("x", int_ast!(1)), + BinaryOp(id_ast!("x"), "=", int_ast!(2)), + EmptyLiteral() + ]) + ); +} + +#[test] +#[should_panic] +fn test_var_chain() { + parse(&tokenize("var x = var y = 1")); +} + +#[test] +#[should_panic] +fn test_var_embedded() { + parse(&tokenize("if true then var x = 3")); +}