Small and fast
SG compiles through C, with no garbage collector and no runtime to carry. A check that safety does not need is left out by default; the checked way is one word away.
Memory safe
Ownership and borrows are checked when the program compiles. 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.
Reads plainly
Types come first, blocks have braces and lines need no semicolons. read and ref say what a function does with what it is given. An error says what is wrong and what to write.
What it looks like
- A program
- A mistake
A function borrows the list it is given (read), gives a Result, and the caller looks at both ways. Nothing is copied, nothing is freed by hand, and the same program runs in the interpreter and compiled through C.
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)
}
}
42 items in stock
A value has one owner. Here the list is given to kept, and names is used after it: the program does not compile, and the message shows both lines. A use after free, a borrow that outlives its value and an error left unread are found the same way, before the program runs.
The book goes through the rules one at a time.
void main() {
var names = List<string> { "Ada", "Grace" }
var kept = names
print(names.length())
print(kept.length())
}
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
| ^^^^^
Measured against C
Each program is written twice, in SG and in C by hand, and both go through the same C compiler with -O2. The time of SG divided by the time of C; 1.00 is the same speed.
| Program | SG / C |
|---|---|
Recursive fib(40) | 0.98 |
| A linked list of a million nodes, built, walked and freed | 1.01 |
| A list of a million numbers, filled, changed, added up | 1.02 |
| Three hundred million lookups that give back a borrow or nothing | 1.01 |
| Counting in a hash table, with a keyed hash against crafted keys | 1.20 |
| Printing two million structs | 0.57 |
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, and how they were taken
Built with it
- SQLite and libcurl: wrapped in modules whose users write no
unsafe - A small kernel: for RISC-V, booted in QEMU, with its devices driven in SG
- A queue without locks: for many producers and many consumers, with no
unsafe - A library for C: built from SG, called from a program of C through its header
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, the cat of SG: a Van cat, one eye blue and one amber.