From 94ea63f90c64d6b1f2b5425550affcd0237e2faf Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Wed, 17 Apr 2024 17:05:36 +0300 Subject: [PATCH] add if, ifelse, return --- lib.nix | 26 ++++++++++++++++------- lib.test.nix | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 7 deletions(-) diff --git a/lib.nix b/lib.nix index 1124fe8..daecc73 100644 --- a/lib.nix +++ b/lib.nix @@ -18,6 +18,9 @@ let inherit (builtins) isString isFloat isInt isBool isList isAttrs isNull isPath; inherit (builtins) concatStringsSep filter mapAttrs attrValues; + isNotNull = v: !(isNull v); + excludeNull = expr: filter isNotNull expr; + ################################################################################ # Utils ################################################################################ @@ -83,16 +86,12 @@ let kw_in = raw "in"; kw_then = raw "then"; kw_else = raw "else"; - kw_elseif = raw "elseif"; kw_local = raw "local"; kw_for = raw "for"; kw_do = raw "do"; kw_until = raw "until"; kw_while = raw "while"; kw_repeat = raw "repeat"; - # kw_false = raw "false"; - # kw_true = raw "true"; - # kw_nil = raw "nil"; op = operation: left: right: wrapParen (join " ${operation} " [ left right ]); @@ -128,6 +127,18 @@ let ++ [ kw_end ]) ); + return = expr: spaceBetween ([ kw_return expr ]); + return_void = return null; + + ifelse = condition: trueBody: falseBody: + (concatLines + ([ (spaceBetween [ kw_if condition kw_then ]) ] + ++ trueBody + ++ (if falseBody != [ ] then [ kw_else ] ++ falseBody else [ ]) + ++ [ kw_end ]) + ); + if' = condition: trueBody: ifelse condition trueBody [ ]; + isLuaNil = expr: getType expr == "nil"; LuaNil = { _type = "nil"; }; @@ -144,7 +155,6 @@ let toLuaList = onValue: expr: let wrapObj = expr: "{ ${concatStringsSep ", " expr} }"; - excludeNull = expr: filter (v: !(isNull v)) expr; in wrapObj (excludeNull (map onValue expr)); toLuaNamedField = name: expr: @@ -154,7 +164,7 @@ let toLuaInternal = depth: expr: let nextDepth = depth + 1; in - if isJoin expr then concatStringsSep expr.sep (map (toLuaInternal depth) expr.parts) + if isJoin expr then concatStringsSep expr.sep (map (toLuaInternal depth) (excludeNull expr.parts)) else if isLuaNil expr then "nil" else if isRaw expr then expr.raw else if isNamedField expr then @@ -181,13 +191,15 @@ in inherit LuaNil; inherit raw join concat concatLines pipe; - inherit namedField call require local set func; + inherit namedField call require local set func ifelse if'; inherit op; inherit eq ne gt lt gte lte; inherit add sub mul div mod exp; inherit and or not; + inherit return return_void; + # useful alias var = raw; } diff --git a/lib.test.nix b/lib.test.nix index 046b027..4568745 100644 --- a/lib.test.nix +++ b/lib.test.nix @@ -178,4 +178,62 @@ with nix2lua; pkgs.lib.runTests { end ''; }; + "test returns if statement" = { + expr = toLua (if' (eq 10 10) [ + (call "print" [ 10 ]) + ]); + expected = '' + if (10 == 10) then + print(10) + end + ''; + }; + "test returns if else statement" = { + expr = toLua (ifelse (eq 10 10) [ + (call "print" [ "10 == 10" ]) + ] [ + (call "print" [ "10 != 10" ]) + ]); + expected = '' + if (10 == 10) then + print("10 == 10") + else + print("10 != 10") + end + ''; + }; + "test returns a deep if else statement" = { + expr = toLua (ifelse (eq 10 10) [ + (if' true [ (call "print" [ "yes" ]) ]) + ] [ + (if' true [ (call "print" [ "no" ]) ]) + ]); + expected = '' + if (10 == 10) then + if true then + print("yes") + end + + else + if true then + print("no") + end + + end + ''; + }; + "test returns a returns keyword" = { + expr = toLua (ifelse (eq 10 10) [ + return_void + ] [ + (return 10) + ]); + expected = '' + if (10 == 10) then + return + else + return 10 + end + ''; + }; }