Skip to content

Latest commit

 

History

History
189 lines (154 loc) · 10.1 KB

README.md

File metadata and controls

189 lines (154 loc) · 10.1 KB

rontolisp examples

Practical, self-contained rontolisp programs. Unless a directory says otherwise, everything in it runs identically on the interpreter, the JVM and WASM. Each directory holds a README of its own; the individual programs explain themselves in their header comments. How big the compiled artifacts are is measured, not documented here: size-report/.

Directory Programs
console/ Algorithms and console I/O — pure, cross-backend
scheme/ Scheme (.scm) programs for the experimental Scheme front end: automatic differentiation, n queens, canonical Huffman codes, streams, a stack-language interpreter
starlark/ Starlark (.star) programs for the experimental Starlark front end: a build graph
java/ Programs for the experimental subset of the Java language (.java): an order book, an HTTP server and its client, a Servlet application, JDBC on PostgreSQL
clojure/ Clojure (.clj, .cljc) programs for the experimental Clojure front end: the core language, a deps.edn project, the bundled namespaces, macros, Ring and the HTTP client, Java interop, the WASM and WIT boundary
ml/ Numerical computing and machine learning (arrays, linalg, --simd, --gpu)
deep-learning-from-scratch/ The book Deep Learning from Scratch (ゼロから作るDeep Learning) ch02–ch08, ported
llm/ llama2.c's run.c ported whole: a Llama 2 inference engine over the real TinyStories checkpoints — the example --simd is for
net/ Sockets, HTTP servers and JSON web services
db/ PostgreSQL through the real cl-postgres driver and postmodern, up to a REST API and a full web app
jvm/ A Java-callable library class, a C library through cffi, java: interop and Swing GUIs (JVM only)
macos/ A native Cocoa window, the Objective-C runtime, and Metal under it, through the built-in appkit / objc / metal / scene packages (macOS only)
browser/ Browser demos: compile to WASM, run in a page
proxy-wasm/ Two Proxy-Wasm SDKs written from scratch, one with all of Lisp and one for --no-gc, and Envoy HTTP filters on them, checked in a real Envoy
count-vowels/, wit/ Crossing the WASM boundary: exporting to a host, implementing and calling a WIT world, composing with Rust
asdf/ Loading real third-party libraries with asdf:load-system / ql:quickload
wasmcloud/, cloudflare-workers/, gae/ Platform templates

Assuming the executable JAR is built (./mvnw clean package):

JAR=target/rontolisp-0.1.0-SNAPSHOT-exec.jar

Where things run

Most programs run on all four backends (see Running below). These areas need a narrower set:

  • net/ servers on WASM need --component plus wasmtime run ... -S inherit-network=y; the http-handler ones run under wasmtime serve. TLS (tls-listen) is interpreter/JVM only.
  • jvm/ is JVM-family only: the CFFI bindings need a foreign function API neither WASM backend has; the java: interop and Swing demos need the JVM and a display, and the native binary carries no reflection metadata for them.
  • macos/ is macOS, every JVM-side shape of the language (java -jar, the native binary, a compiled .class/.jar); the window and Metal examples need a display, so they are not in examples.yaml. The two that open nothing — objc-runtime.lisp, system-frameworks.lisp — run in a terminal and are listed there under os: [mac].

Guides by area

The examples are the runnable half; each has a guide behind it:

Topic Guide
SIMD / GPU acceleration simd-acceleration, gpu-acceleration
Linear algebra, neural networks (linalg, torch) linear-algebra, neural-networks
Web: Clack, serving HTTP clack, http-handler
JVM: calling library classes, C libraries, Java interop jvm-library, cffi, java-interop
macOS GUI and 3-D (appkit, objc, metal, scene) objc-appkit, solid-modeling
Testing with rove testing

Running

Any example can be interpreted, compiled to a JVM .class, or compiled to .wasm:

# 1. Interpreter
java -jar $JAR examples/console/nqueens.lisp

# 2. JVM (the class is named after the output file, so keep it path-free)
java -jar $JAR examples/console/nqueens.lisp -o Prog.class && java Prog

# 3. WASM (requires wasmtime 14+)
java -jar $JAR examples/console/nqueens.lisp -o nqueens.wasm && wasmtime run nqueens.wasm

Programs that touch files need a preopened directory on WASM:

