t27.aiEnglish

Привет, t27

Вы узнаете

Как выглядит спека t27 и что с ней делает компилятор прямо в вашем браузере.

Программа на t27 — это спека: модуль с типизированными константами, функциями и тестами. Строки прозы начинаются с точки с запятой в начале строки. Плеер ниже запускает настоящий компилятор t27, собранный в WebAssembly, внутри вашего браузера: он читает hello_world.t27, прогоняет его тесты и пишет код для семи бэкендов. Ничего не устанавливается и ничего никуда не отправляется. Откройте учебную спеку под виджетом и читайте её рядом с выводом.

Попробуйте

Запустите тесты в плеере, затем переключите бэкенд и найдите одну и ту же функцию на двух языках.

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

hello_world.t27, compiled inside the post
hello_world.t27, compiled inside the post ↗

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));
}

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

Все уроки