miruscriptx 2.1.0

MiruScriptX: a complete, general-purpose scripting language written from scratch in Rust.
Documentation
# The MiruScriptX Stability Guarantee

Version 2.1

This document tells you what will not change while MiruScriptX has the version
number 2. It is short on purpose. A promise that nobody can check is not a
promise.

The [specification](specification.md) says what the language does. This
document says which of those statements you can depend on.

**Version 1 kept this promise from 1.0 to 1.12 and then ended.** Section 7 says
what version 2 changed and why, and
[migrating to version 2](migrating-to-2.md) says what to do about it. A version
1 program does not run here without a look; `miru migrate` says what to change,
and reads a version 1 program in order to do it.

---

## 1. The rule

A program that is correct with version 2.0 stays correct with every later
version 2.x.

"Correct" means the program does the same work and gives the same result. It
does not mean each character of each message stays the same. Section 3 gives
the difference.

---

## 2. What is stable

### 2.1 Syntax

Every program that parses with 2.0 parses with each later 2.x, and has the same
meaning. Sections 2 and 3 of the specification define this.

A later 2.x can add syntax. From the release that adds it, the new syntax has
the same promise as the rest: a later 2.x cannot remove it, and cannot change
what it means. This is the rule section 2.3 states for a builtin.

The syntax 2.0 adds is stable from 2.0 in that way: `match`, a default on a
parameter, and a `...rest` parameter.

**The one rule behind all of this: an error can be given a meaning later, and a
meaning cannot be taken back.** Version 1 added syntax five times and each time
the shape it took was an error before, so no program changed meaning. It is also
why 1.9 spelled interpolation `f"..."` rather than `"${n}"`, which was already a
working program that printed the braces, and why one loop variable over a map
is still an error rather than a guess between the key and the value.

**Fifteen words are reserved and have no meaning.** Section 2.5 of the
specification lists them. Using one is an error, so a later 2.x can give one a
grammar without breaking a program, which is the whole reason they were reserved
together rather than one per major version.

### 2.2 Semantics

The rules in section 5 of the specification are stable. This includes:

- The order of evaluation of each construct.
- The rules for numbers, and for overflow.
- Which values are true and which are false.
- The rules for equality and for order.
- The rules for names, for loops, and for functions.

### 2.3 The builtins

All 68 builtins keep their names, their arguments, and their results. Section 8
of the specification lists them.

Four of them need something from the host: `read_file`, `write_file`,
`file_exists`, and `args`. What is stable is what they do **when the host gives
them a file system and a command line**, and that a host which does not is
refused rather than silently given nothing. `miru` gives both. A host that
embeds the language decides for itself, and the browser playground gives
neither.

`now` and `sleep` need the host's clock, `read_key` and `key_ready` need its
keyboard, and `clear`, `move_to`, `hide_cursor`, `show_cursor`, and `term_size`
need a terminal, on the same terms. These are separate capabilities rather than
more file operations, and a host can have any of them without the others: the
playground has a clock, no file system, no keyboard, and no terminal. What is
stable is what each gives **when the host has the thing it needs**, and that a
host without it is refused.

**A host can have a clock and still refuse `sleep`**, which is the one place two
builtins on one capability part company. A page can say what the time is and
cannot spend it: a browser paints between turns of its event loop, so blocking
would freeze the tab rather than pace anything. That split is stable: `now`
working somewhere is not a promise that `sleep` does.

**Where the output is not a terminal, the four drawing builtins do nothing and
`term_size` refuses.** That difference is deliberate and stable. Nothing is the
honest result of "clear the screen" when the output is a file, and it is what
keeps a program's output free of control characters when it is redirected.
`term_size` has a number to give back and no true one to give, so it refuses
rather than inventing eighty columns.

**The key names `read_key` gives are stable.** Section 8.11 of the
specification lists them. A later 2.x can add a name for a key that gives
`"unknown"` today; it cannot change one that is already there.

A later 2.x can add a builtin. A later 2.x cannot remove one, and cannot change
what one does.

### 2.4 The command line

These commands and options keep their behaviour: `run`, `-e` and its long form
`--eval`, `fmt`, `fmt -w`, `migrate`, `migrate -w`, `disasm`, `repl`,
`--version`, and `--help`.

