fix keyword separator in operations

This commit is contained in:
Dmitriy Pleshevskiy 2024-04-17 18:27:55 +03:00
parent f1f7668117
commit ffcaaabeed
Signed by: pleshevskiy
GPG Key ID: 17041163DA10A9A2
2 changed files with 11 additions and 2 deletions

View File

@ -32,7 +32,8 @@ let
getType = expr: if isAttrs expr && expr ? _type then expr._type else null; getType = expr: if isAttrs expr && expr ? _type then expr._type else null;
validString = expr: validString = expr:
if isString expr || isPath expr then toString expr if isRaw expr then validString expr.raw
else if isString expr || isPath expr then toString expr
else error "Value '${toString expr}' is not a valid string"; else error "Value '${toString expr}' is not a valid string";
validFuncName = fnName: validFuncName = fnName:
@ -93,7 +94,7 @@ let
kw_while = raw "while"; kw_while = raw "while";
kw_repeat = raw "repeat"; kw_repeat = raw "repeat";
op = operation: left: right: wrapParen (join " ${operation} " [ left right ]); op = operation: left: right: wrapParen (join " ${validString operation} " [ left right ]);
add = op "+"; add = op "+";
sub = op "-"; sub = op "-";
@ -203,4 +204,6 @@ in
# useful aliases # useful aliases
var = raw; var = raw;
vars = map raw; vars = map raw;
inherit validString;
} }

View File

@ -162,6 +162,12 @@ with nix2lua; pkgs.lib.runTests {
(10 > 5) (10 > 5)
''; '';
}; };
"test returns rendered condition" = {
expr = toLua (set "name"
(and (gte (var "a") 1) (lte (var "a") 10))
);
expected = "name = ((a >= 1) and (a <= 10))";
};
"test returns defined function" = { "test returns defined function" = {
expr = toLua (func "hello" [ "a" "b" ] [ expr = toLua (func "hello" [ "a" "b" ] [
(eq (mul (add (var "a") 2) (sub 2 1)) 3) (eq (mul (add (var "a") 2) (sub 2 1)) 3)