Skip to main content

SG

Safe by design. Fast by default.

Before its preview: the language and its tools are built and tested, and still change. Where it stands

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 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.

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

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.

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.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, 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.