Language extensions
PxIgnis runs on PxLuaNova, a Lua 5.2 fork. This page documents extensions that aren’t part of standard Lua 5.2.
Lambda literal \{ ... }
Section titled “Lambda literal \{ ... }”PxLuaNova adds a non-standard syntax for inline anonymous functions. Opt in per file by placing the magic comment on line 1:
--# nova syntaxThis enables the \ + { two-character sequence to be recognized as a
lambda literal. Without the pragma, \ outside a string literal is a
syntax error.
Named-arg lambda
Section titled “Named-arg lambda”\{ args -> body } — arguments followed by -> and the body.
I tried to implement implicit return like \{ 5 } that returns 5,
but in Lua is too buggy. So just use return.
--# nova syntaxlocal double = \{ x -> return x * 2 }print(double(21)) -- 42local function double(x) return x * 2endprint(double(21))Multi-arg:
--# nova syntaxlocal sum = \{ a, b, c -> return a + b + c }print(sum(1, 2, 3)) -- 6local function sum(a, b, c) return a + b + cendprint(sum(1, 2, 3))Zero-arg
Section titled “Zero-arg”No parameters. Body is always a chunk; use return for values:
--# nova syntaxlocal answer = \{ return 40 + 2 }print(answer()) -- 42local function answer() return 40 + 2endprint(answer())Body can contain statements:
--# nova syntaxlocal get = \{ print("side effects OK") return 42}print(get()) -- 42local function get() print("side effects OK") return 42endprint(get())Trailing-block sugar
Section titled “Trailing-block sugar”\{ ... } after a function call appends the lambda as the last argument.
The body is always parsed as a chunk (no implicit return). This is
especially useful with register():
--# nova syntaxregister("greet") \{ ctx -> ctx.player:send("hello!")}register("greet", function(ctx) ctx.player:send("hello!")end)Works with any function, not just register():
--# nova syntaxlocal results = {}local function each(t, fn) for i, v in ipairs(t) do fn(v, i) endendeach({ "a", "b", "c" }) \{ v, i -> results[i] = v .. i}-- results == { "a1", "b2", "c3" }Closures
Section titled “Closures”Lambdas capture local variables from the enclosing scope, just like
function:
--# nova syntaxfunction makeCounter() local n = 0 return \{ n = n + 1 return n }end
local c = makeCounter()print(c()) -- 1print(c()) -- 2Standard library additions
Section titled “Standard library additions”The Lua standard library in PxLuaNova includes some Lua 5.3+ functions not present in standard Lua 5.2:
| Function | Description |
|---|---|
math.type(x) | Returns "integer" or "float" |
math.atan(y, x) | Two-argument arctangent (Lua 5.3+) |
math.min(...) | Variadic minimum |
math.max(...) | Variadic maximum |