Nihil

A programming language with no keywords at all

No if, no while, no return: only symbols. Nihil compiles straight to native machine code, with no libc, no linker and no virtual machine, and its compiler is written in Nihil itself.

Install

Linux on x86_64, and macOS on Apple silicon with the Xcode Command Line Tools (xcode-select --install). On Windows, use WSL. No sudo: it installs into ~/.nihil.

curl -fsSL https://nihil-lang.dev/install.sh | sh

Then:

printf '%s\n' '>> "Hello, Nihil!\n";' > hello.nihil
nihil run hello.nihil        # compile and run: Hello, Nihil!
nihil hello.nihil            # or compile into ./hello, a native executable
nihil ide                    # the IDE, in an app window

Next: Learn Nihil in thirteen short chapters, and the library reference.

Why Nihil

The name is Latin for nothing, and _ is the value that is not there.

Self-hosted

A first compiler in assembly bootstraps a second one written in Nihil, which compiles itself to a fixed point.

Typed, without annotations

Types come from values and defaults, settled across several passes. Missing values are explicit optionals.

Batteries for data

Files of fixed-width records with an on-disk key index, and queries like rows.where(price > 10.0).sort(name).

Tiny native binaries

A small runtime with a garbage collector is embedded in each executable. A "hello world" is 6 KB.

A taste

A BOOL before { is an if, * is a while loop, : returns.

fib(n = 0) {
    n < 2 { : n; }                 // a BOOL before "{" is an if; ":" returns
    : fib(n - 1) + fib(n - 2);
}

find(v = [0], x = 0) {
    i = 0;
    * i < #v {                     // "*" is a while loop
        v[i] == x { : i; }
        i = i + 1;
    }
    : _;                           // "_" is "no value": find returns INT?
}

>> fib(30); >> "\n";               // 832040
pos = find([5, 8, 13], 8) || -1;   // "||" takes the value, or the fallback
3 { >> pos; }                      // an INT before "{" repeats the block: 111
>> "\n";
>> 0.1 + 0.2; >> "\n";             // 0.30000000000000004
words = "  pear, apple, fig ".trim().split(", ");
>> words.sort().join(" < ") + "\n";  // apple < fig < pear

And a small table on disk, read and written by key:

PRODUCT = @[#code: INT(4); name: STRING(12); price: FLOAT(6)];   // a data model; # marks the key
FILE = "products.dat";

@{}>> FILE, PRODUCT(1, "Coffee", 12.5);    // write by key: adds it, or replaces it in place
@{}>> FILE, PRODUCT(2, "Tea", 8.0);
@{}>> FILE, PRODUCT(3, "Cake", 15.0);

p = PRODUCT @{2} FILE;                      // one record by its key (a hash index on disk)
p.price = 7.5;
@{}>> FILE, p;

rows = PRODUCT @ FILE;                      // the whole table: an ARRAY of PRODUCT
>> $rows.where(price < 13.0).sort(name);    // a query; $ turns records into table lines
>> "total: " + $rows.sum(price) + "\n";     // total: 35.0
PRODUCT @{3} FILE = _;                      // delete by key

The table is plain text, one record per line, readable in any editor.

The language at a glance

The names after a . exist only there, so they never take a name from your program.

WriteMeaning
x = 1;a variable; its type comes from the value
cond { … } || { … }if / else (cond is a BOOL)
* cond { … }while
n { … }repeat n times
f(a = 0, s = "") { … }a method; every parameter has a default, which gives its type
: value;return
>> x; / << x;write to standard output / read a line
x? = 5;, x || 0, x == _optionals: a value that may be missing
@ path, @>> path, text;read a file, replace it atomically
m = {"ana": 30}a map: values by key, in memory
@ "std:json.nihil";include a library that ships with the compiler

Where it runs

The compiler writes the executable by itself, one pass at a time, until the types settle.

Linux x86_64

Native ELF executables, no libc and no linker. The reference platform, with the full test suite and a fixed-point check on every change.

macOS on Apple silicon

Native executables through an instruction-by-instruction translator to ARM64. The compiler itself runs natively on the Mac.

A web IDE, in Nihil

An editor with a color terminal, the generated machine code one click away, and a table view for record files of any size.

Status

Nihil is open source under the MIT license. The source code is at github.com/nihillang/nihil: issues and pull requests are welcome. To follow the project or ask a question, write to [email protected].