java -jar $JAR examples/console/line-numbers.lisp -o ln.wasm
wasmtime run --dir . ln.wasm

The macOS GUI and Metal examples run the same ways on a Mac; a representative set:

java -jar $JAR examples/macos/counter.lisp
java -jar $JAR examples/macos/menubar.lisp              # no window; look at the menu bar
java -jar $JAR examples/macos/metal-cube.lisp
java -jar $JAR examples/macos/scene-robot-arm.lisp
./target/rontolisp examples/macos/counter.lisp         # the native binary, after ./mvnw -Pnative package
java -jar $JAR examples/macos/counter.lisp -o Counter.class && java Counter

An example that checks itself

Most examples print a result and leave the checking to examples.yaml. Three do it in the Lisp instead, with rove — the shape to copy when what you are writing has a right answer rather than only an output: console/roman.lisp asserts the full 1..3999 Roman-numeral round-trip, cloudflare-workers/httpbin/check.lisp asserts the Worker's parsed reply, and browser/minesweeper/minesweeper-core-test.lisp checks a GUI example's rendering-free core head-less.

The recipe is a few lines. Load rove, silence its ANSI colors, write deftests, and make the verdict the exit code:

(asdf:load-system :rove)
(use-package :rove)
(setf *enable-colors* nil)

(deftest arithmetic
  (testing "adding two integers"
    (ok (= (add 1 2) 3))))

(uiop:quit (if (run-suite *package*) 0 1))

rove is vendored in this repository, so its three directories go on --system-path and nothing is downloaded; outside it, (ql:quickload "rove") fetches the same sources. The systems are spliced in at compile time, so the compiled class / module is self-contained:

SP=src/test/resources/rove:src/test/resources/dissect:src/test/resources/cl-ppcre
java -jar $JAR examples/console/roman.lisp --system-path $SP

Verifying every non-GUI example at once

examples.yaml lists each non-GUI example and the backends it can be verified on; ExamplesE2eTest turns every (example × backend) pair into one dynamic test. interpreter / jvm / wasm / wasm-component-run run the program and check its output; jvm-compile / wasm-component / no-gc only build it, because blocking servers and host-invoked modules never return on their own.

wasm-component and wasm-component-run build the same artifact and differ only in whether it is then run. The component path has its own I/O adapter — files, stdin and directories over WASI 0.3 streams rather than the Preview 1 syscalls the wasm leg drives — so an ordinary program that only compiles here is not verified on that backend at all. Use wasm-component-run unless the example genuinely cannot run (a server, a --invoked world, a component whose host is not on this machine).

Each entry declares its args / stdin and one expect: equals (stdout matches this text), file (matches a file under examples/), contains (every listed substring appears) or skip: true. Omitting expect means "exit 0 and non-empty output". equals/file are checked against all run backends, so a per-backend divergence is a real failure. An example that loads an ASDF system names its directory — or, like the rove ones above, the LIST of directories — under systemPath. One that can only run on a given platform names it under os (os: [mac]): that gates the run legs and leaves the compile legs alone, so its lowering is still checked everywhere. One that calls Java as it runs (java:, a Clojure host form) says javaInterop: true: the native binary interprets no java:, so its interpreter leg runs on the exec jar only, while the native binary still compiles it for the jvm leg. A .java program that compiles a library from its sources jar names the coordinate under javaSourceDep, passed to every leg as --java-source-dep; the suite resolves the jar first (~/.m2, then Maven Central) and skips the legs, saying why, on a machine that has neither.

The suite is opt-in, so a plain ./mvnw test skips it:

./mvnw clean package -DskipTests
./mvnw -Dtest=ExamplesE2eTest -DfailIfNoTests=false -Drontolisp.examples=true test

To run against the native binary instead, pass -Drontolisp.binary="$PWD/target/rontolisp" and drop -Drontolisp.examples. The full suite takes minutes; narrow it with a comma-separated -Drontolisp.examples.only=<substrings> matched against the manifest path (console/,ml/). A pattern matching nothing produces Tests run: 0 rather than the whole suite.

Adding an example means appending an entry to examples.yaml — no Java changes. To regenerate an externalised expected file, run the example and save its stdout. GUI examples (jvm/, macos/ and the browser/ demos) are excluded: they open a window or run in a page and cannot be checked headless — though the part of one that is not GUI can be, which is what minesweeper-core-test.lisp and the terminal-only macos/ programs are.