meson-plus-plus icon indicating copy to clipboard operation
meson-plus-plus copied to clipboard

implement foreach loops

Open dcbaker opened this issue 4 years ago • 3 comments

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 avatar Sep 21 '21 16:09 dcbaker

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 avatar Jan 19 '23 23:01 dcbaker

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 avatar Oct 24 '24 19:10 dcbaker