From 79d01c4f6c45842007b77880fc1d8ca0830770ef Mon Sep 17 00:00:00 2001 From: Garrit Franke Date: Tue, 9 Feb 2021 17:42:04 +0100 Subject: [PATCH] feat: array as function call parameter --- TODO | 1 - examples/playground.sb | 9 +++++---- src/generator/js.rs | 2 +- src/parser/rules.rs | 6 ++++++ src/parser/tests.rs | 1 - 5 files changed, 12 insertions(+), 7 deletions(-) diff --git a/TODO b/TODO index a9b4bec..6e9d5cb 100644 --- a/TODO +++ b/TODO @@ -1,6 +1,5 @@ # Bugs - Nested expressions can have invalid parens. E.g.: 6 % 3 == 0 should be true; is false (See test_complex_nested_expressions) -- Arrays as function arguments don't work (See test_array_as_argument) # Cleanups - Improve error reporting (See ./util/string_util::highlight_position_in_file) diff --git a/examples/playground.sb b/examples/playground.sb index 6b9a030..3ed3f25 100644 --- a/examples/playground.sb +++ b/examples/playground.sb @@ -1,8 +1,9 @@ fn main() { + my_print([1, 2, 3]) +} - let arr = [1, 2, 3] - - for x in arr { - _printf(x) +fn my_print(x: int[]) { + for i in x { + println(i) } } \ No newline at end of file diff --git a/src/generator/js.rs b/src/generator/js.rs index bb649bd..8649982 100644 --- a/src/generator/js.rs +++ b/src/generator/js.rs @@ -212,7 +212,7 @@ fn generate_function_call(func: String, args: Vec) -> String { Expression::ArrayAccess(name, expr) => generate_array_access(name, *expr), Expression::FunctionCall(n, a) => generate_function_call(n, a), Expression::Str(s) | Expression::Variable(s) => s, - Expression::Array(_) => todo!(), + Expression::Array(elements) => generate_array(elements), Expression::BinOp(left, op, right) => generate_bin_op(*left, op, *right), }) .collect::>() diff --git a/src/parser/rules.rs b/src/parser/rules.rs index dcf1596..218c951 100644 --- a/src/parser/rules.rs +++ b/src/parser/rules.rs @@ -188,6 +188,12 @@ impl Parser { args.push(self.parse_expression()?) } TokenKind::Keyword(Keyword::Boolean) => args.push(self.parse_expression()?), + TokenKind::SquareBraceOpen => { + // TODO: Expression parsing currently uses `next` instead of `peek`. + // We have to eat that token here until that is resolved + self.match_token(TokenKind::SquareBraceOpen)?; + args.push(self.parse_array()?); + } _ => { return Err(self.make_error(TokenKind::BraceClose, next)); } diff --git a/src/parser/tests.rs b/src/parser/tests.rs index 0f2d54d..210b96b 100644 --- a/src/parser/tests.rs +++ b/src/parser/tests.rs @@ -766,7 +766,6 @@ fn test_complex_nested_expressions() { } #[test] -#[ignore] fn test_array_as_argument() { let raw = " fn main() {