This adds a for statement which is used to iterate over a list 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 for 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. Co-authored-by: Samuel Gélineau <gelisam@gmail.com>
25 lines
577 B
Plaintext
25 lines
577 B
Plaintext
-- main.mcl --
|
|
import "fmt"
|
|
|
|
$list = ["a", "b", "c",]
|
|
|
|
import "thing.mcl" # works
|
|
|
|
for $index, $value in $list {
|
|
# The semantics are that only one copy of an import is needed... Not one per iteration.
|
|
# XXX: Error: could not find `inside` in env for ExprIterated
|
|
# XXX: I added a hack to catch this obvious case
|
|
#import "thing.mcl" # XXX: doesn't work :(
|
|
|
|
$x = 42 + $thing.inside
|
|
|
|
$s = fmt.printf("%s is %d = %d", $value, $index, $x)
|
|
test [$s,] {}
|
|
}
|
|
-- thing.mcl --
|
|
$inside = 13
|
|
-- OUTPUT --
|
|
Vertex: test[a is 0 = 55]
|
|
Vertex: test[b is 1 = 55]
|
|
Vertex: test[c is 2 = 55]
|