Skip to content

KaioH3/Tuk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

100 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tuk

A compiled language that gets out of your way.

Tuk compiles to native binaries via LLVM-20. Fast to write, fast to run, built-in ML primitives, zero runtime dependencies. Written from scratch in Zig 0.14.

main() i32 {
    print("hello, world")
    > 0
}
tuk run hello.t        # compile + run in one step
tuk build hello.t      # outputs ./hello binary

Why Tuk?

Most languages make you choose: expressiveness or performance. Tuk refuses the tradeoff.

  • ~128ms compile time — feels instant, like Go
  • ~14KB binaries — no stdlib bloat, no GC, no VM
  • ML built-in — matrices, backprop, neural nets, Q-learning — no pip install
  • AI agents in 50 lines — HTTP client, JSON parsing, LLM calls, all native
  • Self-hosting — the lexer and parser are already written in Tuk itself

The language at a glance

Clean syntax, no noise

-- Function with implicit return
square(x: i32) i32 = x * x

-- Default arguments
greet(name: str, greeting: str = "Hello") str {
    > "{greeting}, {name}!"
}

-- Pipe operator
result = "  hello world  " |> .trim() |> .to_upper()
-- result: "HELLO WORLD"

Error handling without exceptions

read_file(path: str) str fail {
    f = open(path, "r") or fwd   -- propagate on error
    content = f.read()
    f.close()
    > content
}

main() i32 {
    data = read_file("config.json") or ""   -- fallback value
    print(data)
    > 0
}

Structs, methods, traits

type Point {
    x: f64
    y: f64
}

(p: Point) distance(other: Point) f64 {
    dx = p.x - other.x
    dy = p.y - other.y
    > isqrt((dx*dx + dy*dy).to_int()).to_float()
}

main() i32 {
    a = Point{3.0, 0.0}
    b = Point{0.0, 4.0}
    print(a.distance(b))   -- 5
    > 0
}

Generics — vec and map with types

sum_vec(v: vec[f64]) f64 {
    total = 0.0
    for i in 0..v.len() {
        total += v.get(i)
    }
    > total
}

main() i32 {
    scores = vec[f64]()
    scores.push(9.2)
    scores.push(8.7)
    scores.push(9.5)
    print(sum_vec(scores))   -- 27.4
    > 0
}

Concurrency — real pthreads

main() i32 {
    ch = chan(4)

    h = go {
        for i in 0..4 {
            ch.send(i * i)
        }
    }

    wait h

    for i in 0..4 {
        v = ch.recv()
        print("got {v}")
    }
    > 0
}

ML — batteries included

Neural network training with backpropagation in pure Tuk. No Python. No NumPy. Just a binary.

-- XOR neural network: 2-layer, trained with backprop
-- Runs in ~18ms on a laptop

main() i32 {
    X = mat(4, 3)     -- 4 samples, 2 inputs + bias
    -- ... initialize XOR dataset ...

    W1 = mat(3, 8)    -- hidden layer weights
    W2 = mat(8, 1)    -- output weights

    for epoch in 0..5000 {
        -- Forward pass
        z1  = mat_mul(X, W1)
        a1  = mat_relu(z1)
        out = mat_sigmoid(mat_mul(a1, W2))

        -- Backward pass
        d_W2 = mat_mul(mat_transpose(a1), mat_hadamard(mat_sub(out, Y), mat_sigmoid_deriv(z2)))
        d_W1 = mat_mul(mat_transpose(X),  mat_hadamard(mat_mul(d_out, mat_transpose(W2)), mat_relu_deriv(z1)))

        -- Update
        W1 = mat_sub(W1, mat_scale(d_W1, 1.0))
        W2 = mat_sub(W2, mat_scale(d_W2, 1.0))
    }

    -- Converges: XOR(0,1)=0.97, XOR(1,1)=0.02
    > 0
}

What's available out of the box:

