Reference
Everything Midnight writes becomes ordinary Lua 5.1 (with goto), so the runtime half of the language (values, metatables, closures, coroutines, the standard library) is Lua's, and the Lua manual still applies. This page is the syntax half, all of it, in two parts:
The language: Lua's features, in Midnight's syntax. If you know Lua, it's mostly a matter of spelling, plus defaults, destructuring, and interpolation.
Beyond Lua: what Lua doesn't have at all.
match,ifas a value,catch,const, classes, and markup. None of it is hard, but none of it is needed to get started either.
Getting Started walks through a first script, and Coming from Lua is the short version for Lua regulars.
Most of this syntax comes from Erde, and its reference is worth a read too.
The language
The shape of it
Blocks are braces. Statements end at the end of a line (or at a ;, if you like them). There's no then, do, or end anywhere.
local function lights_out(rooms) {
for _, room in ipairs(rooms) {
if room.lamp { room.lamp = false }
}
}
Comments are the same as Lua's, including block comments with = levels for text that contains ]]:
-- until the end of the line
--[[ until the brackets ]]
--[==[ even if there's a ]] in here ]==]
Lists inside (), [], and {} can always end with a comma, which keeps diffs quiet when the list grows a line.
Values
nil, true, false, numbers, strings, tables, and functions, as in Lua. Numbers are doubles, written any way Lua 5.4 allows, plus 0b binary:
print(23, 0.5, 2e3) -- 23 0.5 2000
print(0x17, 0b10111) -- 23 23
print(0x1.8p1) -- 3 (a hex float: 1.5 * 2^1)
Strings
Quotes, escapes, and [[block strings]] work like Lua's. What's new is that double quoted and block strings fill in {expressions}:
local hour, owl = 3, 'olive'
print("{owl} is up at {hour}am") -- olive is up at 3am
print("in an hour: {hour + 1}am") -- in an hour: 4am
print('{owl} stays literal') -- {owl} stays literal
print("\{owl}") -- {owl}
Single quotes never interpolate, which makes them the easy way to write text full of braces. In the other two, \{ is a literal brace (escaping the closing } too is allowed, not required). Values go through tostring, so a nil shows up as nil rather than an error.
Formats
Add :%spec to an interpolation to run it through string.format:
local moon = 0.734
print("moon {moon * 100:%.0f}% full") -- moon 73% full
print("color #{0x1d2b53:%06x}") -- color #1d2b53
The spec is a single string.format directive (flags, width, precision, and one of c d i o x X e E f g G q s). The % has to come right after the :, which is how {owl:upper()} stays a method call.
A block string interpolates too, and skips a newline right after [[, as in Lua:
local bedtime = 23
io.write([[
lights out at {bedtime}:00
alarm at {(bedtime + 8) % 24}:00
]])
A string literal can be the start of a method call with no extra parentheses: '%02d:%02d':format(h, m).
Tables
One structure for everything, as in Lua:
local sky = { moon = 'waning', 'orion', 'lyra' }
sky.clouds = false
print(sky.moon, sky[2], #sky) -- waning lyra 2
Fields are separated by commas only (Lua's ; separator isn't allowed).
Destructuring
Declare names from a table's fields with braces, or from its array part with brackets:
local { moon, clouds } = sky
local [ brightest, second ] = sky
Each name can carry a fallback for when the value is nil, and a field can be renamed with field: name (the rename goes first, then the fallback):
local { stars = 0 } = sky -- sky.stars or 0
local { moon: phase } = sky -- phase = sky.moon
local { fog: visibility = 'clear' } = sky -- sky.fog, or 'clear'
Parameters destructure the same way, which is handy for options tables and component props:
local function alarm({ hour = 7, snooze = true }) {
return "alarm at {hour}, snooze {if snooze { 'on' } else { 'off' }}"
}
print(alarm({ hour = 6 })) -- alarm at 6, snooze on
Only one level deep: { a: { b } } isn't valid. Reach for a second statement instead.
Functions
Declarations look like Lua's, with braces. function T.f and function T:m work as usual:
local Lamp = {}
Lamp.__index = Lamp
function Lamp.new(room) {
return setmetatable({ room = room, on = true }, Lamp)
}
function Lamp:off() {
self.on = false
print("{self.room} is dark")
}
Lamp.new('hallway'):off() -- hallway is dark
Classes write that boilerplate for you.
Every call needs parentheses, even with a lone string or table argument.
Defaults
A parameter can declare what it is when it arrives as nil. Any parameter can, in any order:
local function nap(minutes = 20, where, alarm = true) {
return "{minutes}m on the {where}, alarm {alarm}"
}
print(nap(nil, 'couch')) -- 20m on the couch, alarm true
Varargs
... is Lua's. Give it a name and the extra arguments arrive as a table:
local function dreams(who, ...scenes) {
return "{who} dreamt {#scenes} things: {table.concat(scenes, ', ')}"
}
print(dreams('cat', 'mice', 'yarn')) -- cat dreamt 2 things: mice, yarn
Arrow functions
Anonymous functions are arrows. The body is a block, or a single expression that's returned:
local is_late = (hour) -> hour >= 23 || hour < 5
local tuck_in = (kid) -> {
kid.blanket = true
return kid
}
A few details:
One parameter with no default and no
...can drop its parentheses:hour -> hour + 1, or destructured,{ name } -> name.An expression body that's a table, or several values, goes in parentheses, since a bare
{would start a block:() -> ({ ok = true }),() -> (lo, hi).=>instead of->adds a hidden first parameter,self, for methods defined inline:
local clock = {
hour = 2,
show = () => print("it's {self.hour}am"),
}
clock:show() -- it's 2am
Returning several values
Lua returns several values with commas. Midnight also lets you wrap them in parentheses, which reads better once they span lines:
local function bounds(list) {
return (
math.min(unpack(list)),
math.max(unpack(list)),
)
}
Scopes
local is Lua's local. Midnight adds two more ways to declare a name (and const, a local that can't be reassigned).
global
global x = ... writes _G.x, and inside that block, x means the global even if a local x exists outside it:
local hour = 1
do {
global hour = 23
print(hour) -- 23
}
print(hour, _G.hour) -- 1 23
module
module marks an export. A file with module declarations returns them, collected in a table named _MODULE:
-- lamp.mn
module brightness = 0.2
module function dim(by = 0.1) {
brightness = math.max(0, brightness - by)
}
local lamp = require('lamp')
lamp.dim()
local { dim } = require('lamp') -- or import just what you need
_MODULE is a plain table you can modify (give it a metatable, add fields by hand). Two rules: such a file can't also return at the top level, because then it's unclear which value is the module, and the word module is taken, so Lua 5.1's module() function isn't callable.
Operators
| Kind | Operators |
|---|---|
| Arithmetic | + - * / // % ^, unary - |
| Comparison | == != < > <= >= |
| Logic | && || ! |
| Strings and length | .. # |
| Bits | & | ~ (xor) << >>, unary ~ |
!= is "not equal" (Lua's ~= means xor assignment here, see Coming from Lua). && and || behave exactly like Lua's and and or: they short circuit, and give back one of their operands. // is floor division.
Bits
The VM is Lua 5.1, which has no bit operators, so these become calls into the bit32 library that Midnight bundles. They work on unsigned 32 bit values, which matters for negatives and ~:
print(0xf0 & 0x3c, 0xf0 | 0x0f, 0xf0 ~ 0xff) -- 48 255 15
print(1 << 4, 256 >> 4) -- 16 16
print(~0) -- 4294967295
Assignment operators
Put = after any operator that isn't a comparison to update in place: +=, -=, ..=, //=, &&=, |=, and the rest. Several targets work, as with plain assignment:
local sheep, yawns = 0, 0
sheep, yawns += 10, 1
sheep *= 2
local status = 'awake'
status ..= ', barely'
print(sheep, yawns, status) -- 20 1 awake, barely
Index expressions on the left are evaluated once: in t[next_id()] += 1, next_id runs one time.
Control flow
The usual suspects, all with braces:
if hour >= 23 {
print('late')
} elseif hour < 5 {
print('very late')
} else {
print('reasonable')
}
for countdown = 10, 1, -1 { print(countdown) }
for i, star in ipairs({ 'vega', 'deneb', 'altair' }) {
print(i, star)
}
while !asleep { count_sheep() }
repeat {
count_sheep()
} until asleep
do {
local scratch = 'only in here'
}
break, continue, goto
break leaves the innermost loop. continue jumps to its next turn, in any kind of loop:
for hour = 20, 26 {
if hour % 2 == 1 { continue }
print("check on the baby at {hour % 24}:00")
}
goto label and ::label:: are Lua 5.2's, available everywhere:
for _, line in ipairs(lines) {
for _, word in ipairs(line) {
if word == 'goodnight' { goto done }
}
}
::done::
Reserved words
break, continue, do, else, elseif, for, function, global, if, in, local, module, repeat, return, until, while, true, false, and nil.
goto, match, class, catch, and const only act as keywords in their own constructs. Lua's and, or, not, then, and end aren't reserved at all, so they're free to use as names. One nice side effect: a half asleep if not ready fails to compile instead of doing something odd.
Beyond Lua
Each of these lowers to plain Lua you could have written by hand (a chain of ifs, a pcall, a local, a metatable). They're here because writing it by hand every time is where the bugs live.
match
match compares one value against a list of arms, top to bottom, and runs the first that fits:
match signal {
'doorbell' => { wake_up() }
'rain' | 'wind' => { roll_over() } -- either one
_ if snoring => { nudge() } -- a guard
_ => { keep_sleeping() } -- everything else
}
Under the hood it's an if / elseif chain on a local copy of the subject:
do
local subject = signal
if subject == "doorbell" then wake_up()
elseif subject == "rain" or subject == "wind" then roll_over()
elseif snoring then nudge()
else keep_sleeping() end
end
Which leads to the rules:
- The subject is evaluated once. Exactly one arm runs; nothing falls through.
A pattern is an expression tested with
==. A name in a pattern is the value it holds (ex. a constantALARM =>), never a new variable.|joins patterns, so a bitwise or inside a pattern needs parentheses:(FLAG_A | FLAG_B) =>.pattern if cond =>needs both._ if cond =>is only the condition._ =>must be last. An arm after it is a compile error, since it could never run.breakandcontinueinside an arm affect the surrounding loop.matchis only special in the right spot, sos:match(p),t.match,local match = 1, andmatch(x)(a call) keep working.match (x) {is a match.
if and match expressions
Put if or match where a value goes and it produces one:
local lamp = if dark { 'on' } else { 'off' }
local sound = match animal {
'owl' => { 'hoo' }
'cricket' | 'frog' => { 'chirp' }
_ => { '...' }
}
These fix the classic a and b or c bug: an if expression gives you the chosen branch's value even when that value is nil or false. With dark && nil || 'off' you'd get 'off', and midnight lint flags that pattern when it can tell.
Each branch holds one expression, and only its first value is kept. Use a statement when a branch needs to do more.
There has to be a way out:
elseforif,_ =>formatch. A missing case can't quietly becomenil.elseif,|, and guards all work, the same as in the statements.- At the start of a line,
ifandmatchare still statements. On the right of
local x =,x =, orreturn(including an arrow's expression body), they compile to a plainifchain. Anywhere else (a function argument, a table field, a markup child) they're wrapped in a function that's called on the spot, which allocates. In a tight loop, assign to a local first.For
matchto be an expression, the subject has to be on the same line, followed by{and an arm.local m = matchis just a variable.
catch
catch runs an expression in protected mode (Lua's pcall), and if it throws, gives you something else instead:
local function wake(who) {
if who == 'bear' { error('it is winter', 0) }
return "{who} is up"
}
print(wake('owl') catch 'still asleep') -- owl is up
print(wake('bear') catch 'still asleep') -- still asleep
print(wake('bear') catch e { "no luck: {e}" }) -- no luck: it is winter
What comes after catch is the handler, and it has a few shapes:
-- a fallback value
local hour = tonumber(input) catch 7
-- a value, with the error as e
local alarm = parse_alarm(s) catch e { "bad alarm: {e}" }
-- statements that leave
local dream = recall(night) catch e { return nil, e }
-- ignore the error, on purpose
local quiet = snooze() catch nil
-- as a statement, and ignoring it as one
check_locks() catch e { log(e) }
check_locks() catch {}
A block is either one expression, which becomes the value, or statements. Statements run in your function, not a function of their own, so return returns from it and break / continue move your loop along:
local function first_number(lines) {
for _, line in ipairs(lines) {
local n = (tonumber(line) || error('not a number')) catch { continue }
return n
}
}
print(first_number({ 'owl', 'moth', '3', '4' })) -- 3
The recall line above is the pcall you'd otherwise write by hand:
-- lua
local ok, dream = pcall(recall, night)
if not ok then return nil, dream end
The rules:
Nothing gets dropped by accident: every
catchneeds a handler. Ignoring an error is something you write down,catch nil(orcatch {}as a statement).Statements in a block have to leave: they end in
return,break,continue,goto, orerror(...)(or anifwhose branches all do), since there's no value to fall back to otherwise.Like if expressions, on the right of
local x =,x =, orreturna catch compiles to plain statements, and anywhere else it's wrapped in a function that's called on the spot. That function would swallow areturn, so a block with statements only works on the right oflocal x =,x =, orreturn. Anywhere else it's a compile error.catchbinds looser than any operator and protects everything on its left, not just the last call:a + f() catch 0catches an error in the sum, andsky.moon.phase catch nilcatches indexing anilsky. They chain:a() catch b() catch ctriesa(), thenb().Only the first value comes through, from the expression and from the handler alike.
A
{aftercatchalways starts a block, so an empty table iscatch { {} }.In the header of an
if,elseif,while,for, ormatch, a catch doesn't take a block, since the{belongs to the statement:if wake(who) catch false { ... }. Wrap it in parentheses if you need one there.catchis only a keyword after an expression, solocal catch = 1,t.catch, andcatch(x)keep working.
Why there's sometimes a function
pcall can only protect what runs inside it, and Lua evaluates a call's arguments before the call. In pcall(sky.moon.phase), the lookups happen first, unprotected, and a nil sky throws straight past the pcall. So unless the left side is a plain call of a name with plain arguments, catch moves it into a small function and protects that:
-- tonumber(s) catch 0 (names: nothing to evaluate that can fail)
pcall(tonumber, s)
-- vim.fn.jobstart(cmd) catch 0 (a lookup, which can fail)
pcall(function() return vim.fn.jobstart(cmd) end)
The function is made each time the catch runs, which costs about as much as a small table: nothing to worry about, except in a very hot loop.
const
const is a local that can't be assigned again. That's checked when the code compiles, and it becomes a plain local:
const greeting = 'hello'
const { floor, max } = math -- destructures too
local function shout() {
local greeting = 'HEY' -- a new local is fine
greeting ..= '!'
return greeting
}
print(greeting, shout(), floor(max(1.5, 2.5)))
-- greeting = 'bye' -- error: cannot assign to const 'greeting'
Any assignment is an error: =, += and friends, a bare function greeting(), and assignments from inside closures. module const makes a const export, which also can't be changed with module.x = ... (another file can still change the field on the module's table).
A table written right there is frozen too, nested tables included, so const doesn't hand you a "constant" you can still change:
const cfg = { port = 8080, db = { host = 'localhost' } }
print(cfg.db.host)
cfg.db.host = 'x' -- attempt to modify a read-only table
Only literals are frozen: const t = make() makes the name const, but leaves the table alone, since other code might have it (think const json = require('json')). For a table you'll fill in, use local. table.freeze(t) freezes any table yourself (and returns it), and table.isfrozen(t) checks.
const needs a value, and is only a keyword when a name or destructure follows it, so code using const as a name keeps working.
Classes
class declares a table with methods, a constructor, and the metatable wiring that usually goes with them:
local class Lamp {
watts = 40 -- a field, shared by every lamp
new(room, on = true) { -- the constructor
self.room = room
self.on = on
}
off() { -- a method, with an implicit self
self.on = false
print("{self.room} is dark")
}
__tostring() { return "<lamp in the {self.room}>" }
}
local hall = Lamp.new('hallway')
hall:off() -- hallway is dark
print(hall, hall.watts) -- <lamp in the hallway> 40
That's the Lamp from Functions, plus a field and a metamethod. It compiles to the same Lua you'd write by hand:
-- lua
local Lamp = {}
Lamp.__index = Lamp
Lamp.watts = 40
function Lamp.new(room, on)
local self = setmetatable({}, Lamp)
if on == nil then on = true end
self.room = room
self.on = on
return self
end
function Lamp:off() ... end
function Lamp:__tostring() ... end
The members:
name = valueis a field on the class itself, so every instance sees it until it sets its own. Good for defaults and constants.name(params) { ... }is a method, defined asfunction Class:name. The parameters take defaults, destructuring, and...like any function's.Metamethods (
__tostring,__eq,__lt, ...) are just methods. Define__indexyourself and it replaces the generated one.new(params) { ... }is the constructor, called with a dot:Lamp.new('attic').selfarrives as a fresh instance and is returned at the end, unless the body ends in areturnof its own.static name(params) { ... }is a function on the class with noself, defined asfunction Class.nameand called with a dot:Lamp.parse(s). Astatic newis a constructor that builds its instance itself, ex.return setmetatable({ room = room }, Lamp).A class without
newgets one that takes no arguments and returns an empty instance.Members are separated by newlines.
,and;are allowed too, for a class on one line.staticis only a keyword before a name, so a field or method calledstaticstill works.
Inheritance
class Child : Parent looks up anything the child doesn't define on the parent. Inside the child, super is the parent:
local class Lamp {
new(room) { self.room = room }
off() { return "{self.room} is dark" }
__tostring() { return "<lamp in the {self.room}>" }
}
local class NightLight : Lamp {
off() { return super.off(self) .. ", except for a little glow" }
}
local glow = NightLight.new('nursery')
print(glow:off()) -- nursery is dark, except for a little glow
print(glow) -- <lamp in the nursery>
Call parent methods with a dot and pass
selfyourself:super.off(self).super:off()would hand the parent class in asself.A subclass without
new(likeNightLight) gets one that passes its arguments to the parent'snewand returns the result as aNightLight.A subclass with its own
newstarts from a freshselfand doesn't run the parent's constructor. Put shared setup in a method on the parent and call it, ex.super.init(self, room).Metamethods are copied down when the subclass is declared, since Lua looks them up on the metatable itself rather than through
__index. That's whyprint(glow)works. One added to the parent later isn't copied.- The parent can be a dotted name,
class Dimmer : lamps.Lamp. superalways means the parent the class was declared with, even if a method has a local of the same name. Outside a subclass it's an ordinary name.
Declaring classes
local class, global class, and module class work like the other scopes. module class Lamp exports it, so another file can local { Lamp } = require('lamp'). A bare class Lamp works like a bare function: it assigns to a Lamp already in scope, or to a global.
class is only a keyword when a name follows it on the same line, so local class = 'x', t.class, and class="..." on a markup element keep working.
Markup
Elements are part of the grammar. Each one compiles to a call, h(tag, props, ...children), and the midnight.markup runtime turns those into HTML:
local Star = ({ name, bright }) -> <li class={if bright { 'bright' } else { nil }}>
{name}
</li>
local html = require('midnight.markup').render(<ul>
<Star name="sirius" bright/>
<Star name="polaris"/>
</ul>)
print(html)
A < only starts an element where a value can begin, so a < b is still a comparison. The Markup page covers the rest: components, state, scoped styles, and pages that update live.