« BackWriting a DSL in Lua (2015)leafo.netSubmitted by flykespice 4 days ago
  • neilv 6 hours ago

    This is a nice example of one way to implement DSLs in a language that doesn't support them well.

    IIUC, they're using Lua's language's function application syntax, combined with the ability to introduce a different namespace environment for function lookup, and then they're evaluating the tree of function calls at run time.

    In languages that support doing this at compile time, through compile-time evaluation or compile-time syntax transformation, you can make run time faster. For example of syntax transformation, https://www.neilvandyke.org/racket/html-template/ shows an example of expanded syntax, where it's rendered some of the angle brackets and coalesced that with (escaped) HTML CDATA.

    If you wanted to extend that, to support chunks of traditional HTML angle-bracket syntax inline with code (without using string literals), like in React, you could define a Racket reader to parse those chunks and transform them to parentheses syntax above.

    • wahern 6 hours ago

      > This is a nice example of one way to implement DSLs in a language that doesn't support them well.

      It's not a coincidence that Lua supports such syntax for such use cases.

    • roxolotl 5 hours ago

      Lua is a much cooler and more useful language than I think it, publicly, gets credit for. I know it’s widely used in many industries but so many times I’ve read an article like this and thought that it could be even more widely useful.

      I’m currently writing a just for me static site generator in Fennel and it’s sad to see just how many powerful packages are abandoned. A lot of it is because they are mostly complete and part of why I like it is lack of churn but a last update on 18 months ago is very different from 10 years.

      I want to host knitting patterns with my site generator and techniques like this make it super easy to build a little pattern dsl. It’s stuff like that which makes me wish it was more widespread.

      • epcoa 4 hours ago

        > Lua is a much cooler and more useful language than I think it, publicly, gets credit for.

        I think it’s gets about the right amount of credit. It isn’t obscure by any means, openresty is used quite a bit in odd places, so many games have used it. PyTorch (well what became it) was originally written in it (it was just torch but basically LuaTorch)

        It doesn’t get more adoption because its warts and limitations really start to show once pushed into more into a “real” language. It doesn’t have the browser monopoly. It’s also quite slow, and LuaJIT doesn’t always magically fix that.

        • aeadio 2 hours ago

          Lua without JIT is one of the fastest interpreted languages. In fact it might just be the fastest. LuaJIT is also one of the most advanced JIT compilers out there, frequently beating V8 and others.

          There are very valid complaints about Lua. It lacks useful quality of life features. Some of its design decisions grate on users coming from other languages. Its standard library is barebones verging on anemic, and its ecosystem of libraries does not make up for it.

          But after using Lua in many places for well over a decade, I’ve gotta say, this is the first time I’ve heard someone claim it’s slow. Even without JIT, just interpreter to interpreter, it’s consistently 5-10x faster than similar languages like Python or Ruby. Maybe you’re comparing it to AOT systems languages like C and C++? That’s obviously not fair. But if you put LuaJIT head to head with AOT systems languages you’ll find it’s within an order of magnitude, just like all the other high quality JITs.

          • Yliaho 3 hours ago

            > LuaJIT doesn’t always magically fix that.

            Not to mention there are some significant performance issues on aarch64. Love2D, a popular game framework, opted to not use JIT on macOS for this reason.

            https://github.com/LuaJIT/LuaJIT/issues/285

            • aeadio 2 hours ago

              This is strictly about how much memory LuaJIT can address on that platform. There’s no “significant performance issues”. If your workload fits inside 2 GiB (IIRC) it will be fast. LuaJIT is a world class JIT.

              For a long time a new garbage collector has been an open issue for LuaJIT, which would fix this particular issue, and make the language even faster. Last I checked this was actively being worked on.

        • dan353hehe 5 hours ago

          I love Lua for being able to do things like this.

          I was building a bunch of html pages for an htmx frontend and a golang back end, and got really exhausted from using the builtin `html/template` library in golang. I kept trying to build reusable components, and it just didn’t work as I wanted it to.

          I ended up doing this exact thing as mentioned in this blog post. https://github.com/DBarney/Glua Granted this is just for me, and for prototyping. I put it on GitHub so I wouldn’t loose it.

          Writing html got so much easier this way:

              local function Component(data)
               return div{"my name is", data.name}
              end
          
              return function(params)
                html{
                  head{title="this is my page"},
                  body{
                    h1{"This is my small page I wrote"},
                    p{
                      "some content",
                      "more content"
                    },
                    div{"page has some dynamic values",params.count},
                      Component{name="daniel"}
                  }
                }
              end
          
          Edit: formatting
        • gustavopezzi 2 hours ago

          Lua's LPeg library would also be a great option here. A couple of years ago I taught an interpreters class together with Roberto Ierusalimschy and we used LPeg for lexing and parsing. The end result was great. Even if you're not using PEGs for your implementation I would recommend spending 30 minutes and looking at LPeg.

          • moonlet 5 hours ago

            While I’m surprised to see they aren’t writing about https://www.inf.puc-rio.br/~roberto/lpeg/ it looks like they’ve covered LPEG in a previous post, and their chosen method this time around is neat anyway!

            • dang 5 hours ago

              Related:

              Writing a DSL in Lua (2015) - https://news.ycombinator.com/item?id=22223542 - Feb 2020 (41 comments)

              • alberth 6 hours ago

                I've always wanted to recreate a Python like syntax for Lua, this might be a good guide for that.

                • stdbrouw 6 hours ago

                  Around the time when CoffeeScript was popular, there was also https://moonscript.org/ for Lua. Made by the very same prolific Leaf who authored this DSL tutorial.