Interpreter for the i++ programming language
i_plus_plus AGENTS.md
7.7 kB · 171 lines
Markdown
at main

AGENTS.md — i++ (SKI Combinator Language) #

A Rust project implementing a parser, interpreter, and lambda-calculus evaluator for the SKI combinator calculus, with both a CLI REPL and a WASM-backed web frontend.

Development Environment #

# Enter the dev shell (provides rustc, cargo, clippy, rustfmt, wasm-bindgen, lld)
nix develop

All build/lint/test commands below assume you're inside this shell or an equivalent NixOS environment.

Never set LD_LIBRARY_PATH globally; on NixOS this causes glibc version mismatches that crash system tools and test binaries with SIGSEGV or symbol lookup errors. If commands fail with glibc errors (e.g. GLIBC_2.42 not found), the fix is unset LD_LIBRARY_PATH and cargo clean (stale build artifacts carry the wrong linker path).

There is no rustup — the flake provides everything. Do not attempt rustup install or rustup target add.

Build, Test, Lint #

cargo build                          # dev build (native)
cargo build --release                # release build (native)
cargo test                           # all tests
cargo test parse_s                   # single test by name
cargo test -- --nocapture            # with stdout/stderr
cargo test parser::                  # specific module
cargo fmt                            # format code
cargo clippy                         # lint
cargo run                            # CLI REPL
cargo run -- path/to/file.i++        # run file

Benchmarking #

cargo run --release --example bench

Benchmarks are in examples/bench.rs. They measure:

  • import std (cold) — parsing and loading the prelude from scratch
  • eval I, eval S K K, eval I x, eval K x y — basic combinator evaluation
  • eval B B, eval T T T — nested combinator chains (reduces to identity)
  • eval i i i — chained iota reduction

Run baseline benchmarks before any performance-sensitive refactoring. Compare results afterward to detect regressions.

WASM Build (for web frontend) #

The flake sets CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_LINKER so the WASM linker (lld) is found automatically.

cargo build --release --features wasm --target wasm32-unknown-unknown
wasm-bindgen --target web --out-dir web/static/wasm target/wasm32-unknown-unknown/release/i_plus_plus.wasm

Or simply run just wasm, which does the above and keeps web/static/wasm/ in sync.

The wasm-bindgen Rust crate version (Cargo.toml) must match the CLI version provided by the flake. If you get a schema version mismatch, update Cargo.toml to use wasm-bindgen = { version = "0.2", optional = true } (no = pin) and run cargo update.

Web Frontend #

The frontend is a SvelteKit app in web/. It consumes the Rust core as a WASM module built with wasm-bindgen.

just web-dev        # build WASM and start the SvelteKit dev server
just web-build      # build the production app
just serve          # preview the production build locally

WASM artifacts are generated into web/static/wasm/ by just wasm and are gitignored. The dynamic import in web/src/lib/wasm.ts loads them client-side so SSR never touches the WASM module.

Hosting #

The app is built for Cloudflare Pages via @sveltejs/adapter-cloudflare. CI on push to main runs tests, builds the app, and deploys with Wrangler.

CI secrets #

The deploy workflow needs these repository secrets (set in Tangled repo settings):

  • CLOUDFLARE_API_TOKEN — Pages:Edit + Account:Read permissions
  • CLOUDFLARE_ACCOUNT_ID
  • CLOUDFLARE_PAGES_PROJECT_NAME

Custom domain #

Add ipp.saxxie.dev (or whichever subdomain you choose) as a custom domain in the Cloudflare Pages project.

If saxxie.dev DNS is already managed by Cloudflare, Cloudflare will add the required CNAME automatically.

If DNS stays elsewhere (e.g. Namecheap), add the records Cloudflare Pages gives you:

  • A TXT record for domain verification.
  • A CNAME for ipp.saxxie.dev pointing to <project>.pages.dev.

This keeps the rest of the saxxie.dev DNS setup untouched while still getting a Cloudflare Pages site on the subdomain.

Project Layout #

src/
  lib.rs          — Public API: Term enum, Error enum, wasm_export, re-exports
  main.rs         — CLI entry point (REPL or file execution)
  engine.rs       — Engine: unified eval, eval_file, module loading; Environment: definition store
  module.rs       — ModuleResolver: file loading, prelude, cycle detection
  parser.rs       — nom-based parser (parse, parse_definition, Statement enum)
  interpreter.rs  — Stateless term reduction (SKI reduction, definition substitution)
  lambda.rs       — De Bruijn encoding, lambda display, beta reduction
examples/
  bench.rs        — microbenchmarks using std::time::Instant
prelude/
  Combinators.i++ — Standard library: I, K, S, and derived combinators
  Math.i++        — Math library: EXP, E, LN, MINUS using EML
pkg/              — WASM build output (gitignored, regenerated with wasm-bindgen)
web/              — SvelteKit frontend (deployed to Cloudflare Pages)
  static/wasm/    — WASM artifacts copied here at build time (gitignored)
.tangled/workflows/ — Spindle CI pipelines (deploy.yml, check.yml)
scripts/          — Helper scripts (get-wasm-bindgen-version.py for CI)
flake.nix         — Nix flake: dev shell with all tooling
README.md         — Project overview and quick-start

Code Style #

Imports #

  • Group imports as: use std::..., then use crate::{...}, then external crates.
  • Within crate::, list multiple items in a single use block (e.g. use crate::{Error, Term, Parser}).
  • Avoid wildcard imports.

Formatting #

  • Strict cargo fmt output. Run before committing.

Types and Naming #

  • Types/enums: PascalCase (Term, Interpreter, Error, DeBruijn).
  • Functions/methods: snake_case (term_to_debruijn, parse_expr, show_lambda).
  • Module names: snake_case (src/interpreter.rs not src/Interpreter.rs).
  • Variables/fields: snake_case (let max_steps).
  • Constants: UPPER_SNAKE_CASE (e.g. MAX_REDUCTIONS in main.rs).

Error Handling #

  • Use thiserror::Error derive for the project's enum Error in lib.rs.
  • Parser functions return Result<T, Error> using the project's Error::Parse, Error::Eval, etc. variants.
  • Call map_err on nom errors to convert them into the project's Error type.
  • Never unwrap() or expect() outside of tests or CLI glue code (main.rs, io).

Parser Pattern (nom) #

  • Private helper parsers return nom::IResult<&str, T>.
  • Public methods on the Parser struct return Result<T, Error> (project error type, not nom error).
  • Use tag(), take_while(), take_while1() for tokenizing (S and K are single characters not followed by is_name_cont).

Structure #

  • Tests use helper free functions at the top of mod tests for constructing terms:
    fn s() -> Term { Term::S }
    fn k() -> Term { Term::K }
    fn var(s: &str) -> Term { Term::Var(s.to_string()) }
    fn app(l: Term, r: Term) -> Term { Term::App(Box::new(l), Box::new(r)) }
    
  • Keep test blocks focused: one #[test] per behavior, descriptive names.

General #

  • Prefer structural matching over boolean conditionals for variant dispatch (e.g. if let Term::App(ll, lr) = l.as_ref() or full match blocks).
  • Avoid comments inside code — let test names and function names tell the story.
  • WASM support is gated behind #[cfg(feature = "wasm")] and the optional wasm-bindgen dependency; WASM-exposed functions live in lib.rs, not main.rs.