Puffin

Puffin examples

Curated, runnable Puffin programs. lang/ shows the language by example; ffi/ and z3/ show it against the real world through the typed FFI. Every example ships with a .expect golden and is held to it on both self-hosted routes β€” native code and the bytecode VM β€” so everything on this page runs, verbatim, today.

Running them

You need the compiler (bin/bootstrap builds build/puffincc; no Racket required). Compile and run an example directly, or let the test script run the whole set:

build/puffincc examples/z3/sudoku.puf -o sudoku && ./sudoku
tools/test-examples.sh              # all of them, native + VM
tools/test-examples.sh sudoku       # just one

Examples whose foreign library is missing on your machine are skipped, not failed β€” the z3/ examples need brew install z3.

lang/ β€” the language by example

The lang/ programs share sources with the golden test corpus (src/test-programs/), which also feeds the web playground's example menu β€” the playground is the fastest way to try them without building anything.

ffi/ β€” first contact

z3/ β€” a real library as a Puffin API

z3.puf binds the Z3 SMT solver (brew install z3; if your libz3 isn't at Homebrew's default path, edit the library path at the top of z3.puf). The binding is deliberately thin β€” two define-foreign-type handles and six imports, of which Z3_eval_smtlib2_string is the workhorse β€” because SMT-LIB is s-expressions: queries are quasiquoted Puffin data rendered by value->string, and replies are read back into Puffin data by the ~30-line reader in the same file. require it like any module:

(require "z3.puf")
(define ctx (z3-new))
(z3-send* ctx '((declare-const x Int) (assert (> x 41)) (assert (< x 43))))
(z3-check-sat ctx)        ;; => sat
(z3-get-values ctx '(x))  ;; => ((x 42))
(z3-close ctx)

The FFI chapter of the tutorial (docs/tutorial.html) walks the design behind these bindings.

One honest limit: the browser playground has no dlopen, so the FFI and z3 examples compile there but refuse to run at load β€” error: foreign library ... is not available in the browser.