Skip to content

Latest commit

 

History

History
451 lines (326 loc) · 5.95 KB

LanguageGuide.md

File metadata and controls

451 lines (326 loc) · 5.95 KB

rMox Language Guide

Statically typed systems language for Linux x86-64. Source files use .rmx.

Compiler version: 1.4


Types

int       // 64-bit signed integer
bool      // true / false
char      // one byte
string    // pointer to zero-terminated bytes
void      // only as a return type

ptr[T]           // pointer to T
array T [N]      // fixed array of N elements of type T

No NULL. Writing null or NULL is a moral error and is rejected.

Structs and enums are user-defined types (see below).


Variables

var int x = 10;
var bool ok = true;
var string s = "hello";
var char c = 'A';

x = 20;
x += 1;
x -= 1;
x *= 2;
x /= 2;
x %= 3;
x <<= 1;
x >>= 1;

Names live in block scopes { }.


Operators

Arithmetic:

+  -  *  /  %

Comparison:

==  !=  <  <=  >  >=

Logical:

&&  ||  !

Bitwise:

&  |  ^  ~  <<  >>

Other:

-x      // negate
!b      // logical not
~n      // bitwise not
&x      // address of
p^      // dereference
a[i]    // index
p.x     // field access

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


Control flow

if (x > 0) {
    print("pos\n");
} elif (x < 0) {
    print("neg\n");
} else {
    print("zero\n");
}

while (n > 0) {
    n = n - 1;
}

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

for-in

Over a fixed array — iterates elements:

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

Over an integer — counts 0 .. n-1:

for (var int i in 3) {
    print_int(i);
    print(" ");
}
// 0 1 2

switch

switch (x) {
    case 1:
        print("one\n");
    case 2, 3:
        print("two or three\n");
    default:
        print("other\n");
}

break, continue, and return / return expr work as usual.


Functions

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

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

Return type may also use an arrow:

func foo() -> int {
    return 1;
}

Values are passed and returned by value. Recursion is allowed.

Every program needs main. Its return value is the process exit code.


Structs

struct Point {
    int x;
    int y;
}

func main() {
    var Point p = {3, 7};
    print_int(p.x);
    p.y += 1;
    return 0;
}

Structs are passed and returned by value. No methods, no inheritance.


Enums

enum Color {
    Red,
    Green = 10,
    Blue
}

// Red == 0, Green == 10, Blue == 11
print_int(Green);

Arrays

var array int [5] a = {10, 20, 30, 40, 50};
print_int(a[0]);
a[1] = 99;

print_int(len(a));   // 5 — compile-time constant

Length is fixed at compile time. len(a) is only for fixed arrays and folds to a constant.

Out-of-bounds access traps at runtime.


Pointers

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

There is no void*. Pointer validity is the programmer's responsibility.


Strings

var string s = "hello";
print(s);
print_int(strlen(s));

A string is a pointer to bytes, not a managed container.

Built-ins: strlen, strcmp, strcpy, strcat, strbuf.


defer

func main() {
    print("start\n");
    {
        defer print("inner\n");
        print("body\n");
    }
    defer print("outer\n");
    print("before return\n");
    return 0;
}

Output:

start
body
inner
before return
outer

defer runs when the enclosing block exits, including via return. Several defers in one block run in reverse order (LIFO).


Generators

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

func main() {
    print_int(seq());  // 1
    print_int(seq());  // 2
    print_int(seq());  // 3
    return 0;
}

Each call resumes after the previous yield.


async / spawn / await

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

func main() {
    var int t = spawn work(21);
    print_int(await t);   // 42
    return 0;
}

spawn returns a handle (int). await waits and returns the integer result. Up to 16 concurrent tasks.


Standard library

No import required for these:

print(s)
print_int(n)
print_char(c)
input()
input_int()

len(a)          // fixed arrays only

abs(x)
min(a, b)
max(a, b)
rand()

strlen(s)
strcmp(a, b)
strcpy(dst, src)
strcat(dst, src)
strbuf()

Modules

import "lib/util.rmx";

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

Circular imports are not supported.


#override

#override MAX 100
#override DOUBLE(x) ((x) * 2)

Inline assembly

asm {
    "nop"
}

NASM syntax. Register clobbers are not fully analyzed by the compiler.


Errors

The compiler prints a code rN, a message, and a caret under the source.

Examples of codes:

  • r2 — undefined name
  • r3 — type / arity mismatch
  • r5 — moral error (for example NULL)
  • r6 — tier / unsafe rule
  • r7 — unused variable (warning)

Any form of NULL / null is rejected:

It's not 1972 anymore

At runtime the process aborts on division by zero and on out-of-bounds array access.


Full example

func int fib(int n) {
    if (n <= 1) {
        return n;
    }
    return fib(n - 1) + fib(n - 2);
}

func main() {
    var array int [5] xs = {1, 2, 3, 4, 5};

    for (var int v in xs) {
        print_int(v);
        print(" ");
    }
    print("\n");

    print_int(len(xs));
    print("\n");

    print_int(1 << 4);
    print("\n");

    for (var int i = 0; i < 8; i = i + 1) {
        print_int(fib(i));
        print("\n");
    }
    return 0;
}

Reserved words

async await bool break case char continue default
defer elif else enum export false for func gen
if import int return spawn string struct switch
true var void while yield asm sizeof ptr array