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:
@@ -17,4 +17,4 @@ $out2 = $add($val) # hellohello
|
||||
|
||||
test [fmt.printf("%s + %s is %s", $val, $val, $out2),] {} # simple concat
|
||||
-- OUTPUT --
|
||||
# err: errUnify: unify error with: topLevel(singleton(func(x) { call:_operator(str("+"), var(x), var(x)) })): type error: int != str
|
||||
# err: errUnify: unify error with: topLevel(singleton(func(x) { call:_operator(str("+"), var(x), var(x)) })): type error: str != int
|
||||
|
||||
13
lang/interpret_test/TestAstFunc2/stmtfor00.txtar
Normal file
13
lang/interpret_test/TestAstFunc2/stmtfor00.txtar
Normal file
@@ -0,0 +1,13 @@
|
||||
-- main.mcl --
|
||||
import "fmt"
|
||||
|
||||
$list = ["a", "b", "c",]
|
||||
|
||||
for $index, $value in $list {
|
||||
test [$value,] {}
|
||||
}
|
||||
|
||||
-- OUTPUT --
|
||||
Vertex: test[a]
|
||||
Vertex: test[b]
|
||||
Vertex: test[c]
|
||||
15
lang/interpret_test/TestAstFunc2/stmtfor01.txtar
Normal file
15
lang/interpret_test/TestAstFunc2/stmtfor01.txtar
Normal file
@@ -0,0 +1,15 @@
|
||||
-- main.mcl --
|
||||
import "fmt"
|
||||
|
||||
$list = ["a", "b", "c",]
|
||||
|
||||
for $index, $value in $list {
|
||||
$s = $value # our first major bug was triggered by this!
|
||||
test [$s,] {}
|
||||
}
|
||||
|
||||
# The buggy version would return "test[a]" three times!
|
||||
-- OUTPUT --
|
||||
Vertex: test[a]
|
||||
Vertex: test[b]
|
||||
Vertex: test[c]
|
||||
13
lang/interpret_test/TestAstFunc2/stmtfor02.txtar
Normal file
13
lang/interpret_test/TestAstFunc2/stmtfor02.txtar
Normal file
@@ -0,0 +1,13 @@
|
||||
-- main.mcl --
|
||||
import "fmt"
|
||||
|
||||
$list = ["a", "b", "c",]
|
||||
|
||||
for $index, $value in $list {
|
||||
test [fmt.printf("%s is %d", $value, $index),] {}
|
||||
}
|
||||
|
||||
-- OUTPUT --
|
||||
Vertex: test[a is 0]
|
||||
Vertex: test[b is 1]
|
||||
Vertex: test[c is 2]
|
||||
14
lang/interpret_test/TestAstFunc2/stmtfor03.txtar
Normal file
14
lang/interpret_test/TestAstFunc2/stmtfor03.txtar
Normal file
@@ -0,0 +1,14 @@
|
||||
-- main.mcl --
|
||||
import "fmt"
|
||||
|
||||
$list = ["a", "b", "c",]
|
||||
|
||||
for $index, $value in $list {
|
||||
$s = fmt.printf("%s is %d", $value, $index)
|
||||
test [$s,] {}
|
||||
}
|
||||
|
||||
-- OUTPUT --
|
||||
Vertex: test[a is 0]
|
||||
Vertex: test[b is 1]
|
||||
Vertex: test[c is 2]
|
||||
16
lang/interpret_test/TestAstFunc2/stmtfor04.txtar
Normal file
16
lang/interpret_test/TestAstFunc2/stmtfor04.txtar
Normal file
@@ -0,0 +1,16 @@
|
||||
-- main.mcl --
|
||||
import "fmt"
|
||||
|
||||
$list = ["a", "b", "c",]
|
||||
|
||||
$s = "nope" # should be out of scope
|
||||
|
||||
for $index, $value in $list {
|
||||
$s = fmt.printf("%s is %d", $value, $index)
|
||||
test [$s,] {}
|
||||
}
|
||||
|
||||
-- OUTPUT --
|
||||
Vertex: test[a is 0]
|
||||
Vertex: test[b is 1]
|
||||
Vertex: test[c is 2]
|
||||
16
lang/interpret_test/TestAstFunc2/stmtfor05.txtar
Normal file
16
lang/interpret_test/TestAstFunc2/stmtfor05.txtar
Normal file
@@ -0,0 +1,16 @@
|
||||
-- main.mcl --
|
||||
import "fmt"
|
||||
|
||||
$list = ["a", "b", "c",]
|
||||
|
||||
$value = "nope" # should be out of scope
|
||||
|
||||
for $index, $value in $list {
|
||||
$s = fmt.printf("%s is %d", $value, $index)
|
||||
test [$s,] {}
|
||||
}
|
||||
|
||||
-- OUTPUT --
|
||||
Vertex: test[a is 0]
|
||||
Vertex: test[b is 1]
|
||||
Vertex: test[c is 2]
|
||||
16
lang/interpret_test/TestAstFunc2/stmtfor06.txtar
Normal file
16
lang/interpret_test/TestAstFunc2/stmtfor06.txtar
Normal file
@@ -0,0 +1,16 @@
|
||||
-- main.mcl --
|
||||
import "fmt"
|
||||
|
||||
$list = ["a", "b", "c",]
|
||||
|
||||
$value = 42 # should be out of scope (also not the same type)
|
||||
|
||||
for $index, $value in $list {
|
||||
$s = fmt.printf("%s is %d", $value, $index)
|
||||
test [$s,] {}
|
||||
}
|
||||
|
||||
-- OUTPUT --
|
||||
Vertex: test[a is 0]
|
||||
Vertex: test[b is 1]
|
||||
Vertex: test[c is 2]
|
||||
17
lang/interpret_test/TestAstFunc2/stmtfor07.txtar
Normal file
17
lang/interpret_test/TestAstFunc2/stmtfor07.txtar
Normal file
@@ -0,0 +1,17 @@
|
||||
-- main.mcl --
|
||||
import "fmt"
|
||||
|
||||
$list = ["a", "b", "c",]
|
||||
|
||||
$index = 42 # should be out of scope
|
||||
|
||||
for $index, $value in $list {
|
||||
$index = 42
|
||||
$s = fmt.printf("%s is %d", $value, $index)
|
||||
test [$s,] {}
|
||||
}
|
||||
|
||||
-- OUTPUT --
|
||||
Vertex: test[a is 42]
|
||||
Vertex: test[b is 42]
|
||||
Vertex: test[c is 42]
|
||||
20
lang/interpret_test/TestAstFunc2/stmtfor08.txtar
Normal file
20
lang/interpret_test/TestAstFunc2/stmtfor08.txtar
Normal file
@@ -0,0 +1,20 @@
|
||||
-- main.mcl --
|
||||
import "fmt"
|
||||
import "math"
|
||||
|
||||
$list = ["a", "b", "c",]
|
||||
|
||||
$index = 42 # should be out of scope
|
||||
|
||||
for $index, $value in $list {
|
||||
$index = if math.mod($index, 2) == 0 {
|
||||
$index
|
||||
} else {
|
||||
42
|
||||
}
|
||||
$s = fmt.printf("%s is %d", $value, $index)
|
||||
test [$s,] {}
|
||||
}
|
||||
|
||||
-- OUTPUT --
|
||||
# err: errSetScope: recursive reference while setting scope: not a dag
|
||||
22
lang/interpret_test/TestAstFunc2/stmtfor09.txtar
Normal file
22
lang/interpret_test/TestAstFunc2/stmtfor09.txtar
Normal file
@@ -0,0 +1,22 @@
|
||||
-- main.mcl --
|
||||
import "fmt"
|
||||
import "math"
|
||||
|
||||
$list = ["a", "b", "c",]
|
||||
|
||||
$index = 42 # should be out of scope
|
||||
|
||||
for $index, $value in $list {
|
||||
$newindex = if math.mod($index, 2) == 0 {
|
||||
$index
|
||||
} else {
|
||||
42
|
||||
}
|
||||
$s = fmt.printf("%s is %d", $value, $newindex)
|
||||
test [$s,] {}
|
||||
}
|
||||
|
||||
-- OUTPUT --
|
||||
Vertex: test[a is 0]
|
||||
Vertex: test[b is 42]
|
||||
Vertex: test[c is 2]
|
||||
21
lang/interpret_test/TestAstFunc2/stmtfor10.txtar
Normal file
21
lang/interpret_test/TestAstFunc2/stmtfor10.txtar
Normal file
@@ -0,0 +1,21 @@
|
||||
-- main.mcl --
|
||||
import "fmt"
|
||||
|
||||
$list = ["a", "b", "c",]
|
||||
|
||||
$index = 42 # should be out of scope
|
||||
|
||||
for $index, $value in $list {
|
||||
|
||||
$fn = func($x) {
|
||||
"hello " + $x
|
||||
}
|
||||
|
||||
$s = fmt.printf("%s is %d", $fn($value), $index)
|
||||
test [$s,] {}
|
||||
}
|
||||
|
||||
-- OUTPUT --
|
||||
Vertex: test[hello a is 0]
|
||||
Vertex: test[hello b is 1]
|
||||
Vertex: test[hello c is 2]
|
||||
21
lang/interpret_test/TestAstFunc2/stmtfor11.txtar
Normal file
21
lang/interpret_test/TestAstFunc2/stmtfor11.txtar
Normal file
@@ -0,0 +1,21 @@
|
||||
-- main.mcl --
|
||||
import "fmt"
|
||||
|
||||
$list = ["a", "b", "c",]
|
||||
|
||||
$index = 42 # should be out of scope
|
||||
|
||||
for $index, $value in $list {
|
||||
|
||||
func fn($x) {
|
||||
"hello " + $x
|
||||
}
|
||||
|
||||
$s = fmt.printf("%s is %d", fn($value), $index)
|
||||
test [$s,] {}
|
||||
}
|
||||
|
||||
-- OUTPUT --
|
||||
Vertex: test[hello a is 0]
|
||||
Vertex: test[hello b is 1]
|
||||
Vertex: test[hello c is 2]
|
||||
21
lang/interpret_test/TestAstFunc2/stmtfor12.txtar
Normal file
21
lang/interpret_test/TestAstFunc2/stmtfor12.txtar
Normal file
@@ -0,0 +1,21 @@
|
||||
-- main.mcl --
|
||||
import "fmt"
|
||||
|
||||
$list = ["a", "b", "c",]
|
||||
|
||||
$index = 42 # should be out of scope
|
||||
|
||||
for $index, $value in $list {
|
||||
|
||||
$fn = func($x) {
|
||||
"hello " + $value
|
||||
}
|
||||
|
||||
$s = fmt.printf("%s is %d", $fn("whatever"), $index)
|
||||
test [$s,] {}
|
||||
}
|
||||
|
||||
-- OUTPUT --
|
||||
Vertex: test[hello a is 0]
|
||||
Vertex: test[hello b is 1]
|
||||
Vertex: test[hello c is 2]
|
||||
21
lang/interpret_test/TestAstFunc2/stmtfor13.txtar
Normal file
21
lang/interpret_test/TestAstFunc2/stmtfor13.txtar
Normal file
@@ -0,0 +1,21 @@
|
||||
-- main.mcl --
|
||||
import "fmt"
|
||||
|
||||
$list = ["a", "b", "c",]
|
||||
|
||||
$index = 42 # should be out of scope
|
||||
|
||||
for $index, $value in $list {
|
||||
|
||||
func fn($x) {
|
||||
"hello " + $value
|
||||
}
|
||||
|
||||
$s = fmt.printf("%s is %d", fn("whatever"), $index)
|
||||
test [$s,] {}
|
||||
}
|
||||
|
||||
-- OUTPUT --
|
||||
Vertex: test[hello a is 0]
|
||||
Vertex: test[hello b is 1]
|
||||
Vertex: test[hello c is 2]
|
||||
23
lang/interpret_test/TestAstFunc2/stmtfor14.txtar
Normal file
23
lang/interpret_test/TestAstFunc2/stmtfor14.txtar
Normal file
@@ -0,0 +1,23 @@
|
||||
-- main.mcl --
|
||||
import "fmt"
|
||||
|
||||
$list = ["a", "b", "c",]
|
||||
|
||||
$index = 42 # should be out of scope
|
||||
|
||||
for $index, $value in $list {
|
||||
|
||||
$fn = func($x) {
|
||||
"hello" + $val
|
||||
}
|
||||
|
||||
$val = " " + $value
|
||||
|
||||
$s = fmt.printf("%s is %d", $fn("whatever"), $index)
|
||||
test [$s,] {}
|
||||
}
|
||||
|
||||
-- OUTPUT --
|
||||
Vertex: test[hello a is 0]
|
||||
Vertex: test[hello b is 1]
|
||||
Vertex: test[hello c is 2]
|
||||
23
lang/interpret_test/TestAstFunc2/stmtfor15.txtar
Normal file
23
lang/interpret_test/TestAstFunc2/stmtfor15.txtar
Normal file
@@ -0,0 +1,23 @@
|
||||
-- main.mcl --
|
||||
import "fmt"
|
||||
|
||||
$list = ["a", "b", "c",]
|
||||
|
||||
$index = 42 # should be out of scope
|
||||
|
||||
for $index, $value in $list {
|
||||
|
||||
func fn($x) {
|
||||
"hello" + $val
|
||||
}
|
||||
|
||||
$val = " " + $value
|
||||
|
||||
$s = fmt.printf("%s is %d", fn("whatever"), $index)
|
||||
test [$s,] {}
|
||||
}
|
||||
|
||||
-- OUTPUT --
|
||||
Vertex: test[hello a is 0]
|
||||
Vertex: test[hello b is 1]
|
||||
Vertex: test[hello c is 2]
|
||||
22
lang/interpret_test/TestAstFunc2/stmtfor16.txtar
Normal file
22
lang/interpret_test/TestAstFunc2/stmtfor16.txtar
Normal file
@@ -0,0 +1,22 @@
|
||||
-- main.mcl --
|
||||
import "fmt"
|
||||
|
||||
$list = ["a", "b", "c",]
|
||||
|
||||
$index = 42 # should be out of scope
|
||||
|
||||
for $index, $value in $list {
|
||||
|
||||
class foo($x) {
|
||||
$result = "hello " + $x
|
||||
}
|
||||
include foo($value) as included
|
||||
|
||||
$s = fmt.printf("%s is %d", $included.result, $index)
|
||||
test [$s,] {}
|
||||
}
|
||||
|
||||
-- OUTPUT --
|
||||
Vertex: test[hello a is 0]
|
||||
Vertex: test[hello b is 1]
|
||||
Vertex: test[hello c is 2]
|
||||
22
lang/interpret_test/TestAstFunc2/stmtfor17.txtar
Normal file
22
lang/interpret_test/TestAstFunc2/stmtfor17.txtar
Normal file
@@ -0,0 +1,22 @@
|
||||
-- main.mcl --
|
||||
import "fmt"
|
||||
|
||||
$list = ["a", "b", "c",]
|
||||
|
||||
$word = "hello"
|
||||
|
||||
for $index, $value in $list {
|
||||
|
||||
class foo($x) {
|
||||
$result = $word + " " + $x
|
||||
}
|
||||
include foo($value) as included
|
||||
|
||||
$s = fmt.printf("%s is %d", $included.result, $index)
|
||||
test [$s,] {}
|
||||
}
|
||||
|
||||
-- OUTPUT --
|
||||
Vertex: test[hello a is 0]
|
||||
Vertex: test[hello b is 1]
|
||||
Vertex: test[hello c is 2]
|
||||
22
lang/interpret_test/TestAstFunc2/stmtfor18.txtar
Normal file
22
lang/interpret_test/TestAstFunc2/stmtfor18.txtar
Normal file
@@ -0,0 +1,22 @@
|
||||
-- main.mcl --
|
||||
import "fmt"
|
||||
|
||||
$list = ["a", "b", "c",]
|
||||
|
||||
$index = 42 # should be out of scope
|
||||
|
||||
for $index, $value in $list {
|
||||
|
||||
class foo($x) {
|
||||
$result = "hello " + $value
|
||||
}
|
||||
include foo("whatever") as included
|
||||
|
||||
$s = fmt.printf("%s is %d", $included.result, $index)
|
||||
test [$s,] {}
|
||||
}
|
||||
|
||||
-- OUTPUT --
|
||||
Vertex: test[hello a is 0]
|
||||
Vertex: test[hello b is 1]
|
||||
Vertex: test[hello c is 2]
|
||||
22
lang/interpret_test/TestAstFunc2/stmtfor19.txtar
Normal file
22
lang/interpret_test/TestAstFunc2/stmtfor19.txtar
Normal file
@@ -0,0 +1,22 @@
|
||||
-- main.mcl --
|
||||
import "fmt"
|
||||
|
||||
$list = ["a", "b", "c",]
|
||||
|
||||
$word = "hello"
|
||||
|
||||
for $index, $value in $list {
|
||||
|
||||
class foo($x) {
|
||||
$result = $word + " " + $value
|
||||
}
|
||||
include foo("whatever") as included
|
||||
|
||||
$s = fmt.printf("%s is %d", $included.result, $index)
|
||||
test [$s,] {}
|
||||
}
|
||||
|
||||
-- OUTPUT --
|
||||
Vertex: test[hello a is 0]
|
||||
Vertex: test[hello b is 1]
|
||||
Vertex: test[hello c is 2]
|
||||
24
lang/interpret_test/TestAstFunc2/stmtfor20.txtar
Normal file
24
lang/interpret_test/TestAstFunc2/stmtfor20.txtar
Normal file
@@ -0,0 +1,24 @@
|
||||
-- main.mcl --
|
||||
import "fmt"
|
||||
|
||||
$list = ["a", "b", "c",]
|
||||
|
||||
$index = 42 # should be out of scope
|
||||
|
||||
for $index, $value in $list {
|
||||
|
||||
class foo($x) {
|
||||
$result = "hello" + $val
|
||||
}
|
||||
include foo("whatever") as included
|
||||
|
||||
$val = " " + $value
|
||||
|
||||
$s = fmt.printf("%s is %d", $included.result, $index)
|
||||
test [$s,] {}
|
||||
}
|
||||
|
||||
-- OUTPUT --
|
||||
Vertex: test[hello a is 0]
|
||||
Vertex: test[hello b is 1]
|
||||
Vertex: test[hello c is 2]
|
||||
24
lang/interpret_test/TestAstFunc2/stmtfor21.txtar
Normal file
24
lang/interpret_test/TestAstFunc2/stmtfor21.txtar
Normal file
@@ -0,0 +1,24 @@
|
||||
-- main.mcl --
|
||||
import "fmt"
|
||||
|
||||
$list = ["a", "b", "c",]
|
||||
|
||||
$word = "hello"
|
||||
|
||||
for $index, $value in $list {
|
||||
|
||||
class foo($x) {
|
||||
$result = $word + $val
|
||||
}
|
||||
include foo("whatever") as included
|
||||
|
||||
$val = " " + $value
|
||||
|
||||
$s = fmt.printf("%s is %d", $included.result, $index)
|
||||
test [$s,] {}
|
||||
}
|
||||
|
||||
-- OUTPUT --
|
||||
Vertex: test[hello a is 0]
|
||||
Vertex: test[hello b is 1]
|
||||
Vertex: test[hello c is 2]
|
||||
24
lang/interpret_test/TestAstFunc2/stmtfor22.txtar
Normal file
24
lang/interpret_test/TestAstFunc2/stmtfor22.txtar
Normal file
@@ -0,0 +1,24 @@
|
||||
-- main.mcl --
|
||||
import "fmt"
|
||||
|
||||
$list1 = ["a", "b", "c",]
|
||||
$list2 = [42, 13, -4,]
|
||||
|
||||
for $index1, $value1 in $list1 {
|
||||
for $index2, $value2 in $list2 {
|
||||
|
||||
$s = fmt.printf("%s is %d", $value1, $value2)
|
||||
test [$s,] {}
|
||||
}
|
||||
}
|
||||
|
||||
-- OUTPUT --
|
||||
Vertex: test[a is 42]
|
||||
Vertex: test[b is 42]
|
||||
Vertex: test[c is 42]
|
||||
Vertex: test[a is 13]
|
||||
Vertex: test[b is 13]
|
||||
Vertex: test[c is 13]
|
||||
Vertex: test[a is -4]
|
||||
Vertex: test[b is -4]
|
||||
Vertex: test[c is -4]
|
||||
31
lang/interpret_test/TestAstFunc2/stmtfor23.txtar
Normal file
31
lang/interpret_test/TestAstFunc2/stmtfor23.txtar
Normal file
@@ -0,0 +1,31 @@
|
||||
-- main.mcl --
|
||||
import "fmt"
|
||||
|
||||
$list0 = ["a", "b", "c",]
|
||||
$list1 = ["d", "e", "f",]
|
||||
$list2 = ["g", "h", "i",]
|
||||
$list3 = ["j", "k", "l",]
|
||||
|
||||
$list = [$list0, $list1, $list2, $list3,]
|
||||
|
||||
for $index, $value in $list {
|
||||
for $i, $v in $value {
|
||||
|
||||
$s = fmt.printf("%s is %d", $v, $i+$index)
|
||||
test [$s,] {}
|
||||
}
|
||||
}
|
||||
|
||||
-- OUTPUT --
|
||||
Vertex: test[a is 0]
|
||||
Vertex: test[b is 1]
|
||||
Vertex: test[c is 2]
|
||||
Vertex: test[d is 1]
|
||||
Vertex: test[e is 2]
|
||||
Vertex: test[f is 3]
|
||||
Vertex: test[g is 2]
|
||||
Vertex: test[h is 3]
|
||||
Vertex: test[i is 4]
|
||||
Vertex: test[j is 3]
|
||||
Vertex: test[k is 4]
|
||||
Vertex: test[l is 5]
|
||||
17
lang/interpret_test/TestAstFunc2/stmtfor24.txtar
Normal file
17
lang/interpret_test/TestAstFunc2/stmtfor24.txtar
Normal file
@@ -0,0 +1,17 @@
|
||||
-- main.mcl --
|
||||
import "fmt"
|
||||
|
||||
$list = ["a",]
|
||||
|
||||
for $index, $value in $list {
|
||||
|
||||
$fn = func() {
|
||||
"hello " + $value
|
||||
}
|
||||
|
||||
$s = $fn()
|
||||
test [$s,] {}
|
||||
}
|
||||
|
||||
-- OUTPUT --
|
||||
Vertex: test[hello a]
|
||||
17
lang/interpret_test/TestAstFunc2/stmtfor25.txtar
Normal file
17
lang/interpret_test/TestAstFunc2/stmtfor25.txtar
Normal file
@@ -0,0 +1,17 @@
|
||||
-- main.mcl --
|
||||
import "fmt"
|
||||
|
||||
$list = ["a",]
|
||||
|
||||
for $index, $value in $list {
|
||||
|
||||
func fn() {
|
||||
"hello " + $value
|
||||
}
|
||||
|
||||
$s = fn()
|
||||
test [$s,] {}
|
||||
}
|
||||
|
||||
-- OUTPUT --
|
||||
Vertex: test[hello a]
|
||||
41
lang/interpret_test/TestAstFunc2/stmtfor26.txtar
Normal file
41
lang/interpret_test/TestAstFunc2/stmtfor26.txtar
Normal file
@@ -0,0 +1,41 @@
|
||||
-- main.mcl --
|
||||
import "fmt"
|
||||
|
||||
$list1 = ["a", "b", "c",]
|
||||
$list2 = ["x", "y", "z",]
|
||||
|
||||
$word = "hello"
|
||||
|
||||
for $index1, $value1 in $list1 {
|
||||
for $index2, $value2 in $list2 {
|
||||
|
||||
class foo($x, $y) {
|
||||
$result = "hello " + $x + $y + $value1 + $value2
|
||||
$result1 = $x + $value1
|
||||
$result2 = $y + $value2
|
||||
}
|
||||
include foo($value1, $value2) as included
|
||||
|
||||
$s = fmt.printf("%s is {%d,%d}", $included.result, $index1, $index2)
|
||||
$s1 = fmt.printf("one: %s", $included.result1)
|
||||
$s2 = fmt.printf("two: %s", $included.result2)
|
||||
test [$s, $s1, $s2,] {}
|
||||
}
|
||||
}
|
||||
|
||||
-- OUTPUT --
|
||||
Vertex: test[hello axax is {0,0}]
|
||||
Vertex: test[hello ayay is {0,1}]
|
||||
Vertex: test[hello azaz is {0,2}]
|
||||
Vertex: test[hello bxbx is {1,0}]
|
||||
Vertex: test[hello byby is {1,1}]
|
||||
Vertex: test[hello bzbz is {1,2}]
|
||||
Vertex: test[hello cxcx is {2,0}]
|
||||
Vertex: test[hello cycy is {2,1}]
|
||||
Vertex: test[hello czcz is {2,2}]
|
||||
Vertex: test[one: aa]
|
||||
Vertex: test[one: bb]
|
||||
Vertex: test[one: cc]
|
||||
Vertex: test[two: xx]
|
||||
Vertex: test[two: yy]
|
||||
Vertex: test[two: zz]
|
||||
38
lang/interpret_test/TestAstFunc2/stmtfor27.txtar
Normal file
38
lang/interpret_test/TestAstFunc2/stmtfor27.txtar
Normal file
@@ -0,0 +1,38 @@
|
||||
-- main.mcl --
|
||||
import "fmt"
|
||||
|
||||
$list1 = ["a", "b", "c",]
|
||||
$list2 = ["x", "y", "z",]
|
||||
|
||||
$word = "hello"
|
||||
|
||||
for $index1, $value1 in $list1 {
|
||||
|
||||
class foo($x, $y) {
|
||||
$result = "hello " + $x + $y + $value1
|
||||
$result1 = $x + $value1
|
||||
}
|
||||
|
||||
for $index2, $value2 in $list2 {
|
||||
|
||||
include foo($value1, $value2) as included
|
||||
|
||||
$s = fmt.printf("%s is {%d,%d}", $included.result, $index1, $index2)
|
||||
$s1 = fmt.printf("one: %s", $included.result1)
|
||||
test [$s, $s1,] {}
|
||||
}
|
||||
}
|
||||
|
||||
-- OUTPUT --
|
||||
Vertex: test[hello axa is {0,0}]
|
||||
Vertex: test[hello aya is {0,1}]
|
||||
Vertex: test[hello aza is {0,2}]
|
||||
Vertex: test[hello bxb is {1,0}]
|
||||
Vertex: test[hello byb is {1,1}]
|
||||
Vertex: test[hello bzb is {1,2}]
|
||||
Vertex: test[hello cxc is {2,0}]
|
||||
Vertex: test[hello cyc is {2,1}]
|
||||
Vertex: test[hello czc is {2,2}]
|
||||
Vertex: test[one: aa]
|
||||
Vertex: test[one: bb]
|
||||
Vertex: test[one: cc]
|
||||
19
lang/interpret_test/TestAstFunc2/stmtfor28.txtar
Normal file
19
lang/interpret_test/TestAstFunc2/stmtfor28.txtar
Normal file
@@ -0,0 +1,19 @@
|
||||
-- main.mcl --
|
||||
import "fmt"
|
||||
|
||||
$list = ["a", "b", "c",]
|
||||
|
||||
for $i, $x in $list {
|
||||
|
||||
func foo($y) {
|
||||
"hello" + $x + $y
|
||||
}
|
||||
|
||||
$s = foo($x)
|
||||
|
||||
test [$s,] {}
|
||||
}
|
||||
-- OUTPUT --
|
||||
Vertex: test[helloaa]
|
||||
Vertex: test[hellobb]
|
||||
Vertex: test[hellocc]
|
||||
22
lang/interpret_test/TestAstFunc2/stmtfor29.txtar
Normal file
22
lang/interpret_test/TestAstFunc2/stmtfor29.txtar
Normal file
@@ -0,0 +1,22 @@
|
||||
-- main.mcl --
|
||||
import "fmt"
|
||||
|
||||
$list = ["a", "b", "c",]
|
||||
|
||||
$index = 42 # should be out of scope
|
||||
|
||||
for $index, $value in $list {
|
||||
|
||||
class foo() {
|
||||
test [$value + fmt.printf("%d", $index),] {}
|
||||
}
|
||||
include foo() # as included
|
||||
|
||||
#$s = fmt.printf("%s is %d", $included.result, $index)
|
||||
#test [$s,] {}
|
||||
}
|
||||
|
||||
-- OUTPUT --
|
||||
Vertex: test[a0]
|
||||
Vertex: test[b1]
|
||||
Vertex: test[c2]
|
||||
23
lang/interpret_test/TestAstFunc2/stmtfor30.txtar
Normal file
23
lang/interpret_test/TestAstFunc2/stmtfor30.txtar
Normal file
@@ -0,0 +1,23 @@
|
||||
-- main.mcl --
|
||||
import "fmt"
|
||||
|
||||
$list = ["a", "b", "c",]
|
||||
|
||||
$index = 42 # should be out of scope
|
||||
|
||||
for $index, $value in $list {
|
||||
|
||||
class foo($x) {
|
||||
$result = "hello " + $x
|
||||
test [$result,] {}
|
||||
}
|
||||
include foo($value) # as included
|
||||
|
||||
#$s = fmt.printf("%s is %d", $included.result, $index)
|
||||
#test [$s,] {}
|
||||
}
|
||||
|
||||
-- OUTPUT --
|
||||
Vertex: test[hello a]
|
||||
Vertex: test[hello b]
|
||||
Vertex: test[hello c]
|
||||
24
lang/interpret_test/TestAstFunc2/stmtfor31.txtar
Normal file
24
lang/interpret_test/TestAstFunc2/stmtfor31.txtar
Normal file
@@ -0,0 +1,24 @@
|
||||
-- main.mcl --
|
||||
import "fmt"
|
||||
|
||||
$list = ["a", "b", "c",]
|
||||
|
||||
$index = 42 # should be out of scope
|
||||
|
||||
for $index, $value in $list {
|
||||
|
||||
class foo($x) {
|
||||
func result($s) {
|
||||
$s + $x + $value
|
||||
}
|
||||
}
|
||||
include foo($value) as included
|
||||
|
||||
$s = fmt.printf("%s is %d", included.result($value), $index)
|
||||
test [$s,] {}
|
||||
}
|
||||
|
||||
-- OUTPUT --
|
||||
Vertex: test[aaa is 0]
|
||||
Vertex: test[bbb is 1]
|
||||
Vertex: test[ccc is 2]
|
||||
24
lang/interpret_test/TestAstFunc2/stmtfor32.txtar
Normal file
24
lang/interpret_test/TestAstFunc2/stmtfor32.txtar
Normal file
@@ -0,0 +1,24 @@
|
||||
-- main.mcl --
|
||||
import "fmt"
|
||||
|
||||
$list = ["a", "b", "c",]
|
||||
|
||||
$index = 42 # should be out of scope
|
||||
|
||||
for $index, $value in $list {
|
||||
|
||||
class foo($x) {
|
||||
$result = "hello" + $x + $value
|
||||
}
|
||||
include foo($value) as thing
|
||||
$result = "please"
|
||||
|
||||
# XXX: add $thing.some_func and so on... add more tests says sam.
|
||||
$s = fmt.printf("%s is %d is %s", $thing.result, $index, $result)
|
||||
test [$s,] {}
|
||||
}
|
||||
|
||||
-- OUTPUT --
|
||||
Vertex: test[helloaa is 0 is please]
|
||||
Vertex: test[hellobb is 1 is please]
|
||||
Vertex: test[hellocc is 2 is please]
|
||||
24
lang/interpret_test/TestAstFunc2/stmtfor33.txtar
Normal file
24
lang/interpret_test/TestAstFunc2/stmtfor33.txtar
Normal file
@@ -0,0 +1,24 @@
|
||||
-- main.mcl --
|
||||
import "fmt"
|
||||
|
||||
$list = ["a", "b", "c",]
|
||||
|
||||
$index = 42 # should be out of scope
|
||||
|
||||
for $index, $value in $list {
|
||||
|
||||
class foo($x) {
|
||||
$result = func($s) {
|
||||
$s + $x + $value
|
||||
}
|
||||
}
|
||||
include foo($value) as included
|
||||
|
||||
$s = fmt.printf("%s is %d", $included.result($value), $index)
|
||||
test [$s,] {}
|
||||
}
|
||||
|
||||
-- OUTPUT --
|
||||
Vertex: test[aaa is 0]
|
||||
Vertex: test[bbb is 1]
|
||||
Vertex: test[ccc is 2]
|
||||
23
lang/interpret_test/TestAstFunc2/stmtfor34.txtar
Normal file
23
lang/interpret_test/TestAstFunc2/stmtfor34.txtar
Normal file
@@ -0,0 +1,23 @@
|
||||
-- main.mcl --
|
||||
import "fmt"
|
||||
|
||||
$list = ["a", "b", "c",]
|
||||
|
||||
$index = 42 # should be out of scope
|
||||
|
||||
for $index, $value in $list {
|
||||
|
||||
class foo() {
|
||||
$result = "hello " + $value + fmt.printf("%d", $index)
|
||||
test [$result,] {}
|
||||
}
|
||||
include foo() # as included
|
||||
|
||||
#$s = fmt.printf("%s is %d", $included.result, $index)
|
||||
#test [$s,] {}
|
||||
}
|
||||
|
||||
-- OUTPUT --
|
||||
Vertex: test[hello a0]
|
||||
Vertex: test[hello b1]
|
||||
Vertex: test[hello c2]
|
||||
32
lang/interpret_test/TestAstFunc2/stmtfor35.txtar
Normal file
32
lang/interpret_test/TestAstFunc2/stmtfor35.txtar
Normal file
@@ -0,0 +1,32 @@
|
||||
-- main.mcl --
|
||||
import "fmt"
|
||||
|
||||
$list1 = ["a", "b", "c",]
|
||||
$list2 = ["x", "y", "z",]
|
||||
|
||||
$word = "hello"
|
||||
|
||||
class foo($x, $y) {
|
||||
$result = "hello " + $x + $y
|
||||
}
|
||||
|
||||
for $index1, $value1 in $list1 {
|
||||
for $index2, $value2 in $list2 {
|
||||
|
||||
include foo($value1, $value2) as included
|
||||
|
||||
$s = fmt.printf("%s is {%d,%d}", $included.result, $index1, $index2)
|
||||
test [$s,] {}
|
||||
}
|
||||
}
|
||||
|
||||
-- OUTPUT --
|
||||
Vertex: test[hello ax is {0,0}]
|
||||
Vertex: test[hello ay is {0,1}]
|
||||
Vertex: test[hello az is {0,2}]
|
||||
Vertex: test[hello bx is {1,0}]
|
||||
Vertex: test[hello by is {1,1}]
|
||||
Vertex: test[hello bz is {1,2}]
|
||||
Vertex: test[hello cx is {2,0}]
|
||||
Vertex: test[hello cy is {2,1}]
|
||||
Vertex: test[hello cz is {2,2}]
|
||||
29
lang/interpret_test/TestAstFunc2/stmtfor36.txtar
Normal file
29
lang/interpret_test/TestAstFunc2/stmtfor36.txtar
Normal file
@@ -0,0 +1,29 @@
|
||||
-- main.mcl --
|
||||
import "fmt"
|
||||
|
||||
$list = ["a", "b", "c",]
|
||||
|
||||
#$index = 42 # should be out of scope
|
||||
|
||||
for $index, $value in $list {
|
||||
|
||||
class foo($x) {
|
||||
#$result = "hello" + $x + $value # harder
|
||||
#$result = $value # works
|
||||
#$result = $x # works
|
||||
$resultx = "hello" + $x # harder
|
||||
#$result = "hello" + $value # harder
|
||||
#$result = $x + $value # harder
|
||||
}
|
||||
include foo($value)# as included
|
||||
$result = "please"
|
||||
|
||||
# XXX: add $included.some_func and so on... add more tests says sam.
|
||||
$s = fmt.printf("%s is %d is %s", $value, $index, $result)
|
||||
test [$s,] {}
|
||||
}
|
||||
|
||||
-- OUTPUT --
|
||||
Vertex: test[a is 0 is please]
|
||||
Vertex: test[b is 1 is please]
|
||||
Vertex: test[c is 2 is please]
|
||||
28
lang/interpret_test/TestAstFunc2/stmtfor37.txtar
Normal file
28
lang/interpret_test/TestAstFunc2/stmtfor37.txtar
Normal file
@@ -0,0 +1,28 @@
|
||||
-- main.mcl --
|
||||
import "fmt"
|
||||
|
||||
$list = ["a", "b", "c",]
|
||||
|
||||
$index = 42 # should be out of scope
|
||||
|
||||
for $index, $value in $list {
|
||||
|
||||
class foo($x) {
|
||||
$result = func($y1) {
|
||||
"hello" + $x + $value + $y1
|
||||
}
|
||||
}
|
||||
include foo($value) as thing
|
||||
$result = func($y2) {
|
||||
"please" + $y2
|
||||
}
|
||||
|
||||
# XXX: add $thing.some_func and so on... add more tests says sam.
|
||||
$s = fmt.printf("%s is %d is %s", $thing.result("!"), $index, $result("!"))
|
||||
test [$s,] {}
|
||||
}
|
||||
|
||||
-- OUTPUT --
|
||||
Vertex: test[helloaa! is 0 is please!]
|
||||
Vertex: test[hellobb! is 1 is please!]
|
||||
Vertex: test[hellocc! is 2 is please!]
|
||||
21
lang/interpret_test/TestAstFunc2/stmtfor38.txtar
Normal file
21
lang/interpret_test/TestAstFunc2/stmtfor38.txtar
Normal file
@@ -0,0 +1,21 @@
|
||||
-- main.mcl --
|
||||
import "fmt"
|
||||
|
||||
$list1 = ["a", "b", "c",]
|
||||
|
||||
class foo($x) {
|
||||
$result = "hello " + $x
|
||||
}
|
||||
|
||||
for $index, $value in $list1 {
|
||||
|
||||
include foo($value) as included
|
||||
|
||||
$s = fmt.printf("%s is %d", $included.result, $index)
|
||||
test [$s,] {}
|
||||
}
|
||||
|
||||
-- OUTPUT --
|
||||
Vertex: test[hello a is 0]
|
||||
Vertex: test[hello b is 1]
|
||||
Vertex: test[hello c is 2]
|
||||
21
lang/interpret_test/TestAstFunc2/stmtfor39.txtar
Normal file
21
lang/interpret_test/TestAstFunc2/stmtfor39.txtar
Normal file
@@ -0,0 +1,21 @@
|
||||
-- main.mcl --
|
||||
import "fmt"
|
||||
|
||||
$list1 = ["a", "b", "c",]
|
||||
|
||||
class foo($x) {
|
||||
$result = "hello " + fmt.printf("%d", $x)
|
||||
}
|
||||
|
||||
for $index1, $value1 in $list1 {
|
||||
|
||||
include foo($index1) as included
|
||||
|
||||
$s = fmt.printf("%s is %d", $included.result, $index1)
|
||||
test [$s,] {}
|
||||
}
|
||||
|
||||
-- OUTPUT --
|
||||
Vertex: test[hello 0 is 0]
|
||||
Vertex: test[hello 1 is 1]
|
||||
Vertex: test[hello 2 is 2]
|
||||
10
lang/interpret_test/TestAstFunc2/stmtfor40.txtar
Normal file
10
lang/interpret_test/TestAstFunc2/stmtfor40.txtar
Normal file
@@ -0,0 +1,10 @@
|
||||
-- main.mcl --
|
||||
import "fmt"
|
||||
|
||||
$list = ["a", "b", "c",]
|
||||
|
||||
for $index, $value in $list {
|
||||
$foo = $index # does nothing
|
||||
}
|
||||
|
||||
-- OUTPUT --
|
||||
24
lang/interpret_test/TestAstFunc2/stmtfor41.txtar
Normal file
24
lang/interpret_test/TestAstFunc2/stmtfor41.txtar
Normal file
@@ -0,0 +1,24 @@
|
||||
-- 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]
|
||||
27
lang/interpret_test/TestAstFunc2/stmtfor42.txtar
Normal file
27
lang/interpret_test/TestAstFunc2/stmtfor42.txtar
Normal file
@@ -0,0 +1,27 @@
|
||||
-- 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
|
||||
class foo($y) {
|
||||
#import "thing.mcl" # XXX: doesn't work :(
|
||||
$out = $y + 7 + $thing.inside
|
||||
}
|
||||
include foo($index) as usefoo
|
||||
|
||||
$x = 42 + $usefoo.out
|
||||
|
||||
$s = fmt.printf("%s is %d = %d", $value, $index, $x)
|
||||
test [$s,] {}
|
||||
}
|
||||
-- thing.mcl --
|
||||
$inside = 13
|
||||
-- OUTPUT --
|
||||
Vertex: test[a is 0 = 62]
|
||||
Vertex: test[b is 1 = 63]
|
||||
Vertex: test[c is 2 = 64]
|
||||
22
lang/interpret_test/TestAstFunc2/stmtfor43.txtar
Normal file
22
lang/interpret_test/TestAstFunc2/stmtfor43.txtar
Normal file
@@ -0,0 +1,22 @@
|
||||
-- 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: We don't want this to be an error, but it is for now.
|
||||
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 --
|
||||
# err: errInit: a StmtImport can't be contained inside a StmtFor
|
||||
@@ -10,4 +10,4 @@ test "test2" {
|
||||
anotherstr => $id("hello"),
|
||||
}
|
||||
-- OUTPUT --
|
||||
# err: errUnify: unify error with: topLevel(singleton(func(x) { var(x) })): type error: int != str
|
||||
# err: errUnify: unify error with: topLevel(singleton(func(x) { var(x) })): type error: str != int
|
||||
|
||||
@@ -12,4 +12,4 @@ class use_polymorphically($id) {
|
||||
}
|
||||
include use_polymorphically(func($x) {$x})
|
||||
-- OUTPUT --
|
||||
# err: errUnify: unify error with: topLevel(singleton(func(x) { var(x) })): type error: int != str
|
||||
# err: errUnify: unify error with: topLevel(singleton(func(x) { var(x) })): type error: str != int
|
||||
|
||||
11
lang/interpret_test/TestAstFunc2/wat.txtar
Normal file
11
lang/interpret_test/TestAstFunc2/wat.txtar
Normal file
@@ -0,0 +1,11 @@
|
||||
-- main.mcl --
|
||||
$x = ["foo", "bar",]
|
||||
$f = func() {
|
||||
$x[0]
|
||||
}
|
||||
$y = $f()
|
||||
$z = $f()
|
||||
|
||||
test ["${y}${z}",] {}
|
||||
-- OUTPUT --
|
||||
Vertex: test[foofoo]
|
||||
14
lang/interpret_test/TestAstFunc2/watsam1.txtar
Normal file
14
lang/interpret_test/TestAstFunc2/watsam1.txtar
Normal file
@@ -0,0 +1,14 @@
|
||||
-- main.mcl --
|
||||
$call = func($f, $arg) {
|
||||
$f($arg)
|
||||
}
|
||||
|
||||
$lambda = func($x) {
|
||||
$call(func($z) { "hello" + $x }, "nope")
|
||||
}
|
||||
|
||||
$s = $lambda("world")
|
||||
|
||||
test [$s,] {}
|
||||
-- OUTPUT --
|
||||
Vertex: test[helloworld]
|
||||
21
lang/interpret_test/TestAstFunc2/watsam2.txtar
Normal file
21
lang/interpret_test/TestAstFunc2/watsam2.txtar
Normal file
@@ -0,0 +1,21 @@
|
||||
-- main.mcl --
|
||||
$call = func($f, $arg) {
|
||||
$f($arg)
|
||||
}
|
||||
|
||||
$lambda = func($x) {
|
||||
$call(
|
||||
if $x == "nope1" {
|
||||
func($z) { "nope2" + $x }
|
||||
} else {
|
||||
func($z) { "hello" + $x }
|
||||
},
|
||||
"bye"
|
||||
)
|
||||
}
|
||||
|
||||
$s = $lambda("world")
|
||||
|
||||
test [$s,] {}
|
||||
-- OUTPUT --
|
||||
Vertex: test[helloworld]
|
||||
Reference in New Issue
Block a user