fix lambda

This commit is contained in:
Dmitriy Pleshevskiy 2024-04-20 20:12:52 +03:00
parent ce62a78f5a
commit adc3641605
Signed by: pleshevskiy
GPG key ID: 17041163DA10A9A2
2 changed files with 16 additions and 1 deletions

13
lib.nix
View file

@ -138,7 +138,18 @@ let
++ [ kw_end ]) ++ [ kw_end ])
); );
func0 = fnName: func fnName [ ]; func0 = fnName: func fnName [ ];
lambda = func "";
lambda = params: body:
(spaceBetween
([
(concat [
kw_function
(wrapParen (join ", " (map raw params)))
])
]
++ (validBlockBody body)
++ [ kw_end ])
);
lambda0 = lambda [ ]; lambda0 = lambda [ ];
return = expr: spaceBetween ([ kw_return expr ]); return = expr: spaceBetween ([ kw_return expr ]);

View file

@ -173,6 +173,10 @@ with nix2lua; pkgs.lib.runTests {
expr = toLua (func "hello" [ "a" ] (call1 "h.world" (var "a"))); expr = toLua (func "hello" [ "a" ] (call1 "h.world" (var "a")));
expected = "function hello(a) h.world(a) end"; expected = "function hello(a) h.world(a) end";
}; };
"test returns defined lambda" = {
expr = toLua (lambda [ "a" ] (call1 "h.world" (var "a")));
expected = "function(a) h.world(a) end";
};
"test returns if statement" = { "test returns if statement" = {
expr = toLua (if' (eq 10 10) (call "print" [ 10 ])); expr = toLua (if' (eq 10 10) (call "print" [ 10 ]));
expected = ''if (10 == 10) then print(10) end''; expected = ''if (10 == 10) then print(10) end'';