t27.aiEnglish

Семь бэкендов и t27b

Вы узнаете

Одна спека, много целевых языков и нативный компилятор без LLVM.

Одна спека t27 может стать Zig, C, Rust, JavaScript, TypeScript или Verilog; Verilog получается двумя путями, прямо из спеки и через HIR, — вот и семь бэкендов. Плеер показывает Rust, который t27c пишет для hello_world. t27b — второй компилятор: нативный машинный код AArch64, без LLVM, JIT для тестов и объектные файлы Mach-O. Он не закончен, и мы говорим, насколько: в запуске 26c447ad8 он прошёл 307 из 449 спек, которые эталонный компилятор проходит с тестами, 68.4%.

Попробуйте

Переключите плеер на JavaScript в «Ещё попробуйте», сгенерируйте модуль и запустите его.

Открыть интерактивный урок →

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

Открыть спеку урока в плеере ↗

Все уроки