Compare commits

..

2 commits

Author SHA1 Message Date
6ec872969f
doc: update readme 2024-05-17 18:45:29 +03:00
958fe9e674
add aliases for set and func
lset = local . set
lfunc = local . func
lfunc0 = local . func0
2024-05-17 18:38:57 +03:00
2 changed files with 52 additions and 6 deletions

View file

@ -219,7 +219,7 @@ Add `nix2lua` as input to your `flake.nix`
> lte = op "<="; > lte = op "<=";
> >
> and = op kw_and; > and = op kw_and;
> or = op kw_or; > or' = op kw_or;
> not = expr: spaceBetween [ kw_not expr ]; > not = expr: spaceBetween [ kw_not expr ];
> ``` > ```
@ -231,6 +231,11 @@ Add `nix2lua` as input to your `flake.nix`
> toLua (set "vim.opt.number" true); > toLua (set "vim.opt.number" true);
> # vim.opt.number = true > # vim.opt.number = true
> ``` > ```
>
> Useful aliases:
> ```
> lset = l: r: local (set l r);
> ```
`func fnName params body` `func fnName params body`
@ -244,6 +249,9 @@ Add `nix2lua` as input to your `flake.nix`
> Useful aliases: > Useful aliases:
> ``` > ```
> func0 = fnName: func fnName [ ]; > func0 = fnName: func fnName [ ];
>
> lfunc = n: p: b: local (func n p b);
> lfunc0 = n: b: local (func0 n b);
> ``` > ```
`lambda params body` `lambda params body`
@ -277,6 +285,38 @@ Add `nix2lua` as input to your `flake.nix`
> # local function foo(bar) bar.baz() end > # local function foo(bar) bar.baz() end
> ``` > ```
`return expr`
> Return expression from a function.
>
> ```nix
> toLua (func "foo" ["bar"] (return (pipe1 "bar" "baz"));
> # function foo(bar) return bar.baz end
> ```
>
> Useful aliases:
> ```
> return_void = return null;
> ```
`ifelse condition trueBody falseBody`
> Make a Lua if else statement.
>
> ```nix
> toLua (ifelse (eq 10 10) (call "print" "yes") (call "print" "no"));
> # if (10 == 10) print("yes") else print("no") end
> ```
`if' condition trueBody`
> Make a Lua if statement without else.
>
> ```nix
> toLua (if' (eq 10 10) (call "print" "yes"));
> # if (10 == 10) print("yes") end
> ```
# License # License
GNU General Public License v3.0 or later GNU General Public License v3.0 or later

16
lib.nix
View file

@ -79,6 +79,7 @@ let
call0 = fnName: call fnName [ ]; call0 = fnName: call fnName [ ];
call1 = fnName: arg: call fnName [ arg ]; call1 = fnName: arg: call fnName [ arg ];
require = name: call "require" name; require = name: call "require" name;
requireTo = name: local (set (require name));
kw_and = raw "and"; kw_and = raw "and";
kw_or = raw "or"; kw_or = raw "or";
@ -115,7 +116,7 @@ let
lte = op "<="; lte = op "<=";
and = op kw_and; and = op kw_and;
or = op kw_or; or' = op kw_or;
not = expr: spaceBetween [ kw_not expr ]; not = expr: spaceBetween [ kw_not expr ];
# Type: validBlockBody :: a -> [b] # Type: validBlockBody :: a -> [b]
@ -151,8 +152,7 @@ let
lambda0 = lambda [ ]; lambda0 = lambda [ ];
return = expr: spaceBetween ([ kw_return expr ]); return = expr: spaceBetween ([ kw_return expr ]);
return_void = return return_void = return null;
null;
ifelse = condition: trueBody: falseBody: ifelse = condition: trueBody: falseBody:
(spaceBetween (concatLists [ (spaceBetween (concatLists [
@ -223,15 +223,21 @@ in
inherit raw join concat spaceBetween; inherit raw join concat spaceBetween;
inherit pipe pipe1; inherit pipe pipe1;
inherit namedField require local set ifelse if'; inherit namedField local;
inherit ifelse if';
inherit set;
lset = l: r: local (set l r);
inherit require requireTo;
inherit call call0 call1; inherit call call0 call1;
inherit func func0 lambda lambda0; inherit func func0 lambda lambda0;
lfunc = n: p: b: local (func n p b);
lfunc0 = n: b: local (func0 n b);
inherit op; inherit op;
inherit eq ne gt lt gte lte; inherit eq ne gt lt gte lte;
inherit add sub mul div mod exp; inherit add sub mul div mod exp;
inherit and or not; inherit and or' not;
inherit return return_void; inherit return return_void;