/** * Copyright (C) 2022, Dmitriy Pleshevskiy * * 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 . */ let inherit (builtins) isString isFloat isInt isBool isList isAttrs isNull isPath; inherit (builtins) concatStringsSep filter mapAttrs attrValues; ################################################################################ # Utils ################################################################################ error = message: throw "[nix2lua] ${message}"; warn = msg: builtins.trace "[nix2lua] warning: ${msg}"; deprecated = before: now: warn "`${before}` is deprecated. Use `${now}` instead"; 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 ################################################################################ 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"; concat = join ""; concatLines = lines: join "\n" ( lines # add an empty line at the end ++ [ (raw "") ] ); pipe = join "."; isRaw = expr: getType expr == "raw"; raw = expr: if isRaw expr then { _type = "raw"; raw = expr.raw; } else if isString expr then { _type = "raw"; raw = expr; } else error "Value '${toString expr}' is not supported for a raw type"; call = fnName: args: let luaFn = if isString fnName && builtins.stringLength fnName > 0 then raw fnName else error "Value '${toString fnName}' is not a valid function name"; in concat ( [ luaFn (raw "(") ] ++ [ (join ", " args) ] ++ [ (raw ")") ] ); require = name: call "require" [ name ]; set = variable: value: join " = " [ (raw variable) value ]; lset = variable: value: join " " [ (raw "local") (set variable value) ]; isLuaNil = expr: getType expr == "nil"; LuaNil = { _type = "nil"; }; 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}\""; toLuaList = onValue: expr: let wrapObj = expr: "{ ${concatStringsSep ", " expr} }"; excludeNull = expr: filter (v: !(isNull v)) expr; in wrapObj (excludeNull (map onValue expr)); toLuaNamedField = name: expr: if isNull expr then null else "[${toLuaString name}] = ${expr}"; toLuaTable = onValue: expr: onValue (attrValues (mapAttrs namedField expr)); toLuaInternal = depth: expr: let nextDepth = depth + 1; in if isJoin expr then concatStringsSep expr.sep (map (toLuaInternal depth) expr.parts) else if isLuaNil expr then "nil" else if isRaw expr then expr.raw else if isNamedField expr then if depth > 0 then toLuaNamedField expr.name expr.value else error "You cannot render table field at the top level" 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 else if isBool expr then toLuaBool expr else if isNull expr then null else error "Value '${toString expr}' is not supported yet"; toLua = val: toLuaInternal 0 val; in { # Deprecated mkLuaNil = deprecated "mkLuaNil" "Nil" LuaNil; mkLuaRaw = deprecated "mkLuaRaw" "raw" raw; mkCall = deprecated "mkCall" "call" call; mkNamedField = deprecated "mkNamedField" "namedField" namedField; inherit toLua; inherit LuaNil; inherit raw join concat concatLines pipe; inherit namedField call require set lset; }