From 654eda07372a9a2a03e746d9758f6b39de396a83 Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Mon, 20 May 2024 14:22:47 +0300 Subject: [PATCH] add do statement --- README.md | 46 +++++++++++++++++++++++++++++++++------------- lib.nix | 21 +++++++++++++++------ 2 files changed, 48 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index bffd538..a3bfc7e 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib.nix b/lib.nix index 92901d1..5d736f6 100644 --- a/lib.nix +++ b/lib.nix @@ -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; }