Nothing in this interpreter mallocs; every pool is a fixed array sized by an AKBASIC_MAX_* constant, so sizeof(akbasic_Runtime) is a compile-time number and most of it was headroom nobody was using. Measured concurrent-use high-water marks off examples/breakout and examples/megademo -- the two most demanding programs this interpreter runs -- against each pool's ceiling: AKBASIC_MAX_ENVIRONMENTS 32 -> 12 (measured peak concurrency: 6-7) AKBASIC_MAX_FUNCTIONS 64 -> 8 (measured: 0, neither program uses DEF FN) AKBASIC_MAX_ARRAY_VALUES 4096 -> 2048 (measured peak: 1618 slots) AKBASIC_MAX_SOURCE_LINES 9999 -> 2048 (measured: ~1270-1496 non-blank lines) AKBASIC_SYMTAB_MAX_SLOTS 256 -> 172 (no caller ever requests more than 128) AKBASIC_SYMTAB_MAX_KEY 64 -> 24 (longest identifier measured: 11 chars) AKBASIC_MAX_LINE_LENGTH 256 -> 80 (Commodore BASIC's own line limit) AKBASIC_MAX_VARIABLES (128) is untouched on purpose: breakout alone reaches 121 of 128 concurrent named variables, so it has the least slack of any pool measured and is not a shrink candidate. akbasic_Variable.name shrinks from AKBASIC_MAX_STRING_LENGTH (256) to AKBASIC_SYMTAB_MAX_KEY: every variable name is registered with akbasic_symtab_set() right after this field is populated (akbasic_environment_create(), src/environment.c), and that call already refuses anything AKBASIC_SYMTAB_MAX_KEY characters or longer. The wider field was headroom nothing could ever put a byte into. Two defects surfaced while testing the line-length drop against the golden corpus, both fixed here because the 80-byte ceiling makes them routine rather than theoretical: - sourcepath (runtime.h) was borrowing AKBASIC_MAX_LINE_LENGTH by accident. It holds a directory, not a line of BASIC, and this checkout's own test paths are 81+ characters deep -- every golden test failed to load until this split into its own AKBASIC_MAX_SOURCE_PATH_LENGTH, backed by PATH_MAX the way libakerror already sizes its own path buffers. - src/sink_stdio.c's stdio_readline() called aksl_fgets() but never checked its own documented contract: a full buffer with no trailing newline means the line was longer than the buffer, and the unread remainder is still in the stream. Unchecked, the next readline() picks that remainder up as its own statement -- a real line silently becomes two wrong ones instead of a clean AKBASIC_ERR_BOUNDS refusal. At 256 bytes this was theoretical; at 80 it is not, so it now refuses loudly. tests/value_pool.c's test_pool_is_untouched_by_scopes() was pinned to the old 4x1024=4096 pool math (four max-size arrays proving nothing leaked); rewritten to 2x1024=2048 for the same proof against the new AKBASIC_MAX_ARRAY_VALUES. Known consequence, tracked in andrew/akbasic#32 rather than worked around here: two files in the protected tests/reference/ corpus (language/functions/mod.bas, language/flowcontrol/nestedforloopwaitingfor command.bas) have 82-character lines and cannot be shortened -- MAINTENANCE.md and CMakeLists.txt:585 are explicit that tests/reference/ is never edited to suit this interpreter. Twelve tests/language/ cases and one docs/18 line are in the same position but are this project's own content. Shipping 80 anyway, with the fallout tracked rather than hidden, was an explicit call on this PR rather than something decided here. Verified: cmake --build build-akgl && ctest --test-dir build-akgl, 97/112 (15 known failures, all AKBASIC_MAX_LINE_LENGTH-related, filed as #32). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Co-Authored-By: Andrew Kesterson <andrew@aklabs.net>
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/language/ 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/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(), noabort(), nopanic. Errors come back asakerr_ErrorContext *for the host to handle.FINISH_NORETURNappears only in amain(). - Nothing calls
malloc. Every object comes from a fixed pool insideakbasic_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_Runtimeyou own. Two of them in one process do not interfere. - The host owns the loop.
akbasic_runtime_run(rt, n)executes at mostnsteps and returns. A script containing10 GOTO 10costs younsteps 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/language/README.md |
The editable language corpus 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 whyVAL("garbage")is an error rather than a silent0. - libakgl 0.9.0 — optional, only for
-DAKBASIC_WITH_AKGL=ON. Pulls in SDL3. Its soname carriesMAJOR.MINORwhile the major is 0, so rebuild rather than relink. - basicinterpret — the Go original. Not linked, not built, and safe to omit.