refac functions

This commit is contained in:
Dmitriy Pleshevskiy 2024-04-20 22:27:27 +03:00
parent adc3641605
commit 8c4f3ca834
Signed by: pleshevskiy
GPG key ID: 17041163DA10A9A2
2 changed files with 22 additions and 24 deletions

44
lib.nix
View file

@ -16,7 +16,7 @@
*/
let
inherit (builtins) isString isFloat isInt isBool isList isAttrs isNull isPath;
inherit (builtins) concatStringsSep filter mapAttrs attrValues;
inherit (builtins) concatStringsSep filter mapAttrs attrValues concatLists;
isNotNull = v: !(isNull v);
excludeNull = expr: filter isNotNull expr;
@ -126,30 +126,28 @@ let
local = expr: spaceBetween [ kw_local expr ];
set = variable: value: join " = " [ (raw variable) value ];
funcParams' = params: wrapParen (join ", " (map raw params));
func = fnName: params: body:
(spaceBetween
([
(spaceBetween (concatLists [
[
(concat [
(spaceBetween [ kw_function (validFuncName fnName) ])
(wrapParen (join ", " (map raw params)))
(funcParams' params)
])
]
++ (validBlockBody body)
++ [ kw_end ])
);
(validBlockBody body)
[ kw_end ]
]));
func0 = fnName: func fnName [ ];
lambda = params: body:
(spaceBetween
([
(concat [
kw_function
(wrapParen (join ", " (map raw params)))
])
]
++ (validBlockBody body)
++ [ kw_end ])
);
(spaceBetween (concatLists [
[ (concat [ kw_function (funcParams' params) ]) ]
(validBlockBody body)
[ kw_end ]
]));
lambda0 = lambda [ ];
return = expr: spaceBetween ([ kw_return expr ]);
@ -157,12 +155,12 @@ let
null;
ifelse = condition: trueBody: falseBody:
(spaceBetween
([ kw_if condition kw_then ]
++ (validBlockBody trueBody)
++ (if falseBody != [ ] then [ kw_else ] ++ (validBlockBody falseBody) else [ ])
++ [ kw_end ])
);
(spaceBetween (concatLists [
[ kw_if condition kw_then ]
(validBlockBody trueBody)
(if falseBody != [ ] then [ kw_else ] ++ (validBlockBody falseBody) else [ ])
[ kw_end ]
]));
if' = condition: trueBody: ifelse condition trueBody [ ];
isLuaNil = expr: getType expr == "nil";

View file

@ -174,7 +174,7 @@ with nix2lua; pkgs.lib.runTests {
expected = "function hello(a) h.world(a) end";
};
"test returns defined lambda" = {
expr = toLua (lambda [ "a" ] (call1 "h.world" (var "a")));
expr = toLua (lambda [ "a" ] (call "h.world" (var "a")));
expected = "function(a) h.world(a) end";
};
"test returns if statement" = {