lang: parser, funcs, core, iter: Rename xmap to map

The map function previously existed as "xmap" because this was a
reserved word in the parser. This now adds a special case for this
identifier name.
This commit is contained in:
James Shubin
2023-06-01 18:08:37 -04:00
parent 66edf22ea3
commit d1c7770949
8 changed files with 103 additions and 15 deletions

View File

@@ -29,8 +29,7 @@ import (
const (
// MapFuncName is the name this function is registered as.
// XXX: rename to map once our parser sees a function name and not a type
MapFuncName = "xmap"
MapFuncName = "map"
)
func init() {

View File

@@ -7,7 +7,7 @@ $fn = func($x) { # notable because concrete type is fn(t1) t2, where t1 != t2
$in1 = ["a", "bb", "ccc", "dddd", "eeeee",]
$out1 = iter.xmap($in1, $fn) # XXX: change to map
$out1 = iter.map($in1, $fn)
$t1 = template("out1: {{ . }}", $out1)

View File

@@ -1,8 +1,8 @@
-- main.mcl --
import "iter"
func iterxmap($a, $b) { # XXX: change to map
iter.xmap($a, $b) # XXX: change to map
func itermap($a, $b) {
iter.map($a, $b)
}
$fn = func($x) {
@@ -12,10 +12,10 @@ $fn = func($x) {
$in1 = [5, 4, 3, 2, 1,]
$in2 = ["a", "b", "c", "d", "e",]
$out1 = iter.xmap($in1, $fn) # XXX: change to map
$out2 = iter.xmap($in2, $fn) # XXX: change to map
$out3 = iterxmap($in1, $fn) # XXX: change to map
$out4 = iterxmap($in2, $fn) # XXX: change to map
$out1 = iter.map($in1, $fn)
$out2 = iter.map($in2, $fn)
$out3 = itermap($in1, $fn)
$out4 = itermap($in2, $fn)
$t1 = template("out1: {{ . }}", $out1)
$t2 = template("out2: {{ . }}", $out2)

View File

@@ -7,7 +7,7 @@ $fn = func($x) { # ignore arg
$in = [5, 4, 3, 2, 1,]
$out = iter.xmap($in, $fn) # XXX: change to map
$out = iter.map($in, $fn)
$t = template("out: {{ . }}", $out)

View File

@@ -7,7 +7,7 @@ $fn = func($x) { # type changes from str to int
$in = ["a", "bb", "ccc", "dddd", "eeeee",]
$out = iter.xmap($in, $fn) # XXX: change to map
$out = iter.map($in, $fn)
$t = template("out: {{ . }}", $out)

View File

@@ -4,10 +4,10 @@ import "iter"
$in = ["a", "bb", "ccc", "dddd", "eeeee",]
# the inline lambda format is more readable with the func as the second arg
$out = iter.xmap($in, func($x) {
$out = iter.map($in, func($x) {
len($x)
}) # XXX: change to map
})
$t = template("out: {{ . }}", $out)

View File

@@ -2073,6 +2073,81 @@ func TestLexParse0(t *testing.T) {
exp: exp,
})
}
{
fn := &ast.ExprFunc{
Args: []*interfaces.Arg{
{
Name: "x",
//Type: types.TypeInt,
},
},
//Return: types.TypeInt,
Body: &ast.ExprCall{
Name: funcs.OperatorFuncName,
Args: []interfaces.Expr{
&ast.ExprStr{
V: "*",
},
&ast.ExprVar{
Name: "x",
},
&ast.ExprVar{
Name: "x",
},
},
},
}
//if err := fn.SetType(types.NewType("func(x int) int")); err != nil {
// t.Fatal("could not build type")
//}
exp := &ast.StmtProg{
Body: []interfaces.Stmt{
&ast.StmtImport{
Name: "iter",
},
&ast.StmtBind{
Ident: "fn",
Value: fn,
},
&ast.StmtBind{
Ident: "out",
Value: &ast.ExprCall{
Name: "iter.map", // does this name lex/parse correctly?
Args: []interfaces.Expr{
&ast.ExprList{
Elements: []interfaces.Expr{
&ast.ExprInt{
V: 1,
},
&ast.ExprInt{
V: 2,
},
&ast.ExprInt{
V: 3,
},
},
},
&ast.ExprVar{
Name: "fn",
},
},
},
},
},
}
testCases = append(testCases, test{
name: "iter.map",
code: `
import "iter"
$fn = func($x) { $x * $x }
$out = iter.map([1,2,3,], $fn)
`,
fail: false,
exp: exp,
})
}
if testing.Short() {
t.Logf("available tests:")

View File

@@ -499,6 +499,7 @@ struct_field:
;
call:
// fmt.printf(...)
// iter.map(...)
dotted_identifier OPEN_PAREN call_args CLOSE_PAREN
{
posLast(yylex, yyDollar) // our pos
@@ -1303,13 +1304,26 @@ type_func_arg:
}
}
;
dotted_identifier:
undotted_identifier:
IDENTIFIER
{
posLast(yylex, yyDollar) // our pos
$$.str = $1.str
}
| dotted_identifier DOT IDENTIFIER
// a function could be named map()!
| MAP_IDENTIFIER
{
posLast(yylex, yyDollar) // our pos
$$.str = $1.str
}
;
dotted_identifier:
undotted_identifier
{
posLast(yylex, yyDollar) // our pos
$$.str = $1.str
}
| dotted_identifier DOT undotted_identifier
{
posLast(yylex, yyDollar) // our pos
$$.str = $1.str + interfaces.ModuleSep + $3.str