Browse Source

feat: nested arrays

github-actions
Garrit Franke 3 years ago
parent
commit
2822fddfc1
  1. 1
      TODO
  2. 10
      examples/playground.sb
  3. 6
      src/generator/js.rs
  4. 7
      src/parser/rules.rs
  5. 3
      src/parser/tests.rs

1
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

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

6
src/generator/js.rs

@ -155,11 +155,7 @@ fn generate_array(elements: Vec<Expression>) -> 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::<Vec<String>>()
.join(", ");

7
src/parser/rules.rs

@ -267,12 +267,9 @@ impl Parser {
let value = self.next()?.raw.parse::<u32>().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() {

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

Loading…
Cancel
Save