lang: The in keyword can't be a variable name
At least for now until we figure out some fancier parser magic. (If at all possible!)
This commit is contained in:
@@ -2,25 +2,25 @@
|
|||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
# recursive function (not supported!)
|
# recursive function (not supported!)
|
||||||
$sum1 = func($in) {
|
$sum1 = func($i) {
|
||||||
if $in < 0 {
|
if $i < 0 {
|
||||||
-1 * $sum2(-1 * $in)
|
-1 * $sum2(-1 * $i)
|
||||||
} else {
|
} else {
|
||||||
if $in == 0 {
|
if $i == 0 {
|
||||||
0 # terminate recursion
|
0 # terminate recursion
|
||||||
} else {
|
} else {
|
||||||
$in + $sum2($in - 1)
|
$i + $sum2($i - 1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$sum2 = func($in) {
|
$sum2 = func($i) {
|
||||||
if $in < 0 {
|
if $i < 0 {
|
||||||
-1 * $sum1(-1 * $in)
|
-1 * $sum1(-1 * $i)
|
||||||
} else {
|
} else {
|
||||||
if $in == 0 {
|
if $i == 0 {
|
||||||
0 # terminate recursion
|
0 # terminate recursion
|
||||||
} else {
|
} else {
|
||||||
$in + $sum1($in - 1)
|
$i + $sum1($i - 1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,14 +2,14 @@
|
|||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
# recursive function (not supported!)
|
# recursive function (not supported!)
|
||||||
$sum = func($in) {
|
$sum = func($i) {
|
||||||
if $in < 0 {
|
if $i < 0 {
|
||||||
-1 * $sum(-1 * $in)
|
-1 * $sum(-1 * $i)
|
||||||
} else {
|
} else {
|
||||||
if $in == 0 {
|
if $i == 0 {
|
||||||
0 # terminate recursion
|
0 # terminate recursion
|
||||||
} else {
|
} else {
|
||||||
$in + $sum($in - 1)
|
$i + $sum($i - 1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
-- main.mcl --
|
-- main.mcl --
|
||||||
import "fmt"
|
import "fmt"
|
||||||
|
# unfortunately, for now `in` is a reserved keyword, see:
|
||||||
|
# https://github.com/purpleidea/mgmt/issues/728
|
||||||
$map = 55
|
$map = 55
|
||||||
$fn = func($in) { # in is a special keyword
|
$fn = func($in) { # in is a special keyword
|
||||||
13
|
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)) {}
|
||||||
test fmt.printf("%d", fn(0)) {}
|
test fmt.printf("%d", fn(0)) {}
|
||||||
-- OUTPUT --
|
-- OUTPUT --
|
||||||
Vertex: test[13]
|
# err: errLexParse: parser: `syntax error: unexpected IN, expecting MAP_IDENTIFIER or IDENTIFIER` @5:2
|
||||||
Vertex: test[97]
|
|
||||||
|
|||||||
10
lang/interpret_test/TestAstFunc2/lexer-parser1.txtar
Normal file
10
lang/interpret_test/TestAstFunc2/lexer-parser1.txtar
Normal file
@@ -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]
|
||||||
@@ -5,12 +5,12 @@ $fn = func($x) { # notable because concrete type is fn(t1) t2, where t1 != t2
|
|||||||
len($x)
|
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 --
|
-- OUTPUT --
|
||||||
Vertex: test[out1: [1 2 3 4 5]]
|
Vertex: test[out: [1 2 3 4 5]]
|
||||||
|
|||||||
@@ -5,9 +5,9 @@ $fn = func($x) { # ignore arg
|
|||||||
42
|
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)
|
$t = template("out: {{ . }}", $out)
|
||||||
|
|
||||||
|
|||||||
@@ -5,9 +5,9 @@ $fn = func($x) { # type changes from str to int
|
|||||||
len($x)
|
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)
|
$t = template("out: {{ . }}", $out)
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
-- main.mcl --
|
-- main.mcl --
|
||||||
import "iter"
|
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
|
# 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)
|
len($x)
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -2,25 +2,25 @@
|
|||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
# recursive function (not supported!)
|
# recursive function (not supported!)
|
||||||
func sum1($in) {
|
func sum1($i) {
|
||||||
if $in < 0 {
|
if $i < 0 {
|
||||||
-1 * sum2(-1 * $in)
|
-1 * sum2(-1 * $i)
|
||||||
} else {
|
} else {
|
||||||
if $in == 0 {
|
if $i == 0 {
|
||||||
0 # terminate recursion
|
0 # terminate recursion
|
||||||
} else {
|
} else {
|
||||||
$in + sum2($in - 1)
|
$i + sum2($i - 1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func sum2($in) {
|
func sum2($i) {
|
||||||
if $in < 0 {
|
if $i < 0 {
|
||||||
-1 * sum1(-1 * $in)
|
-1 * sum1(-1 * $i)
|
||||||
} else {
|
} else {
|
||||||
if $in == 0 {
|
if $i == 0 {
|
||||||
0 # terminate recursion
|
0 # terminate recursion
|
||||||
} else {
|
} else {
|
||||||
$in + sum1($in - 1)
|
$i + sum1($i - 1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,14 +2,14 @@
|
|||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
# recursive function (not supported!)
|
# recursive function (not supported!)
|
||||||
func sum($in) {
|
func sum($i) {
|
||||||
if $in < 0 {
|
if $i < 0 {
|
||||||
-1 * sum(-1 * $in)
|
-1 * sum(-1 * $i)
|
||||||
} else {
|
} else {
|
||||||
if $in == 0 {
|
if $i == 0 {
|
||||||
0 # terminate recursion
|
0 # terminate recursion
|
||||||
} else {
|
} else {
|
||||||
$in + sum($in - 1)
|
$i + sum($i - 1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -139,6 +139,11 @@
|
|||||||
lval.str = yylex.Text()
|
lval.str = yylex.Text()
|
||||||
return NOT
|
return NOT
|
||||||
}
|
}
|
||||||
|
/in/ {
|
||||||
|
yylex.pos(lval) // our pos
|
||||||
|
lval.str = yylex.Text()
|
||||||
|
return IN
|
||||||
|
}
|
||||||
/\->/ {
|
/\->/ {
|
||||||
yylex.pos(lval) // our pos
|
yylex.pos(lval) // our pos
|
||||||
lval.str = yylex.Text()
|
lval.str = yylex.Text()
|
||||||
@@ -306,11 +311,6 @@
|
|||||||
lval.str = yylex.Text()
|
lval.str = yylex.Text()
|
||||||
return IDENTIFIER
|
return IDENTIFIER
|
||||||
}
|
}
|
||||||
/in/ {
|
|
||||||
yylex.pos(lval) // our pos
|
|
||||||
lval.str = yylex.Text()
|
|
||||||
return IN
|
|
||||||
}
|
|
||||||
/[A-Z]([a-z0-9_]*[a-z0-9]+)?/
|
/[A-Z]([a-z0-9_]*[a-z0-9]+)?/
|
||||||
{
|
{
|
||||||
yylex.pos(lval) // our pos
|
yylex.pos(lval) // our pos
|
||||||
|
|||||||
Reference in New Issue
Block a user