Category Primitives
Matrix ops mat_mul, mat_add, mat_sub, mat_hadamard, mat_transpose, mat_scale
Activations mat_relu, mat_sigmoid, mat_tanh + derivatives
Loss mat_mean, MSE
RL Q-learning, reward tables
AutoML model selection, hyperparameter search
Data CSV loading, normalization, DataFrame ops

AI agent in 50 lines

A compiled AI agent that calls Groq's LLM API, parses JSON responses, and takes system actions. 14KB binary. Compiles in 44ms.

main() i32 {
    api_key = env("GROQ_API_KEY")
    hostname = exec("hostname")
    disk     = exec("df -h / | tail -1")

    body = build_request("You are a sysadmin AI.", "Host: " + hostname + " Disk: " + disk)
    response = http_post("https://api.groq.com/openai/v1/chat/completions", body, "application/json", "Bearer " + api_key)

    content = json_get_nested(response, "choices.0.message.content")
    print(content)
    > 0
}

Run it: GROQ_API_KEY=gsk_... tuk run examples/tuk_agent.t


Multi-agent orchestration

Router → specialist dispatch with full trace logging — like a mini LangChain in 18KB.

-- Creates an agent pool and routes queries automatically
agents = vec[*Agent]()
agents.push(make_agent(1, "Router",  AGENT_ROUTER,  "gpt-4o-mini"))
agents.push(make_agent(2, "Coder",   AGENT_CODER,   "claude-sonnet"))
agents.push(make_agent(3, "Analyst", AGENT_ANALYST, "claude-sonnet"))

result = orchestrate(agents, "write code to solve fibonacci", traces)
print_traces(traces)   -- shows full call graph with latency + token counts

REST API + CSV processing

main() i32 {
    csv  = read_csv("data/credit_risk_sample.csv")
    app  = http_new()

    app.get("/api/stats", (req: str) str {
        j = json_new()
        j = json_set_int(j, "total_records", csv.rows())
        > http_json(json_build(j))
    })

    app.post("/predict", (req: str) str {
        -- scoring logic here
        > http_json("{\"risk\":\"medium\",\"confidence\":0.72}")
    })

    app.listen(8080)
    > 0
}

Self-hosting

The Tuk lexer and parser are being rewritten in Tuk itself. stdlib/lexer.t and stdlib/parser.t already tokenize and parse real Tuk programs.

use mathlib

main() i32 {
    src = "fn add(x i32, y i32) i32 { x + y }"
    tokens = tokenize(src)
    ast = parse(tokens)
    print("parsed {ast.len()} nodes")
    > 0
}

Goal: tuk0 (Zig) compiles tuk1 (Tuk) → tuk1 compiles tuk2tuk1 == tuk2 → bootstrap complete.


Benchmark — Scientific Method

Task: Sieve of Eratosthenes, N=1,000,000 (expected: 78,498 primes) Protocol: 3 warmup runs + 10 measured runs, wall clock (nanosecond resolution) OS: Linux 6.17.0-19-generic

Compile time (median of 10 runs)

Compiler Flags Median Stdev
Tuk debug LLVM O2 51.0 ms ±8.3 ms
Tuk release O2 + strip 52.0 ms ±7.6 ms
C gcc -O2 45.0 ms ±4.1 ms
Go go build (warm cache) 155.5 ms ±15.8 ms
Go go build (cold cache) 175.0 ms ±731.7 ms
Rust rustc opt-level=2 176.5 ms ±9.6 ms

Tuk compiles as fast as C. Go and Rust are 3–4× slower.

Runtime (O2 binaries, correctness verified)

All implementations produce identical output: 78,498 primes ≤ 1,000,000.

Language Median Stdev
C (gcc -O2) 7.0 ms ±1.0 ms
Rust (opt-level=2) 8.0 ms ±1.3 ms
Go (go build) 9.5 ms ±1.3 ms
Tuk (LLVM O2) 19.5 ms ±1.7 ms
Python 3 (bytearray) 76.5 ms ±2.2 ms

Tuk is 2–3× behind C/Rust/Go. vec[i32] uses a C-backed dynamic array (runtime.c) — disclosed. Codegen optimizations are ongoing.

Binary size (release, stripped)

