tokay-lang / tokay

Tokay is a programming language designed for ad-hoc parsing, inspired by awk.
https://tokay.dev
MIT License
236 stars 7 forks source link

Establishing Iterators #101

Closed phorward closed 1 year ago

phorward commented 1 year ago

This PR implements iterators for Tokay.

The following Tokay functions are made available:

The implementation is only a place to start. There are still some restrictions and problems discussed in PR #101. There's also the need for aggregation functions as described in issue #88.


What's already working:

for i in (1,2,3) print(i)

d = (first => 1, second => 2, third => 3)
for k in d print(k)
for k in d.keys print(k)
for k in d.items print(k)

 # create lists from iters
print(list(iter(d)))
print(list(d.keys))

Still existing problems:

phorward commented 1 year ago

Ok I think Iterators are now in a state to be merged into Tokay.

phorward commented 1 year ago

Ok I think Iterators are now in a state to be merged into Tokay.

After some consideration, let's finish iterators by providing some more functionality.

phorward commented 1 year ago

88 refers to this

phorward commented 1 year ago

The iterator feature in the currently started version with the mapping unfortunately seems to be a bottomless pit, since several problems arise:

  1. due to the possibility of using functions as callbacks, the context must necessarily be passed and passed through, which is partially implemented here.
  2. since the iterator is borrowed mutable a further access possibly within a callback to the same iterator object leads inevitably to a panic.
  3. in general the passing of the context/args/nargs parameter configuration (or even if parameters are already on the stack) should be done in the form of a separate struct or enum, but this entails major rebuilding work in the entire code.

It would therefore be better if the map feature - possibly even using a yield function - were implemented in pure VM code.

Nevertheless, this branch contains important and necessary changes, which is why it should be remembered. Whether the map feature will be removed again or not remains open at the moment.