Examples

Annotated AXON programs demonstrating every major language feature. Click tabs to switch between source and output.

👋 Hello World

The simplest AXON program — call an external function and return an exit code.

(module
  (extern print_i64 ((n i64)) void)

  (fn main () i32
    (block
      (call print_i64 (i64 42))
      (i32 0))))
42

🔢 Fibonacci

Iterative Fibonacci with a while loop — demonstrates variables, mutation, and arithmetic.

(module
  (extern print_i64 ((n i64)) void)

  (fn fib ((n i64)) i64
    (block
      (let a i64 (i64 0))
      (let b i64 (i64 1))
      (let i i64 (i64 0))
      (while (< i n)
        (block
          (let tmp i64 b)
          (set b (+ a b))
          (set a tmp)
          (set i (+ i (i64 1)))))
      a))

  (fn main () i32
    (block
      (call print_i64 (call fib (i64 10)))
      (i32 0))))
55

🏷️ Enum + Pattern Matching NEW

Define an enum with named variants and use exhaustive pattern matching to dispatch on values.

(module
  (extern print_i64 ((n i64)) void)
  (enum Color i32 ((Red 0) (Green 1) (Blue 2)))

  (fn color_value ((c Color)) i64
    (match c
      ((Color Red)   (i64 10))
      ((Color Green) (i64 20))
      ((Color Blue)  (i64 30))))

  (fn main () i32
    (block
      (call print_i64 (call color_value (Color Red)))
      (call print_i64 (call color_value (Color Green)))
      (call print_i64 (call color_value (Color Blue)))
      (i32 0))))
10
20
30

🧬 Sum Types NEW

Tagged unions with payload data — declare variants with associated types, construct with dot notation, and destructure with pattern matching.

(module
  (extern print_i64 ((n i64)) void)
  (sum Option ((None) (Some i64)))

  (fn unwrap_or ((opt Option) (default i64)) i64
    (match opt
      ((Option.Some val) val)
      ((Option.None) default)))

  (fn main () i32
    (block
      (call print_i64 (call unwrap_or (Option.Some (i64 42)) (i64 0)))
      (call print_i64 (call unwrap_or (Option.None) (i64 -1)))
      (i32 0))))
42
-1

🔗 Function Pointers NEW

First-class function pointers with auto-coercion — pass functions as arguments and call them indirectly.

(module
  (extern print_i64 ((n i64)) void)

  (fn double ((x i64)) i64
    (* x (i64 2)))

  (fn triple ((x i64)) i64
    (* x (i64 3)))

  (fn apply ((f (fnptr (i64) i64)) (val i64)) i64
    (call f val))

  (fn main () i32
    (block
      ;; auto-coerce fn → fnptr
      (call print_i64 (call apply double (i64 5)))
      (call print_i64 (call apply triple (i64 5)))
      ;; store in local fnptr variable
      (let fp (fnptr (i64) i64) double)
      (call print_i64 (call fp (i64 7)))
      (i32 0))))
10
15
14

📦 Structs

Value-type structs with named fields — construct, access fields, and pass by pointer.

(module
  (extern print_i64 ((n i64)) void)
  (struct Point ((x i64) (y i64)))

  (fn manhattan ((p (ptr Point))) i64
    (+ (field (deref p) x)
       (field (deref p) y)))

  (fn main () i32
    (block
      (let pt Point (struct Point
        (x (i64 3))
        (y (i64 4))))
      (call print_i64 (call manhattan (addr pt)))
      (call print_i64 (field pt x))
      (call print_i64 (field pt y))
      (i32 0))))
7
3
4

🃏 Wildcard Matching NEW

Use the wildcard _ pattern for catch-all arms in integer and enum matches.

(module
  (extern print_i64 ((n i64)) void)

  (fn classify ((x i32)) i64
    (match x
      ((i32 0)  (i64 100))      ;; zero
      ((i32 1)  (i64 200))      ;; one
      ((i32 2)  (i64 300))      ;; two
      (_       (i64 999))))     ;; everything else

  (fn main () i32
    (block
      (call print_i64 (call classify (i32 0)))
      (call print_i64 (call classify (i32 1)))
      (call print_i64 (call classify (i32 2)))
      (call print_i64 (call classify (i32 42)))
      (i32 0))))
100
200
300
999