diff --git a/lang/interpret_test/TestAstFunc2/lambdafunc-recursive-double.txtar b/lang/interpret_test/TestAstFunc2/lambdafunc-recursive-double.txtar index 770fe5179..9e42ca336 100644 --- a/lang/interpret_test/TestAstFunc2/lambdafunc-recursive-double.txtar +++ b/lang/interpret_test/TestAstFunc2/lambdafunc-recursive-double.txtar @@ -2,25 +2,25 @@ import "fmt" # recursive function (not supported!) -$sum1 = func($in) { - if $in < 0 { - -1 * $sum2(-1 * $in) +$sum1 = func($i) { + if $i < 0 { + -1 * $sum2(-1 * $i) } else { - if $in == 0 { + if $i == 0 { 0 # terminate recursion } else { - $in + $sum2($in - 1) + $i + $sum2($i - 1) } } } -$sum2 = func($in) { - if $in < 0 { - -1 * $sum1(-1 * $in) +$sum2 = func($i) { + if $i < 0 { + -1 * $sum1(-1 * $i) } else { - if $in == 0 { + if $i == 0 { 0 # terminate recursion } else { - $in + $sum1($in - 1) + $i + $sum1($i - 1) } } } diff --git a/lang/interpret_test/TestAstFunc2/lambdafunc-recursive.txtar b/lang/interpret_test/TestAstFunc2/lambdafunc-recursive.txtar index d44fbbc76..d6e06c1ce 100644 --- a/lang/interpret_test/TestAstFunc2/lambdafunc-recursive.txtar +++ b/lang/interpret_test/TestAstFunc2/lambdafunc-recursive.txtar @@ -2,14 +2,14 @@ import "fmt" # recursive function (not supported!) -$sum = func($in) { - if $in < 0 { - -1 * $sum(-1 * $in) +$sum = func($i) { + if $i < 0 { + -1 * $sum(-1 * $i) } else { - if $in == 0 { + if $i == 0 { 0 # terminate recursion } else { - $in + $sum($in - 1) + $i + $sum($i - 1) } } } diff --git a/lang/interpret_test/TestAstFunc2/lexer-parser0.txtar b/lang/interpret_test/TestAstFunc2/lexer-parser0.txtar index d12ca1bc7..eaf4aed5b 100644 --- a/lang/interpret_test/TestAstFunc2/lexer-parser0.txtar +++ b/lang/interpret_test/TestAstFunc2/lexer-parser0.txtar @@ -1,6 +1,7 @@ -- main.mcl -- import "fmt" - +# unfortunately, for now `in` is a reserved keyword, see: +# https://github.com/purpleidea/mgmt/issues/728 $map = 55 $fn = func($in) { # in is a special keyword 13 @@ -12,5 +13,4 @@ func fn($in) { # in is a special keyword test fmt.printf("%d", $fn(0)) {} test fmt.printf("%d", fn(0)) {} -- OUTPUT -- -Vertex: test[13] -Vertex: test[97] +# err: errLexParse: parser: `syntax error: unexpected IN, expecting MAP_IDENTIFIER or IDENTIFIER` @5:2 diff --git a/lang/interpret_test/TestAstFunc2/lexer-parser1.txtar b/lang/interpret_test/TestAstFunc2/lexer-parser1.txtar new file mode 100644 index 000000000..663b56ef7 --- /dev/null +++ b/lang/interpret_test/TestAstFunc2/lexer-parser1.txtar @@ -0,0 +1,10 @@ +-- main.mcl -- +$s = if 42 in [13, -5, 42, 0,] { # make sure the `in` operator works + "pass" +} else { + "fail" +} + +test $s {} +-- OUTPUT -- +Vertex: test[pass] diff --git a/lang/interpret_test/TestAstFunc2/map-iterator0.txtar b/lang/interpret_test/TestAstFunc2/map-iterator0.txtar index b443f5908..cd020351e 100644 --- a/lang/interpret_test/TestAstFunc2/map-iterator0.txtar +++ b/lang/interpret_test/TestAstFunc2/map-iterator0.txtar @@ -5,12 +5,12 @@ $fn = func($x) { # notable because concrete type is fn(t1) t2, where t1 != t2 len($x) } -$in1 = ["a", "bb", "ccc", "dddd", "eeeee",] +$ins = ["a", "bb", "ccc", "dddd", "eeeee",] -$out1 = iter.map($in1, $fn) +$out = iter.map($ins, $fn) -$t1 = template("out1: {{ . }}", $out1) +$t = template("out: {{ . }}", $out) -test $t1 {} +test $t {} -- OUTPUT -- -Vertex: test[out1: [1 2 3 4 5]] +Vertex: test[out: [1 2 3 4 5]] diff --git a/lang/interpret_test/TestAstFunc2/map-iterator2.txtar b/lang/interpret_test/TestAstFunc2/map-iterator2.txtar index f9eb3b760..01dccb96e 100644 --- a/lang/interpret_test/TestAstFunc2/map-iterator2.txtar +++ b/lang/interpret_test/TestAstFunc2/map-iterator2.txtar @@ -5,9 +5,9 @@ $fn = func($x) { # ignore arg 42 } -$in = [5, 4, 3, 2, 1,] +$ins = [5, 4, 3, 2, 1,] -$out = iter.map($in, $fn) +$out = iter.map($ins, $fn) $t = template("out: {{ . }}", $out) diff --git a/lang/interpret_test/TestAstFunc2/map-iterator3.txtar b/lang/interpret_test/TestAstFunc2/map-iterator3.txtar index 32435072f..dc9195b03 100644 --- a/lang/interpret_test/TestAstFunc2/map-iterator3.txtar +++ b/lang/interpret_test/TestAstFunc2/map-iterator3.txtar @@ -5,9 +5,9 @@ $fn = func($x) { # type changes from str to int len($x) } -$in = ["a", "bb", "ccc", "dddd", "eeeee",] +$ins = ["a", "bb", "ccc", "dddd", "eeeee",] -$out = iter.map($in, $fn) +$out = iter.map($ins, $fn) $t = template("out: {{ . }}", $out) diff --git a/lang/interpret_test/TestAstFunc2/map-iterator4.txtar b/lang/interpret_test/TestAstFunc2/map-iterator4.txtar index b2bda417e..2d04a72e4 100644 --- a/lang/interpret_test/TestAstFunc2/map-iterator4.txtar +++ b/lang/interpret_test/TestAstFunc2/map-iterator4.txtar @@ -1,10 +1,10 @@ -- main.mcl -- import "iter" -$in = ["a", "bb", "ccc", "dddd", "eeeee",] +$ins = ["a", "bb", "ccc", "dddd", "eeeee",] # the inline lambda format is more readable with the func as the second arg -$out = iter.map($in, func($x) { +$out = iter.map($ins, func($x) { len($x) }) diff --git a/lang/interpret_test/TestAstFunc2/stmtfunc-recursive-double.txtar b/lang/interpret_test/TestAstFunc2/stmtfunc-recursive-double.txtar index 9b962ead3..6e5a7ac67 100644 --- a/lang/interpret_test/TestAstFunc2/stmtfunc-recursive-double.txtar +++ b/lang/interpret_test/TestAstFunc2/stmtfunc-recursive-double.txtar @@ -2,25 +2,25 @@ import "fmt" # recursive function (not supported!) -func sum1($in) { - if $in < 0 { - -1 * sum2(-1 * $in) +func sum1($i) { + if $i < 0 { + -1 * sum2(-1 * $i) } else { - if $in == 0 { + if $i == 0 { 0 # terminate recursion } else { - $in + sum2($in - 1) + $i + sum2($i - 1) } } } -func sum2($in) { - if $in < 0 { - -1 * sum1(-1 * $in) +func sum2($i) { + if $i < 0 { + -1 * sum1(-1 * $i) } else { - if $in == 0 { + if $i == 0 { 0 # terminate recursion } else { - $in + sum1($in - 1) + $i + sum1($i - 1) } } } diff --git a/lang/interpret_test/TestAstFunc2/stmtfunc-recursive.txtar b/lang/interpret_test/TestAstFunc2/stmtfunc-recursive.txtar index 0ca7ed748..b43149cdc 100644 --- a/lang/interpret_test/TestAstFunc2/stmtfunc-recursive.txtar +++ b/lang/interpret_test/TestAstFunc2/stmtfunc-recursive.txtar @@ -2,14 +2,14 @@ import "fmt" # recursive function (not supported!) -func sum($in) { - if $in < 0 { - -1 * sum(-1 * $in) +func sum($i) { + if $i < 0 { + -1 * sum(-1 * $i) } else { - if $in == 0 { + if $i == 0 { 0 # terminate recursion } else { - $in + sum($in - 1) + $i + sum($i - 1) } } } diff --git a/lang/parser/lexer.nex b/lang/parser/lexer.nex index bf603aa77..ca6e716f4 100644 --- a/lang/parser/lexer.nex +++ b/lang/parser/lexer.nex @@ -139,6 +139,11 @@ lval.str = yylex.Text() return NOT } +/in/ { + yylex.pos(lval) // our pos + lval.str = yylex.Text() + return IN + } /\->/ { yylex.pos(lval) // our pos lval.str = yylex.Text() @@ -306,11 +311,6 @@ lval.str = yylex.Text() return IDENTIFIER } -/in/ { - yylex.pos(lval) // our pos - lval.str = yylex.Text() - return IN - } /[A-Z]([a-z0-9_]*[a-z0-9]+)?/ { yylex.pos(lval) // our pos