Andrew Kesterson 40198cbc07 File the four things this work deliberately left alone
Nothing here is built. Each is written down so the reasoning does not have to be
reconstructed by whoever picks it up.

**§9 item 9: the sprite breakout spends two of its eight sprites on the screen.**
It draws its play field, captures the whole 800x540 region with SSHAPE, and
installs the capture as sprite 2 -- and does the same for the HUD strip as sprite
1. That reads as a silly thing to do. It is not: it is the only thing that works,
and it is working around items 3 and 5 together rather than choosing anything.
Item 3 says the text layer owns every row by default, and that half is now
answerable since WINDOW became reachable. Item 5 is what WINDOW does not fix --
the frontend never clears and SDL double-buffers, so a drawing has to be
re-issued every frame and fit inside one 256-line batch. Breakout's field is
sixty GSHAPE stamps plus two BOXes plus a drawn banner; it does not fit and never
will. A sprite is the one thing the interpreter redraws from its own state for
nothing.

What that costs is now measured rather than asserted: two of eight sprite slots,
which is the root of every design compromise in that game and the reason chapter
18 opens with a budget table; **4.5% of a frame in collision alone**, because
sprite 2's box covers the whole field so every moving sprite overlaps it
permanently and the broad-phase reject can never throw those pairs out -- 211.8
ns a scan against 54.9 for eight sprites that do not overlap, on every one of 256
scans a frame, to collide with the backdrop; and a chapter section that exists
only to teach the workaround.

The fix is somewhere to draw that persists and is not a sprite -- a layer the
sink composites under the text and the sprites, that a program writes once and
the frontend does not discard. Nothing in libakgl 0.8.0 supplies it; there is no
render-to-texture layer and `frame_start` clears. Filed rather than fixed because
it is a design decision about what a frame owns.

**§6 items 38-40**, the follow-ups to the collision integration:

- Finishing the COLLISION/BUMP migration. The proxies are deliberately not
  registered with a partitioner -- a uniform grid over eight of them costs more
  than an all-pairs loop over 28 pairs saves. What is worth recording is the
  *threshold*, so that raising AKBASIC_MAX_SPRITES is a decision made with the
  number in front of it.
- Converting both breakout listings and chapters 17 and 18 to the new verbs.
  About 200 of their 230 collision lines are a sprite against a BASIC array,
  which static collision geometry is what changes. Separate because both chapters
  were rewritten and validated immediately before this work, their examples are
  executed by ctest, and folding a listing rewrite into a library integration
  would make neither reviewable.
- `akbasic_runtime_call_function()`. A verb cannot take a BASIC function today.
  The finding worth keeping is that the hard half already exists at
  src/runtime.c:1031 -- the multi-line DEF path already re-enters the line loop
  from inside expression evaluation -- so this is splitting one argument-binding
  loop, not building a mechanism.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EwxGB6TdoVvZ11KQQME9cL
2026-08-02 09:51:17 -04:00

akbasic

A BASIC interpreter written in C, styled after Commodore BASIC 7.0 and the Dartmouth BASIC of 1964. It runs a .bas file, it gives you a prompt, and — the point of the exercise — it links into a C program as a scripting engine.

It is a rewrite of basicinterpreter, a Go implementation that started from the Java Lox instructions in craftinginterpreters.com and then struck off on its own. That project is deprecated. It is vendored here as the behavioural spec to read when a question about semantics comes up, and its acceptance corpus is checked in at tests/reference/ and runs on every build — so nothing about building or testing this project needs it.

Quickstart

git submodule update --init --recursive
cmake -S . -B build
cmake --build build --parallel
ctest --test-dir build --output-on-failure
./build/basic                                          # the REPL
./build/basic tests/reference/language/functions.bas   # run a program
10 FOR I# = 1 TO 3
20   PRINT "HELLO " + I#
30 NEXT I#
HELLO 1
HELLO 2
HELLO 3

