<div align="center">
<img src="https://raw.githubusercontent.com/stiven-gjekaj/miruscriptx/main/assets/Miru.png" alt="MiruScriptX" width="300">
### A complete, general-purpose scripting language, written from scratch in Rust
_Compiled to bytecode, run on a stack virtual machine_
<p align="center">
<img src="https://img.shields.io/badge/Rust-1.94%2B-CE422B?style=for-the-badge&logo=rust&logoColor=white" alt="Rust"/>
<img src="https://img.shields.io/badge/dependencies-4_(57),_1_dev-007ec6?style=for-the-badge" alt="The language declares 4 direct dependencies, two of them platform-specific, over 57 total crates, 1 of them a dev dependency"/>
<img src="https://img.shields.io/badge/tests-535_passing-427819?style=for-the-badge" alt="535 tests passing"/>
</p>
<p align="center">
<a href="https://github.com/stiven-gjekaj/miruscriptx/actions/workflows/ci.yml"><img src="https://github.com/stiven-gjekaj/miruscriptx/actions/workflows/ci.yml/badge.svg" alt="CI"/></a>
<a href="https://crates.io/crates/miruscriptx"><img src="https://img.shields.io/crates/v/miruscriptx?style=flat-square&color=blue" alt="The latest version on crates.io"/></a>
<a href="https://github.com/stiven-gjekaj/miruscriptx/releases/latest"><img src="https://img.shields.io/github/v/release/stiven-gjekaj/miruscriptx?style=flat-square&color=blue&label=release" alt="The latest GitHub release"/></a>
<img src="https://img.shields.io/badge/license-MIT-green?style=flat-square" alt="MIT License"/>
</p>
<p align="center">
<a href="https://stiven-gjekaj.github.io/MiruScriptX/"><b>Try it in your browser</b></a> |
<a href="#quick-start"><b>Quick Start</b></a> |
<a href="#features"><b>Features</b></a> |
<a href="#examples"><b>Examples</b></a> |
<a href="#documentation"><b>Documentation</b></a>
</p>
</div>
---
## Overview
**MiruScriptX** is a minimalist, dynamically typed scripting language with a
clean, modern syntax, written from scratch in Rust. Write functions, closures,
loops, arrays, and maps in familiar syntax, split a program across files when it
outgrows one, handle the errors you expect and let the rest stop the program,
then run it from a file or an interactive REPL. Programs use the `.miru`
extension.
The name carries a lineage. MiruScriptX revives MiruScript, an earlier language
by the same author written in C; the X marks the successor.
Programs are compiled to bytecode and run on a stack virtual machine. A tree
walker came first and was replaced in v0.5, once a corpus of golden tests had
frozen its behavior exactly; `miru disasm` will show you what any program
compiles to.
```
fn greet(name) {
return "Hello, " + name + "!"
}
let people = ["Aiko", "Ken"]
for name in people {
print(greet(name))
}
```
---
## Features
<table>
<tr>
<td width="50%" valign="top">
### Language
- Integers, floats, booleans, strings, nil
- Arrays and maps, with indexing
- Functions, closures, and recursion
- `if` / `else if` / `else`, `while`, `for ... in`
- `if` also where a value goes: `let s = if n > 3 { "big" } else { "small" }`
- `for key, value in map` to walk a map's entries, and `for i, x in array` for
the position alongside the element
- `let [x, y] = pair` to give an array's elements their own names, in a `let`
or a loop
- `match`, with a guard on an arm and several values per arm
- `break` and `continue`
- Modules: `import` a file and reach its names through a dot
- `try`, which turns a failure into a value you can check
- Reading input with `input`
- Reading and writing files, and reading the command line, with `read_file`,
`write_file`, `file_exists`, and `args`
- Two output streams, `print` and `eprint`, and an exit code chosen with `exit`
- Arithmetic, comparison, and short-circuit logic, and `+=` for changing a
variable in place
- Strings you can index and interpolate: `s[0]`, and `f"{name} scored {score}"`
- A negative index counts from the end: `a[-1]` is the last element
- Parameters with defaults, and `...rest` for any number of arguments
</td>
<td width="50%" valign="top">
### Engine and tooling
- Lexer, Pratt parser, bytecode compiler, stack virtual machine
- A standard library of string, array, math, map, and I/O builtins, including
`copy`, `repeat`, `pad_left`, `pad_right`, `chr`, and `ord`
- Higher-order builtins: `map`, `filter`, and `reduce`
- File runner, a source formatter (`miru fmt`), and a REPL with history
- A disassembler (`miru disasm`) that prints the bytecode for a program
- A migration tool (`miru migrate`) that says what version 2 changed about a
version 1 program, and renames the half of it a tool can decide
- Errors with a line, a column, and an underline under the token at fault
- Errors you can catch as values, carrying the call path they came through
- Minimal dependencies: rustyline at runtime, nix or windows-sys for raw-mode
key reading, criterion for benchmarks
- A terminal to draw on: `clear`, `move_to`, and the cursor, for programs that
animate rather than print
- A WebAssembly build and an in-browser playground, in a separate crate
- Unit, golden, session, and end-to-end tests, benchmarks, and CI
</td>
</tr>
</table>
---
## Quick Start
Install the latest release. The script checks the download against the
published checksum and refuses if it does not match:
```
Prebuilt binaries cover Linux and macOS on x86-64 and arm64, and Windows on
x86-64. Or install from [crates.io](https://crates.io/crates/miruscriptx):
```
cargo install miruscriptx
```
Or build from source (a recent stable Rust toolchain is all you need):
```
cargo build --release
```
Run a program from a file:
```
miru run examples/greet.miru
```
Evaluate a short program directly from the command line with `-e` (or its
long form, `--eval`):
```sh
miru -e 'print(6 * 7)'
```
Or start the REPL and type expressions:
```
miru
```
```
miru> let x = 21
miru> x * 2
42
```
Reformat a program in the canonical style (add `-w` to rewrite it in place):
```
miru fmt examples/greet.miru
```
For a step-by-step guide, start the wiki at
[wiki/01-introduction.md](wiki/01-introduction.md).
---
## Examples
Runnable programs live in [examples/](examples):
| [greet.miru](examples/greet.miru) | Functions, arrays, and a loop |
| [fib.miru](examples/fib.miru) | Recursion |
| [fizzbuzz.miru](examples/fizzbuzz.miru) | Control flow and the modulo operator |
| [contacts.miru](examples/contacts.miru) | Maps, lookups, and iteration |
| [greeter.miru](examples/greeter.miru) | Reading a line with `input`, and handling the end of it |
| [transform.miru](examples/transform.miru) | Higher-order functions: map, filter, reduce |
| [shop.miru](examples/shop.miru) + [prices.miru](examples/prices.miru) | Two files: `import`, and names that belong to a file |
| [recover.miru](examples/recover.miru) | `try`: surviving an error instead of stopping at it |
| [guess.miru](examples/guess.miru) | A guessing game: `random_int`, `seed`, and `input` in a loop |
| [words.miru](examples/words.miru) | Word frequency: `read_file`, `split`, and counting in a map |
| [dice.miru](examples/dice.miru) | Ten thousand rolls, drawn as a histogram |
| [keys.miru](examples/keys.miru) | Arrow keys: `read_key` without waiting for a line |
| [life.miru](examples/life.miru) | Conway's Life, redrawn in place: `clear` and `sleep` |
| [snake.miru](examples/snake.miru) | Snake: `key_ready`, joining arrays, and a paced loop |
| [pong.miru](examples/pong.miru) | Pong: a ball that keeps moving while you hold still |
| [tetris.miru](examples/tetris.miru) | Tetris: turning a piece without trigonometry, and clearing a row |
Run one with `miru run examples/contacts.miru`.
---
## Language at a glance
```
// Maps, loops, and loop control
let book = {"Aiko": "555-0100", "Ken": "555-0142"}
book["Mai"] = "555-0177"
for name in keys(book) {
if name == "Ken" { continue }
print(name + ": " + book[name])
}
```
See the [language reference](docs/language-reference.md) for the whole language
on one page.
---
## Project structure
Source becomes tokens, tokens become an abstract syntax tree, the tree is
compiled to bytecode, and the bytecode runs on a stack virtual machine.
| **Lexer** | token.rs, lexer.rs | 1756 | Source text to tokens, with line, column, and span tracking |
| **Parser** | ast.rs, parser.rs | 2410 | Recursive descent plus a Pratt expression parser |
| **Runtime model** | value.rs, ops.rs, builtins.rs, random.rs | 4986 | Values, operator and indexing rules, the builtin library |
| **Bytecode engine** | chunk.rs, globals.rs, compiler.rs, vm.rs | 4911 | Compiles the AST to bytecode, runs it on a stack VM, loads modules, and catches errors |
| **Diagnostics** | suggest.rs | 220 | Chooses the name an error offers back when a program misspells one |
| **Formatter** | formatter.rs | 930 | Reprints a program in canonical form (`miru fmt`) |
| **Migration** | migrate.rs | 639 | Renames what version 2.0 reserves, and reports what no tool should rewrite (`miru migrate`) |
| **CLI and REPL** | main.rs, repl.rs, keyboard.rs | 1416 | File runner, `fmt`, `migrate` and `disasm` commands, the REPL, and raw-mode key reading |
| **Library** | lib.rs | 1191 | Ties it together (`parse_program`, `run_source`, `disassemble_source`) |
| **Total** | **20 files** | **18714** | Written from scratch in Rust |
The playground is a separate crate: 800 lines of Rust binding the language to
WebAssembly, and 1699 of hand-written HTML, CSS, and JavaScript. It is counted
apart because it is not the language, and neither its code nor its dependencies
are involved in running a `.miru` file.
```
src/ the language (lexer, parser, compiler, VM, CLI, REPL)
playground/ WebAssembly bindings and the in-browser playground
editors/ syntax highlighting for .miru files
examples/ runnable .miru programs
wiki/ step-by-step learning lessons
docs/ language reference, architecture, and roadmap
tests/ golden, language, session, and end-to-end tests
benches/ criterion benchmarks for the bytecode engine
scripts/ build_reference.sh regenerates the single-page reference
```
---
## Documentation
<table>
<tr>
<td align="center" width="25%" valign="top">
<h3>Learn</h3>
<p>A guided tour,<br/>read in order</p>
<a href="wiki/01-introduction.md"><b>Wiki</b></a>
</td>
<td align="center" width="25%" valign="top">
<h3>Look up</h3>
<p>The whole language<br/>on one page</p>
<a href="docs/language-reference.md"><b>Reference</b></a>
</td>
<td align="center" width="25%" valign="top">
<h3>Internals</h3>
<p>How the interpreter<br/>is built</p>
<a href="docs/architecture.md"><b>Architecture</b></a>
</td>
<td align="center" width="25%" valign="top">
<h3>Roadmap</h3>
<p>Status and what<br/>comes next</p>
<a href="docs/milestones.md"><b>Milestones</b></a>
</td>
</tr>
<tr>
<td align="center" width="25%" valign="top">
<h3>Define</h3>
<p>What a program<br/>means, exactly</p>
<a href="docs/specification.md"><b>Specification</b></a>
</td>
<td align="center" width="25%" valign="top">
<h3>Depend</h3>
<p>What will not<br/>change in 2.x</p>
<a href="docs/stability.md"><b>Stability</b></a>
</td>
<td align="center" width="25%" valign="top">
<h3>Upgrade</h3>
<p>What version 2<br/>changed, and why</p>
<a href="docs/migrating-to-2.md"><b>Migrating</b></a>
</td>
<td align="center" width="25%" valign="top"></td>
</tr>
</table>
---
## Testing
```
cargo test
```
Unit tests sit next to each module. Beyond those, `tests/golden.rs` pins a
corpus of programs to the exact result each must produce, values and errors
alike, down to the line and column a caret points at. Those expectations are
literals rather than regenerated, so a test cannot quietly absorb a change in
behavior, which is what made rewriting the engine's hot paths safe.
`tests/integration.rs` runs the compiled binary against the example programs.
The same checks run in CI, along with `cargo fmt --check` and
`cargo clippy -D warnings`.
Benchmark the engine with:
```
cargo bench
```
---
## Contributing
Contributions are welcome. See [CONTRIBUTING.md](CONTRIBUTING.md) to get
started, follow the [Code of Conduct](CODE_OF_CONDUCT.md), and check
[SUPPORT.md](SUPPORT.md) if you need help. The [changelog](CHANGELOG.md) records
what changed between versions.
---
## Support
If you find MiruScriptX useful, you can support its development here.
[](https://ko-fi.com/U1G623RXOE)
---
## License
Released under the MIT License. See [LICENSE](LICENSE) for the full text, and
[TERMS.md](TERMS.md) for the project terms.
<div align="center">
<sub>Built from scratch in Rust. Start writing MiruScriptX with the <a href="wiki/01-introduction.md">wiki</a>.</sub>
</div>