nix2lua/lib.nix

241 lines
7.4 KiB
Nix
Raw Normal View History

2022-11-19 04:46:37 +03:00
/**
* Copyright (C) 2022, Dmitriy Pleshevskiy <dmitriy@pleshevski.ru>
*
* nix2lua is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* nix2lua is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with nix2lua. If not, see <https://www.gnu.org/licenses/>.
*/
2022-11-18 02:36:55 +03:00
let
2022-11-19 04:41:15 +03:00
inherit (builtins) isString isFloat isInt isBool isList isAttrs isNull isPath;
2024-04-20 23:43:21 +03:00
inherit (builtins) concatStringsSep filter mapAttrs attrValues concatLists match;
2022-11-18 12:40:18 +03:00
2024-04-17 17:05:36 +03:00
isNotNull = v: !(isNull v);
excludeNull = expr: filter isNotNull expr;
2024-02-28 23:40:47 +03:00
################################################################################
# Utils
################################################################################
2024-02-28 15:04:43 +03:00
2024-02-28 23:40:47 +03:00
error = message: throw "[nix2lua] ${message}";
2024-04-20 19:11:24 +03:00
validationError = expr: message:
let val = if builtins.isFunction expr then "Function" else "Value '${toString expr}'"; in
error "${val} ${message}";
2024-02-28 15:04:43 +03:00
warn = msg: builtins.trace "[nix2lua] warning: ${msg}";
deprecated = before: now: warn "`${before}` is deprecated. Use `${now}` instead";
2024-02-28 12:50:08 +03:00
2024-02-28 23:40:47 +03:00
getType = expr: if isAttrs expr && expr ? _type then expr._type else null;
validString = expr:
2024-04-17 18:27:55 +03:00
if isRaw expr then validString expr.raw
else if isString expr || isPath expr then toString expr
2024-04-20 19:11:24 +03:00
else validationError expr "is not a valid string";
2024-02-28 23:40:47 +03:00
2024-04-17 15:45:30 +03:00
validFuncName = fnName:
if isString fnName && builtins.stringLength fnName > 0 then raw fnName
2024-04-20 19:11:24 +03:00
else validationError fnName "is not a valid function name";
2024-04-17 15:45:30 +03:00
2024-02-28 23:40:47 +03:00
################################################################################
# Low-Level
################################################################################
2024-04-17 15:45:30 +03:00
isRaw = expr: getType expr == "raw";
raw = expr:
2024-04-20 19:11:24 +03:00
if isRaw expr then { _type = "raw"; raw = expr.raw; }
else if isString expr then { _type = "raw"; raw = expr; }
else validationError expr "is not supported for a raw type";
2024-04-17 15:45:30 +03:00
2024-02-28 16:54:16 +03:00
isJoin = expr: getType expr == "_join";
join = sep: expr:
if isList expr then { _type = "_join"; sep = validString sep; parts = expr; }
2024-04-20 19:11:24 +03:00
else validationError expr "is not supported for a join type";
2024-02-28 16:54:16 +03:00
concat = join "";
2024-04-17 12:12:50 +03:00
pipe = join ".";
2024-04-19 02:09:35 +03:00
pipe1 = lft: rgt: pipe [ lft rgt ];
2024-04-17 14:55:12 +03:00
spaceBetween = join " ";
2024-04-17 15:45:30 +03:00
wrap = start: end: expr:
concat [ (raw start) expr (raw end) ];
wrapParen = wrap "(" ")";
call = fnName: args:
2024-04-20 19:11:24 +03:00
concat [
(validFuncName fnName)
(wrapParen (
if isList args then join ", " args
else if builtins.isFunction args then validationError args "is not supported to call args"
else args
))
];
2024-04-20 17:36:34 +03:00
call0 = fnName: call fnName [ ];
call1 = fnName: arg: call fnName [ arg ];
2024-04-20 19:11:24 +03:00
require = name: call "require" name;
2024-04-17 15:45:30 +03:00
kw_and = raw "and";
kw_or = raw "or";
kw_not = raw "not";
kw_function = raw "function";
kw_end = raw "end";
kw_return = raw "return";
kw_if = raw "if";
kw_then = raw "then";
kw_else = raw "else";
kw_local = raw "local";
2024-04-17 18:48:16 +03:00
kw_break = raw "break";
2024-04-17 15:45:30 +03:00
kw_for = raw "for";
2024-04-17 18:48:16 +03:00
kw_in = raw "in";
2024-04-17 15:45:30 +03:00
kw_do = raw "do";
kw_until = raw "until";
kw_while = raw "while";
kw_repeat = raw "repeat";
2024-04-17 18:27:55 +03:00
op = operation: left: right: wrapParen (join " ${validString operation} " [ left right ]);
2024-04-17 14:55:12 +03:00
add = op "+";
sub = op "-";
mul = op "*";
div = op "/";
mod = op "%";
exp = op "^";
eq = op "==";
ne = op "~=";
gt = op ">";
lt = op "<";
gte = op ">=";
lte = op "<=";
2024-04-17 15:45:30 +03:00
and = op kw_and;
or = op kw_or;
not = expr: spaceBetween [ kw_not expr ];
2024-04-17 12:12:50 +03:00
2024-04-20 19:11:24 +03:00
# Type: validBlockBody :: a -> [b]
validBlockBody = body:
if isList body then body
else if isAttrs body then [ body ]
else validationError body "is not valid block body";
2024-04-17 15:45:30 +03:00
local = expr: spaceBetween [ kw_local expr ];
2024-04-17 12:12:50 +03:00
set = variable: value: join " = " [ (raw variable) value ];
2024-04-20 22:27:27 +03:00
funcParams' = params: wrapParen (join ", " (map raw params));
2024-04-20 17:36:34 +03:00
func = fnName: params: body:
2024-04-20 22:27:27 +03:00
(spaceBetween (concatLists [
[
2024-04-17 14:55:12 +03:00
(concat [
2024-04-17 15:45:30 +03:00
(spaceBetween [ kw_function (validFuncName fnName) ])
2024-04-20 22:27:27 +03:00
(funcParams' params)
2024-04-17 14:55:12 +03:00
])
]
2024-04-20 22:27:27 +03:00
(validBlockBody body)
[ kw_end ]
]));
2024-04-20 19:11:24 +03:00
func0 = fnName: func fnName [ ];
2024-04-20 20:12:52 +03:00
lambda = params: body:
2024-04-20 22:27:27 +03:00
(spaceBetween (concatLists [
[ (concat [ kw_function (funcParams' params) ]) ]
(validBlockBody body)
[ kw_end ]
]));
2024-04-20 16:51:00 +03:00
lambda0 = lambda [ ];
2024-04-17 14:22:15 +03:00
2024-04-17 17:05:36 +03:00
return = expr: spaceBetween ([ kw_return expr ]);
2024-04-20 19:11:24 +03:00
return_void = return
null;
2024-04-17 17:05:36 +03:00
ifelse = condition: trueBody: falseBody:
2024-04-20 22:27:27 +03:00
(spaceBetween (concatLists [
[ kw_if condition kw_then ]
(validBlockBody trueBody)
(if falseBody != [ ] then [ kw_else ] ++ (validBlockBody falseBody) else [ ])
[ kw_end ]
]));
2024-04-17 17:05:36 +03:00
if' = condition: trueBody: ifelse condition trueBody [ ];
2022-11-19 04:41:15 +03:00
isLuaNil = expr: getType expr == "nil";
2024-02-28 16:54:16 +03:00
LuaNil = { _type = "nil"; };
2024-02-28 12:50:08 +03:00
isNamedField = expr: getType expr == "table_field";
namedField = name: expr: {
_type = "table_field";
name = validString name;
value = toLua expr;
};
toLuaBool = expr: if expr then "true" else "false";
toLuaNumber = toString;
2024-04-20 23:43:21 +03:00
toLuaString = expr:
let str = validString expr; in
let isLines = match ".*\n.*" str; in
if isLines != null then "[[\n${str}]]" else "\"${str}\"";
2024-02-28 23:40:47 +03:00
toLuaList = onValue: expr:
let
wrapObj = expr: "{ ${concatStringsSep ", " expr} }";
in
wrapObj (excludeNull (map onValue expr));
2022-11-19 04:41:15 +03:00
toLuaNamedField = name: expr:
if isNull expr then null
else "[${toLuaString name}] = ${expr}";
toLuaTable = onValue: expr: onValue (attrValues (mapAttrs namedField expr));
2022-11-18 02:36:55 +03:00
2022-11-19 04:41:15 +03:00
toLuaInternal = depth: expr:
2022-11-19 03:43:29 +03:00
let nextDepth = depth + 1; in
2024-04-17 17:05:36 +03:00
if isJoin expr then concatStringsSep expr.sep (map (toLuaInternal depth) (excludeNull expr.parts))
2024-02-28 16:54:16 +03:00
else if isLuaNil expr then "nil"
else if isRaw expr then expr.raw
2022-11-19 04:41:15 +03:00
else if isNamedField expr then
if depth > 0 then toLuaNamedField expr.name expr.value
2022-11-19 03:43:29 +03:00
else error "You cannot render table field at the top level"
2024-02-28 23:40:47 +03:00
else if isAttrs expr then toLuaTable (toLuaInternal nextDepth) expr
else if isList expr then toLuaList (toLuaInternal nextDepth) expr
else if isString expr || isPath expr then toLuaString expr
else if isFloat expr || isInt expr then toLuaNumber expr
2022-11-19 04:41:15 +03:00
else if isBool expr then toLuaBool expr
else if isNull expr then null
else error "Value '${toString expr}' is not supported yet";
2022-11-18 12:40:18 +03:00
2024-02-28 23:40:47 +03:00
toLua = val: toLuaInternal 0 val;
2022-11-18 02:36:55 +03:00
in
{
# Deprecated
mkLuaNil = deprecated "mkLuaNil" "Nil" LuaNil;
mkLuaRaw = deprecated "mkLuaRaw" "raw" raw;
mkCall = deprecated "mkCall" "call" call;
mkNamedField = deprecated "mkNamedField" "namedField" namedField;
2024-04-20 19:11:24 +03:00
concatLines = deprecated "concatLines" "spaceBetween" spaceBetween;
2024-02-28 16:54:16 +03:00
2022-11-18 02:36:55 +03:00
inherit toLua;
inherit LuaNil;
2024-04-20 19:11:24 +03:00
inherit raw join concat spaceBetween;
2024-04-20 16:51:00 +03:00
inherit pipe pipe1;
inherit namedField require local set ifelse if';
inherit call call0 call1;
2024-04-20 19:11:24 +03:00
inherit func func0 lambda lambda0;
2024-04-17 14:22:15 +03:00
inherit op;
inherit eq ne gt lt gte lte;
inherit add sub mul div mod exp;
inherit and or not;
2024-04-17 17:05:36 +03:00
inherit return return_void;
2024-04-17 17:22:56 +03:00
# useful aliases
2024-04-17 14:22:15 +03:00
var = raw;
2024-04-17 17:22:56 +03:00
vars = map raw;
2024-04-20 17:00:42 +03:00
nf = namedField;
2022-11-18 02:36:55 +03:00
}