test, examples: lang: Add new syntax for map lookup

Sugar at last!
This commit is contained in:
James Shubin
2023-10-17 15:32:13 -04:00
parent ea0af4dc43
commit 96093984e4
10 changed files with 18 additions and 23 deletions

View File

@@ -2,7 +2,7 @@ import "fmt"
import "sys"
$env = sys.env()
$m = map_lookup_optional($env, "GOPATH", "")
$m = $env["GOPATH"] || ""
print "print0" {
msg => if sys.hasenv("GOPATH") {

View File

@@ -2,13 +2,13 @@ import "fmt"
$m = {"k1" => 42, "k2" => 13,}
$found = map_lookup_optional($m, "k1", 99)
$found = $m["k1"] || 99
print "print1" {
msg => fmt.printf("found value of: %d", $found),
}
$notfound = map_lookup_optional($m, "k3", 99)
$notfound = $m["k3"] || 99
print "print2" {
msg => fmt.printf("notfound value of: %d", $notfound),

View File

@@ -3,7 +3,7 @@ import "world"
$ns = "estate"
$exchanged = world.kvlookup($ns)
$state = map_lookup_optional($exchanged, $hostname, "default")
$state = $exchanged[$hostname] || "default"
exec "exec0" {
cmd => "echo hello world && echo goodbye world 1>&2", # to stdout && stderr

View File

@@ -2,7 +2,7 @@ import "world"
$ns = "estate"
$exchanged = world.kvlookup($ns)
$state = map_lookup_optional($exchanged, $hostname, "default")
$state = $exchanged[$hostname] || "default"
if $state == "one" or $state == "default" {

View File

@@ -4,7 +4,7 @@ import "world"
$ns = "estate"
$exchanged = world.kvlookup($ns)
$state = map_lookup_optional($exchanged, $hostname, "default")
$state = $exchanged[$hostname] || "default"
if $state == "one" or $state == "default" {

View File

@@ -16,10 +16,8 @@ $generate = func($idn) {
$foo = iter.map([$id1, $id2,], $generate)
#test $foo[0] {}
#test $foo[1] {}
test list_lookup_optional($foo, 0, "fail") {} # TODO: add syntactic sugar for list_lookup_optional
test list_lookup_optional($foo, 1, "fail") {} # TODO: add syntactic sugar for list_lookup_optional
test $foo[0] || "fail" {}
test $foo[1] || "fail" {}
-- OUTPUT --
Vertex: test[foo]
Vertex: test[foofoo]

View File

@@ -3,12 +3,10 @@ $l1 = ["a", "b", "c",]
$l2 = [$l1, ["hello", "world",],]
#test $l1[0] {}
#test $l1[1] {}
test list_lookup_optional($l1, 0, "fail") {} # TODO: add syntactic sugar for list_lookup_optional
test list_lookup_optional($l1, 2, "fail") {} # TODO: add syntactic sugar for list_lookup_optional
test list_lookup_optional($l1, 3, "pass") {} # TODO: add syntactic sugar for list_lookup_optional
test list_lookup_optional($l2, 1, ["fail",]) {} # TODO: add syntactic sugar for list_lookup_optional
test $l1[0] || "fail" {}
test $l1[2] || "fail" {}
test $l1[3] || "pass" {}
test $l2[1] || ["fail",] {}
-- OUTPUT --
Vertex: test[a]
Vertex: test[c]

View File

@@ -1,15 +1,15 @@
-- main.mcl --
$map1 map{int: str} = {42 => "hello1",}
test map_lookup_optional($map1, 42, "not found") {}
test $map1[42] || "not found" {}
$map2 map{int: str} = {42 => "hello2",}
test map_lookup_optional($map2, 13, "world2") {}
test $map2[13] || "world2" {}
$map3 = {42 => "hello3",}
test map_lookup_optional($map3, 42, "not found") {}
test $map3[42] || "not found" {}
$map4 = {42 => "hello4",}
test map_lookup_optional($map4, 13, "world4") {}
test $map4[13] || "world4" {}
-- OUTPUT --
Vertex: test[hello1]
Vertex: test[hello3]

View File

@@ -72,7 +72,6 @@ $generate = func($idn) {
# this code should be rejected during type unification
$foo = iter.map([$id1, $id2,], $generate)
#test $foo[0] {}
test list_lookup_optional($foo, 0, "fail") {} # TODO: add syntactic sugar for list_lookup_optional
test $foo[0] || "fail" {}
-- OUTPUT --
# err: errUnify: can't unify, invariant illogicality with equals: base kind does not match (Str != Int)

View File

@@ -15,7 +15,7 @@ $t = sys.hasenv("TEST")
$f = sys.hasenv("DOESNOTEXIST")
$env = sys.env()
$m = map_lookup_optional($env, "TEST", "321")
$m = $env["TEST"] || "321"
file "${tmpdir}/environ" {
state => $const.res.file.state.exists,