lang: Add a forkv loop statement for iterating over a map

This adds a forkv statement which is used to iterate over a map with a
body of statements. This is an important data transformation tool which
should be used sparingly, but is important to have.

An import statement inside of a forkv loop is not currently supported.
We have a simple hack to detect the obvious cases, but more deeply
nested scenarios probably won't be caught, and you'll get an obscure
error message if you try to do this.

This was incredibly challenging to get right, and it's all thanks to Sam
for his brilliance.

Note, I couldn't think of a better keyword that "forkv" but suggestions
are welcome if you think you have a better idea. Other ideas were formap
and foreach, but neither got me very excited.
This commit is contained in:
James Shubin
2025-03-05 13:18:35 -05:00
parent cf7e73bbf6
commit 2899bc234a
51 changed files with 1885 additions and 2 deletions

View File

@@ -109,6 +109,15 @@ expression
}
```
- **forkv**: loop over a map with a body of statements
```mcl
$map = {0 => "a", 1 => "b", 2 => "c",}
forkv $key, $val in $map {
# some statements go here
}
```
- **resource**: produces a resource
```mcl