Skip to content

Commit

Permalink
lang: The in keyword can't be a variable name
Browse files Browse the repository at this point in the history
At least for now until we figure out some fancier parser magic. (If
at all possible!)
  • Loading branch information
purpleidea committed Jan 12, 2024
1 parent dff9c9a commit f92f34d
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 49 deletions.
20 changes: 10 additions & 10 deletions lang/interpret_test/TestAstFunc2/lambdafunc-recursive-double.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions lang/interpret_test/TestAstFunc2/lambdafunc-recursive.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions lang/interpret_test/TestAstFunc2/lexer-parser0.txtar
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
10 changes: 10 additions & 0 deletions lang/interpret_test/TestAstFunc2/lexer-parser1.txtar
Original file line number Diff line number Diff line change
@@ -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]
10 changes: 5 additions & 5 deletions lang/interpret_test/TestAstFunc2/map-iterator0.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
4 changes: 2 additions & 2 deletions lang/interpret_test/TestAstFunc2/map-iterator2.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions lang/interpret_test/TestAstFunc2/map-iterator3.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions lang/interpret_test/TestAstFunc2/map-iterator4.txtar
Original file line number Diff line number Diff line change
@@ -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)

})
Expand Down
20 changes: 10 additions & 10 deletions lang/interpret_test/TestAstFunc2/stmtfunc-recursive-double.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions lang/interpret_test/TestAstFunc2/stmtfunc-recursive.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions lang/parser/lexer.nex
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f92f34d

Please sign in to comment.