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

21
lib.nix
View file

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