Code that reads like what it means.
Zeno is a programming language for a quick script and for a microcontroller. Keywords are whole words, punctuation stays only where it helps, and every program is checked before it runs.
if (!not ready &&and (retries > 3 ||or timed_out)) {
throw new raise Timeout("gave up");
} else if (count != 0) {
print("retrying");
}
Two rules shape every line.
Most languages inherit their punctuation. Zeno asks each mark what it tells the reader, keeps the ones everyone already reads, and turns the rest into words.
Kept, because it helps
- { }
- You always see where a block ends, and a stray space cannot change what the code does.
- ( ) [ ] ,
- Calls, lists and items. Everyone already reads these.
- : ->
- A name is set apart from its type at a glance.
- #
- A comment. One character, hard to miss.
- & | ^ << >>
- Bit operators, written the way every datasheet writes a register.
Dropped, because it repeats
- ;
- The line break already ends the statement.
- && || !
and,orandnotsay it in words.- ( ) around a condition
if x > 0 {needs no help.- fn => ?
- Every keyword is a word you can say aloud:
function,match,case. - truthiness
- A condition is
trueorfalse. An empty list is not quietly false.
A tour, with the compiler's own answers.
Every sample was run with zeno run. What is printed under it is exactly what Zeno said, error messages included.
Say once if it changes
let is set once, var can change, and a plain = updates. A type goes after the name, and only where you want one.
let name = "Zeno" # let: set once
var count = 3 # var: can change
count = count + 1 # plain = updates it
const LIMIT = 10 # const: fixed when the program is built
let ports: list[int] = [80, 443, 8080] # a type, only where you want one
print(name, count, LIMIT, ports)Zeno 4 10 [80, 443, 8080]Functions are values
Types are written where you want them. Arguments can be named, parameters can have defaults, and a function with no name is written in place.
function area(width: float, height: float) -> float {
return width * height
}
function loose_area(width, height) { # the same, with no types written
return width * height
}
function greet(who: str, excited: bool = false) -> str {
if excited {
return f"Hello, {who}!"
}
return f"Hello, {who}."
}
let triple = function(n) { return n * 3 } # functions are values
print(area(2.0, 3.5), loose_area(2, 3))
print(greet("Achilles"), greet("tortoise", excited=true))
print([1, 2, 3].map(triple), [n * n for n in range(6) if n % 2 == 0])7.0 6
Hello, Achilles. Hello, tortoise!
[3, 6, 9] [0, 4, 16]Every case, or nothing runs
Each case of an enum can carry values, and match takes them apart. This program forgets one case, so Zeno names it and stops.
enum Shape {
Circle(radius: float)
Rect(width: float, height: float)
Dot
}
function area(shape: Shape) -> float {
match shape {
case Circle(r) { return 3.14159 * r * r }
case Rect(w, h) { return w * h }
}
}
print(area(Rect(2.0, 3.0)))error: this `match` does not handle Dot. Add a `case` for it, or a final `case _`
--> shapes.zeno:8:5
|
8 | match shape {
| ^
1 error found. Nothing was run.Errors you can see coming
A function says what it raises, and the checker makes sure someone handles it. Comparisons chain, as they do in maths.
function parse_port(text: str) -> int raises ValueError {
let port = int(text) # int() may raise; it passes up
if not 1 <= port <= 65535 { # comparisons chain, as in maths
raise ValueError("port out of range")
}
return port
}
try {
print("listening on", parse_port("8080"))
print("listening on", parse_port("99999"))
} except ValueError as err {
print(f"error: {err}")
}listening on 8080
error: port out of rangeNo surprise none
A value can be missing only if its type says or none, and it has to be checked before use. The first print skips the check.
function find_user(id: int) -> str or none {
if id == 1 {
return "partha"
}
return none
}
let user = find_user(1)
print(user.upper()) # not allowed: it might be none
if user is not none {
print(user.upper()) # allowed inside this check
}error: this is str or none. Check it with `is not none` before using `.upper`
--> users.zeno:9:7
|
9 | print(user.upper()) # not allowed: it might be none
| ^
1 error found. Nothing was run.Units are part of the type
500 ms and 3.3 V are quantities. A function that wants milliseconds will not take a bare number.
from time import sleep
function blink_for(delay: ms) -> str {
sleep(delay)
return f"waited {delay}"
}
print(blink_for(20 ms))
print(3.3 V > 2.9 V)
print(blink_for(20))error: `delay` of `blink_for` should be a quantity in ms, but this is int
--> delay.zeno:10:17
|
10 | print(blink_for(20))
| ^
1 error found. Nothing was run.A u8 is one byte
Sized numbers and the bit operators, for registers and packets. u8(n) checks that n fits, and u8.wrap(n) cuts on purpose.
class Header {
sensor: u8 # one byte in the compiled struct
value: u16 # two bytes
}
let total = 200 + 100 # arithmetic gives a full int: nothing wraps by accident
print(total, u8.wrap(total)) # wrapping is something you ask for
let flags = 0b1010 << 4 | 0x03 # the operators every datasheet uses
print(f"{flags:02x}", Header(sensor=3, value=u16(64000)))
print(u8(total)) # 300 does not fit in a byte300 44
a3 Header(sensor=3, value=64000)
error: uncaught OverflowError: 300 does not fit in a u8 (0 to 255)
--> packet.zeno:10:7
|
10 | print(u8(total)) # 300 does not fit in a byte
| ^Written once, for any type
T stands for a type, and a call never has to say which. Each use is compiled as if written by hand for that type.
class Stack[T] {
items: list[T]
function push(self, item: T) {
self.items.append(item)
}
function top(self) -> T or none {
if len(self.items) == 0 {
return none
}
return self.items[len(self.items) - 1]
}
}
function largest[T](items: list[T]) -> T {
var best = items[0]
for item in items {
if item > best {
best = item
}
}
return best
}
let bytes: Stack[u8] = Stack([])
bytes.push(u8(72))
print(bytes.top(), largest([3.1 V, 3.3 V, 2.9 V]), largest(["boot", "ready"]))72 3.3 V readyOne owner, freed on time
An owned value is freed where its owner's block ends. A parameter only looks, unless it says take. Use a value after giving it away, and the checker points at the line.
owned class Frame {
channel: int
samples: owned list[int]
}
function capture(channel: int) -> Frame {
let samples: owned list[int] = [n * channel for n in range(4)]
return Frame(channel, samples) # samples moves into the frame
}
function average(samples: owned list[int]) -> int { # only looks
return sum(samples) // len(samples)
}
function send(frame: take Frame) -> int { # takes it over, and frees it
return len(frame.samples)
}
let frame = capture(7)
print(average(frame.samples))
print(send(frame))
print(frame.channel)error: `frame` was given away on line 21 and holds nothing now. Use it before it goes, or give it only to what looks: a parameter without `take`
--> frame.zeno:22:7
|
22 | print(frame.channel)
| ^
1 error found. Nothing was run.Mistakes stop at the checker.
Nothing runs until the whole program makes sense. Each message points at the line and says what to write instead.
A misspelt name
Plain
=only updates, so a typo cannot quietly make a new variable.cuont = count + 1count = count + 1A forgotten case
A
matchmust handle every case of the enum.no case for Dotcase Dot { return 0.0 }A value that may be missing
Anything that can be
noneis checked before it is used.user.upper()if user is not none { }A number with no unit
Milliseconds stay milliseconds, all the way down.
blink_for(20)blink_for(20 ms)A condition that is not a yes or no
There is no truthiness to remember.
if items {if len(items) > 0 {A value used after it was given away
Ownership is checked, with no garbage collector needed.
print(frame.channel)send(frame) last
One job, four ways.
A sensor gives five readings. Throw away the ones out of range and average the rest. Zeno does not make you pick a style.
let readings = [21.5, 22.0, -99.0, 21.0, 150.0]
class Sensor {
low: float
high: float
function average(self, readings: list[float]) -> float {
var total: float = 0.0
var count: int = 0
for reading in readings {
if self.low <= reading <= self.high {
total += reading
count += 1
}
}
return total / count
}
}
print(Sensor(-40.0, 85.0).average(readings))function average(readings, low, high) {
var total = 0.0
var count = 0
for reading in readings {
if low <= reading <= high {
total += reading
count += 1
}
}
return total / count
}
print(average(readings, -40.0, 85.0))function in_range(low: float, high: float) {
return function(reading: float) -> bool {
return low <= reading <= high
}
}
function add(total: float, reading: float) -> float {
return total + reading
}
let valid = readings.filter(in_range(-40.0, 85.0))
print(valid.reduce(add, 0.0) / len(valid))let kept = [r for r in readings if -40.0 <= r <= 85.0]
print(sum(kept) / len(kept))All four print 21.5
The same language, on a microcontroller.
Firmware is where Zeno started. This traffic light is a state machine on three pins. On a computer the board is simulated, and each line of the log is what a logic analyser on that pin would show.
function hold(light: Light) -> ms {
match light {
case Red { return 40 ms }
case Green { return 30 ms }
case Amber { return 10 ms }
}
}
var light = Red
for step in range(7) {
show(light)
led.toggle() # a heartbeat: the program is alive
sleep(hold(light))
light = after(light)
}
pin 2 on
led on
The excerpt leaves out show and after. The whole program is examples/signal.zeno.
Errors as values
--firmwareturns every error into a return value the caller tests. Nosetjmp.No garbage collector
--no-collectorleaves it out. Every value has one owner and is freed on a line you can point to.One fixed pool
No
malloc. Lists and objects live in one array whose size you choose.Four C functions to a board
A compiled program meets real hardware through four functions you write once.
memory: 1691 blocks made (2646563 bytes), at most 13336 bytes in use at once, no collector, 0 blocks left at the end
pool: 25384 bytes taken, in a pool of 32768
text: at most 53 bytes in use at once, in a room of 1024
That program makes 2.6 MB of values over its run and never holds more than 13 KB at once.
One front end, four engines.
The compiler is plain Rust with no dependencies, so every stage is there to read. The tests run each program on all four engines, and they must print the same.
.zeno file.none, errors and owners.zeno runzeno run --vmzeno buildzeno build --llvmMemory, three ways
A small collector by default, owned values freed where their owner ends, or no collector at all on a fixed pool.
Every stage is written up
Each part of the compiler has a walkthrough that ends with things to try.
Every decision has a reason
One document records each choice, why it was made, and what was turned down.
One language, every job.
The goal is that anything you would reach for C, C++, Python, JavaScript, Java or Rust to do, you can do in Zeno. This is where that road stands.
- ScriptsAn interpreter and a bytecode VM.Works today
- FirmwareC with no heap, no collector, memory by hand.Works today
- Library and modulesFiles, input, maths, dictionaries, programs in many files.Works today
- Version 0.1Packages, tests, a formatter and a language server.Works today
- Hardware, web, serversReading real pins, WebAssembly,
asyncand C interop.Next
Run it yourself.
You need Rust to build Zeno, and a C compiler for zeno build. There is nothing else to install.
git clone https://github.com/partham20/zeno-lang
cd zeno-lang
cargo build --release
./target/release/zeno run examples/tour.zeno