lang: Add a for loop statement for iterating over a list

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>
This commit is contained in:
James Shubin
2025-03-04 21:55:29 -05:00
parent c456a5ab97
commit cf7e73bbf6
63 changed files with 2814 additions and 203 deletions

View File

@@ -507,7 +507,7 @@ func TestAstFunc1(t *testing.T) {
}
// build the function graph
fgraph, err := iast.Graph()
fgraph, err := iast.Graph(interfaces.EmptyEnv()) // XXX: Ask Sam
if (!fail || !failGraph) && err != nil {
t.Errorf("test #%d: FAIL", index)
t.Errorf("test #%d: functions failed with: %+v", index, err)
@@ -1101,8 +1101,29 @@ func TestAstFunc2(t *testing.T) {
// in implementation and before unification, and static
// once we've unified the specific resource.
// build the env
//fgraph := &pgraph.Graph{Name: "functionGraph"}
env := interfaces.EmptyEnv()
// XXX: Do we need to do something like this?
//for k, v := range scope.Variables {
// g, builtinFunc, err := v.Graph(nil)
// if err != nil {
// t.Errorf("test #%d: FAIL", index)
// t.Errorf("test #%d: calling Graph on builtins errored: %+v", index, err)
// return
// }
// fgraph.AddGraph(g)
// env.Variables[k] = builtinFunc // XXX: Ask Sam (.Functions ???)
//}
//for k, closure := range scope.Functions {
// env.Functions[k] = &interfaces.Closure{
// Env: interfaces.EmptyEnv(),
// Expr: closure.Expr, // XXX: Ask Sam
// }
//}
// build the function graph
fgraph, err := iast.Graph()
fgraph, err := iast.Graph(env) // XXX: Ask Sam
if (!fail || !failGraph) && err != nil {
t.Errorf("test #%d: FAIL", index)
t.Errorf("test #%d: functions failed with: %+v", index, err)
@@ -1931,7 +1952,7 @@ func TestAstFunc3(t *testing.T) {
// once we've unified the specific resource.
// build the function graph
fgraph, err := iast.Graph()
fgraph, err := iast.Graph(interfaces.EmptyEnv()) // XXX: Ask Sam
if (!fail || !failGraph) && err != nil {
t.Errorf("test #%d: FAIL", index)
t.Errorf("test #%d: functions failed with: %+v", index, err)