Lua Notes

short fn syntax
Login

short fn syntax

From discord. my_dsl.nelua:

fn f() {
  print 'hello'
}

f()

And to run it:

##[[
-- Load the AST builder/parser
local aster = require 'nelua.aster'
-- Load the language syntax definitions
local syntaxdefs = require 'nelua.syntaxdefs'
-- Patch the grammar to add the `fn` syntax
local new_syntaxdefs = {
  errors = syntaxdefs.errors,
  defs = syntaxdefs.defs,
  grammar = syntaxdefs.grammar,
  extension = "nelua",
}
new_syntaxdefs.grammar = new_syntaxdefs.grammar:gsub("local%s*<%-%-[^\n]+\n",
[=[
local           <-- `local` (localfunc / localvar) / fn
fn : FuncDef    <== `fn` $'local' @namedecl @funcbody
]=])
new_syntaxdefs.grammar = new_syntaxdefs.grammar:gsub("funcbody%s*<%-%-[^\n]+\n",
[=[
funcbody        <-- `(` funcargs @`)` (`:` @funcrets)~? annots~? (`{`  Block @`}` / Block @`end`)
]=])
-- Update the compiler syntax
aster.register_syntax(new_syntaxdefs)
]]

require 'my_dsl'