t27.aiРусский

Seven backends, and t27b

You will learn

One spec, many target languages, and a native compiler with no LLVM.

One t27 spec can become Zig, C, Rust, JavaScript, TypeScript or Verilog; Verilog comes two ways, straight from the spec and through the HIR, which makes seven backends. The player shows the Rust t27c writes for hello_world. t27b is a second compiler: native AArch64 machine code, no LLVM, a JIT for tests and Mach-O object files. It is not finished, and we say how far it is: in run 26c447ad8 it passed 307 of the 449 specs that the reference compiler passes with tests, 68.4%.

Try it

Switch the player to JavaScript under Also try, generate the module, and run it.

Open the interactive lesson →

hello_world.t27 to Rust, in a terminal inside the post
hello_world.t27 to Rust, in a terminal inside the post ↗

A terminal in your browser running the t27 compiler as WebAssembly: t27c gen rust prints the Rust it emits for hello_world.t27, and t27c test runs the spec's tests.

specs/tutorial/08_modules_and_arrays.t27

// SPDX-License-Identifier: Apache-2.0
; 08 — Modules, visibility and arrays
; Lesson 8, the last one. How a spec names itself, what it exposes, how it
; pulls in another spec, and the array/index syntax used by every lookup table
; in the corpus.
; .
; After this, open specs/numeric/gf16.t27 -- it is a real spec built entirely
; out of what these eight lessons cover.
; phi^2 + 1/phi^2 = 3 | TRINITY

module tutorial-08-modules;

// ============================================================================
// use
//
//   use path::to::spec;
//
// Pulls in another spec. Paths are `::`-separated and rooted at the corpus,
// not at the filesystem.
// ============================================================================

use base::types;

// ============================================================================
// pub
//
// `pub` makes a name part of this spec's surface. Without it the name is
// internal: usable here, invisible elsewhere. Default to leaving things
// private -- a spec's exported surface is a promise to whoever imports it.
// ============================================================================

pub const EXPORTED_WIDTH : u8 = 16;

// No `pub`: an implementation detail of this module.
const INTERNAL_SCALE : u8 = 4;

pub fn scaled_width() u8 {
    return EXPORTED_WIDTH / INTERNAL_SCALE;
}

// ============================================================================
// Arrays
//
//   [_]TYPE{ a, b, c }
//
// The `_` means "as many as I wrote". Fixed tables like this are how the
// numeric specs carry their constants.
// ============================================================================

pub const TRIT_VALUES = [_]i8{ -1, 0, 1 };

pub const POWERS_OF_TWO = [_]u16{ 1, 2, 4, 8, 16, 32, 64, 128 };

pub const EXPONENT_BIAS = [_]u8{ 15, 31, 63, 127 };

// ============================================================================
// Indexing
//
// `array[i]`, zero-based.
// ============================================================================

pub fn power_of_two(index: u8) u16 {
    return POWERS_OF_TWO[index];
}

pub fn bias_for_width(index: u8) u8 {
    return EXPONENT_BIAS[index];
}

// ============================================================================
// Tests
// ============================================================================

test "the power table matches its own definition" {
    try std.testing.expectEqual(@as(u16, 1), power_of_two(0));
    try std.testing.expectEqual(@as(u16, 128), power_of_two(7));
}

test "bias table carries the four standard widths" {
    try std.testing.expectEqual(@as(u8, 15), bias_for_width(0));
    try std.testing.expectEqual(@as(u8, 127), bias_for_width(3));
}

test "a private constant is still usable inside its own module" {
    try std.testing.expectEqual(@as(u8, 4), scaled_width());
}

invariant exported_width_is_a_power_of_two
    assert EXPORTED_WIDTH == 16

Open the lesson's spec in the player ↗

All lessons