Quirrel4.53.0

Compiler directives

A line starting with # is a compiler directive. The directive is an identifier-like word, with an optional default: prefix, that turns a language feature on or off for the whole file. It is not a comment (see Lexical structure). It is also not an #if-style preprocessor: there is no expression, no macro and no condition.

Three features start off and need an explicit allow (or enable) to turn on: the delete operator, the switch statement, and $${ ... } blocks. The other four start on and need an explicit forbid (or disable) to turn off: root-table access, the clone operator, implicit type methods, and the bytecode optimizer.

#strict and #relaxed change only root-table access and delete. They do not change switch or any other feature.

Placement and scope

Directives go at the top of a file, before the imports and before all other statements. Only comments and the module docstring can come before them. A directive applies to the whole file, and to every function in it. A directive in a different place is a compile error. Only #pos, described below, can be anywhere:

import "math"
#allow-switch-statement
// directives must be at the top of the file, before imports

When two directives change the same feature, the last one wins.

default:

A directive with the default: prefix also changes the VM's default for every unit compiled later in the same process. This includes a module that the current file requires later:

// main.nut
#default:forbid-root-table
let cfg = require("config.nut")   // config.nut inherits the ban, even
                                   // though it has no directive of its own

Without default:, a directive never applies to a separately compiled file. Each file's parser starts from the VM's current default.

Root table access

#forbid-root-table
::spawnPoint <- "hangar"
// Access to root table is forbidden

Everything placed in the root table through :: is visible from every other unit that shares the VM.

delete and clone

delete is forbidden by default; #allow-delete-operator turns it on. clone is allowed by default; #forbid-clone-operator turns it off. See Operators and expressions for what each operator does and why rawdelete is the usual replacement for delete.

switch

switch is off by default. Without #allow-switch-statement, switch, case and default parse as ordinary identifiers.

Implicit type methods

By default, indexing a table or instance with a name it does not have falls back to a type method of that name. params.filter is filter when params has no filter slot. #forbid-implicit-type-methods turns that fallback off. A missing slot is then always an "index does not exist" error, and never returns a function:

#forbid-implicit-type-methods
let params = { name = "alpha" }
println(params.len)
// the index 'len' (type='string') does not exist

Compiler internals

$${ statements } is a statement block used as an expression. Its value is the value that a return inside it produces. It is for code that must run more than one statement where only an expression is allowed. It is forbidden unless #allow-compiler-internals is in effect.

examples/pages/language/directives-compiler-internals.nut
#allow-compiler-internals

// $${ ... } runs a statement block in place of an expression; its
// 'return' supplies the value. Forbidden unless allowed explicitly.
let baseHitPoints = $${
  let bonus = 20
  return 100 + bonus
}
println(baseHitPoints)
Output:
120

The optimizer

#disable-optimizer turns off the peephole passes that fold constant arithmetic and collapse jump chains in the generated bytecode. It changes the size and shape of the bytecode. It does not change what a script computes or prints. Use it only to inspect a -bytecode-dump or to narrow down a suspected optimizer bug.

#pos

#pos:<line>:<column> is not in the list above. It can be anywhere in a file, it does not accept default: and it cannot be turned off. It sets the compiler's line and column counters to the given position. Every error and every __LINE__ after it is reported against that position, not against the line count of the real file. Tools that generate Quirrel source use it so that error messages point at their own input, not at the generated text.