Size
Tuk --release 14 KB
C gcc -O2 -s 14 KB
Rust -C strip=symbols 374 KB
Go -ldflags -s -w 1,400 KB

Methodology

  • Timing: date +%s%N (nanosecond wall clock), no instrumentation inside the process
  • Metric: median over 10 runs — robust to outliers — plus standard deviation
  • Tuk vec[i32]: backed by runtime.c dynamic array, not a hand-rolled sieve array — this is disclosed above
  • Go binary: includes the GC runtime (~2 MB overhead reflected in binary size)
  • Rust: timed as single-file rustc — no Cargo project overhead
  • Python: interpreted; no compile step; startup time included in runtime
  • Correctness: all programs verified to produce identical output before timing

Reproduce: sh benchmark/run_scientific.sh


Numbers

Metric Value
Tests 272 (244 integration + 28 unit), 0 failures
Compiler LOC ~11,800
Stdlib LOC ~1,960
Compile time (debug) ~51ms (median, sieve benchmark)
Compile time (release) ~52ms (median, sieve benchmark)
Binary size ~14KB
LLVM version 20
Written in Zig 0.14

Install & Build

Requirements: Zig 0.14, LLVM-20, clang

git clone https://github.com/KaioH3/Tuk.git
cd Tuk
zig build

# Compile and run a program
./zig-out/bin/tuk run examples/tuk_agent.t

# Release build
./zig-out/bin/tuk build --release myapp.t

CLI

tuk build   <file.t>            Compile to native binary
tuk run     <file.t>            Compile + run (no artifact)
tuk fix     <file.t>            Auto-fix common errors
tuk doc     <file.t>            Generate documentation
tuk annotate <file.t>           Show inferred types + warnings

Run tests

zig build test                  # unit tests
sh tests/run_integration.sh    # integration tests (272 total)

Standard library

Module Contents
mathlib arithmetic, primes, combinatorics, geometry, neural activations
stdlib/lexer.t Tuk tokenizer written in Tuk
stdlib/parser.t Tuk recursive-descent parser written in Tuk
stdlib/alloc.t Arena allocator in Tuk
stdlib/gboost.t Gradient boosting primitives
stdlib/automl.t Automated model selection
stdlib/native_vec.t Vec implementation in Tuk

Roadmap

[ done ] Core language — structs, enums, match, traits, defer, pipe
[ done ] Error handling — fail/or fwd/or panic/or value
[ done ] Collections — vec[T], fvec, map[str]
[ done ] Concurrency — go func(), channels, pthreads
[ done ] ML — backprop, Q-learning, AutoML, regression, classification
[ done ] HTTP — client + server, JSON, CSV
[ done ] Self-hosted lexer + parser (stdlib/lexer.t, stdlib/parser.t)
[  ~80%] Full self-hosting (sema + codegen in Tuk)

[ next ] Pointer-to-struct (*T) — unlocks trees, graphs, linked lists
[ next ] Full generics (map[K,V]) — unlocks sema in Tuk
[ next ] Closures / lambdas — |x| x + 1
[ next ] Optional (?T) — null safety
[ next ] WASM target — online playground
[ next ] Package manager — tuk get github.com/user/pkg
[ next ] LSP / VS Code extension
[ next ] CNN, LSTM, Transformer blocks
[ future] GPU / CUDA support
[ future] GUI framework (Flutter-style widget tree)
[ future] Self-hosting bootstrap complete

Examples

File What it shows
examples/tuk_agent.t AI agent calling LLM API, 14KB binary
examples/agent_orchestrator.t Multi-agent routing + tracing, 18KB
examples/credit_risk_api.t REST API + real CSV data processing
examples/sentiment_api.t ML inference over HTTP
tests/integration/174_neural_backprop.t XOR net with backprop
tests/integration/178_qlearning.t Q-learning RL agent
tests/integration/179_automl.t Automated model selection
tests/integration/175_linear_regression.t Linear regression from scratch

License

EUPL v1.2 — same license as GoatCounter. Copyleft, source-available, compatible with AGPL v3.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors