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:

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:

midnight
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}:

midnight
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:

midnight
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:

midnight
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:

midnight
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:

midnight
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:

midnight
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:

midnight
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:

midnight
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:

midnight
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:

midnight
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

KindOperators
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 ~:

midnight
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:

midnight
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:

midnight
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:

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.

catch

catch runs an expression in protected mode (Lua's pcall), and if it throws, gives you something else instead:

midnight
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:

midnight
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:

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:

midnight
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:

midnight
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:

midnight
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:

Inheritance

class Child : Parent looks up anything the child doesn't define on the parent. Inside the child, super is the parent:

midnight
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>

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:

midnight
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.