From 8c4f3ca834ef7c07c9e3be3e006d7395c69a176c Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Sat, 20 Apr 2024 22:27:27 +0300 Subject: [PATCH] refac functions --- lib.nix | 44 +++++++++++++++++++++----------------------- lib.test.nix | 2 +- 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/lib.nix b/lib.nix index 9383803..46f4c6e 100644 --- a/lib.nix +++ b/lib.nix @@ -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"; diff --git a/lib.test.nix b/lib.test.nix index 69e9832..3543aca 100644 --- a/lib.test.nix +++ b/lib.test.nix @@ -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" = {