Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement for...of syntax for Iterables #576

Closed
joshwd36 opened this issue Jul 18, 2020 · 4 comments · Fixed by #704
Closed

Implement for...of syntax for Iterables #576

joshwd36 opened this issue Jul 18, 2020 · 4 comments · Fixed by #704
Assignees
Labels
enhancement New feature or request

Comments

@joshwd36
Copy link
Contributor

As more collections are added, it would be useful to implement the for...of syntax for Iterables, and adding iterables to the String, Array, and Map types. ECMAScript specification.

This code should now work and give the expected result:

let a = [1, 2, 3];
for (i of a) {
    console.log(i);
}

The expected output is:

1
2
3

I'm happy to have a go at this myself, although as this will be my first time adding syntax features, I'm wondering whether I should wait for #559 to land or just go right ahead with the current lexer?

@joshwd36 joshwd36 added the enhancement New feature or request label Jul 18, 2020
@Lan2u
Copy link

Lan2u commented Jul 19, 2020

You could make a branch-based from #559 if you wanted to get started right away, I expect that most of the work regarding this would be in the parser (syntax/parser/statement/iteration) and exec (exec/iteration/mod.rs).

In terms of the lexer I think you will just need to add a new 'of' keyword (syntax/ast/keyword)

@dvtkrlbs
Copy link

dvtkrlbs commented Aug 30, 2020

I can take a look at this. Does this include the for-in statement too?

@joshwd36
Copy link
Contributor Author

I can take a look at this. Does this include the for-in statement too?

I've made a start on it already, but haven't been able to work on it recently. I should have more time this week though so I'll try and finish it then, if not I'm happy to transfer what I've done over to you.

@jcdickinson
Copy link
Contributor

Dropping #671 for whoever wants to take this on. See my comments there, as there is a tricky design question. My current thoughts are:

pub enum IterableForLoopKind {
    In,
    Of
}
struct InnerIterableForLoop {
    kind: IterableForLoopKind,
    variable: SingleVarLetOrConstDecl, // or something
    object: Node,
    body: Node
}

Note that #670 would also be closed, as for..in is simply a for..of with a built-in iterator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants