Rex, the mascot

Rex

A small, fast language. It looks like Go, runs like C, and you never write anything low-level.


Rex compiles to C and then to a native program. There is no garbage collector, no pointers and no manual memory: you write plain code and the compiler works out the rest.

enum Shape {
    Circle(r: float)
    Rect(w, h: float)
}

func area(s: Shape): float {
    return match s {
        Circle(r) => 3.14 * r * r
        Rect(w, h) => w * h
    }
}

func parse_age(text: string): (int, error) {
    var n, err = text.to_int()
    if err != nil { return 0, err }
    if n < 0 { return 0, error("age cannot be negative") }
    return n, nil
}

func main() {
    var shapes = [Shape.Circle(1.0), Shape.Rect(2.0, 3.0)]
    for s in shapes { print(area(s)) }

    var counts: map[string, int] = {}
    for word in "the cat and the hat".split(" ") { counts[word] += 1 }
    print(counts)

    var age, err = parse_age("-4")
    if err != nil { print("error:", err) }
}
3.14
6.0
{the: 2, cat: 1, and: 1, hat: 1}
error: age cannot be negative

Why Rex

  • Simple syntax. var, func, for, match. One loop keyword, no semicolons, no parentheses around conditions. Types are inferred.
  • Fast. Your code becomes C and is built by cc, so it runs at native speed.
  • Safe memory without the work. Values are copied, not shared by accident. Mutation is worked out for you. shared and weak exist for the few times you need a graph or a cache.
  • Errors you can see. A function that can fail returns (T, error). A value that may be missing is T?. Both must be handled.
  • Modern basics. Enums that carry data, exhaustive match, generics, interfaces, closures, maps, characters, modules.
  • C and C++ libraries. import "raylib.h" and call it: Rex reads the header itself, links the library, and builds your own .c/.cpp files next to the program.
  • Packages from git. rex add git.joji.li/joji/json, then import json. Exact versions are kept in rex.lock; rex update moves to newer ones when you ask.
  • Tooling included. A formatter, a language server, a package manager and editor files, all in the one rex program.

Get started

You need a Rust toolchain to build the compiler and a C compiler (gcc or clang, available as cc) to build your programs. Rex itself has no dependencies.

cargo build --release
cp target/release/rex ~/.local/bin/      # or anywhere on your PATH

Save this as hello.rex:

func main() {
    print("Hello, Rex!")
}
rex run hello.rex
Command What it does
rex run file.rex [args...] Compile and run. Extra words reach the program as os.args.
rex build file.rex -o out Make an executable you can keep.
rex check file.rex Look for errors without building.
rex emit-c file.rex Print the generated C.
rex fmt [--check] path... Format code (--check only reports).
rex lsp Start the language server (editors run this for you).
rex header file.h [word] Show what Rex can use from a C header.

Learn the language

The docs/ folder is a short course, written to be read in order: first steps, values, control flow, functions, lists and maps, structs and enums, errors, memory, generics and interfaces, modules, C and C++ libraries, and a full cheatsheet. Every example in it is run by the test suite, so what you read is what happens.

Chapter 12 builds a calculator over five files. It lives in examples/calc, next to many smaller programs in examples/basics. examples/c has a small raylib game.

For the exact rules of the language, see SPEC.md.

Editors

Syntax colours for VS Code and Vim/Neovim live in editors/. The language server gives you error underlines as you type, format on save, an outline, go to definition, hover, completion and parameter hints in any editor that speaks LSP.

Project layout

Path What is there
src/ The compiler: lexer, parser, checker, C generator, formatter, language server
tests/ The test suite (programs are compiled and run with leak checking)
docs/ The course
examples/ Programs to read and run
editors/ VS Code and Vim files, and setup notes

Run the tests with cargo test.

S
Description
No description provided
Readme
1.3 MiB
0 Stars 1 Watchers 0 Forks
Languages
Rust 96.8%
C 2.9%
Vim Script 0.2%