The language
This chapter describes the language: what the compiler accepts, and what each construct does at runtime. The standard library is not here. Each function has its own page under Modules and Types.
Quirrel is a C-family language with dynamic types, closures, generators, classes and reference-counted memory. If you already write Python, JavaScript, Lua or Squirrel, start at the introduction. It maps what you know onto these pages, so you can read only the parts that differ.
The pages are in reading order. Each one is short, states the rule, and shows a sample that runs.
Pages
| Lexical structure | Comments, identifiers, keywords, number literals and statement separators. |
| Bindings and constants | local, let, const and enum, and the scope of each one. |
| Values and types | The fourteen value types, truthiness, number precision, and equality. |
| Functions | Definitions, default and vararg parameters, calls and closures. |
| Operators and expressions | Operator precedence, values that count as false, and the null-safe operators. |
| Control flow | if, while, for, foreach, and switch (off by default). |
| Tables and arrays | Tables and arrays: slots, order, growth and freezing. |
| Strings | Ordinary, verbatim and interpolated string literals; strings are immutable bytes. |
| Classes and instances | Class bodies, instances, inheritance and static members. |
| Metamethods | The seventeen reserved method names the VM calls, and the contract of each. |
| Destructuring | Patterns that bind a table's keys or an array's positions to names. |
| Generators and threads | yield, resume, generator states, and the differences from threads. |
| async and await | async functions, await, and the Future an async call returns. |
| Modules | require, from-import, module exports, persist and reloading. |
| Type annotations | Type annotations on a parameter, a return type or a binding. |
| Errors and exceptions | throw, try/catch, assert, and uncaught errors. |
| Compiler directives | The # directives that turn a language feature on or off. |
The shape of the language
- Everything is a value. A function, a class, a generator and a module can all be stored in variables and passed around.
- A name is bound once with let, and the compiler rejects a second assignment to it. Use
localwhen a name has to change. - A table slot has to exist before
=writes to it.<-creates a slot. Most ported code trips over this rule. - A missing key throws. It does not give
null. Use?.and??to handle an absent value. - There is no
undefined, no automatic string-to-number conversion in arithmetic, and noNaNfrom integer division. Each of those is an error.
See also
- Cheat sheet - the same rules on two printable pages
- Traps - common mistakes
- Guides - attributes, limits and other reference material
- Embedding Quirrel - running the VM from a C++ host