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;
|
2022-11-18 12:40:18 +03:00
|
|
|
inherit (builtins) concatStringsSep filter mapAttrs attrValues;
|
|
|
|
|
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-02-28 15:04:43 +03:00
|
|
|
warn = msg: builtins.trace "[nix2lua] warning: ${msg}";
|
2024-04-17 10:56:41 +03:00
|
|
|
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:
|
|
|
|
if isString expr || isPath expr then toString expr
|
|
|
|
else error "Value '${toString expr}' is not a valid string";
|
|
|
|
|
|
|
|
################################################################################
|
|
|
|
# Low-Level
|
|
|
|
################################################################################
|
|
|
|
|
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; }
|
|
|
|
else error "Value '${toString expr}' is not supported for a join type";
|
|
|
|
|
2024-04-17 10:56:41 +03:00
|
|
|
concat = join "";
|
|
|
|
concatLines = lines:
|
|
|
|
join "\n" (
|
|
|
|
lines
|
|
|
|
# add an empty line at the end
|
|
|
|
++ [ (raw "") ]
|
|
|
|
);
|
|
|
|
|
|
|
|
isRaw = expr: getType expr == "raw";
|
|
|
|
raw = expr:
|
|
|
|
if isRaw expr then
|
2024-02-28 15:25:21 +03:00
|
|
|
{ _type = "raw"; raw = expr.raw; }
|
2024-02-28 23:40:47 +03:00
|
|
|
else if isString expr then
|
2024-02-28 15:25:21 +03:00
|
|
|
{ _type = "raw"; raw = expr; }
|
|
|
|
else
|
|
|
|
error "Value '${toString expr}' is not supported for a raw type";
|
|
|
|
|
2024-04-17 10:56:41 +03:00
|
|
|
call = fnName: args:
|
2024-02-28 16:54:16 +03:00
|
|
|
let
|
|
|
|
luaFn =
|
2024-04-17 10:56:41 +03:00
|
|
|
if isString fnName && builtins.stringLength fnName > 0 then raw fnName
|
2024-02-28 16:54:16 +03:00
|
|
|
else error "Value '${toString fnName}' is not a valid function name";
|
|
|
|
in
|
2024-04-17 10:56:41 +03:00
|
|
|
concat (
|
|
|
|
[ luaFn (raw "(") ]
|
2024-02-28 16:54:16 +03:00
|
|
|
++ [ (join ", " args) ]
|
2024-04-17 10:56:41 +03:00
|
|
|
++ [ (raw ")") ]
|
2024-02-28 16:54:16 +03:00
|
|
|
);
|
2022-11-19 00:25:30 +03:00
|
|
|
|
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
|
|
|
|
2024-04-17 10:56:41 +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;
|
|
|
|
toLuaString = expr: "\"${validString expr}\"";
|
2024-02-28 23:40:47 +03:00
|
|
|
toLuaList = onValue: expr:
|
|
|
|
let
|
|
|
|
wrapObj = expr: "{ ${concatStringsSep ", " expr} }";
|
|
|
|
excludeNull = expr: filter (v: !(isNull v)) expr;
|
|
|
|
in
|
|
|
|
wrapObj (excludeNull (map onValue expr));
|
2022-11-19 04:41:15 +03:00
|
|
|
toLuaNamedField = name: expr:
|
|
|
|
if isNull expr then null
|
2024-04-17 10:56:41 +03:00
|
|
|
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-02-28 16:54:16 +03:00
|
|
|
if isJoin expr then concatStringsSep expr.sep (map (toLuaInternal depth) expr.parts)
|
|
|
|
else if isLuaNil expr then "nil"
|
2024-04-17 10:56:41 +03:00
|
|
|
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
|
2024-04-17 10:56:41 +03:00
|
|
|
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 22:46:17 +03:00
|
|
|
|
2022-11-18 02:36:55 +03:00
|
|
|
in
|
|
|
|
{
|
2024-04-17 10:56:41 +03:00
|
|
|
# Deprecated
|
|
|
|
mkLuaNil = deprecated "mkLuaNil" "Nil" LuaNil;
|
|
|
|
mkLuaRaw = deprecated "mkLuaRaw" "raw" raw;
|
|
|
|
mkCall = deprecated "mkCall" "call" call;
|
|
|
|
mkNamedField = deprecated "mkNamedField" "namedField" namedField;
|
2024-02-28 16:54:16 +03:00
|
|
|
|
2022-11-18 02:36:55 +03:00
|
|
|
inherit toLua;
|
2024-04-17 10:56:41 +03:00
|
|
|
|
|
|
|
inherit LuaNil;
|
|
|
|
inherit raw join concat concatLines;
|
|
|
|
inherit call namedField;
|
2022-11-18 02:36:55 +03:00
|
|
|
}
|