add lua operators
This commit is contained in:
parent
bfa12f0d3f
commit
190a991b02
2 changed files with 42 additions and 0 deletions
28
lib.nix
28
lib.nix
|
@ -75,6 +75,26 @@ let
|
||||||
set = variable: value: join " = " [ (raw variable) value ];
|
set = variable: value: join " = " [ (raw variable) value ];
|
||||||
lset = variable: value: join " " [ (raw "local") (set variable value) ];
|
lset = variable: value: join " " [ (raw "local") (set variable value) ];
|
||||||
|
|
||||||
|
op = operation: left: right: concat [ (raw "(") (join " ${operation} " [ left right ]) (raw ")") ];
|
||||||
|
|
||||||
|
add = op "+";
|
||||||
|
sub = op "-";
|
||||||
|
mul = op "*";
|
||||||
|
div = op "/";
|
||||||
|
mod = op "%";
|
||||||
|
exp = op "^";
|
||||||
|
|
||||||
|
eq = op "==";
|
||||||
|
ne = op "~=";
|
||||||
|
gt = op ">";
|
||||||
|
lt = op "<";
|
||||||
|
gte = op ">=";
|
||||||
|
lte = op "<=";
|
||||||
|
|
||||||
|
and = op "and";
|
||||||
|
or = op "or";
|
||||||
|
not = expr: join " " [ (raw "not") expr ];
|
||||||
|
|
||||||
isLuaNil = expr: getType expr == "nil";
|
isLuaNil = expr: getType expr == "nil";
|
||||||
LuaNil = { _type = "nil"; };
|
LuaNil = { _type = "nil"; };
|
||||||
|
|
||||||
|
@ -129,4 +149,12 @@ in
|
||||||
inherit LuaNil;
|
inherit LuaNil;
|
||||||
inherit raw join concat concatLines pipe;
|
inherit raw join concat concatLines pipe;
|
||||||
inherit namedField call require set lset;
|
inherit namedField call require set lset;
|
||||||
|
|
||||||
|
inherit op;
|
||||||
|
inherit eq ne gt lt gte lte;
|
||||||
|
inherit add sub mul div mod exp;
|
||||||
|
inherit and or not;
|
||||||
|
|
||||||
|
# useful alias
|
||||||
|
var = raw;
|
||||||
}
|
}
|
||||||
|
|
14
lib.test.nix
14
lib.test.nix
|
@ -148,4 +148,18 @@ with nix2lua; pkgs.lib.runTests {
|
||||||
expr = toLua (lset "parser_config.d2" { });
|
expr = toLua (lset "parser_config.d2" { });
|
||||||
expected = "local parser_config.d2 = { }";
|
expected = "local parser_config.d2 = { }";
|
||||||
};
|
};
|
||||||
|
"test returns all operations" = {
|
||||||
|
expr = toLua (concatLines [
|
||||||
|
(eq (mul (add 1 2) (sub 2 1)) 3)
|
||||||
|
(not (eq (mul (add 1 2) (sub 2 1)) 3))
|
||||||
|
(not 10)
|
||||||
|
(gt 10 5)
|
||||||
|
]);
|
||||||
|
expected = ''
|
||||||
|
(((1 + 2) * (2 - 1)) == 3)
|
||||||
|
not (((1 + 2) * (2 - 1)) == 3)
|
||||||
|
not 10
|
||||||
|
(10 > 5)
|
||||||
|
'';
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue