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:
James Shubin
2024-01-12 17:54:17 -05:00
parent dff9c9ad22
commit f92f34dc54
11 changed files with 59 additions and 49 deletions

View File

@@ -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)
}
}
}

View File

@@ -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)
}
}
}

View File

@@ -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

View 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]

View File

@@ -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]]

View File

@@ -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)

View File

@@ -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)

View File

@@ -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)
})

View File

@@ -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)
}
}
}

View File

@@ -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)
}
}
}

View File

@@ -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