Two things in that program are not Commodore BASIC and will catch you out immediately: variables carry a type suffix (I# is an integer), and + concatenates a string with a number. Chapter 3 explains both; Chapter 13 is the whole list of what differs from a C128.

Graphics, sound and sprites need the SDL build, which is off by default because the interpreter and its entire test suite build on a machine with no SDL installed at all:

cmake -S . -B build-akgl -DAKBASIC_WITH_AKGL=ON
cmake --build build-akgl --parallel

That basic is a different program: it opens a window, draws BASIC output into it in the Commodore font, and still puts every byte on stdout.

Why rewrite it in C?

Three reasons, in the order they matter.

The interpreter is meant to end up inside libakgl as a scripting engine for game authors, and libakgl is C. Embedding a Go runtime in a C game is not a thing anybody should do to themselves.

The Go version was already written against static pools and explicit state structs — a fixed source table, a fixed variable pool, a 32-leaf ceiling per line — so it ports across almost directly. It reads like C that happens to be spelled in Go.

And the port is a good excuse to find out what the original actually does, as opposed to what it looks like it does. It found five defects nobody knew about.

Design philosophy

A game engine cannot tolerate a scripting language that surprises it. Five rules follow from that, and between them they explain most of what looks unusual in this interpreter:

  • Nothing in the library terminates the process. No exit(), no abort(), no panic. Errors come back as akerr_ErrorContext * for the host to handle. FINISH_NORETURN appears only in a main().
  • Nothing calls malloc. Every object comes from a fixed pool inside akbasic_Runtime. Exhausting one is a diagnosable error, not a crash and not a slow leak.
  • No file-scope mutable state. Interpreter state lives in an akbasic_Runtime you own. Two of them in one process do not interfere.
  • The host owns the loop. akbasic_runtime_run(rt, n) executes at most n steps and returns. A script containing 10 GOTO 10 costs you n steps per frame and nothing else.
  • Hardware is a record of function pointers. Graphics, audio, input and sprites attach as backends, and any of them may be NULL — that is how a host withholds a capability, and how a host that renders some other way never links libakgl at all.

Nothing is silently ignored, either. A verb that needs a device it was not given, or a capability nothing underneath can supply, refuses by name and says why.

Two ways to use it

As a program. basic is a REPL and a script runner, and the standalone driver owns the things a library has no business owning: argv, QUIT, and the window in the SDL build. The guide is written for this reader.

As a library. A host links akbasic::akbasic, hands the interpreter a script, and steps it a frame at a time:

add_subdirectory(deps/akbasic EXCLUDE_FROM_ALL)
target_link_libraries(YOUR_GAME PRIVATE akbasic::akbasic)

Host and script exchange variables through the same pool the script itself uses — no marshalling layer and no copy. PRINT goes through an akbasic_TextSink the host supplies, so a game draws BASIC output into its own text layer. examples/embed.c and examples/hostvars.c are complete and runnable, and both are built and run by every build, so they cannot rot. Chapter 10 walks through the API.

What state it is in

Everything the Go version does, and by now a good deal more. All 41 .bas files of the reference's corpus produce the expected output, including error messages, each as a separate CTest case so a failure names the file.

What is still missing, what is refused deliberately, and the eleven defects inherited from the Go version are catalogued in TODO.md and summarised for a BASIC programmer in Chapter 13.

Where everything else lives

docs/ The guide: eighteen chapters, the language then each hardware area then a reference section for every verb and function, Chapter 14 on the interpreter's own architecture, Chapter 15 listing every error code, and Chapters 17 and 18 building a whole game twice
MAINTENANCE.md For contributors and maintainers: the documentation-example harness, the three test lists, mutation testing, error-code allocation, style
TODO.md Outstanding defects, with file, line and consequence
tests/reference/README.md Where the golden corpus came from, and the rule for changing it

API documentation builds with doxygen Doxyfile, into build/docs/html.

Dependencies

Everything is a submodule; git submodule update --init --recursive gets all of it. There is nothing to install first.

  • libakerror 2.0.1 — TRY/CATCH-style error contexts. Every function that can fail returns one. 2.0.0 made it thread safe and broke the ABI; anything built against a 1.x header must be rebuilt rather than relinked.
  • libakstdlib 0.2.0 — libc wrappers that report through libakerror. String-to-number conversion goes straight to it, which is why VAL("garbage") is an error rather than a silent 0.
  • libakgl 0.8.0 — optional, only for -DAKBASIC_WITH_AKGL=ON. Pulls in SDL3. Its soname carries MAJOR.MINOR while the major is 0, so rebuild rather than relink.
  • basicinterpret — the Go original. Not linked, not built, and safe to omit.
Description
A BASIC interpreter
Readme 4.6 MiB
Languages
C 93.7%
Shell 2.1%
CMake 1.9%
Python 1.2%
BASIC 1.1%