BRAIDGROUP
RESEARCH & DEV
4. Documentation

Language Tour

This tour walks through every major Braid language feature. Each example is valid Braid syntax that the braidc parser accepts.

Comments

Braid supports C++-style and C-style comments:

// Line comment (C++ style)

/* Block comment
   spanning multiple lines */

Hash-style line comments (#) are also supported by the lexer.

Variables

Use let to declare variables with type inference:

let x = 10              // int
let y = 3.14            // float
let name = "Alice"      // string
let flag = true         // bool
let maybe = nil         // null value

Functions

fn add(x: int, y: int) -> int {
    return x + y
}

fn greet() {
    print("Hello!")
}

// Anonymous function (closure)
let double = fn(x: int) -> int { return x * 2 }
let result = double(5)    // 10

If / While

fn main() {
    let x = 10
    if x > 5 {
        print("greater")
    } else {
        print("less or equal")
    }

    let i = 0
    while i < 3 {
        print(i)
        i = i + 1
    }
}

Structs

struct Point {
    x: int
    y: int
}

struct User {
    name: string
    age: int
    active: bool
}

fn main() {
    let p = Point { x: 10, y: 20 }
    let user = User { name: "Bob", age: 25, active: true }
    print(p.x)
    print(user.name)
}

Enums

enum Color {
    Red
    Green
    Blue
}

enum Status {
    OK
    NotFound
    Error
}

let c = Color.Red
let s = Status.OK

Match

match code {
    200 => print("OK"),
    404 => print("Not Found"),
    500 => print("Server Error"),
    _ => print("Unknown")
}

match command {
    "start" => print("Starting..."),
    "stop" => print("Stopping..."),
    _ => print("Unknown command")
}

match flag {
    true => print("It's true!"),
    false => print("It's false!")
}

Imports

import std.io
import std.math
import std.time
import ui.widget

Imports use dot-separated module paths. The compiler searches for .br or .bx files matching the path.

Diameter (Dialectical Programming)

diameter temperature: {
    pole hot: {
        return 80.0
    }
    pole cold: {
        return 10.0
    }
}

fn main() {
    temperature.evolve()
    let t = temperature.observe("tension")
    let v = temperature.observe("state")
    print(t)
    print(v)
}

Tensors

let scalar = [[42]]                // 0-d tensor
let vector = [[1, 2, 3]]           // 1-d tensor
let matrix = [[1, 2], [3, 4]]      // 2-d tensor

let val = matrix[0][1]             // 2
let slice = matrix[0:2]            // rows 0-1

// Device transfer
let gpu_tensor = tensor.to("cuda")
let cpu_tensor = tensor.to("cpu")

Model Definitions

model my_llm = Transformer {
    vocab_size: 50257;
    d_model: 512;
    num_layers: 6;
    num_heads: 8;
    d_ff: 2048;
    dropout: 0.1;
    activation: "gelu";
    learning_rate: 0.001;
    batch_size: 8;
    seq_len: 512;
    num_epochs: 5;
    seed: 42;
}

Async / Concurrency

import std.async

fn main() {
    // Async functions and channels
    // (standard library planned)
    print("Concurrency support coming")
}