// examples

What tape code looks like

These are real design-target examples. The syntax is settled — the compiler is catching up.

hello world

hello.tape
import io from "io"; fn main() -> i32 { io.println("Hello, world!"); return 0; }

$ tape run hello.tape  ·  $ tape build hello.tape -o hello

error handling

math.tape
import io from "io"; enum MathError : u8 { DivByZero = 1: "division by zero"; } fn divide(a: i64, b: i64) -> i64 or MathError { if (b == 0) { return MathError.DivByZero; } return a / b; } fn main() -> i32 { let result: i64 = divide(10, 0) or { io.println("Error: {err}"); return 1; }; io.print_i64(result); return 0; }

compile-time code generation

collections.tape
import mem from "mem"; @tape fn gen_vec(T: type) { @emit struct Vec_$T { data: *T; len: i64; cap: i64; } @emit fn vec_$T_new() -> Vec_$T { return Vec_$T { data: null; len: 0; cap: 0; }; } @emit fn vec_$T_push(v: *Vec_$T, item: T) { if (v.len == v.cap) { var new_cap: i64 = 8; if (v.cap > 0) { new_cap = v.cap * 2; } v.cap = new_cap; v.data = mem.realloc(v.data, v.cap * @sizeof(T)); } v.data[v.len] = item; v.len = v.len + 1; } } @tape { gen_vec(i64); } @tape { gen_vec(string); }

components with events

todo.tape
// #code import ui from "ui"; import widgets from "widgets"; var items: [64]string; var item_count: i64 = 0; fn add_item(text: string) { items[item_count] = text; item_count = item_count + 1; } // #view component TodoItem { prop index: i64; prop text: string; event on_delete(index: i64); fn handle_click() { fire on_delete(index); } view { ui.Row { widgets.Label { text: text; } widgets.Button { label: "x"; on_click: handle_click; } } } }

C interop

sdl.tape
@profile(t1); @link("SDL2"); struct Window { handle: i64; } extern fn SDL_Init(flags: u32) -> i32; extern fn SDL_CreateWindow( title: *const u8, x: i32, y: i32, w: i32, h: i32, flags: u32 ) -> i64; pub fn create(title: string, w: i32, h: i32) -> Window { SDL_Init(0x20); let h: i64 = SDL_CreateWindow(title.data, 0, 0, w, h, 0); return Window { handle: h; }; } @export pub fn plugin_init() -> i32 { // callable from C return 0; }