From 716e21e97d5514ae16d7173defaa2b9ed04a9060 Mon Sep 17 00:00:00 2001 From: shubhiscoding Date: Sun, 29 Mar 2026 22:32:02 +0530 Subject: [PATCH 1/2] Add support for nested array literals and indexing in parser --- src/parser/mod.rs | 12 +++++------ tests/integration_tests.rs | 43 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 4b918f8..0bba39a 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -450,20 +450,20 @@ impl Parser { } fn parse_index(&mut self, array_name: String) -> Expression { - if let Some(Token::SquareLeft) = self.peek() { + let mut result = Expression::Identifier(array_name.clone()); + while Some(&Token::SquareLeft) == self.peek() { self.advance(); // consume '[' let index_expr = self.parse_first(); match self.advance() { Some(Token::SquareRight) => {} _ => panic!("Expected ']' after array index"), } - Expression::Index { - array: Box::new(Expression::Identifier(array_name)), + result = Expression::Index { + array: Box::new(result), index: Box::new(index_expr), - } - } else { - panic!("Expected '[' after identifier for array indexing"); + }; } + result } fn parse_array_literal(&mut self) -> Expression { diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index dc650eb..e22b0f3 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -976,3 +976,46 @@ print arr; let out = run_rts(code); assert_eq!(out, "[1, 2, 3]"); } + +// ========== Nested Arrays ========== + +#[test] +fn test_nested_array_literal() { + let out = run_rts("let arr = [1, [2, 3]];\nprint arr[1];"); + assert_eq!(out, "[2, 3]"); +} + +#[test] +fn test_nested_array_index_depth_2() { + let out = run_rts("let arr = [[10, 20], [30, 40]];\nprint arr[1][0];"); + assert_eq!(out, "30"); +} + +#[test] +fn test_nested_array_index_depth_3() { + let out = run_rts("let x = [10, [3, [6, 11]], 5];\nprint x[1][1][0];"); + assert_eq!(out, "6"); +} + +#[test] +fn test_nested_array_index_first_element() { + let out = run_rts("let arr = [[1, 2], [3, 4]];\nprint arr[0][0];"); + assert_eq!(out, "1"); +} + +#[test] +fn test_nested_array_index_with_expression() { + let code = r#" +let arr = [[10, 20], [30, 40]]; +let i = 1; +print arr[i][i - 1]; +"#; + let out = run_rts(code); + assert_eq!(out, "30"); +} + +#[test] +fn test_nested_array_print_whole() { + let out = run_rts("let arr = [[1, 2], [3, 4]];\nprint arr;"); + assert_eq!(out, "[[1, 2], [3, 4]]"); +} From 81780fba252fff17d8a160474b8f6ab09cc0473f Mon Sep 17 00:00:00 2001 From: shubhiscoding Date: Sun, 29 Mar 2026 22:36:09 +0530 Subject: [PATCH 2/2] Fix formatting in parse_index function for consistency --- src/parser/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 0bba39a..2991662 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -450,7 +450,7 @@ impl Parser { } fn parse_index(&mut self, array_name: String) -> Expression { - let mut result = Expression::Identifier(array_name.clone()); + let mut result = Expression::Identifier(array_name.clone()); while Some(&Token::SquareLeft) == self.peek() { self.advance(); // consume '[' let index_expr = self.parse_first();