rue-cli 0.13.0

A CLI tool for invoking the Rue compiler.
<!--
  Keep this README short. The docs (https://rue-lang.com, xch-dev/rue-docs) are the source of truth
  for language features, the standard library, lints, CLI flags, compiler options and the language
  server. Don't document a new feature here: describe it in the PR, which the docs update reads.
-->

# Rue

[![crate](https://img.shields.io/crates/v/rue-cli.svg)](https://crates.io/crates/rue-cli)
[![build](https://github.com/xch-dev/rue/actions/workflows/build.yml/badge.svg)](https://github.com/xch-dev/rue/actions/workflows/build.yml)
[![license](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](LICENSE)

Rue is a statically typed programming language that compiles to [CLVM](https://chialisp.com/clvm) bytecode. It's designed as an alternative to [Chialisp](https://chialisp.com) for writing coin puzzles on the [Chia blockchain](https://chia.net).

[**Docs**](https://rue-lang.com) · [**Playground**](https://play.rue-lang.com) · [**Discord**](https://discord.gg/sagewallet)

```rs
fn main(public_key: PublicKey, conditions: List<Condition>) -> List<Condition> {
    let agg_sig = AggSigMe { public_key, message: tree_hash(conditions) };
    [agg_sig, ...conditions]
}
```

## Highlights

- **Type safe:** Structs, enums, generics, type guards, and flow-sensitive narrowing catch entire classes of mistakes at compile time, before a puzzle ever touches the chain.
- **Optimized:** The compiler works hard to minimize the size and runtime cost of the generated bytecode.
- **Familiar:** A simplified, Rust-like syntax that feels more at home for most developers than Lisp.
- **Batteries included:** A standard library with conditions, tree hashing, currying, singletons, offers, and more.
- **Tooling:** A built-in test runner, formatter, CLVM debugger, and language server.

## Installation

Install the CLI from [crates.io](https://crates.io/crates/rue-cli):

```bash
cargo install rue-cli
```

See the [Installation](https://rue-lang.com/docs/installation) page for more options. If you just want to try the language, the [Playground](https://play.rue-lang.com) runs entirely in your browser.

## Quick Start

Create a new project:

```bash
rue init my-project
cd my-project
```

This creates a `Rue.toml` manifest and a `puzzles/main.rue` entrypoint:

```rs
fn main() -> String {
    "Hello, world!"
}
```

Compile it to CLVM:

```bash
$ rue build
(q . "Hello, world!")
```

## Documentation

The [docs](https://rue-lang.com) are the reference for the language, the standard library and the tooling: syntax, types, modules, control flow, lints, the CLI, the language server, and the compiler options in `Rue.toml`. This README only covers getting started and working on the compiler.

| Command                          | Description                                                       |
| -------------------------------- | ----------------------------------------------------------------- |
| `rue init [path]`                | Create a new project with a `Rue.toml` manifest.                  |
| `rue build [file]`               | Compile the project (or a single file) and print the CLVM output. |
| `rue test [file]`                | Run every `test fn` in the project (or a single file).            |
| `rue fmt [paths...] [--check]`   | Format Rue source files, or check that they're already formatted. |
| `rue debug <program> [solution]` | Run a CLVM program against a solution and print the result.       |
| `rue lints`                      | List every lint and its default level, and the lint groups.       |

Run `rue <command> --help` for its flags, or see the [CLI](https://rue-lang.com/docs/cli) page.

## Examples

The [`examples`](examples) directory contains small programs like [FizzBuzz](examples/fizz_buzz.rue), a [JSON parser](examples/json.rue), and a [Brainf\*ck interpreter](examples/brainfck.rue), as well as real-world [puzzles](examples/puzzles) such as a [CAT](examples/puzzles/cat.rue), [singleton](examples/puzzles/singleton.rue), and [royalty split](examples/puzzles/royalty_split.rue).

> [!WARNING]
> The example puzzles have not been audited and are for educational purposes only.

## Repository Layout

| Path                                       | Description                                                  |
| ------------------------------------------ | ------------------------------------------------------------ |
| [`crates/rue-cli`]crates/rue-cli         | The `rue` command line tool.                                 |
| [`crates/rue-compiler`]crates/rue-compiler | The compiler driver and the [standard library]crates/rue-compiler/src/std. |
| [`crates/rue-lexer`]crates/rue-lexer, [`rue-parser`]crates/rue-parser, [`rue-ast`]crates/rue-ast | Tokenizing, parsing, and the syntax tree. |
| [`crates/rue-hir`]crates/rue-hir, [`rue-types`]crates/rue-types | Semantic analysis and the type system. |
| [`crates/rue-lir`]crates/rue-lir         | Low-level IR, optimization, and CLVM code generation.        |
| [`crates/rue-formatter`]crates/rue-formatter | The source code formatter.                               |
| [`crates/rue-lsp`]crates/rue-lsp         | The language server.                                         |
| [`wasm`]wasm                             | WebAssembly bindings for the compiler.                       |
| [`playground`]playground                 | The web-based [Rue Playground]https://play.rue-lang.com.   |
| [`tests`]tests                           | Snapshot tests for the compiler.                             |

## Contributing

Contributions are welcome! The project builds with a stable Rust toolchain (pinned in [`rust-toolchain.toml`](rust-toolchain.toml)).

```bash
# Run the full test suite, which also checks the snapshots without changing them
cargo test --workspace

# Check the .yaml snapshots, std_tests.yaml and the formatting of the .rue files, printing what
# differs without changing any files (`-- --help` describes what's checked and the filter)
cargo run -p rue-tests --release -- [filter]

# Rewrite the snapshots, and reformat the .rue files, to match the current output
cargo run -p rue-tests --release -- --bless [filter]

# Lint and format
cargo clippy --workspace --all-targets
cargo fmt --all
cargo run -p rue-cli -- fmt 'examples/**/*.rue' 'tests/**/*.rue' 'crates/rue-compiler/src/std/**/*.rue'
```

Each test in `tests` and `examples` (a `.rue` file, or a directory of them) has a matching `.yaml` snapshot with its compiled output, runtime cost, and test results. Review snapshot diffs carefully, since they show exactly how a change affects the generated bytecode.

To run the playground locally:

```bash
cd wasm && pnpm install && pnpm build
cd ../playground && pnpm install && pnpm dev
```

## Community

Join the [Sage Discord](https://discord.gg/sagewallet) to get help with Rue, suggest ideas, or chat about the project.

## License

Rue is licensed under the [Apache License 2.0](LICENSE).