From 2822fddfc10b284f75bbc239b97d3eebda501dfa Mon Sep 17 00:00:00 2001 From: Garrit Franke Date: Sat, 6 Feb 2021 21:47:35 +0100 Subject: [PATCH] feat: nested arrays --- TODO | 1 - examples/playground.sb | 10 ++++------ src/generator/js.rs | 6 +----- src/parser/rules.rs | 7 ++----- src/parser/tests.rs | 3 +-- 5 files changed, 8 insertions(+), 19 deletions(-) diff --git a/TODO b/TODO index 66f427e..421c57d 100644 --- a/TODO +++ b/TODO @@ -7,7 +7,6 @@ - stdlib moves line of file, which makes error reporting impossible # Features -- Nested arrays like [[1, 2, 3], [1, 2, 3]] - Type inference - Multi-file support - Argument overloading diff --git a/examples/playground.sb b/examples/playground.sb index e7a49a3..d096418 100644 --- a/examples/playground.sb +++ b/examples/playground.sb @@ -1,11 +1,9 @@ fn main() { - let arr = [1, 2, 3] - for x in arr { - if x == 2 { - break - } else { - println(x) + let arr = [[11, 12, 13], [21, 22, 23], [31, 32, 33]] + for i in arr { + for j in i { + println(j) } } } \ No newline at end of file diff --git a/src/generator/js.rs b/src/generator/js.rs index 91525bd..bf78fc6 100644 --- a/src/generator/js.rs +++ b/src/generator/js.rs @@ -155,11 +155,7 @@ fn generate_array(elements: Vec) -> String { out_str += &elements .iter() - .map(|el| match el { - Expression::Int(x) => x.to_string(), - Expression::Str(x) => x.to_string(), - _ => todo!("Not yet implemented"), - }) + .map(|el| generate_expression(el.clone())) .collect::>() .join(", "); diff --git a/src/parser/rules.rs b/src/parser/rules.rs index 1239a1d..39774eb 100644 --- a/src/parser/rules.rs +++ b/src/parser/rules.rs @@ -267,12 +267,9 @@ impl Parser { let value = self.next()?.raw.parse::().map_err(|e| e.to_string())?; elements.push(Expression::Int(value)); } - TokenKind::Literal(Value::Str) => { - elements.push(Expression::Str(self.next()?.raw)); - } _ => { - let n = self.next()?; - return Err(self.make_error(TokenKind::Identifier("Argument".into()), n)); + let expr = self.parse_expression()?; + elements.push(expr); } }; if self.peek_token(TokenKind::SquareBraceClose).is_ok() { diff --git a/src/parser/tests.rs b/src/parser/tests.rs index d4049b9..2fd98b2 100644 --- a/src/parser/tests.rs +++ b/src/parser/tests.rs @@ -676,14 +676,13 @@ fn test_nested_for_loop() { } #[test] -#[ignore] fn test_nested_array() { let raw = " fn main() { let arr = [[11, 12, 13], [21, 22, 23], [31, 32, 33]] for i in arr { - for j in arr { + for j in i { println(j) } }