TezzNative Examples

Searchable copy/paste cookbook

Example Cookbook

Type in search to filter by feature, syntax, or module.

Search examples

Each example is intentionally short and directly runnable.

Hello World

import "std"

fn main() -> int:
  say "Hello from TezzNative."
  ret 0

Input + Calculator

import "std"

fn main() -> int:
  a:int = input("A = ") as int
  b:int = input("B = ") as int
  say(a + b)
  ret 0

if / else

fn main() -> int:
  n:int = 42
  if n > 50:
    say "high"
  else if n > 0:
    say "normal"
  else:
    say "low"
  ret 0

while loop

fn main() -> int:
  i:int = 0
  while i < 5:
    if i == 2:
      i = i + 1
      continue
    say i
    i = i + 1
  ret 0

for loop

fn main() -> int:
  sum:int = 0
  for i:int = 0; i < 10; i = i + 1:
    sum = sum + i
  say sum
  ret 0

switch / case

fn main() -> int:
  code:int = 404
  switch code:
  case 200:
    say "OK"
  case 404:
    say "Not Found"
  default:
    say "Unknown"
  ret 0

Struct + Function

struct User:
  id:int
  active:int

fn is_active(u:User) -> int:
  ret u.active

fn main() -> int:
  u:User
  u.id = 7
  u.active = 1
  say(is_active(u))
  ret 0

File IO

import "std"

fn main() -> int:
  f:*io.File = io.open_w("note.txt")
  io.file_write_line(f, "TezzNative IO")
  io.file_close(f)
  ret 0

Network Download

import "std"

fn main() -> int:
  r:int = net.download("https://example.com", "example.html")
  if r != 0:
    say "download failed"
    ret 1
  say "download ok"
  ret 0

Route Table + WebSocket Service

import "std"
import "net"

fn route_root(cli:int, req:str) -> int:
  ret net.http_respond_text(cli, 200, "TezzNative Router online")

fn route_health(cli:int, req:str) -> int:
  ret net.http_respond_json(cli, 200, "{\"ok\":true,\"service\":\"tezz\"}")

fn route_api(cli:int, req:str) -> int:
  ret net.http_respond_text(cli, 200, "api namespace")

fn build_routes() -> *HttpRouteTable:
  rt:*HttpRouteTable = net.http_route_table_new(8)
  unsafe:
    net.http_route_table_add(rt, "GET", "/", route_root as *char)
    net.http_route_table_add(rt, "GET", "/health", route_health as *char)
    net.http_route_table_add(rt, "GET", "/api/*", route_api as *char)
  ret rt

fn handle_http(rt:*HttpRouteTable, cli:int, req:str) -> int:
  if net.http_route_table_serve_or_404(rt, cli, req) < 0:
    ret 1
  ret 0

fn main() -> int:
  net.init()
  srv:int = net.http_server_listen("0.0.0.0", 8080, 64)
  rt:*HttpRouteTable = build_routes()
  if srv < 0:
    say "listen failed"
    ret 1

  say "HTTP:  http://127.0.0.1:8080"
  say "WS:    ws://127.0.0.1:8080/ws"

  while 1:
    cli:int = net.http_server_accept(srv, 1000)
    if cli < 0:
      continue
    req:str = net.http_read_request(cli, 65536)
    if req != 0:
      if net.ws_is_upgrade_request(req) != 0 && net.http_route_request_match(req, "GET", "/ws") != 0:
        net.ws_upgrade_and_session_echo(cli, req, 65536, 0)
      else:
        handle_http(rt, cli, req)
      unsafe:
        free(req)
    net.close(cli)

  net.http_route_table_free(rt)
  net.cleanup()
  ret 0

TezzApi CRUD + TNXB Packet

import "std"
import "tezzapi"

fn h_create(cli:int, req:str) -> int:
  payload:TnxbBuffer = tezzapi.tnxb_encode(tezzapi.tnxb_op_create(), "/users", "{\"created\":true}")
  packet:TnxbPacket = tezzapi.tnxb_decode_buf(payload)
  rc:int = net.http_respond_json(cli, 201, packet.body)
  tezzapi.tnxb_packet_release(&packet)
  tezzapi.tnxb_release(payload)
  ret rc

fn h_read(cli:int, req:str) -> int:
  ret net.http_respond_json(cli, 200, "{\"users\":[{\"id\":1}]}")

fn h_update(cli:int, req:str) -> int:
  ret net.http_respond_json(cli, 200, "{\"updated\":true}")

fn h_delete(cli:int, req:str) -> int:
  ret net.http_respond_json(cli, 200, "{\"deleted\":true}")

fn main() -> int:
  api:*TezzApi = tezzapi.api_new(16)
  unsafe:
    tezzapi.api_crud(api, "/users", h_create as *char, h_read as *char, h_update as *char, h_delete as *char)

  srv:int = net.http_server_listen("0.0.0.0", 9090, 64)
  while 1:
    cli:int = net.http_server_accept(srv, 1000)
    if cli < 0:
      continue
    req:str = net.http_read_request(cli, 65536)
    if req != 0:
      tezzapi.api_serve_or_404(api, cli, req)
      unsafe:
        free(req)
    net.close(cli)

  tezzapi.api_free(api)
  ret 0

Routing + Session Helpers

import "std"
import "net"

fn main() -> int:
  req:str = "GET /api/v1/user?id=42 HTTP/1.1\nAuthorization: Bearer demo.token\nCookie: theme=dark%20mode\n\n"
  say(net.http_route_request_match(req, "GET", "/api/*"))  # 1
  say(net.http_route_path(req))                             # /api/v1/user
  say(net.query_param(req, "id"))                           # 42
  say(net.http_bearer_token(req))                           # demo.token
  say(net.http_get_cookie(req, "theme"))                    # dark mode

  # In a real socket loop:
  # frames:int = net.ws_session_echo_loop(client_sock, 65536, 0)
  # say(frames)
  ret 0