Browse Source

feat: array as function call parameter

c_for-loops
Garrit Franke 3 years ago
parent
commit
79d01c4f6c
  1. 1
      TODO
  2. 9
      examples/playground.sb
  3. 2
      src/generator/js.rs
  4. 6
      src/parser/rules.rs
  5. 1
      src/parser/tests.rs

1
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)

9
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)
}
}

2
src/generator/js.rs

@ -212,7 +212,7 @@ fn generate_function_call(func: String, args: Vec<Expression>) -> 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::<Vec<String>>()

6
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));
}

1
src/parser/tests.rs

@ -766,7 +766,6 @@ fn test_complex_nested_expressions() {
}
#[test]
#[ignore]
fn test_array_as_argument() {
let raw = "
fn main() {

Loading…
Cancel
Save