Skip to main content
SG

Safe by design. Fast by default.

A small systems language: as fast as C, memory safe like Rust, and written the way C# reads.

Before preview Built and tested on Linux, macOS and Windows; it still changes. Where it stands

main.sg
struct Item {
string name
int count
}

Result<int, string> total(read List<Item> items) {
var sum = 0
for item in items {
if item.count < 0 {
return Error($"{item.name} has a negative count".toString())
}
sum += item.count
}
return Ok(sum)
}

void main() {
var items = List<Item> {
Item { name = "bolts", count = 40 },
Item { name = "nuts", count = 2 },
}
match total(read items) {
Ok(sum) => print($"{sum} items in stock")
Error(why) => print(why)
}
}
sg main.sg
42 items in stock

Why SG?

Memory safe

Ownership and borrows are checked when the program compiles, with no garbage collector. What depends on data, an index or an overflow, stops the program with a message that says what to write instead. unsafe marks the rest.

As fast as C

SG compiles through C, with no runtime to carry. A check that safety does not need is left out by default; the checked way is one word away. Every claim is measured against C, Rust and Zig.

Small and plain

Types come first, blocks have braces and lines need no semicolons. read and ref say what a function does with what it is given. A feature has to pay for itself, at run time and in what it costs to learn.

Caught before the program runs

A use after free, a borrow that outlives its value and an error left unread are compile errors in SG, each with the line that caused it and, when it is known, the fix.

A value used after it was given away

A value has one owner. Here the list is given to kept, and names is used after it: the message shows both lines.

main.sg
void main() {
var names = List<string> { "Ada", "Grace" }
var kept = names
print(names.length())
print(kept.length())
}
sg main.sg
error: `names` was moved and can no longer be used
--> main.sg:4:11
4 | print(names.length())
| ^^^^^
it was moved here:
--> main.sg:3:16
3 | var kept = names
| ^^^^^

A borrow that outlives its value

A function lends back best, which ends when the function returns. In C this is a dangling pointer; here it does not compile, and the hint says what to write.

main.sg
read string longest(read List<string> names) {
var best = names[0].clone()
for name in read names {
if name.length() > best.length() {
best = name.clone()
}
}
return read best
}

void main() {
var names = List<string> { "Ada", "Grace" }
print(longest(read names))
}
sg main.sg
error: this returns a borrow of `best`, and `best` is gone when the function returns
--> main.sg:8:12
8 | return read best
| ^^^^^^^^^
hint: return the value itself instead of a borrow of it

The book goes through the rules one at a time.

Build it in SG

Command-line tools

Arguments and options, files and folders, other programs, regular expressions checked when the program compiles, JSON and a log: in the library, measured against Rust and Zig.

The command line →

Network services

Tasks and async/await on one pool of threads, TCP and UDP, cancellation, channels and locks. A task is always waited for, and the thread rule is checked when the program compiles.

Tasks and async →

Next to C

Call C and be called by it, lay out its types as C does, and ship a library of SG with a header. SQLite, libcurl and zlib are wrapped in modules whose users write no unsafe.

Calling C →

Without an operating system

A program built --freestanding runs on the machine itself: a small kernel for RISC-V boots in QEMU with its devices driven in SG, and atomics follow the orders of C11.

Building programs →

Measured, not promised

61programs written in SG and in C by hand
50of them in the time of C within 5%, or faster
100 MBof JSON read, walked and written faster than serde_json and Zig

Each program is written twice, in SG and in C by hand, and both go through the same C compiler with -O2. Real programs and every module of the library are measured the same way against Rust and Zig, and a case SG loses is published too.

Small programs on two machines, macOS and Windows on Arm, not proof. With a plain hash the hash table is 0.99.

All the numbers
ProgramSG / C
Recursive fib(40)0.98
A linked list of a million nodes, built, walked and freed1.01
A list of a million numbers, filled, changed, added up1.02
Three hundred million lookups that give back a borrow or nothing1.01
Counting in a hash table, with a keyed hash against crafted keys1.20
Printing two million structs0.47

Try it

You need Rust to build sg, and a C compiler for compiled programs. The interpreter needs nothing more.

git clone https://github.com/selcukgural/sg-lang
cd sg-lang/interpreter
cargo build --release
target/release/sg examples/stack.sg
Pasha, a white Van cat with one blue and one amber eye, curled up in a circle

Pasha, the cat of SG: a Van cat, one eye blue and one amber.