Debian Codegraph @

lang/interpret_test/TestAstFunc2/nested-changing-scoped-lambda.txtar

raw
1 -- main.mcl --
2 import "fmt"
3 # this can return changing functions, and could be optimized, too
4 $funcgen = func($a) {
5 if $a {
6 func($b) {
7 if $b == "hello" {
8 func($c1) {
9 fmt.printf("true-true-%d", $c1)
10 }
11 } else {
12 func($c2) {
13 if $c2 >= 0 {
14 fmt.printf("true-false-%d", $c2)
15 } else {
16 "true-false-negative"
17 }
18 }
19 }
20 }
21 } else {
22 func($b) {
23 if $b == "hello" {
24 func($c3) {
25 if $c3 > 20 {
26 "false-true-big"
27 } else {
28 "false-true-small"
29 }
30 }
31 } else {
32 func($c4) { # $c4 is ignored, $b is from parent
33 "false-false-some-" + $b
34 }
35 }
36 }
37 }
38 }
39
40 $fn1 = $funcgen(true)
41 $fn2 = $funcgen(false)
42
43 $out1 = $fn1("hello")
44 $out2 = $fn1("world")
45 $out3 = $fn2("hello")
46 $out4 = $fn2("world")
47
48 test [$out1(10),] {}
49 test [if true { $out2(20) } else { $out2(-20) },] {}
50 test [if false { $out2(20) } else { $out2(-20) },] {}
51 test [$out3(30),] {}
52 test [$out4(40),] {}
53 -- OUTPUT --
54 Vertex: test[true-true-10]
55 Vertex: test[true-false-20]
56 Vertex: test[true-false-negative]
57 Vertex: test[false-true-big]
58 Vertex: test[false-false-some-world]