Hello, t27
You will learn
What a t27 spec looks like, and what the compiler does with it in your browser.
A t27 program is a spec: a module with typed constants, functions and tests. Prose lines start with a semicolon at the start of a line. The player below runs the real t27 compiler, built to WebAssembly, inside your browser: it reads hello_world.t27, runs its tests and writes code for seven backends. Nothing is installed and nothing is sent anywhere. Open the tutorial spec under the widget and read it next to the output.
Try it
Run the tests in the player, then switch the backend and find the same function in two languages.

The t27 compiler, as WebAssembly, running on one spec in your browser: lexer to seven backends, the spec's own tests, the emitted code, its Verilog read for synthesis, and a terminal.
specs/tutorial/02_functions.t27
// SPDX-License-Identifier: Apache-2.0
; 02 — Functions
; Lesson 2. Parameter types and the return type are always written out. A
; signature is part of the specification, so it is never inferred.
; phi^2 + 1/phi^2 = 3 | TRINITY
module tutorial-02-functions;
pub const TRIT_NEG : i8 = -1;
pub const TRIT_POS : i8 = 1;
// ============================================================================
// A function
//
// pub fn NAME(param: TYPE, ...) RETURN_TYPE { ... }
//
// The return type sits after the parameter list with no arrow before it.
// ============================================================================
pub fn double(a: i8) i8 {
return a + a;
}
pub fn add(a: i8, b: i8) i8 {
return a + b;
}
// ============================================================================
// Locals
//
// `const` inside a body is a local binding. Prefer it: an immutable local
// cannot be accidentally rebound, and lesson 05 shows the one sharp edge that
// mutable locals still have.
// ============================================================================
pub fn scaled_sum(a: i8, b: i8) i8 {
const sum = a + b;
const scaled = sum + sum;
return scaled;
}
// ============================================================================
// Calling
//
// Functions call each other by name. There is no forward-declaration step --
// order in the file does not matter.
// ============================================================================
pub fn quadruple(a: i8) i8 {
return double(double(a));
}
// ============================================================================
// Unary operators
// ============================================================================
pub fn negate(a: i8) i8 {
return -a;
}
pub fn invert(flag: bool) bool {
return !flag;
}
// ============================================================================
// Tests
//
// A test states a concrete example. It is emitted into every target that can
// express one, so the same claim gets checked in Zig, C and Rust alike.
// ============================================================================
test "double and add agree on the same value" {
try std.testing.expectEqual(@as(i8, 4), double(2));
try std.testing.expectEqual(@as(i8, 4), add(2, 2));
}
test "quadruple composes double with itself" {
try std.testing.expectEqual(@as(i8, 8), quadruple(2));
}
test "negate flips a trit" {
try std.testing.expectEqual(@as(i8, TRIT_NEG), negate(TRIT_POS));
}
All lessons
Module 1 · The chip
What an FPGA is, which chip we use, and how its pins meet the board.
Module 2 · Numbers in hardware
Bits, trits and number formats, and what arithmetic costs in logic.
Module 3 · Your t27 program
Write a spec, test it, and see why compiling is not the same as being right.
Module 4 · Inside t27c
How the compiler reads a spec and what it writes, including the native t27b.
Module 5 · From spec to hardware
The Verilog t27c writes, the cells it becomes, and what one LUT does.
Module 6 · Reading synthesis
What yosys reports about your design, and which warnings matter.
Module 7 · Place, route, timing
Where the cells land on the die, and whether the clock is met.
Module 8 · The bitstream
How a routed design becomes the bits the chip loads, with no Vivado.
Module 9 · On the board
Ask the chip who it is, load the bits, and check them.