Port the standalone SDL frontend, and give the sink a line editor

An AKGL build of `basic` was a terminal program with unused SDL linked into
it. It now opens the reference's 800x600 window, draws BASIC output in the
Commodore font, pumps events, lets you type at it, and still mirrors every
byte to stdout.

The stdout mirror is a composing sink rather than a second write inside the
interpreter, and lives in the core library where it needs no SDL. The line
editor waits for a typed line by borrowing one frame at a time from the host,
so nothing blocks and nothing owns an event loop it should not.

The whole golden corpus now runs through the SDL binary as well as the stdio
one, byte for byte.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Andrew Kesterson <andrew@aklabs.net>
This commit is contained in:
2026-07-31 11:21:39 -04:00
parent 66f415dfe8
commit 23dda12e24
14 changed files with 1816 additions and 106 deletions

View File

@@ -3,21 +3,28 @@
* @brief The standalone driver.
*
* Everything that belongs to a program rather than to a library lives here: argv
* handling, sink selection, the unbounded run loop, and a FINISH_NORETURN --
* which belongs only in a main(). The interpreter library itself never
* terminates the process.
* handling, which frontend to bring up, and a FINISH_NORETURN -- which belongs
* only in a main(). The interpreter library itself never terminates the process.
*
* There are two drivers in here and the build option picks between them. Without
* AKBASIC_HAVE_AKGL this is a terminal program: stdio in, stdout out, no window.
* With it, the program is an SDL host -- window, font, event pump, frame loop --
* and its output goes to the window *and* to stdout, so a piped program produces
* the same bytes either way. The whole of that second driver is
* akbasic/frontend.h; what is left here is argv and the choice.
*
* The runtime is static rather than automatic because it carries every pool the
* interpreter owns -- several megabytes -- and that will not fit on a default
* stack. An embedding game would place it in its own state for the same reason.
*/
/* clock_gettime and CLOCK_MONOTONIC. C99 alone does not declare either. */
/* clock_gettime, CLOCK_MONOTONIC and isatty. C99 alone declares none of them. */
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <akerror.h>
#include <akstdlib.h>
@@ -27,6 +34,13 @@
#include <akbasic/sink.h>
static akbasic_Runtime RUNTIME;
#ifdef AKBASIC_HAVE_AKGL
#include <akbasic/frontend.h>
static akbasic_AkglFrontend FRONTEND;
#else
static akbasic_TextSink SINK;
static akbasic_StdioSink SINKSTATE;
@@ -82,6 +96,85 @@ static akerr_ErrorContext AKERR_NOIGNORE *drive(akbasic_Runtime *obj)
SUCCEED_RETURN(errctx);
}
/**
* @brief The terminal driver: no window, no SDL, output on stdout.
*
* @param program The already-opened program file, or NULL for an interactive REPL.
*/
static akerr_ErrorContext AKERR_NOIGNORE *run_stdio(FILE *program)
{
PREPARE_ERROR(errctx);
if ( program != NULL ) {
/*
* A file argument: read the program from it in RUNSTREAM mode, which
* files each line under its line number and then switches to RUN.
*/
PASS(errctx, akbasic_sink_init_stdio(&SINK, &SINKSTATE, stdout, program));
PASS(errctx, akbasic_runtime_init(&RUNTIME, &SINK));
PASS(errctx, akbasic_runtime_start(&RUNTIME, AKBASIC_MODE_RUNSTREAM));
} else {
PASS(errctx, akbasic_sink_init_stdio(&SINK, &SINKSTATE, stdout, stdin));
PASS(errctx, akbasic_runtime_init(&RUNTIME, &SINK));
PASS(errctx, akbasic_runtime_start(&RUNTIME, AKBASIC_MODE_REPL));
}
PASS(errctx, drive(&RUNTIME));
SUCCEED_RETURN(errctx);
}
#endif /* !AKBASIC_HAVE_AKGL */
#ifdef AKBASIC_HAVE_AKGL
/**
* @brief Where to find the Commodore font.
*
* Compiled in by CMake, because the reference's relative "./fonts/..." only ever
* worked from its own source directory. AKBASIC_FONT overrides it at runtime,
* which is what an installed copy or a different font needs.
*/
#ifndef AKBASIC_FONT_PATH
#define AKBASIC_FONT_PATH "fonts/C64_Pro_Mono-STYLE.ttf"
#endif
/**
* @brief The SDL driver: an 800x600 window, and stdout still gets everything.
*
* @param program The already-opened program file, or NULL to type at the window.
*/
static akerr_ErrorContext AKERR_NOIGNORE *run_akgl(FILE *program)
{
PREPARE_ERROR(errctx);
const char *fontpath = getenv("AKBASIC_FONT");
FILE *input = program;
int mode = AKBASIC_MODE_RUNSTREAM;
if ( fontpath == NULL ) {
fontpath = AKBASIC_FONT_PATH;
}
if ( program == NULL ) {
/*
* With a terminal on the other end of stdin the window is the console
* and typed lines come from its line editor. Piped or redirected, they
* come from the pipe instead -- so `basic < program.bas` still behaves
* in an AKGL build, and the golden corpus can be driven through this
* binary as well as through the stdio one.
*/
input = (isatty(fileno(stdin)) ? NULL : stdin);
mode = AKBASIC_MODE_REPL;
}
PASS(errctx, akbasic_frontend_akgl_init(&FRONTEND, "BASIC",
AKBASIC_FRONTEND_WIDTH,
AKBASIC_FRONTEND_HEIGHT,
fontpath, AKBASIC_FRONTEND_FONT_SIZE,
stdout, input));
PASS(errctx, akbasic_frontend_akgl_attach(&FRONTEND, &RUNTIME));
PASS(errctx, akbasic_runtime_start(&RUNTIME, mode));
PASS(errctx, akbasic_frontend_akgl_drive(&FRONTEND, &RUNTIME));
SUCCEED_RETURN(errctx);
}
#endif
int main(int argc, char **argv)
{
PREPARE_ERROR(errctx);
@@ -90,24 +183,20 @@ int main(int argc, char **argv)
ATTEMPT {
if ( argc > 1 ) {
/*
* A file argument: read the program from it in RUNSTREAM mode, which
* files each line under its line number and then switches to RUN.
*/
CATCH(errctx, aksl_fopen(argv[1], "r", &program));
CATCH(errctx, akbasic_sink_init_stdio(&SINK, &SINKSTATE, stdout, program));
CATCH(errctx, akbasic_runtime_init(&RUNTIME, &SINK));
CATCH(errctx, akbasic_runtime_start(&RUNTIME, AKBASIC_MODE_RUNSTREAM));
} else {
CATCH(errctx, akbasic_sink_init_stdio(&SINK, &SINKSTATE, stdout, stdin));
CATCH(errctx, akbasic_runtime_init(&RUNTIME, &SINK));
CATCH(errctx, akbasic_runtime_start(&RUNTIME, AKBASIC_MODE_REPL));
}
CATCH(errctx, drive(&RUNTIME));
#ifdef AKBASIC_HAVE_AKGL
CATCH(errctx, run_akgl(program));
#else
CATCH(errctx, run_stdio(program));
#endif
} CLEANUP {
if ( program != NULL ) {
IGNORE(aksl_fclose(program));
}
#ifdef AKBASIC_HAVE_AKGL
akbasic_frontend_akgl_shutdown(&FRONTEND);
#endif
} PROCESS(errctx) {
} HANDLE_DEFAULT(errctx) {
LOG_ERROR_WITH_MESSAGE(errctx, "akbasic terminated on an unhandled error");