diff --git a/lib.nix b/lib.nix index 32ec9f2..8e639ec 100644 --- a/lib.nix +++ b/lib.nix @@ -75,6 +75,26 @@ let set = variable: value: join " = " [ (raw 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"; LuaNil = { _type = "nil"; }; @@ -129,4 +149,12 @@ in inherit LuaNil; inherit raw join concat concatLines pipe; 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; } diff --git a/lib.test.nix b/lib.test.nix index 4cff866..97c128a 100644 --- a/lib.test.nix +++ b/lib.test.nix @@ -148,4 +148,18 @@ with nix2lua; pkgs.lib.runTests { expr = toLua (lset "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) + ''; + }; }