**`migrate` reads a version 1 program**, which is the one thing in this binary
that does. It exists so that upgrading first and migrating second, which is the
order most people will do it in, does not mean reinstalling 1.12 to get out.
Section 7 covers what it changes and what it will not.

`0` and `1` keep their meanings. `0` means the program did all its work. `1`
means an error stopped it.

Since 1.2 a program can choose its own code with `exit`, from 0 to 255. This
does not change what `0` and `1` mean, and it does not change what a program
that never calls `exit` gives back. Before 1.2 those were the only two codes,
because there was no way to ask for another.

### 2.5 The shape of an error

An error report keeps this shape:

- A line with the position and the message.
- The source line and a mark below it, when the error is in the file that ran.
- The name of the file, when the error is in a different file.
- One line for each call.

A caught error keeps its five fields: `message`, `line`, `column`, `file`, and
`trace`.

### 2.6 Termination

**No program makes the process fail in a way the program cannot see.** This is
the general form. Three cases show what it means:

- A value that contains itself gives an error, or prints a mark. It does not
  stop the process.
- A value nested to any depth, and a chain of values of any length, is released
  without stopping the process. A loop can build one far deeper than any
  literal.
- Source nested past the limit in section 9 of the specification gives a syntax
  error with a line and a column.

Recursion that does not stop gives the call depth error. `try` cannot catch it.

Before 1.1 the first case was the whole promise, and the other two stopped the
process outright: no message, nothing to catch, and at the end of a program the
output still in the buffer was lost as well. The wording was narrow because the
defect was seen as one value rather than as a class.

### 2.7 Modules

An `import` resolves against the directory of the file that holds it. Each
module runs one time. A cycle is an error. A module gives the names it defines.

---

## 3. What is not stable

Do not depend on these. A later 2.x can change any of them.

### 3.1 The words in a message

The **shape** of an error report is stable. The **words** are not. A later 2.x
can make a message clearer.

**How many reports a program gets is not stable either.** Since 1.7 a program
with several syntax mistakes gets a report for each, where it used to get one.
The shape of each is what section 2.5 promises; how many appear, and which of
them a later 1.x decides are consequences of an earlier one rather than
separate mistakes, is not promised. Section 6.2.1 of the specification gives
the rules as they are today.

Do not compare a message with a fixed string. Use `is_error` to find out that a
program holds an error. Read the `message` field to show it to a person.

### 3.2 The numbers in the limits

The limits in section 9 of the specification can change. A later 2.x can raise
one.

Two of them are recent, and neither has evidence behind the value: the nesting
limit of 256 for comparing and printing, and the mark `[...]` that printing
uses for a value that contains itself. Do not depend on either.

The two limits on the source text can also change, and are more likely to than
the others: 1000 on how deeply a program nests, and 10000 on how long one
expression is. Each is set by how much stack the tightest supported build has.
A later 2.x can raise either. No 2.x lowers either.

Both were chosen to sit above what 1.0 did, and not only above what aborts.
1.0 had no limit, so it ran until the stack ran out, and its release binary on
the 1 MiB stack the playground had reached 917 levels of nesting and a sum of
4959 terms. Every program that worked on every 1.0 build still works.

A sum longer than about 10000 terms is the one case that does not. 1.0 reached
40255 of them on an 8 MiB main thread, and nothing near that in a browser, so
such a program ran on one build and stopped the process on another. Section 2.1
is about a program that was correct with 1.0, and a program that aborted the
process wherever the stack was smaller was not.

What does not change is what happens at either limit: a program that goes past
one stops with an error that gives a line and a column. Before 1.1 there was no
limit, and such a program stopped the whole process with no message.

### 3.3 The stack the limits assume

This is a condition on the guarantee, not a limit. Read it if you use
MiruScriptX as a Rust library.

The interpreter walks its own structures by recursion, so the limits above hold
only where there is stack for them. Two builds supply that stack:

- The `miru` program runs its work on a thread of 64 MiB.
- The WebAssembly build links a shadow stack of 16 MiB.

**The `miruscriptx` crate does not.** A call to `run_source` uses the stack of
the thread that makes the call, which is 2 MiB by default and does not support
either limit. Give the call a thread with a larger stack:

