dcbaker / meson-plus-plus

An experimental and incomplete implementation of Meson in C++, for solving Meson's bootstrapping issue.
Apache License 2.0
50 stars 7 forks source link

implement foreach loops #21

Open dcbaker opened 3 years ago

dcbaker commented 3 years ago

This one is actually rather annoying to think about, as it's complex control flow.

I think, since foreach is determanisticly sized, we can basically implement it as something like:

Block(..., condition(on_true: self, on_false: next)

where the on true (more iterations required) we loop back to self, but on false (break, continue, end of loop) we jump to the next block. This could of course be very annoying to code due to unique_ptr

dcbaker commented 1 year ago

To make this more complicated than when I first wrote the issue, we have a range() function now, which means that while the number of iterations of a foreach loop is bounded, it can be practically unbounded when using range(), since you could write:

foreach f : range(100000000000000000000000000)
   ...
endforeach

Which is both easy to write, and could be a problem if we unrolled the whole loop

Although we might run into issues with the maximum size of numbers too...

dcbaker commented 1 month ago

I think for a first pass we just want to unroll. That might require some reworking for more complicated situations, but this should be sufficient for self-hosting, and for most real-world Meson programs

dcbaker commented 1 week ago

Foreach loops leak from their context, so in a case like:

a = 'b'
message(a)
foreach a : ['a']
  message(a)
endforeach
message(a)

the result will be:

b
a
a