Replaced use of 'set' with assignments

This commit is contained in:
Leon Mika 2025-05-18 10:42:32 +10:00
parent 94aad41712
commit c0d9f950ab
2 changed files with 93 additions and 93 deletions

View file

@ -22,15 +22,15 @@ func TestInst_Eval(t *testing.T) {
{desc: "simple ident", expr: `firstarg a-test`, want: "a-test"}, {desc: "simple ident", expr: `firstarg a-test`, want: "a-test"},
// String interpolation // String interpolation
{desc: "interpolate string 1", expr: `set what "world" ; firstarg "hello $what"`, want: "hello world"}, {desc: "interpolate string 1", expr: `$what = "world" ; firstarg "hello $what"`, want: "hello world"},
{desc: "interpolate string 2", expr: `set what "world" ; set when "now" ; firstarg "$when, hello $what"`, want: "now, hello world"}, {desc: "interpolate string 2", expr: `$what = "world" ; $when = "now" ; firstarg "$when, hello $what"`, want: "now, hello world"},
{desc: "interpolate string 3", expr: `set what "world" ; set when "now" ; firstarg "${when}, hello ${what}"`, want: "now, hello world"}, {desc: "interpolate string 3", expr: `$what = "world" ; $when = "now" ; firstarg "${when}, hello ${what}"`, want: "now, hello world"},
{desc: "interpolate string 4", expr: `set crazy [far: "unknown"] ; firstarg "hello ${crazy.far}"`, want: "hello unknown"}, {desc: "interpolate string 4", expr: `$crazy = [far: "unknown"] ; firstarg "hello ${crazy.far}"`, want: "hello unknown"},
{desc: "interpolate string 5", expr: `set oldWords ["hither" "thither" "yonder"] ; firstarg "hello ${oldWords.(1)}"`, want: "hello thither"}, {desc: "interpolate string 5", expr: `$oldWords = ["hither" "thither" "yonder"] ; firstarg "hello ${oldWords.(1)}"`, want: "hello thither"},
{desc: "interpolate string 6", expr: `set oldWords ["hither" "thither" "yonder"] ; firstarg "hello ${oldWords.(add 1 1)}"`, want: "hello yonder"}, {desc: "interpolate string 6", expr: `$oldWords = ["hither" "thither" "yonder"] ; firstarg "hello ${oldWords.(add 1 1)}"`, want: "hello yonder"},
{desc: "interpolate string 7", expr: `set oldWords ["hither" "thither" "yonder"] ; firstarg "hello ${oldWords.(add 2 | sub (sub 2 1) | sub 1)}"`, want: "hello hither"}, {desc: "interpolate string 7", expr: `$oldWords = ["hither" "thither" "yonder"] ; firstarg "hello ${oldWords.(add 2 | sub (sub 2 1) | sub 1)}"`, want: "hello hither"},
{desc: "interpolate string 8", expr: `set words ["old": ["hither" "thither" "yonder"] "new": ["near" "far"]] ; firstarg "hello ${words.old.(2)}"`, want: "hello yonder"}, {desc: "interpolate string 8", expr: `$words = ["old": ["hither" "thither" "yonder"] "new": ["near" "far"]] ; firstarg "hello ${words.old.(2)}"`, want: "hello yonder"},
{desc: "interpolate string 9", expr: `set what "world" ; firstarg "hello $($what)"`, want: "hello world"}, {desc: "interpolate string 9", expr: `$what = "world" ; firstarg "hello $($what)"`, want: "hello world"},
{desc: "interpolate string 10", expr: `firstarg "hello $([1 2 3] | len)"`, want: "hello 3"}, {desc: "interpolate string 10", expr: `firstarg "hello $([1 2 3] | len)"`, want: "hello 3"},
{desc: "interpolate string 11", expr: `firstarg "hello $(add (add 1 2) 3)"`, want: "hello 6"}, {desc: "interpolate string 11", expr: `firstarg "hello $(add (add 1 2) 3)"`, want: "hello 6"},
{desc: "interpolate string 12", expr: `firstarg ("$(add 2 (add 1 1)) + $([1 2 3].(1) | cat ("$("")")) = $(("$(add 2 (4))"))")`, want: "4 + 2 = 6"}, {desc: "interpolate string 12", expr: `firstarg ("$(add 2 (add 1 1)) + $([1 2 3].(1) | cat ("$("")")) = $(("$(add 2 (4))"))")`, want: "4 + 2 = 6"},
@ -57,48 +57,48 @@ func TestInst_Eval(t *testing.T) {
// Multi-statements // Multi-statements
{desc: "multi 1", expr: `firstarg "hello" ; firstarg "world"`, want: "world"}, {desc: "multi 1", expr: `firstarg "hello" ; firstarg "world"`, want: "world"},
{desc: "multi 2", expr: `list "hello" | toUpper ; firstarg "world"`, want: "world"}, {desc: "multi 2", expr: `list "hello" | toUpper ; firstarg "world"`, want: "world"},
{desc: "multi 3", expr: `set new "this is new" ; firstarg $new`, want: "this is new"}, {desc: "multi 3", expr: `$new = "this is new" ; firstarg $new`, want: "this is new"},
// Lists // Lists
{desc: "list 1", expr: `firstarg ["1" "2" "3"]`, want: []any{"1", "2", "3"}}, {desc: "list 1", expr: `firstarg ["1" "2" "3"]`, want: []any{"1", "2", "3"}},
{desc: "list 2", expr: `set one "one" ; firstarg [$one (list "two" | map { |x| toUpper $x } | head) "three"]`, want: []any{"one", "TWO", "three"}}, {desc: "list 2", expr: `$one = "one" ; firstarg [$one (list "two" | map { |x| toUpper $x } | head) "three"]`, want: []any{"one", "TWO", "three"}},
{desc: "list 3", expr: `firstarg []`, want: []any{}}, {desc: "list 3", expr: `firstarg []`, want: []any{}},
{desc: "list 4", expr: `set x ["a" "b" "c"] ; firstarg [$x.(2) $x.(1) $x.(0)]`, want: []any{"c", "b", "a"}}, {desc: "list 4", expr: `$x = ["a" "b" "c"] ; firstarg [$x.(2) $x.(1) $x.(0)]`, want: []any{"c", "b", "a"}},
// Maps // Maps
{desc: "map 1", expr: `firstarg [one:"1" two:"2" three:"3"]`, want: map[string]any{"one": "1", "two": "2", "three": "3"}}, {desc: "map 1", expr: `firstarg [one:"1" two:"2" three:"3"]`, want: map[string]any{"one": "1", "two": "2", "three": "3"}},
{desc: "map 2", expr: `firstarg ["one":"1" "two":"2" "three":"3"]`, want: map[string]any{"one": "1", "two": "2", "three": "3"}}, {desc: "map 2", expr: `firstarg ["one":"1" "two":"2" "three":"3"]`, want: map[string]any{"one": "1", "two": "2", "three": "3"}},
{desc: "map 3", expr: ` {desc: "map 3", expr: `
set one "one" ; set n1 "1" $one = "one" ; $n1 = "1"
firstarg [ firstarg [
$one:$n1 $one:$n1
(list "two" | map { |x| toUpper $x } | head):(list "2" | map { |x| toUpper $x } | head) (list "two" | map { |x| toUpper $x } | head):(list "2" | map { |x| toUpper $x } | head)
three:"3" three:"3"
]`, want: map[string]any{"one": "1", "TWO": "2", "three": "3"}}, ]`, want: map[string]any{"one": "1", "TWO": "2", "three": "3"}},
{desc: "map 4", expr: `firstarg [:]`, want: map[string]any{}}, {desc: "map 4", expr: `firstarg [:]`, want: map[string]any{}},
{desc: "map 5", expr: `set x ["a" "b" "c"] ; firstarg ["one":$x.(2) "two":$x.(1) "three":$x.(0)]`, want: map[string]any{"one": "c", "two": "b", "three": "a"}}, {desc: "map 5", expr: `$x = ["a" "b" "c"] ; firstarg ["one":$x.(2) "two":$x.(1) "three":$x.(0)]`, want: map[string]any{"one": "c", "two": "b", "three": "a"}},
{desc: "map 6", expr: `set x [a:"A" b:"B" c:"C"] ; firstarg ["one":$x.c "two":$x.b "three":$x.a]`, want: map[string]any{"one": "C", "two": "B", "three": "A"}}, {desc: "map 6", expr: `$x = [a:"A" b:"B" c:"C"] ; firstarg ["one":$x.c "two":$x.b "three":$x.a]`, want: map[string]any{"one": "C", "two": "B", "three": "A"}},
// Dots // Dots
{desc: "dot expr 1", expr: `set x [1 2 3] ; $x.(0)`, want: 1}, {desc: "dot expr 1", expr: `$x = [1 2 3] ; $x.(0)`, want: 1},
{desc: "dot expr 2", expr: `set x [1 2 3] ; $x.(1)`, want: 2}, {desc: "dot expr 2", expr: `$x = [1 2 3] ; $x.(1)`, want: 2},
{desc: "dot expr 3", expr: `set x [1 2 3] ; $x.(2)`, want: 3}, {desc: "dot expr 3", expr: `$x = [1 2 3] ; $x.(2)`, want: 3},
{desc: "dot expr 4", expr: `set x [1 2 3] ; $x.(3)`, want: nil}, {desc: "dot expr 4", expr: `$x = [1 2 3] ; $x.(3)`, want: nil},
{desc: "dot expr 5", expr: `set x [1 2 3] ; $x.(add 1 1)`, want: 3}, {desc: "dot expr 5", expr: `$x = [1 2 3] ; $x.(add 1 1)`, want: 3},
{desc: "dot expr 6", expr: `set x [1 2 3] ; $x.(-1)`, want: 3}, {desc: "dot expr 6", expr: `$x = [1 2 3] ; $x.(-1)`, want: 3},
{desc: "dot expr 7", expr: `set x [1 2 3] ; $x.(-2)`, want: 2}, {desc: "dot expr 7", expr: `$x = [1 2 3] ; $x.(-2)`, want: 2},
{desc: "dot expr 8", expr: `set x [1 2 3] ; $x.(-3)`, want: 1}, {desc: "dot expr 8", expr: `$x = [1 2 3] ; $x.(-3)`, want: 1},
{desc: "dot expr 9", expr: `set x [1 2 3] ; $x.(-4)`, want: nil}, {desc: "dot expr 9", expr: `$x = [1 2 3] ; $x.(-4)`, want: nil},
{desc: "dot idents 1", expr: `set x [alpha:"hello" bravo:"world"] ; $x.alpha`, want: "hello"}, {desc: "dot idents 1", expr: `$x = [alpha:"hello" bravo:"world"] ; $x.alpha`, want: "hello"},
{desc: "dot idents 2", expr: `set x [alpha:"hello" bravo:"world"] ; $x.bravo`, want: "world"}, {desc: "dot idents 2", expr: `$x = [alpha:"hello" bravo:"world"] ; $x.bravo`, want: "world"},
{desc: "dot idents 3", expr: `set x [alpha:"hello" bravo:"world"] ; $x.charlie`, want: nil}, {desc: "dot idents 3", expr: `$x = [alpha:"hello" bravo:"world"] ; $x.charlie`, want: nil},
{desc: "dot idents 4", expr: `set x [alpha:"hello" bravo:"world"] ; $x.("alpha")`, want: "hello"}, {desc: "dot idents 4", expr: `$x = [alpha:"hello" bravo:"world"] ; $x.("alpha")`, want: "hello"},
{desc: "dot idents 5", expr: `set x [alpha:"hello" bravo:"world"] ; $x.("bravo")`, want: "world"}, {desc: "dot idents 5", expr: `$x = [alpha:"hello" bravo:"world"] ; $x.("bravo")`, want: "world"},
{desc: "dot idents 6", expr: `set x [alpha:"hello" bravo:"world"] ; $x.("charlie")`, want: nil}, {desc: "dot idents 6", expr: `$x = [alpha:"hello" bravo:"world"] ; $x.("charlie")`, want: nil},
{desc: "dot idents 7", expr: `set x [MORE:"stuff"] ; $x.("more" | toUpper)`, want: "stuff"}, {desc: "dot idents 7", expr: `$x = [MORE:"stuff"] ; $x.("more" | toUpper)`, want: "stuff"},
{desc: "dot idents 8", expr: `set x [MORE:"stuff"] ; $x.(toUpper ("more"))`, want: "stuff"}, {desc: "dot idents 8", expr: `$x = [MORE:"stuff"] ; $x.(toUpper ("more"))`, want: "stuff"},
{desc: "dot idents 9", expr: `set x [MORE:"stuff"] ; x.y`, want: nil}, {desc: "dot idents 9", expr: `$x = [MORE:"stuff"] ; x.y`, want: nil},
{desc: "parse comments 1", expr: parseComments1, wantErr: ucl.ErrNotConvertable}, {desc: "parse comments 1", expr: parseComments1, wantErr: ucl.ErrNotConvertable},
{desc: "parse comments 2", expr: parseComments2, wantErr: ucl.ErrNotConvertable}, {desc: "parse comments 2", expr: parseComments2, wantErr: ucl.ErrNotConvertable},
@ -136,9 +136,9 @@ func TestInst_SetPseudoVar(t *testing.T) {
{desc: "read var 2", expr: `@bar`, wantRes: "this is bar"}, {desc: "read var 2", expr: `@bar`, wantRes: "this is bar"},
{desc: "read var 3", expr: `@fla`, wantRes: "missing value of fla"}, {desc: "read var 3", expr: `@fla`, wantRes: "missing value of fla"},
{desc: "write var 1", expr: `set "@foo" "hello" ; @foo`, wantRes: "hello"}, {desc: "write var 1", expr: `@foo = "hello" ; @foo`, wantRes: "hello"},
{desc: "write var 2", expr: `set "@bar" "world" ; @bar`, wantRes: "world", wantBarVar: "world"}, {desc: "write var 2", expr: `@bar = "world" ; @bar`, wantRes: "world", wantBarVar: "world"},
{desc: "write var 3", expr: `set "@blong" "hello"`, wantErr: true}, {desc: "write var 3", expr: `@blong = "hello"`, wantErr: true},
} }
for _, tt := range tests { for _, tt := range tests {

View file

@ -131,10 +131,10 @@ func TestBuiltins_Echo(t *testing.T) {
echo "world" # command after this echo "world" # command after this
; ;
`, want: "Hello\nworld\n"}, `, want: "Hello\nworld\n"},
{desc: "interpolated string 1", expr: `set what "world" ; echo "Hello, $what"`, want: "Hello, world\n"}, {desc: "interpolated string 1", expr: `$what = "world" ; echo "Hello, $what"`, want: "Hello, world\n"},
{desc: "interpolated string 2", expr: `set what "world" ; echo "Hello, \$what"`, want: "Hello, $what\n"}, {desc: "interpolated string 2", expr: `$what = "world" ; echo "Hello, \$what"`, want: "Hello, $what\n"},
{desc: "interpolated string 3", expr: `echo "separate\nlines\n\tand tabs"`, want: "separate\nlines\n\tand tabs\n"}, {desc: "interpolated string 3", expr: `echo "separate\nlines\n\tand tabs"`, want: "separate\nlines\n\tand tabs\n"},
{desc: "interpolated string 4", expr: `set what "Hello" ; set where "world" ; echo "$what, $where"`, want: "Hello, world\n"}, {desc: "interpolated string 4", expr: `$what = "Hello" ; $where = "world" ; echo "$what, $where"`, want: "Hello, world\n"},
{desc: "interpolated string 5", expr: ` {desc: "interpolated string 5", expr: `
foreach [123 "foo" true ()] { |x| foreach [123 "foo" true ()] { |x|
echo "[[$x]]" echo "[[$x]]"
@ -167,19 +167,19 @@ func TestBuiltins_If(t *testing.T) {
want string want string
}{ }{
{desc: "single then", expr: ` {desc: "single then", expr: `
set x "Hello" $x = "Hello"
if $x { if $x {
echo "true" echo "true"
}`, want: "true\n(nil)\n"}, }`, want: "true\n(nil)\n"},
{desc: "single then and else", expr: ` {desc: "single then and else", expr: `
set x "Hello" $x = "Hello"
if $x { if $x {
echo "true" echo "true"
} else { } else {
echo "false" echo "false"
}`, want: "true\n(nil)\n"}, }`, want: "true\n(nil)\n"},
{desc: "single then, elif and else", expr: ` {desc: "single then, elif and else", expr: `
set x "Hello" $x = "Hello"
if $y { if $y {
echo "y is true" echo "y is true"
} elif $x { } elif $x {
@ -188,14 +188,14 @@ func TestBuiltins_If(t *testing.T) {
echo "nothings x" echo "nothings x"
}`, want: "x is true\n(nil)\n"}, }`, want: "x is true\n(nil)\n"},
{desc: "single then and elif, no else", expr: ` {desc: "single then and elif, no else", expr: `
set x "Hello" $x = "Hello"
if $y { if $y {
echo "y is true" echo "y is true"
} elif $x { } elif $x {
echo "x is true" echo "x is true"
}`, want: "x is true\n(nil)\n"}, }`, want: "x is true\n(nil)\n"},
{desc: "single then, two elif, and else", expr: ` {desc: "single then, two elif, and else", expr: `
set x "Hello" $x = "Hello"
if $z { if $z {
echo "z is true" echo "z is true"
} elif $y { } elif $y {
@ -213,15 +213,15 @@ func TestBuiltins_If(t *testing.T) {
} else { } else {
echo "none is true" echo "none is true"
}`, want: "none is true\n(nil)\n"}, }`, want: "none is true\n(nil)\n"},
{desc: "compressed then", expr: `set x "Hello" ; if $x { echo "true" }`, want: "true\n(nil)\n"}, {desc: "compressed then", expr: `$x = "Hello" ; if $x { echo "true" }`, want: "true\n(nil)\n"},
{desc: "compressed else", expr: `if $x { echo "true" } else { echo "false" }`, want: "false\n(nil)\n"}, {desc: "compressed else", expr: `if $x { echo "true" } else { echo "false" }`, want: "false\n(nil)\n"},
{desc: "compressed if", expr: `if $x { echo "x" } elif $y { echo "y" } else { echo "false" }`, want: "false\n(nil)\n"}, {desc: "compressed if", expr: `if $x { echo "x" } elif $y { echo "y" } else { echo "false" }`, want: "false\n(nil)\n"},
{desc: "if of itr 1", expr: `set i (itr) ; if $i { echo "more" } else { echo "none" }`, want: "more\n(nil)\n"}, {desc: "if of itr 1", expr: `$i = itr ; if $i { echo "more" } else { echo "none" }`, want: "more\n(nil)\n"},
{desc: "if of itr 2", expr: `set i (itr) ; foreach (seq 1) { head $i } ; if $i { echo "more" } else { echo "none" }`, want: "more\n(nil)\n"}, {desc: "if of itr 2", expr: `$i = itr ; foreach (seq 1) { head $i } ; if $i { echo "more" } else { echo "none" }`, want: "more\n(nil)\n"},
{desc: "if of itr 3", expr: `set i (itr) ; foreach (seq 3) { head $i } ; if $i { echo "more" } else { echo "none" }`, want: "none\n(nil)\n"}, {desc: "if of itr 3", expr: `$i = itr ; foreach (seq 3) { head $i } ; if $i { echo "more" } else { echo "none" }`, want: "none\n(nil)\n"},
{desc: "if of itr 4", expr: `set i (itr | map { |x| add 2 $x }) ; if $i { echo "more" } else { echo "none" }`, want: "more\n(nil)\n"}, {desc: "if of itr 4", expr: `$i = (itr | map { |x| add 2 $x }) ; if $i { echo "more" } else { echo "none" }`, want: "more\n(nil)\n"},
{desc: "if of itr 5", expr: `set i (itr | filter { |x| () }) ; if $i { echo "more" } else { echo "none" }`, want: "none\n(nil)\n"}, {desc: "if of itr 5", expr: `$i = (itr | filter { |x| () }) ; if $i { echo "more" } else { echo "none" }`, want: "none\n(nil)\n"},
{desc: "if of itr 6", expr: `set i (itr | filter { |x| 1 }) ; if $i { echo "more" } else { echo "none" }`, want: "more\n(nil)\n"}, {desc: "if of itr 6", expr: `$i = (itr | filter { |x| 1 }) ; if $i { echo "more" } else { echo "none" }`, want: "more\n(nil)\n"},
} }
for _, tt := range tests { for _, tt := range tests {
@ -281,53 +281,53 @@ func TestBuiltins_While(t *testing.T) {
want string want string
}{ }{
{desc: "iterate while true 1", expr: ` {desc: "iterate while true 1", expr: `
set x 0 $x = 0
while (lt $x 5) { while (lt $x 5) {
echo $x echo $x
set x (add $x 1) $x = (add $x 1)
} }
echo "done"`, want: "0\n1\n2\n3\n4\ndone\n(nil)\n"}, echo "done"`, want: "0\n1\n2\n3\n4\ndone\n(nil)\n"},
{desc: "iterate while true 2", expr: ` {desc: "iterate while true 2", expr: `
set x 20 $x = 20
while (lt $x 5) { while (lt $x 5) {
echo $x echo $x
set x (add $x 1) $x = (add $x 1)
} }
echo "done"`, want: "done\n(nil)\n"}, echo "done"`, want: "done\n(nil)\n"},
{desc: "iterate while true with pipeline", expr: ` {desc: "iterate while true with pipeline", expr: `
set x 0 $x = 0
while (lt $x 5) { while (lt $x 5) {
echo $x echo $x
set x (add $x 1) $x = (add $x 1)
if (ge $x 3) { if (ge $x 3) {
break "Ahh" break "Ahh"
} }
} | echo " was the break" } | echo " was the break"
echo "done"`, want: "0\n1\n2\nAhh was the break\ndone\n(nil)\n"}, echo "done"`, want: "0\n1\n2\nAhh was the break\ndone\n(nil)\n"},
{desc: "iterate for ever with break 1", expr: ` {desc: "iterate for ever with break 1", expr: `
set x 0 $x = 0
while { while {
echo $x echo $x
set x (add $x 1) $x = (add $x 1)
if (ge $x 5) { if (ge $x 5) {
break break
} }
} }
echo "done"`, want: "0\n1\n2\n3\n4\ndone\n(nil)\n"}, echo "done"`, want: "0\n1\n2\n3\n4\ndone\n(nil)\n"},
{desc: "iterate for ever with break 2", expr: ` {desc: "iterate for ever with break 2", expr: `
set x 0 $x = 0
echo (while { echo (while {
echo $x echo $x
set x (add $x 1) $x = add $x 1
if (ge $x 5) { if (ge $x 5) {
break $x break $x
} }
}) })
`, want: "0\n1\n2\n3\n4\n5\n(nil)\n"}, `, want: "0\n1\n2\n3\n4\n5\n(nil)\n"},
{desc: "iterate for ever with continue", expr: ` {desc: "iterate for ever with continue", expr: `
set x 0 $x = 0
while { while {
set x (add $x 1) $x = (add $x 1)
if (or (eq $x 2) (eq $x 4)) { if (or (eq $x 2) (eq $x 4)) {
echo "quack" echo "quack"
continue continue
@ -487,10 +487,10 @@ func TestBuiltins_Procs(t *testing.T) {
} }
} }
set helloGreater (makeGreeter "Hello") $helloGreater = makeGreeter "Hello"
$helloGreater "world" $helloGreater "world"
set goodbye (makeGreeter "Goodbye cruel") $goodbye = makeGreeter "Goodbye cruel"
$goodbye "world" $goodbye "world"
call (makeGreeter "Quick") ["call me"] call (makeGreeter "Quick") ["call me"]
@ -498,13 +498,13 @@ func TestBuiltins_Procs(t *testing.T) {
`, want: "Hello, world\nGoodbye cruel, world\nQuick, call me\n(nil)\n"}, `, want: "Hello, world\nGoodbye cruel, world\nQuick, call me\n(nil)\n"},
{desc: "modifying closed over variables", expr: ` {desc: "modifying closed over variables", expr: `
proc makeSetter { proc makeSetter {
set bla "X" $bla = "X"
proc appendToBla { |x| proc appendToBla { |x|
set bla (cat $bla $x) $bla = cat $bla $x
} }
} }
set er (makeSetter) $er = makeSetter
echo (call $er ["xxx"]) echo (call $er ["xxx"])
echo (call $er ["yyy"]) echo (call $er ["yyy"])
`, want: "Xxxx\nXxxxyyy\n(nil)\n"}, `, want: "Xxxx\nXxxxyyy\n(nil)\n"},
@ -606,7 +606,7 @@ func TestBuiltins_Return(t *testing.T) {
echo "world" echo "world"
} }
proc greet { proc greet {
set what (greetWhat) $what = (greetWhat)
echo "Hello, " $what echo "Hello, " $what
} }
@ -670,7 +670,7 @@ func TestBuiltins_Return(t *testing.T) {
proc test-thing { proc test-thing {
foreach [1 2 3] { |x| foreach [1 2 3] { |x|
set myClosure (proc { echo $x }) $myClosure = proc { echo $x }
do-thing $myClosure do-thing $myClosure
} }
} }
@ -697,12 +697,12 @@ func TestBuiltins_Return(t *testing.T) {
proc test-thing { proc test-thing {
[1 2 3] | map { |x| [1 2 3] | map { |x|
set myProc (proc { echo $x }) $myProc = proc { echo $x }
proc { do-thing $myProc } proc { do-thing $myProc }
} }
} }
set hello "xx" $hello = "xx"
foreach (test-thing) { |y| call $y ; echo $hello } foreach (test-thing) { |y| call $y ; echo $hello }
`, want: "1\nxx\n2\nxx\n3\nxx\n(nil)\n"}, `, want: "1\nxx\n2\nxx\n3\nxx\n(nil)\n"},
{desc: "check closure 7", expr: ` {desc: "check closure 7", expr: `
@ -711,15 +711,15 @@ func TestBuiltins_Return(t *testing.T) {
} }
proc test-thing { proc test-thing {
set f 0 $f = 0
[1 2 3] | map { |x| [1 2 3] | map { |x|
set myProc (proc { echo $f }) $myProc = proc { echo $f }
set f (add $f 1) $f = (add $f 1)
proc { do-thing $myProc } proc { do-thing $myProc }
} }
} }
set hello "xx" $hello = "xx"
foreach (test-thing) { |y| call $y ; echo $hello } foreach (test-thing) { |y| call $y ; echo $hello }
`, want: "3\nxx\n3\nxx\n3\nxx\n(nil)\n"}, `, want: "3\nxx\n3\nxx\n3\nxx\n(nil)\n"},
{desc: "check closure 7", expr: ` {desc: "check closure 7", expr: `
@ -728,16 +728,16 @@ func TestBuiltins_Return(t *testing.T) {
} }
proc test-thing { proc test-thing {
set f 1 $f = 1
[1 2 3] | map { |x| [1 2 3] | map { |x|
set g $f $g = $f
set myProc (proc { echo $g }) $myProc = (proc { echo $g })
set f (add $f 1) $f = (add $f 1)
proc { do-thing $myProc } proc { do-thing $myProc }
} }
} }
set hello "xx" $hello = "xx"
foreach (test-thing) { |y| call $y ; echo $hello } foreach (test-thing) { |y| call $y ; echo $hello }
`, want: "1\nxx\n2\nxx\n3\nxx\n(nil)\n"}, `, want: "1\nxx\n2\nxx\n3\nxx\n(nil)\n"},
} }
@ -831,12 +831,12 @@ func TestBuiltins_Map(t *testing.T) {
map ["a" "b" "c"] (proc { |x| makeUpper $x }) map ["a" "b" "c"] (proc { |x| makeUpper $x })
`, want: "A\nB\nC\n"}, `, want: "A\nB\nC\n"},
{desc: "map list 2", expr: ` {desc: "map list 2", expr: `
set makeUpper (proc { |x| $x | toUpper }) $makeUpper = proc { |x| $x | toUpper }
map ["a" "b" "c"] $makeUpper map ["a" "b" "c"] $makeUpper
`, want: "A\nB\nC\n"}, `, want: "A\nB\nC\n"},
{desc: "map list with pipe", expr: ` {desc: "map list with pipe", expr: `
set makeUpper (proc { |x| $x | toUpper }) $makeUpper = proc { |x| $x | toUpper }
["a" "b" "c"] | map $makeUpper ["a" "b" "c"] | map $makeUpper
`, want: "A\nB\nC\n"}, `, want: "A\nB\nC\n"},
@ -844,15 +844,15 @@ func TestBuiltins_Map(t *testing.T) {
map ["a" "b" "c"] { |x| toUpper $x } map ["a" "b" "c"] { |x| toUpper $x }
`, want: "A\nB\nC\n"}, `, want: "A\nB\nC\n"},
{desc: "map list with stream", expr: ` {desc: "map list with stream", expr: `
set makeUpper (proc { |x| toUpper $x }) $makeUpper = proc { |x| toUpper $x }
set l (["a" "b" "c"] | map $makeUpper) $l = ["a" "b" "c"] | map $makeUpper
echo $l echo $l
`, want: "[A B C]\n(nil)\n"}, `, want: "[A B C]\n(nil)\n"},
{desc: "map itr stream", expr: ` {desc: "map itr stream", expr: `
set add2 (proc { |x| add $x 2 }) $add2 = proc { |x| add $x 2 }
set l (itr | map $add2) $l = itr | map $add2
foreach $l { |x| echo $x } foreach $l { |x| echo $x }
`, want: "3\n4\n5\n(nil)\n"}, `, want: "3\n4\n5\n(nil)\n"},
} }
@ -1106,7 +1106,7 @@ func TestBuiltins_Filter(t *testing.T) {
}}, }},
{desc: "filter map 3", expr: `filter [alpha:"hello" bravo:"world"] { |k v| eq $v "alpha" }`, want: map[string]any{}}, {desc: "filter map 3", expr: `filter [alpha:"hello" bravo:"world"] { |k v| eq $v "alpha" }`, want: map[string]any{}},
{desc: "filter itr 1", expr: `set s "" ; itr | filter { |x| ne $x 2 } | foreach { |x| set s "$s $x" }; $s`, want: " 1 3"}, {desc: "filter itr 1", expr: `$s = "" ; itr | filter { |x| ne $x 2 } | foreach { |x| $s = "$s $x" }; $s`, want: " 1 3"},
} }
for _, tt := range tests { for _, tt := range tests {
@ -1158,10 +1158,10 @@ func TestBuiltins_Head(t *testing.T) {
{desc: "head list 1", expr: `head [1 2 3]`, want: 1}, {desc: "head list 1", expr: `head [1 2 3]`, want: 1},
{desc: "head itr 1", expr: `head (itr)`, want: 1}, {desc: "head itr 1", expr: `head (itr)`, want: 1},
{desc: "head itr 2", expr: `set h (itr) ; head $h`, want: 1}, {desc: "head itr 2", expr: `$h = (itr) ; head $h`, want: 1},
{desc: "head itr 3", expr: `set h (itr) ; head $h ; head $h`, want: 2}, {desc: "head itr 3", expr: `$h = (itr) ; head $h ; head $h`, want: 2},
{desc: "head itr 4", expr: `set h (itr) ; head $h ; head $h ; head $h`, want: 3}, {desc: "head itr 4", expr: `$h = (itr) ; head $h ; head $h ; head $h`, want: 3},
{desc: "head itr 5", expr: `set h (itr) ; head $h ; head $h ; head $h ; head $h`, want: nil}, {desc: "head itr 5", expr: `$h = (itr) ; head $h ; head $h ; head $h ; head $h`, want: nil},
} }
for _, tt := range tests { for _, tt := range tests {
@ -1497,7 +1497,7 @@ func TestBuiltins_Cat(t *testing.T) {
{desc: "cat 6", expr: `cat "array = " [1 3 2 4]`, want: "array = [1 3 2 4]"}, {desc: "cat 6", expr: `cat "array = " [1 3 2 4]`, want: "array = [1 3 2 4]"},
{desc: "cat 7", expr: `cat 1 $true 3 [4]`, want: "1true3[4]"}, {desc: "cat 7", expr: `cat 1 $true 3 [4]`, want: "1true3[4]"},
{desc: "cat 8", expr: `cat`, want: ""}, {desc: "cat 8", expr: `cat`, want: ""},
{desc: "cat 9", expr: `set x ["a" "b" "c"] ; cat "array = " [1 $x.(0) $x.(2) $x.(1)]`, want: "array = [1 a c b]"}, {desc: "cat 9", expr: `$x = ["a" "b" "c"] ; cat "array = " [1 $x.(0) $x.(2) $x.(1)]`, want: "array = [1 a c b]"},
} }
for _, tt := range tests { for _, tt := range tests {