# The MiruScriptX Language Specification
Version 1.1
This document defines the MiruScriptX language. It tells you what a program
means. The [wiki](../wiki/01-introduction.md) teaches the language, and the
[language reference](language-reference.md) summarizes it. This document is
different: it is the statement of record.
MiruScriptX is a revival of MiruScript, an earlier language by the same author,
written in C. The X shows that this is the successor.
For the promise about which parts of this document will not change, read the
[stability guarantee](stability.md).
---
## 1. Scope and notation
### 1.1 What this document defines
This document defines:
- The characters and tokens that a program can contain.
- The grammar that makes tokens into a program.
- The meaning of each construct.
- The types of value, and the operations on them.
- The errors, and the conditions that cause them.
- The limits, and the message at each limit.
### 1.2 What this document does not define
This document does not define the bytecode, the virtual machine, or the Rust
API. These are parts of the implementation. They can change in a 1.x release.
The [architecture guide](architecture.md) describes them.
### 1.3 Language of this document
This document uses ASD-STE100 Simplified Technical English. Each word has one
meaning. Each sentence is short. This makes the document less pleasant to read
than the wiki. It also makes the document harder to misread, which matters more
here.
### 1.4 One word for one thing
An **error** is a condition that stops a program. `try` can catch almost all
errors. A caught error is a value, and this document calls it an error too.
There is no second word for it.
### 1.5 Grammar notation
The grammar uses this notation:
| `x y` | `x`, and then `y` |
| `x \| y` | `x`, or `y` |
| `[ x ]` | `x` zero times or one time |
| `{ x }` | `x` zero or more times |
| `( x )` | Groups `x` |
| `"x"` | The characters `x` |
### 1.6 How to read a rule
Each rule in this document is a statement about all programs. If a rule and the
implementation disagree, one of the two has a defect. Tell the maintainer.
---
## 2. Lexical structure
The lexer reads the source text. The lexer makes a list of tokens. The parser
reads the tokens.
### 2.1 Source text
The source text is a sequence of Unicode characters in UTF-8. Positions in an
error message are in characters, not in bytes.
### 2.2 Comments
A comment starts with `//`. A comment continues to the end of the line. There
are no block comments.
```
// This is a comment.
let x = 1 // This is also a comment.
```
### 2.3 Statement separators
A newline ends a statement. A semicolon also ends a statement. A semicolon is
always optional.
The lexer does not make a newline token inside parentheses `( )` or brackets
`[ ]`. An expression can continue on more than one line inside these.
```
let total = (1 +
2)
```
The lexer removes a newline at the start of the source. The lexer makes one
newline token for a group of empty lines.
### 2.4 Identifiers
An identifier starts with a letter or `_`. An identifier continues with a
letter, a digit, or `_`. Letters and digits include Unicode letters and digits.
### 2.5 Keywords
These 16 words are keywords. A program cannot use a keyword as an identifier.
```
fn let return if else while for in
break continue import as try true false nil
```
A keyword is also not permitted as a field name after `.`. Use the bracket form
for a map key that is a keyword.
```
let m = {"if": 1}
print(m["if"]) // 1
print(m.if) // Error: expected a field name after '.'
```
### 2.6 Integer literals
An integer literal is a sequence of decimal digits.
There is no exponent form. There is no hexadecimal, octal, or binary form.
There is no digit separator. A literal cannot start with a minus sign: `-5` is
the negation operator and the literal `5`.
An integer literal must be in the range of a 64-bit signed integer. A literal
that is too large gives the error `integer literal '<text>' is out of range`.
> **Note.** The smallest integer, -9223372036854775808, has no literal form.
> Its magnitude is larger than the largest literal. Write
> `-9223372036854775807 - 1` instead.
### 2.7 Float literals
A float literal is a sequence of decimal digits, then `.`, then one or more
decimal digits.
A digit after the point is necessary. The text `1.` is the integer literal `1`
and then the `.` operator.
### 2.8 String literals
A string literal starts with `"` and ends with `"`.
A string literal cannot contain a newline character. A string literal that
reaches the end of a line, or the end of the source, gives the error
`unterminated string literal`.
These six escape sequences are permitted:
| `\n` | Newline |
| `\t` | Tab |
| `\r` | Carriage return |
| `\\` | Backslash |
| `\"` | Quotation mark |
| `\0` | Null |
Another escape sequence gives the error `unknown escape sequence '\<c>'`. There
is no `\u` escape and no `\x` escape.
### 2.9 Operators and punctuation
```
+ - * / %
== != < > <= >=
&& || !
= . , : ;
( ) [ ] { }
```
---
## 3. Grammar
### 3.1 Program
```
program = { statement }
statement = import | let | assign | function | return
| if | while | for | break | continue | expression
```
A statement ends at a newline or a semicolon. Section 2.3 gives the rule.
### 3.2 Statements
```
import = "import" string "as" identifier
let = "let" identifier "=" expression
assign = target "=" expression
function = "fn" identifier "(" [ params ] ")" block
params = identifier { "," identifier }
return = "return" [ expression ]
if = "if" expression block [ "else" ( if | block ) ]
while = "while" expression block
for = "for" identifier "in" expression block
break = "break"
continue = "continue"
block = "{" { statement } "}"
```
`else` can be on the same line as the previous `}`, or on the next line.
### 3.3 Expressions
```
binary = unary { operator unary }
unary = ( "-" | "!" ) unary | postfix
postfix = primary { call | index | field }
call = "(" [ arguments ] ")"
arguments = expression { "," expression }
index = "[" expression "]"
field = "." identifier
primary = integer | float | string | "true" | "false" | "nil"
| identifier | array | map | closure | "(" expression ")"
array = "[" [ expression { "," expression } ] "]"
map = "{" [ entry { "," entry } ] "}"
entry = expression ":" expression
closure = "fn" "(" [ params ] ")" block
```
An array literal and a map literal permit a trailing comma.
### 3.4 Precedence
The table gives the precedence of each operator. A larger number binds more
strongly. All binary operators are left-associative.
| 7 (strongest) | `f(x)` `a[i]` `a.b` | Left |
| 6 | `-x` `!x` | Right |
| 5 | `*` `/` `%` | Left |
| 4 | `+` `-` | Left |
| 3 | `<` `>` `<=` `>=` | Left |
| 2 | `==` `!=` | Left |
| 1 | `&&` | Left |
| 0 | `\|\|` | Left |
| Weakest | `try` | None |
`try` binds less strongly than every operator. `try a / b` applies `try` to the
division. Use parentheses to make `try` apply to less: `(try a) / b`.
---
## 4. Values and types
### 4.1 The eight types
The `type` builtin gives the name of the type of a value. There are eight
names.
| `int` | A 64-bit signed integer |
| `float` | A 64-bit IEEE 754 binary floating-point number |
| `bool` | `true` or `false` |
| `string` | A sequence of Unicode characters |
| `array` | An ordered sequence of values |
| `map` | A set of pairs of a string key and a value |
| `function` | A function, a closure, or a builtin |
| `nil` | The single value `nil` |
An error is a ninth type. The `type` builtin gives the name `error` for it.
Section 6 describes it. A program cannot make an error value without `try`.
### 4.2 Value types and reference types
An `int`, a `float`, a `bool`, a `string`, and `nil` are value types. An
operation on one of these gives a new value.
An `array` and a `map` are reference types. Two names can hold the same array.
A change through one name is visible through the other name.
```
let a = [1, 2]
let b = a
push(b, 3)
print(a) // [1, 2, 3]
```
The `slice`, `sort`, `reverse`, `map`, and `filter` builtins give a new array.
The `push` builtin changes the array and gives the same array.
### 4.3 Map key order
A map keeps its keys in sorted order. The `keys` builtin, the `values` builtin,
a `for` loop, and printing all use this order. The order does not depend on the
order of insertion.
```
print(keys({"c": 1, "a": 2, "b": 3})) // ["a", "b", "c"]
```
### 4.4 How a value prints
The `print` builtin writes a string without quotation marks. Inside an array or
a map, a string has quotation marks and escapes.
| `1` | `1` | `1` |
| `1.5` | `1.5` | `1.5` |
| `1.0` | `1.0` | `1.0` |
| `"a"` | `a` | `"a"` |
| `true` | `true` | `true` |
| `nil` | `nil` | `nil` |
| A function `f` | `<fn f>` | `<fn f>` |
| A builtin `len` | `<builtin len>` | `<builtin len>` |
| An error | `<error: MESSAGE>` | `<error: MESSAGE>` |
A float always has a decimal point, an exponent, `nan`, `inf`, or `-inf`.
---
## 5. Semantics
### 5.1 Order of evaluation
| `a + b` | `a`, then `b` |
| `f(x, y)` | `f`, then `x`, then `y` |
| `[x, y]` | `x`, then `y` |
| `{k: v}` | `k`, then `v`, for each pair in order |
| `a[i]` | `a`, then `i` |
| `a[i] = v` | **`v`, then `a`, then `i`** |
| `a.b = v` | **`v`, then `a`** |
| `let x = e` | `e`, then the name `x` starts to exist |
An assignment through an index evaluates the value first. This is the same
order as Python. Write the parts as separate statements if the order matters.
Because `let` evaluates the value first, `let x = x` reads the outer `x`.
### 5.2 Numbers
An operation on two integers gives an integer. An operation on two floats gives
a float. An operation on an integer and a float changes the integer to a float
first, and gives a float.
> **Note.** The change to a float loses precision for an integer with a
> magnitude of more than 2^53. The language does not give a warning.
Every integer operation tests for overflow. An operation that overflows gives
an error. An integer operation never wraps around.
```
print(9223372036854775807 + 1) // Error: integer overflow in addition
```
Division by zero gives the error `division by zero`. This applies to an integer
and to a float. `1.0 / 0.0` does not give `inf`.
The `%` operator gives the remainder. The sign of the result is the sign of the
left operand. `-7 % 3` is `-1`. Modulo by zero gives the error `modulo by
zero`.
### 5.2.1 Infinity and not-a-number
Arithmetic never gives `inf` or `nan`. Division by zero is an error, and every
other operation that could give one of these is an error.
Three operations can give one:
- `float("inf")` and `float("-inf")` give an infinity.
- `float("nan")` gives a not-a-number.
- `pow` gives an infinity when the result is too large.
A program can hold these values. The rules for them are:
- Arithmetic on an infinity works. `float("inf") + 1.0` is `inf`.
- An order comparison with an infinity works.
- `nan` is not equal to `nan`. `==` gives `false`, and does not give an error.
- An order comparison with `nan` gives the error `cannot compare with NaN`.
- `sort` on an array with `nan` gives the error `sort cannot order NaN`.
- `min` and `max` with `nan` give the error `<name> cannot compare NaN`.
- `int`, `floor`, `ceil`, and `round` on `inf` or `nan` give the error
`<name> of a non-finite number`.
### 5.3 Truth
Only `false` and `nil` are false. Every other value is true. `0`, `0.0`, `""`,
`[]`, and `{}` are all true.
An error is not true and not false. A test on an error stops the program.
Section 6.4 gives the rule.
The `&&` and `||` operators do not evaluate the right operand if the left
operand decides the result. Both operators give a `bool`, not the operand.
```
print(1 && 2) // true, not 2
```
### 5.4 Equality
`==` compares two values. `!=` gives the opposite result.
- An integer and a float compare as numbers. `1 == 1.0` is true.
- A `bool` is equal only to a `bool`. `true == 1` is false.
- A string compares by its characters.
- An array compares by its length and its elements.
- A map compares by its keys and its values.
- A function is equal only to itself.
- Two values of different types are not equal.
- `nan` is not equal to `nan`.
Comparison of a value that contains itself gives an error. Section 5.7 gives
the rule.
### 5.5 Order
`<`, `>`, `<=`, and `>=` compare two integers, two floats, an integer and a
float, or two strings. A string compares by the code point of each character.
A comparison with `nan` gives the error `cannot compare with NaN`. A comparison
of two other types gives an error that names both types.
### 5.6 Strings
A string holds Unicode characters. Every builtin counts characters, not bytes.
`len("héllo")` is 5.
A string does not support the index operator. `"abc"[0]` gives the error
`cannot index a string`. Use the `slice` builtin.
### 5.7 A value that contains itself
An array can contain itself. A map can contain itself.
- To print such a value, the language writes `[...]` or `{...}` at the point
the value comes back to itself.
- To compare such a value with itself gives `true`. The language compares the
identity first.
- To compare two different such values gives an error. There is no answer to
give.
A value that nests very deeply, but does not contain itself, also gives these
results at a limit. Section 9 gives the limit.
```
let a = []
push(a, a)
print(a) // [[...]]
print(a == a) // true
```
### 5.8 Names
A name at the top level of a file is a global name. A name inside a block is a
local name.
A `let` statement makes a name. An assignment does not make a name. An
assignment to a name that no `let` made gives the error `cannot assign to
undefined variable '<name>'`.
A `let` at the top level can use the name of a builtin. The file then has its
own name, and every other file keeps the builtin.
```
let print = 1
print(print) // Error: a int is not callable
```
An inner `let` can use the name of an outer variable. The inner name hides the
outer name until the end of the block.
### 5.9 Loops
A `for` loop reads an array. A `for` loop on another type gives the error
`cannot iterate over a <type>`.
A `for` loop makes a copy of the array before the first step. A change to the
array inside the loop does not change the number of steps.
The loop variable is a new name at each step. A closure that a step makes keeps
the value of that step.
### 5.10 Functions
A function that reaches its end without a `return` gives `nil`.
A call with the wrong number of arguments gives an error. The error names the
function, the number it needs, and the number it received.
A closure keeps the variables it uses. While the enclosing function runs, the
closure and the function share each variable. After the enclosing function
returns, the closure keeps its own copy.
---
## 6. Errors
### 6.1 What an error stops
An error stops the program. The program writes the error to the standard error
stream and gives the exit code 1.
### 6.2 The shape of a report
An error report has this shape:
```
error (line LINE, column COLUMN): MESSAGE
THE SOURCE LINE
^^^^
in NAME, called from line LINE
```
- The first line gives the position and the message.
- The next two lines show the source and mark the part at fault. These two
lines are not present when the error is in a different file.
- Each remaining line gives one call, from the innermost outward.
An error in an imported file names the file:
```
error (./math.miru, line 2, column 12): division by zero
```
The program does not show the source in this case. It holds the text of the
file it started with, not the text of the module.
### 6.3 Catching an error
`try EXPRESSION` gives the value of the expression. If the expression gives an
error, `try` gives the error as a value, and the program continues.
`try` applies to the whole expression after it. Section 3.4 gives the
precedence.
### 6.4 What a program can do with an error
A program can do these things with an error value:
- Put it in a variable, an array, or a map.
- Give it to `type`.
- Give it to `is_error`.
- Read one of its five fields.
Every other operation stops the program. The message is `unhandled error:` and
then the message of the original error. The position is the position of the
operation that used the error, not the position of the original error.
An error is not true and not false. `if r { }` on an error stops the program.
This is necessary: a correct result of `0`, `false`, `nil`, or `""` would look
the same as an error if an error were false.
### 6.5 The fields of an error
| `message` | `string` | What went wrong |
| `line` | `int` | The line of the error |
| `column` | `int` | The column of the error |
| `file` | `string` or `nil` | The module, or `nil` for the first file |
| `trace` | `array` | The calls, as strings |
A name that is not one of these five gives the error `an error has no field
'<name>'`.
```
fn half(n) { return n / 0 }
let r = try half(4)
print(r.message) // division by zero
print(r.trace) // ["in half, called from line 2"]
```
### 6.6 What `try` cannot catch
`try` cannot catch the call depth limit. Section 9 gives the limit. Recursion
that does not stop is a defect in the program, not a condition to handle.
---
## 7. Modules
### 7.1 The import statement
```
import "./math.miru" as math
```
The path is a string. The name after `as` is necessary.
The program reads the names of the module through the alias:
```
print(math.add(2, 3))
```
### 7.2 How the path resolves
The path resolves against the directory of the file that contains the `import`,
not against the working directory.
A program that is not in a file cannot import. The error is `cannot import:
this program was not loaded from a file`.
A path that is not a file gives the error `cannot import '<path>': no such
file`.
### 7.3 Each module runs one time
The language makes the path absolute before it looks in the cache. Two
different paths to one file are the same module. The module runs one time.
```
import "./sub/../shared.miru" as a
import "./shared.miru" as b
```
Both names give the same module. The module runs one time.
### 7.4 A cycle is an error
A module that imports itself, through any number of steps, gives an error. The
error names each step:
```
import cycle: ./y.miru -> ./x.miru -> ./y.miru
```
### 7.5 What a module gives
A module gives a map. The map holds each name that the module defines at its
top level. A name that the module uses but does not define is not in the map.
### 7.6 Names in a module
Each module has its own names. Two modules can define one name, and the two
names are separate.
The builtins are the same in every module. A module that declares the name of a
builtin changes that name for itself only.
### 7.7 When a module runs
The language resolves and runs every import before it compiles the file that
contains the import.
---
## 8. Builtins
There are 41 builtins. A program can use each of them without an import.
A builtin refuses a caught error, and stops the program. There are two
exceptions: `type` and `is_error` accept one, because a program uses them to
find out that it holds one.
### 8.1 Output and input
| `print(...)` | Any number | Writes the values, separated by a space, then a newline. Gives `nil`. |
| `input()` | 0 or 1 | Reads one line. With an argument, writes the argument first. Gives a string, or `nil` at the end of the input. |
### 8.2 Files
| `read_file(path)` | 1 string | Gives the contents of the file as a string. |
| `write_file(path, text)` | 2 strings | Writes the text to the file, and replaces what was there. Gives `nil`. |
| `file_exists(path)` | 1 string | Gives `true` if there is a file at the path. |
| `args()` | 0 | Gives the arguments the program was given, as an array of strings. The program's own path is not among them. |
**A path is resolved against the working directory**, which is the directory the
program was started from. This is not the rule for `import`, which resolves
against the file that holds it.
The two rules are different because the two things are different. A module is
part of the program and stays with it. A data file belongs to the person who
runs the program, and is where that person is. A program started with `-e` can
read a file for the same reason, although it cannot `import`.
`read_file` and `write_file` give an error if the host has no file system, for
example in a browser. `file_exists` gives `false` in that situation, because the
honest answer to the question is then no. `try` catches the error.
### 8.3 General
| `len(v)` | 1 | The number of characters, elements, or pairs. Refuses another type. |
| `type(v)` | 1 | The name of the type, as a string. |
| `is_error(v)` | 1 | `true` if the value is a caught error. |
| `str(v)` | 1 | The value as a string, in the form `print` writes. |
### 8.4 Arrays
| `push(a, v)` | 2 | Adds `v` to the end of `a`. Changes `a`. Gives `a`. |
| `pop(a)` | 1 | Removes the last element and gives it. Refuses an empty array. |
| `index_of(a, v)` | 2 | The index of the first equal element, or `-1`. |
| `slice(v, s, e)` | 3 | A new array or string, from `s` to `e`. The language limits `s` and `e` to the length. |
| `sort(a)` | 1 | A new array, sorted. Needs all numbers or all strings. |
| `reverse(v)` | 1 | A new array or string, reversed. |
| `range(e)` or `range(s, e)` | 1 or 2 | An array of integers, from `s` (or 0) to `e`. `e` is not in the result. |
### 8.5 Strings
| `upper(s)` | 1 | The string in upper case. |
| `lower(s)` | 1 | The string in lower case. |
| `trim(s)` | 1 | The string without space at each end. |
| `replace(s, a, b)` | 3 | The string with each `a` changed to `b`. |
| `split(s, sep)` | 2 | An array of strings. An empty `sep` gives each character. |
| `join(a, sep)` | 2 | The elements of `a` as one string, with `sep` between. |
| `contains(v, x)` | 2 | `true` if the string holds the substring, or the array holds an equal element. |
| `find(s, x)` | 2 | The character index of the first `x`, or `-1`. |
### 8.6 Maps
| `keys(m)` | 1 | An array of the keys, in sorted order. |
| `values(m)` | 1 | An array of the values, in the order of the keys. |
| `has(m, k)` | 2 | `true` if the map holds the key. |
### 8.7 Numbers
| `abs(n)` | 1 | The magnitude. |
| `min(...)` | 1 or more | The smallest. |
| `max(...)` | 1 or more | The largest. |
| `floor(n)` | 1 | The largest integer that is not larger than `n`. Gives an `int`. |
| `ceil(n)` | 1 | The smallest integer that is not smaller than `n`. Gives an `int`. |
| `round(n)` | 1 | The nearest integer. A half goes away from zero. Gives an `int`. |
| `sqrt(n)` | 1 | The square root. Refuses a negative number. |
| `pow(b, e)` | 2 | `b` to the power `e`. Gives an `int` for two integers with `e` not negative. Gives a `float` in each other condition. |
| `int(v)` | 1 | A string or a number as an `int`. A float goes towards zero. |
| `float(v)` | 1 | A string or a number as a `float`. |
### 8.8 Higher-order
| `map(a, f)` | 2 | A new array of `f(x)` for each element. |
| `filter(a, f)` | 2 | A new array of the elements for which `f(x)` is true. |
| `reduce(a, f, init)` | 3 | One value, from `f(accumulator, x)` for each element. |
`filter` asks whether the result of `f` is true. A result that is a caught
error stops the program, as `if` does. `map` and `reduce` only keep the result,
so they do not refuse one.
---
## 9. Limits
A program that goes past a limit stops with the message in the table. Each
limit below was reached by a test program.
| Call depth | 10000 | `call depth limit of 10000 exceeded` |
| Call arguments | 255 | `too many call arguments` |
| Captured variables in one function | 255 | `too many captured variables in one function` |
| Local variables in scope | 65536 | `too many local variables in scope` |
| Constants in one chunk | 65536 | `too many constants in one chunk` |
| Functions in one chunk | 65536 | `too many functions in one chunk` |
| Global names in one program | 65536 | `too many global variables in one program` |
| Array literal elements | 65535 | `array literal has too many elements` |
| Map literal pairs | 65535 | `map literal has too many entries` |
| Jump distance in bytes | 65535 | `the compiled body is too large to jump over` |
| Loop body in bytes | 65535 | `the loop body is too large to compile` |
| Nesting for comparing and printing | 256 | `value is nested too deeply to compare` |
| Nesting in the source text | 1000 | `the program is nested too deeply` |
| Length of one expression | 10000 | `the expression is too long` |
The value stack has no limit. Only the call depth stops recursion.
The three nesting limits count different things, and none of them follows from
another.
The limit of 1000 counts how deeply a *program* nests: brackets inside
brackets, as in `[[[ ... ]]]`.
The limit of 10000 counts how *long* one expression is: the number of operators
in one chain, as in `a + b + c`, and equally a chain of index operations such as
`a[0][0][0]` or of field accesses such as `a.b.b.b`. Nothing is nested inside
anything in these, which is why the message is different.
The two figures differ because the constructs cost different amounts of memory
to read. Nesting is read by a procedure that calls itself once for each level.
A chain is read by a loop, which costs nothing per term, and shows its cost only
later, when the program is compiled or formatted.
The limit of 256 counts how deeply a *value* nests, which a loop can make much
deeper than the program that builds it.
The call depth limit is the one limit `try` cannot catch. Section 6.6 gives the
reason.
> **Note.** The limits on constants, functions, and global names are the number
> of different items, not the number of times a program uses one.
---
## 10. The command line
| `miru` | Starts the REPL. |
| `miru repl` | Starts the REPL. |
| `miru run FILE` | Runs a program. |
| `miru -e PROGRAM` | Runs a program supplied on the command line. |
| `miru --eval PROGRAM` | Runs a program supplied on the command line. |
| `miru fmt FILE` | Writes the program in the standard form. |
| `miru fmt -w FILE` | Writes the standard form into the file. |
| `miru disasm FILE` | Writes the bytecode of a program. |
| `miru --version` | Writes the version. |
| `miru --help` | Writes the usage. |
There are two exit codes. `0` means the program did all its work. `1` means an
error stopped it, or the command line was not correct.
> **Note.** `miru disasm` shows the bytecode. The bytecode is not part of this
> specification, and it can change in a 1.x release.