[HN Gopher] Writing a DSL in Lua (2015)
___________________________________________________________________
Writing a DSL in Lua (2015)
Author : flykespice
Score : 36 points
Date : 2025-02-21 14:46 UTC (3 days ago)
(HTM) web link (leafo.net)
(TXT) w3m dump (leafo.net)
| neilv wrote:
| 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 wrote:
| > 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.
| alberth wrote:
| I've always wanted to recreate a Python like syntax for Lua, this
| might be a good guide for that.
| stdbrouw wrote:
| 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.
| moonlet wrote:
| 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!
| dan353hehe wrote:
| 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
| dang wrote:
| Related:
|
| _Writing a DSL in Lua (2015)_ -
| https://news.ycombinator.com/item?id=22223542 - Feb 2020 (41
| comments)
___________________________________________________________________
(page generated 2025-02-24 23:00 UTC)