add if, ifelse, return

This commit is contained in:
Dmitriy Pleshevskiy 2024-04-17 17:05:36 +03:00
parent e80b4f0b88
commit 94ea63f90c
Signed by: pleshevskiy
GPG key ID: 17041163DA10A9A2
2 changed files with 77 additions and 7 deletions

26
lib.nix
View file

@ -18,6 +18,9 @@ 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;
isNotNull = v: !(isNull v);
excludeNull = expr: filter isNotNull expr;
################################################################################ ################################################################################
# Utils # Utils
################################################################################ ################################################################################
@ -83,16 +86,12 @@ let
kw_in = raw "in"; kw_in = raw "in";
kw_then = raw "then"; kw_then = raw "then";
kw_else = raw "else"; kw_else = raw "else";
kw_elseif = raw "elseif";
kw_local = raw "local"; kw_local = raw "local";
kw_for = raw "for"; kw_for = raw "for";
kw_do = raw "do"; kw_do = raw "do";
kw_until = raw "until"; kw_until = raw "until";
kw_while = raw "while"; kw_while = raw "while";
kw_repeat = raw "repeat"; 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 ]); op = operation: left: right: wrapParen (join " ${operation} " [ left right ]);
@ -128,6 +127,18 @@ let
++ [ kw_end ]) ++ [ 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"; isLuaNil = expr: getType expr == "nil";
LuaNil = { _type = "nil"; }; LuaNil = { _type = "nil"; };
@ -144,7 +155,6 @@ let
toLuaList = onValue: expr: toLuaList = onValue: expr:
let let
wrapObj = expr: "{ ${concatStringsSep ", " expr} }"; wrapObj = expr: "{ ${concatStringsSep ", " expr} }";
excludeNull = expr: filter (v: !(isNull v)) expr;
in in
wrapObj (excludeNull (map onValue expr)); wrapObj (excludeNull (map onValue expr));
toLuaNamedField = name: expr: toLuaNamedField = name: expr:
@ -154,7 +164,7 @@ let
toLuaInternal = depth: expr: toLuaInternal = depth: expr:
let nextDepth = depth + 1; in 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 isLuaNil expr then "nil"
else if isRaw expr then expr.raw else if isRaw expr then expr.raw
else if isNamedField expr then else if isNamedField expr then
@ -181,13 +191,15 @@ in
inherit LuaNil; inherit LuaNil;
inherit raw join concat concatLines pipe; 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 op;
inherit eq ne gt lt gte lte; inherit eq ne gt lt gte lte;
inherit add sub mul div mod exp; inherit add sub mul div mod exp;
inherit and or not; inherit and or not;
inherit return return_void;
# useful alias # useful alias
var = raw; var = raw;
} }

View file

@ -178,4 +178,62 @@ with nix2lua; pkgs.lib.runTests {
end 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
'';
};
} }