add keywords

This commit is contained in:
Dmitriy Pleshevskiy 2024-04-17 15:45:30 +03:00
parent 176026c58d
commit e80b4f0b88
Signed by: pleshevskiy
GPG key ID: 17041163DA10A9A2

85
lib.nix
View file

@ -32,10 +32,23 @@ let
if isString expr || isPath expr then toString expr
else error "Value '${toString expr}' is not a valid string";
validFuncName = fnName:
if isString fnName && builtins.stringLength fnName > 0 then raw fnName
else error "Value '${toString fnName}' is not a valid function name";
################################################################################
# Low-Level
################################################################################
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";
isJoin = expr: getType expr == "_join";
join = sep: expr:
if isList expr then { _type = "_join"; sep = validString sep; parts = expr; }
@ -51,7 +64,37 @@ let
pipe = join ".";
spaceBetween = join " ";
op = operation: left: right: concat [ (raw "(") (join " ${operation} " [ left right ]) (raw ")") ];
wrap = start: end: expr:
concat [ (raw start) expr (raw end) ];
wrapParen = wrap "(" ")";
call = fnName: args:
concat [ (validFuncName fnName) (wrapParen (join ", " args)) ];
require = name: call "require" [ name ];
kw_and = raw "and";
kw_or = raw "or";
kw_not = raw "not";
kw_function = raw "function";
kw_end = raw "end";
kw_break = raw "break";
kw_return = raw "return";
kw_if = raw "if";
kw_in = raw "in";
kw_then = raw "then";
kw_else = raw "else";
kw_elseif = raw "elseif";
kw_local = raw "local";
kw_for = raw "for";
kw_do = raw "do";
kw_until = raw "until";
kw_while = raw "while";
kw_repeat = raw "repeat";
# kw_false = raw "false";
# kw_true = raw "true";
# kw_nil = raw "nil";
op = operation: left: right: wrapParen (join " ${operation} " [ left right ]);
add = op "+";
sub = op "-";
@ -67,46 +110,22 @@ let
gte = op ">=";
lte = op "<=";
and = op "and";
or = op "or";
not = expr: spaceBetween [ (raw "not") expr ];
and = op kw_and;
or = op kw_or;
not = expr: spaceBetween [ kw_not expr ];
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 ];
local = expr: spaceBetween [ (raw "local") expr ];
local = expr: spaceBetween [ kw_local expr ];
set = variable: value: join " = " [ (raw variable) value ];
func = name: params: body:
func = fnName: params: body:
(concatLines
([
(concat [
(spaceBetween (map raw [ "function" name ]))
(raw "(")
(join ", " (map raw params))
(raw ")")
(spaceBetween [ kw_function (validFuncName fnName) ])
(wrapParen (join ", " (map raw params)))
])
]
++ body
++ [ (raw "end") ])
++ [ kw_end ])
);
isLuaNil = expr: getType expr == "nil";