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

for...in-loops & iterators #7

Closed
phorward opened this issue Jul 15, 2021 · 3 comments
Closed

for...in-loops & iterators #7

phorward opened this issue Jul 15, 2021 · 3 comments
Labels
feature New feature or request help wanted Extra attention is needed syntax

Comments

@phorward
Copy link
Member

To implement the for...in-loop construct in Tokay, it is required to make iterators available for values like dict, list or string.

@phorward phorward added the help wanted Extra attention is needed label Jul 15, 2021
@phorward phorward changed the title Implementation of iterators Implementation of iterators / for...in-loops Jul 15, 2021
@phorward phorward mentioned this issue Nov 30, 2021
33 tasks
@phorward phorward added the syntax label Dec 1, 2021
phorward added a commit that referenced this issue Dec 20, 2022
- This primarily addresses to fix #90
- The syntax of for-loops is a stopgap;
  The iterator-part can only be a Statement, not a Sequence;
  Generally the C/awk-style for-syntax should be removed, and
  `for...in` should be reinforced. See issue #7 for details.
@phorward phorward pinned this issue Dec 20, 2022
@phorward
Copy link
Member Author

phorward commented Dec 20, 2022

#90 and d1f2f80 show up general problems with Tokay's parentheses-less syntax attempt.


The syntax of for-loops is currently a stopgap and trade-off between C/awk-style, Rust/Python-style and Tokay's own syntactic specialities. The iterator-part can only be a Statement, not a Sequence, as expressed in tokay.tok:

Atomic : @{
    # ...
    'for' _SeparatedIdentifier (Sequence | Nop) ';' _ (Sequence | Nop) ';' _ Statement _ Block  ast("op_for")
    'for' _SeparatedIdentifier (Sequence | Nop) ';' _ (Sequence | Nop) ';' _ Nop _ Block  ast("op_for")
    # ...
}

This allows for simple

for i = 0; i < 10; i++ { print(i) }

but it's getting stuck here

for i = 0, j= 100; i < 10; i++, j-- { print(i, j) }

which will throw a syntax error with d1f2f80. Note, that previously, even less was possible, though!
Anyway, the syntax error is because Statement is used rather than Sequence on the iterator part, and a block is part of a Sequence, so this even more causes a syntax error.

The syntax is disambiguating here, and

for i = 0, j= 100; i < 10; (i++, j--) { print(i, j) }

is an acceptable, but not nice workaround.


Also, generally adding parentheses to the syntax would solve the problem.

Atomic : @{
    # ...
    'for' _ '(' _ (Sequence | Nop) ';' _ (Sequence | Nop) ';' _ (Sequence | Nop) ')' _ Block  ast("op_for")
    # ...
}

would enforce the original C/awk-style syntax

for (i = 0, j= 100; i < 10; i++, j--) { print(i, j) }

but this isn't wanted due personal preference, and Tokay's "different" syntax from existing syntax.


Generally, maybe its better to reinforced for...in and use this as the one-and-only-for syntax.

@phorward phorward changed the title Implementation of iterators / for...in-loops for...in-loops & Iterators Dec 20, 2022
@phorward phorward changed the title for...in-loops & Iterators for...in-loops & Iterators Dec 20, 2022
@phorward phorward changed the title for...in-loops & Iterators for...in-loops & iterators Dec 20, 2022
@phorward phorward added the feature New feature or request label Dec 20, 2022
@phorward
Copy link
Member Author

#101 is the first pull request on this topic.

@phorward
Copy link
Member Author

phorward commented Mar 3, 2023

#101 merged, issue fixed.

@phorward phorward closed this as completed Mar 3, 2023
@phorward phorward unpinned this issue Mar 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request help wanted Extra attention is needed syntax
Projects
None yet
Development

No branches or pull requests

1 participant