```rust
std::thread::Builder::new()
    .stack_size(64 * 1024 * 1024)
    .spawn(|| miruscriptx::run_source(source, out))
```

Everything in section 2 holds when you do. Without it, deep source can still
stop the process.

### 3.4 The bytecode

The bytecode, the numbers of the opcodes, and the output of `miru disasm` are
not stable. They are how the language runs, not what it means.

### 3.5 The Rust API

The `miruscriptx` crate has public Rust items. They are not stable. They exist
for the `miru` program and for the playground.

### 3.6 The WebAssembly interface

The functions the playground uses are not stable.

### 3.7 Speed

Speed is not part of the promise. A later 2.x can be faster or slower.

### 3.8 The numbers a seed produces

`random` gives a float from 0 up to but not including 1. `random_int(a, b)`
gives an integer from `a` to `b`. Those ranges are stable, and so is the rule
that two runs from the same seed give the same numbers.

**Which numbers is not stable.** A later 2.x can change the generator, and then
a seed gives a different sequence. A program that stores a seed and expects to
reproduce a result later needs the same release of MiruScriptX, and not only the
same seed.

This is a decision and not an omission. A promise about the sequence would fix
the algorithm for the whole of version 2, including any defect found in it.

It is last in this section rather than beside 3.1, which it sits closest to in
subject. The changelog refers to these subsections by number, so inserting one
in the middle would make a record of an earlier release point at the wrong
thing.

---

## 4. What a 2.x release can do

A later version 2.x can:

- Add a builtin.
- Add syntax, if each 2.0 program keeps its meaning. Giving one of the fifteen
  reserved words a grammar is the readiest example.
- Make an error message clearer.
- Change a limit.
- Change the bytecode or the virtual machine.
- Change the speed.

---

## 5. What needs version 3

A change that makes a correct 2.0 program incorrect needs version 3. This
includes:

- Removal of a builtin, or a change to what one does.
- A change to the meaning of any syntax.
- A change to the order of evaluation.
- Removal of a command or an option.

---

## 6. If the implementation disagrees

If the implementation and the specification disagree, one of the two has a
defect. Tell the maintainer. Do not write a program that depends on the
disagreement.

---

## 7. What version 2 changed, and why

Version 1 kept the promise in section 1 from 1.0 to 1.12, across thirteen
releases. **1.12 was the last of them**, and there is no maintenance branch.
[Migrating to version 2](migrating-to-2.md) is the practical guide; this section
is the reason.

**A major version was spent on two things that could not be had any other way.**
Everything else in 2.0 rode along, because a break costs the same whether one
thing changes or six, and spending it twice would have been the waste.

- **Sixteen ordinary words became keywords.** Every construct the language may
  ever want needs a word, and version 1 had none free: all sixteen were legal
  names. Reserving them one at a time would have meant a major version for each
  feature that wanted one. Fifteen of them still mean nothing, which is the
  budget doing its job: a later 2.x can give one a grammar for free.
- **A negative index counts from the end.** `a[-1]` is the last element rather
  than an error. This had to reach `slice` and `insert` as well, or the language
  would have gained a rule that held in one place only.

Four changes followed from those rather than being chosen beside them:

- **`index_of` and `find` give `nil` rather than `-1`.** Not optional. The
  moment `-1` names the last element, `a[index_of(a, missing)]` answers with it:
  a wrong answer where there used to be an error. Shipping the two apart would
  have shipped a trap.
- **`pow` refuses where the result is not a real number**, in `sqrt`'s words.
- **`int`, `floor`, `ceil` and `round` refuse a float too large for an
  integer**, rather than answering with the nearest one there is.
- **`map`, `filter` and `reduce` check their function argument eagerly**, as
  `sort` always did.

And three additions that needed no break at all, included because a release
this size is the right place for them: `match`, a default on a parameter, and
a `...rest` parameter. Each was an error in version 1, so section 2.1 would have
permitted them in a 1.x; they are here because the parts they lean on are.

**What version 2 did not change** is worth as much as what it did. Nothing in
sections 2.2 to 2.7 moved: the evaluation order, the truth rules, the equality
and order rules, the shape of an error, the termination promise, and the module
rules are the same statements they were in 1.0. A major version is permission,
not an instruction.