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

View file

@ -174,7 +174,7 @@ with nix2lua; pkgs.lib.runTests {
expected = "function hello(a) h.world(a) end"; expected = "function hello(a) h.world(a) end";
}; };
"test returns defined lambda" = { "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"; expected = "function(a) h.world(a) end";
}; };
"test returns if statement" = { "test returns if statement" = {