Skip to content
SpawnCPublic

Latest commit

 

History

56 Commits

Folders and files

Repository files navigation

rMox

rMox is a statically typed systems language for Linux x86-64.

The compiler is written in C11. It turns .rmx source into native ELF binaries through an IR backend, NASM, and GNU ld.

Current release: 1.4

Features

  • Static types (no C-style NULL; future null is zero)
  • Native x86-64 code generation (System V AMD64)
  • Functions, recursion, structs, enums
  • Typed pointers ptr[T] and fixed arrays array T [N]
  • if / elif / else, while, C-style for, for … in
  • switch with multi-case arms
  • defer, generators (yield), async (spawn / await)
  • Modules and imports
  • Inline NASM
  • Built-in stdlib (print, print_int, len, …)
  • Constant folding and peephole opts
  • Shift operators << >>
  • Compile-time len for fixed arrays

In development

Version Planned
1.5 float, print_float
1.6 function pointers, human-readable any / voidptr
1.7 split ir.c into src/ir/

Requirements

  • Linux x86-64
  • C11 compiler (gcc or clang)
  • GNU Make
  • NASM
  • GNU binutils (ld)

Building

git clone https://github.com/SpawnC/rmox.git
make

Or:

./build.sh

This produces the rmoxc binary.

Getting started

hello.rmx:

func main() -> int {
    print("Hello, World!\n");
    return 0;
}

Compile and run:

./rmoxc hello.rmx          # writes a.out (or similar)
./a.out

./rmoxc hello.rmx -run     # compile, link, and execute
./rmoxc hello.rmx -S -o hello.asm   # emit NASM only

Every program needs a main that returns an int exit code.

Language overview

Types and variables

Types are explicit. Variables are declared with var and initialized.

var int age = 18;
var bool enabled = true;
var char letter = 'A';
var string message = "Hello";
Type Meaning
int signed 64-bit integer
bool true / false
char one byte
string pointer to zero-terminated bytes
void return type only
ptr[T] pointer to T
array T [N] fixed array of N elements

Structs and enums are user-defined. C-style NULL / null is rejected with a moral error. A proper null pointer will arrive later as the keyword zero.

Operators

+  -  *  /  %
==  !=  <  >  <=  >=
&&  ||  !
&  |  ^  ~  <<  >>
+=  -=  *=  /=  %=  <<=  >>=

&& and || short-circuit. Division or remainder by zero traps at runtime.

Control flow

if (x > 10) {
    print("greater\n");
} elif (x == 10) {
    print("equal\n");
} else {
    print("less\n");
}

while (i < 5) {
    print_int(i);
    i = i + 1;
}

for (var int i = 0; i < 10; i = i + 1) {
    print_int(i);
}

for-in over a fixed array (elements) or an integer range 0 .. n-1:

var array int [4] a = {10, 20, 30, 40};
for (var int x in a) {
    print_int(x);
}

for (var int i in 3) {
    print_int(i);   // 0 1 2
}
switch (x) {
    case 1:
        print("one\n");
    case 2, 3:
        print("two or three\n");
    default:
        print("other\n");
}

break, continue, and return work as usual.

Functions

func int add(int a, int b) {
    return a + b;
}

func main() -> int {
    print_int(add(2, 3));
    return 0;
}

Return type may be written before the name or with ->.

Arrays and len

var array int [5] a = {10, 20, 30, 40, 50};
print_int(a[0]);
print_int(len(a));   // 5, folded at compile time

Out-of-bounds access traps at runtime.

Pointers

var int x = 42;
var ptr[int] p = &x;
print_int(p^);
p^ = 99;

Structs and enums

struct Point {
    int x;
    int y;
}

enum Color {
    Red,
    Green = 10,
    Blue
}

Structs are passed and returned by value.

defer

{
    defer print("after\n");
    print("before\n");
}

Defers run when the enclosing block exits (including via return), in reverse order.

Generators and async

gen func int seq() {
    yield 1;
    yield 2;
    return 0;
}

async func int work(int n) {
    return n * 2;
}

func main() -> int {
    print_int(seq());
    var int t = spawn work(21);
    print_int(await t);
    return 0;
}

Up to 16 concurrent tasks (fork + pipe).

Modules

import "lib/util.rmx";

export func int helper(int x) {
    return x + 1;
}

Circular imports are not supported.

Inline assembly

asm {
    "nop"
}

NASM syntax. Register clobbers are not fully analyzed.

Standard library

Category Functions
I/O print, print_int, print_char, input, input_int
Numbers abs, min, max, rand, len
Strings strlen, strcmp, strcpy, strcat, strbuf

No import is required for these.

Runtime and diagnostics

Runtime traps:

  • division / remainder by zero
  • array index out of bounds

Compiler diagnostics use codes r1–r9 plus a source caret. Example:

file.rmx:4:5: error (r3): type mismatch in assignment: cannot assign 'string' to 'int'

NULL / null is rejected with a moral error. The planned readable null is the keyword zero.

Compiler architecture

  1. Lexical analysis
  2. Parsing
  3. Import processing
  4. Checking
  5. Semantic analysis
  6. IR
  7. NASM emission
  8. nasm -f elf64
  9. ld

Limitations

  • Linux x86-64 only
  • No garbage collector (planned later)
  • No ownership / borrow checker
  • No circular imports
  • Async is a simple task model, not a full scheduler

Project structure

rmox/
├── src/           # compiler sources
│   └── runtime/   # runtime support
├── examples/      # sample programs
├── Makefile
├── build.sh
├── LanguageGuide.md
└── README.md

Documentation

License

MIT. See LICENSE.

Releases

Packages

Contributors

Languages