Some checks failed
akbasic CI Build / cmake_build (push) Failing after 3m29s
akbasic CI Build / coverage (push) Failing after 3m40s
akbasic CI Build / sanitizers (push) Failing after 4m37s
akbasic CI Build / mutation_test (push) Failing after 3m35s
akbasic CI Build / akgl_build (push) Failing after 7m20s
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>
138 lines
4.9 KiB
C
138 lines
4.9 KiB
C
/**
|
|
* @file sink_stdio.c
|
|
* @brief The stdio-backed text sink.
|
|
*
|
|
* This is what makes the golden corpus runnable with no SDL on the machine. It
|
|
* writes exactly what the reference's fmt.Printf/fmt.Println mirror wrote, byte
|
|
* for byte, including the newline writeln appends -- an error line therefore
|
|
* ends in two newlines, because the caller's message already carries one. That
|
|
* is the acceptance contract, not an accident. See TODO.md section 1.8.
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <akerror.h>
|
|
#include <akstdlib.h>
|
|
|
|
#include <akbasic/error.h>
|
|
#include <akbasic/sink.h>
|
|
|
|
static akerr_ErrorContext *stdio_write(akbasic_TextSink *self, const char *text)
|
|
{
|
|
PREPARE_ERROR(errctx);
|
|
akbasic_StdioSink *state = NULL;
|
|
int count = 0;
|
|
|
|
FAIL_ZERO_RETURN(errctx, (self != NULL && text != NULL), AKERR_NULLPOINTER,
|
|
"NULL argument in sink write");
|
|
state = (akbasic_StdioSink *)self->self;
|
|
PASS(errctx, aksl_fprintf(&count, state->out, "%s", text));
|
|
SUCCEED_RETURN(errctx);
|
|
}
|
|
|
|
static akerr_ErrorContext *stdio_writeln(akbasic_TextSink *self, const char *text)
|
|
{
|
|
PREPARE_ERROR(errctx);
|
|
akbasic_StdioSink *state = NULL;
|
|
int count = 0;
|
|
|
|
FAIL_ZERO_RETURN(errctx, (self != NULL && text != NULL), AKERR_NULLPOINTER,
|
|
"NULL argument in sink writeln");
|
|
state = (akbasic_StdioSink *)self->self;
|
|
PASS(errctx, aksl_fprintf(&count, state->out, "%s\n", text));
|
|
SUCCEED_RETURN(errctx);
|
|
}
|
|
|
|
static akerr_ErrorContext *stdio_readline(akbasic_TextSink *self, char *dest, size_t len, bool *eof)
|
|
{
|
|
PREPARE_ERROR(errctx);
|
|
akbasic_StdioSink *state = NULL;
|
|
size_t used = 0;
|
|
|
|
FAIL_ZERO_RETURN(errctx, (self != NULL && dest != NULL && eof != NULL), AKERR_NULLPOINTER,
|
|
"NULL argument in sink readline");
|
|
FAIL_ZERO_RETURN(errctx, (len > 1), AKBASIC_ERR_BOUNDS,
|
|
"Read buffer of %zu bytes is too small", len);
|
|
|
|
state = (akbasic_StdioSink *)self->self;
|
|
*eof = false;
|
|
dest[0] = '\0';
|
|
|
|
/*
|
|
* The end of the stream is AKERR_EOF rather than a NULL return, so it is
|
|
* handled here and turned back into the *eof flag this sink promises. A
|
|
* genuine read error is left to propagate, which is what a NULL from
|
|
* fgets(3) could never be told apart from.
|
|
*/
|
|
ATTEMPT {
|
|
CATCH(errctx, aksl_fgets(dest, len, state->in, &used));
|
|
} CLEANUP {
|
|
} PROCESS(errctx) {
|
|
} HANDLE(errctx, AKERR_EOF) {
|
|
*eof = true;
|
|
} FINISH(errctx, true);
|
|
if ( *eof ) {
|
|
SUCCEED_RETURN(errctx);
|
|
}
|
|
/*
|
|
* aksl_fgets(3)'s own contract: a full buffer with no trailing newline is
|
|
* how a caller spots a line longer than the buffer, because the rest of it
|
|
* is still sitting unread in the stream. Refusing here is what makes that
|
|
* true -- without it, the unread remainder is picked up by the *next*
|
|
* readline() as if it were its own statement, which does not fail, it just
|
|
* runs the wrong program. AKBASIC_MAX_LINE_LENGTH is small enough now that
|
|
* this is not a hypothetical: examples/breakout's own longest line used to
|
|
* clear the old 256-byte ceiling by more than half.
|
|
*/
|
|
FAIL_NONZERO_RETURN(errctx, (used == len - 1 && dest[used - 1] != '\n' && dest[used - 1] != '\r'),
|
|
AKBASIC_ERR_BOUNDS,
|
|
"Source line exceeds the %zu character limit", len - 1);
|
|
/*
|
|
* Strip the line terminator. The scanner treats \r and \n as end-of-line
|
|
* anyway, but leaving them on would make a stored source line differ from
|
|
* the same line typed at the REPL.
|
|
*/
|
|
while ( used > 0 && (dest[used - 1] == '\n' || dest[used - 1] == '\r') ) {
|
|
dest[used - 1] = '\0';
|
|
used -= 1;
|
|
}
|
|
SUCCEED_RETURN(errctx);
|
|
}
|
|
|
|
static akerr_ErrorContext *stdio_clear(akbasic_TextSink *self)
|
|
{
|
|
PREPARE_ERROR(errctx);
|
|
|
|
FAIL_ZERO_RETURN(errctx, (self != NULL), AKERR_NULLPOINTER, "NULL sink in clear");
|
|
/* A terminal has no screen to clear that the golden corpus would agree on. */
|
|
SUCCEED_RETURN(errctx);
|
|
}
|
|
|
|
akerr_ErrorContext *akbasic_sink_init_stdio(akbasic_TextSink *obj, akbasic_StdioSink *state, FILE *out, FILE *in)
|
|
{
|
|
PREPARE_ERROR(errctx);
|
|
|
|
FAIL_ZERO_RETURN(errctx, (obj != NULL), AKERR_NULLPOINTER, "NULL sink in init");
|
|
FAIL_ZERO_RETURN(errctx, (state != NULL), AKERR_NULLPOINTER, "NULL sink state in init");
|
|
|
|
state->out = (out != NULL ? out : stdout);
|
|
state->in = (in != NULL ? in : stdin);
|
|
|
|
obj->self = state;
|
|
obj->write = stdio_write;
|
|
obj->writeln = stdio_writeln;
|
|
obj->readline = stdio_readline;
|
|
obj->clear = stdio_clear;
|
|
/*
|
|
* The optional entry points, cleared rather than left alone. A stream has no
|
|
* character grid, so `CHAR` and `WINDOW` must refuse by name -- and they
|
|
* decide that by testing the pointer for NULL. A caller with a sink on the
|
|
* stack would otherwise hand them whatever was in that memory.
|
|
*/
|
|
obj->moveto = NULL;
|
|
obj->window = NULL;
|
|
obj->grid = NULL;
|
|
obj->graphic = NULL;
|
|
SUCCEED_RETURN(errctx);
|
|
}
|