diff --git a/lang/funcs/core/iter/map_func.go b/lang/funcs/core/iter/map_func.go index eafcfba7..1981f20a 100644 --- a/lang/funcs/core/iter/map_func.go +++ b/lang/funcs/core/iter/map_func.go @@ -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() { diff --git a/lang/interpret_test/TestAstFunc2/map-iterator0.txtar.TODO b/lang/interpret_test/TestAstFunc2/map-iterator0.txtar.TODO index f2cc6e57..b443f590 100644 --- a/lang/interpret_test/TestAstFunc2/map-iterator0.txtar.TODO +++ b/lang/interpret_test/TestAstFunc2/map-iterator0.txtar.TODO @@ -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) diff --git a/lang/interpret_test/TestAstFunc2/map-iterator1.txtar.TODO b/lang/interpret_test/TestAstFunc2/map-iterator1.txtar.TODO index dc4ccf20..e7b2af3f 100644 --- a/lang/interpret_test/TestAstFunc2/map-iterator1.txtar.TODO +++ b/lang/interpret_test/TestAstFunc2/map-iterator1.txtar.TODO @@ -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) diff --git a/lang/interpret_test/TestAstFunc2/map-iterator2.txtar.TODO b/lang/interpret_test/TestAstFunc2/map-iterator2.txtar.TODO index aca65502..f9eb3b76 100644 --- a/lang/interpret_test/TestAstFunc2/map-iterator2.txtar.TODO +++ b/lang/interpret_test/TestAstFunc2/map-iterator2.txtar.TODO @@ -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) diff --git a/lang/interpret_test/TestAstFunc2/map-iterator3.txtar.TODO b/lang/interpret_test/TestAstFunc2/map-iterator3.txtar.TODO index 1ae4f098..32435072 100644 --- a/lang/interpret_test/TestAstFunc2/map-iterator3.txtar.TODO +++ b/lang/interpret_test/TestAstFunc2/map-iterator3.txtar.TODO @@ -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) diff --git a/lang/interpret_test/TestAstFunc2/map-iterator4.txtar.TODO b/lang/interpret_test/TestAstFunc2/map-iterator4.txtar.TODO index 20be31a3..b2bda417 100644 --- a/lang/interpret_test/TestAstFunc2/map-iterator4.txtar.TODO +++ b/lang/interpret_test/TestAstFunc2/map-iterator4.txtar.TODO @@ -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) diff --git a/lang/parser/lexparse_test.go b/lang/parser/lexparse_test.go index a7569a89..4dbf982f 100644 --- a/lang/parser/lexparse_test.go +++ b/lang/parser/lexparse_test.go @@ -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:") diff --git a/lang/parser/parser.y b/lang/parser/parser.y index 5070c49f..4aa306f7 100644 --- a/lang/parser/parser.y +++ b/lang/parser/parser.y @@ -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