The self-hosting Bats compiler. Bats is a programming language that compiles to ATS2.
.bats → lexer → emitter → .sats/.dats → patsopt → _dats.c → cc → binary
# Bootstrap build (first time, requires bats-old)
bats-old build --repository ../repository-prototype
# Self-compilation
dist/debug/bats build --repository ../repository-prototype
# Type-check only
dist/debug/bats check --repository ../repository-prototypesrc/bin/bats.bats— entry point (argument parsing, command dispatch)src/helpers.bats— globals, string/builder helpers, process spawning, file I/Osrc/lexer.bats— tokenizer (produces spans from.batssource)src/emitter.bats— code generator (produces.sats/.datsfrom spans)src/build.bats— build pipeline (preprocessing, patsopt, cc, link)src/commands.bats— subcommands (init, test, upload, check, run, etc.)src/cache.bats— patsopt's C made path-independent, and the shared cache (BATS_CACHE_DIR)src/facts.bats— the commit a build is made from, for a project that staloads_bats_facts.sats
bats build [--only debug|release|wasm]— compile the projectbats check— type-check without linkingbats run [--bin name]— build and run a binarybats lock --repository <dir>— resolve dependencies to their newest versions and writebats.lock; with abats.lock, the other commands use exactly the versions it locksbats init binary|library— create a new projectbats test— build and run testsbats upload --repository <dir>— package and publish a librarybats clean— remove build artifactsbats tree— show dependency treebats add <package>/bats remove <package>— manage dependencies
make installinbootstrap/c(or in a--to-cdirectory) puts the compiler at~/.bats/bin/bats(PREFIX=...for elsewhere), beside the ATS2 toolchain bats keeps in~/.bats/ats2; put~/.bats/binonPATHonce.BATS_REPOSITORY=<dir>: the package repository, when--repositoryis not given (the flag wins).BATS_CACHE_DIR=<dir>: a cache of patsopt's output that every checkout of a project shares. An entry is keyed by the toolchain (patsopt and its prelude and libraries, by content) and by every file the module's staloads reach (by content), and only a run that passed is kept, so a hit never lets through what a cold build rejects. Entries unused for 7 days are removed; removing the directory is always safe.bats with-env <env-file> <command> [args...]runs a command with the variables of a dotenv file (the settings above), for a shell that reads no profile; the file wins, and the command's arguments and exit status are its own. A command name with no/is looked for in the PATH the command runs with (the file's, when it sets one). Limits: the file is at most 64 KiB and sets at most 1024 variables, the command line at most 4095 bytes. Known limits of the platform: bats runs the command as a child rather than replacing itself, so a signal sent to bats (kill) does not reach the command (it is left running, and what it holds stays held: inbats with-env env bats checkthe project lock and machine slots of the innerbats checkstay taken until it ends), and a command ended by a signal has status 1 (bats-lang/process#51 for exec, #52 for the signal status).bats fixtures <dir> [--only <member>]... [--bless] [--cold] [--work <dir>] [--repository <dir>]runs a directory of static-test fixtures (accept/<name>andreject/<name>, each a snippet put into a module of a copy of the project) and saysok accept/<name> (N s)or why not: an accept must passbats check, a reject must fail it with exactly the error itsexpectfile says (the function, the line of the snippet and patsopt's error, with only its variable numbers compared as_).--blesswrites each reject'sexpectfrom the error patsopt gave (the new expects are read in the PR diff). Exit status 1 when a fixture fails, 2 for a usage error.
bats takes a lock on the project (flock on bats.toml) for every command that writes build/, dist/ or bats_modules/, so two bats on one checkout take turns; the one that waits says so. bats run releases it before the program runs.
A module that staloads "_bats_facts.sats" gets the commit the build is made from: BATS_COMMIT (any revision git takes), else HEAD.
staload F = "_bats_facts.sats"
implement main0 () = println! ($F.bats_commit_version (), " (", $F.bats_commit_short (), ")")bats_commit_time (): int (committer time, seconds since 1970), bats_commit_version () (that time in UTC as YEAR.MONTH.DAY.SECONDS, as bats versions packages), bats_commit_short (): string 7 and bats_commit_sha (): string 40. They are the commit's, never the build's, and a new commit compiles one small module again and nothing else.
argparse, array, arith, builder, env, file, path, process, result, sha256, str, toml