add do statement

This commit is contained in:
Dmitriy Pleshevskiy 2024-05-20 14:22:47 +03:00
parent 61cd06db30
commit 654eda0737
Signed by: pleshevskiy
GPG key ID: 17041163DA10A9A2
2 changed files with 48 additions and 19 deletions

View file

@ -104,14 +104,14 @@ Add `nix2lua` as input to your `flake.nix`
`pipe expr`
> Joins expressions with a '.' (dot) separator.
> If list of expressions contains a string it will
> convert to the raw.
> Joins expressions with a '.' (dot) separator. If list of expressions contains
> a string it will convert to the raw.
>
> ```nix
> toLua (pipe ["vim" "opt" "line"]);
> # vim.opt.line
> ```
>
> ```nix
> toLua (pipe ["luasnip" (call0 "setup")]);
> # luasnip.setup()
@ -128,8 +128,8 @@ Add `nix2lua` as input to your `flake.nix`
`spaceBetween expr`
> Join expressions with a space separator. You can use it to separate
> all expressions in the Lua configuration.
> Join expressions with a space separator. You can use it to separate all
> expressions in the Lua configuration.
>
> This is alias for `join " "`
>
@ -144,14 +144,15 @@ Add `nix2lua` as input to your `flake.nix`
`call fnName args`
> Util to call a Lua function with arguments.
>
> If `args` is list that each list item render as function argument.
> Otherwise, it will be rendered as first argument.
>
> If `args` is list that each list item render as function argument. Otherwise,
> it will be rendered as first argument.
>
> ```nix
> toLua (call "setup" [ "foo" "bar" ]);
> # setup("foo", "bar")
> ```
>
> ```nix
> toLua (call "setup" { foo = "bar" });
> # or: toLua (call "setup" [{ foo = "bar" }]);
@ -171,9 +172,8 @@ Add `nix2lua` as input to your `flake.nix`
`call1 fnName arg`
> Call a Lua function with one argument.
> This is useful when a Lua function has only one argument of type list,
> and we want to avoid using an inner list.
> Call a Lua function with one argument. This is useful when a Lua function has
> only one argument of type list, and we want to avoid using an inner list.
>
> This is alias for `call fnName [arg]`.
>
@ -187,6 +187,7 @@ Add `nix2lua` as input to your `flake.nix`
> Call a require function to import module with `name`.
>
> This is alias for `call "require" name`.
>
> ```nix
> toLua (require "luasnip")
> # require("luasnip")
@ -233,6 +234,7 @@ Add `nix2lua` as input to your `flake.nix`
> ```
>
> Useful aliases:
>
> ```
> lset = l: r: local (set l r);
> ```
@ -247,6 +249,7 @@ Add `nix2lua` as input to your `flake.nix`
> ```
>
> Useful aliases:
>
> ```
> func0 = fnName: func fnName [ ];
>
@ -256,8 +259,8 @@ Add `nix2lua` as input to your `flake.nix`
`lambda params body`
> Define a lambda function with specific parameters and body.
> Useful to use Lua function inline.
> Define a lambda function with specific parameters and body. Useful to use Lua
> function inline.
>
> ```nix
> toLua (call "setup" {
@ -267,6 +270,7 @@ Add `nix2lua` as input to your `flake.nix`
> ```
>
> Useful aliases:
>
> ```
> lambda0 = lambda [ ];
> ```
@ -295,6 +299,7 @@ Add `nix2lua` as input to your `flake.nix`
> ```
>
> Useful aliases:
>
> ```
> return_void = return null;
> ```
@ -317,6 +322,21 @@ Add `nix2lua` as input to your `flake.nix`
> # if (10 == 10) print("yes") end
> ```
`do body`
> Limit scope of the variables
>
> ```nix
> toLua (do [(requireTo "foo" "foo")])
> # do local set foo = require("foo") end
> ```
>
> Aliases:
>
> ```
> scope = do
> ```
# License
GNU General Public License v3.0 or later

21
lib.nix
View file

@ -172,6 +172,8 @@ let
]));
if' = condition: trueBody: ifelse condition trueBody [ ];
do = body: spaceBetween [ kw_do ] ++ body ++ [ kw_end ];
isLuaNil = expr: getType expr == "nil";
LuaNil = { _type = "nil"; };
@ -230,10 +232,19 @@ in
inherit toLua;
inherit LuaNil;
inherit raw join concat spaceBetween;
inherit raw;
var = raw;
vars = map raw;
inherit join concat spaceBetween;
inherit pipe pipe1;
inherit namedField local;
inherit local;
inherit namedField;
nf = namedField;
inherit ifelse if';
inherit set;
lset = l: r: local (set l r);
@ -251,8 +262,6 @@ in
inherit return return_void;
# useful aliases
var = raw;
vars = map raw;
nf = namedField;
inherit do;
scope = do;
}