Finish the language: every remaining verb group, and the defects that blocked them

Closes groups A, C, D, E, F, H and J of TODO.md section 4, plus RESTORE and
RENUMBER, and closes section 6 -- all seventeen reference defects. Seven of
those turned out to have been fixed or never ported and nobody had written it
down; the audit records the evidence for each.

Two of the seventeen were real. math_plus mutated its left operand when the
operand was mutable, so A# + 1 could modify A#; it was gated on FOR/NEXT
coverage because NEXT relied on the mutation, so tests/for_next.c came first
and NEXT now writes the counter back itself. And the binary operators summed
both numeric fields of their right operand, which no BASIC program can reach
-- that one needed a test written against the value API.

Writing the tests turned up eight defects nobody had listed. Seven are fixed:

  IF A = 2 THEN was a parse error; only == worked
  IF ... AND ... was a parse error, because a condition parsed as one relation
  IF A = 1 OR B = 2 THEN was silently always false, and so was IF A THEN
  EXIT before any NEXT restarted the program and exhausted the variable pool
  READ never found a DATA line above it, and swallowed the lines between
  PRINT 2 + 2 at the prompt was filed as program text instead of answering
  a short read discarded its bytes, so COPY produced empty files
  every verb taking an argument list said "peek() returned nil token!" on none

The eighth is not fixed and cannot be quietly: a FOR whose step overshoots
runs its body one extra time, and FOR I = 1 TO 1 runs it zero times. The two
errors cancel for a step of 1, which is why neither was noticed. Correcting
them changes the expected output of a checked-in acceptance file, and
tests/reference/README.md forbids editing one to suit this interpreter. It is
tests/for_semantics.c in AKBASIC_KNOWN_FAILING_TESTS, asserting the correct
contract, and TODO.md items 19 and 20.

Sprites are real libakgl actors with a renderfunc of their own, because
akgl_actor_render draws every sprite square and an actor has no per-axis
scale. Both are filed upstream. SPRSAV takes an image file, an SSHAPE handle
or a 63-element integer array -- a string here cannot hold a zero byte.

Verbs that need hardware that does not exist are refused by name with the
reason rather than faked: SYS, HEADER, COLLECT, BACKUP, BOOT, FILTER, and
DIRECTORY, which is refused for a missing libakstdlib wrapper filed upstream.

94 tests in the default build, 93 with SDL, 94 under ASan and UBSan, doxygen
clean. The Go acceptance corpus stayed green throughout.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-07-31 21:50:37 -04:00
parent 3aade4947a
commit 9845e77a5c
87 changed files with 11024 additions and 325 deletions

View File

@@ -28,9 +28,16 @@
#include <akerror.h>
#include <akgl/actor.h>
#include <akgl/character.h>
#include <akgl/controller.h>
#include <akgl/draw.h>
#include <akgl/error.h>
/* game.h for the `camera` global, which akgl_actor_render() reads. */
#include <akgl/game.h>
#include <akgl/heap.h>
#include <akgl/registry.h>
#include <akgl/sprite.h>
#include <akbasic/error.h>
#include <akbasic/frontend.h>
@@ -832,6 +839,128 @@ static akerr_ErrorContext AKERR_NOIGNORE *test_repl_session(void)
SUCCEED_RETURN(errctx);
}
/**
* @brief The frontend leaves libakgl able to draw an actor, which is what a sprite is.
*
* Three things the host owes libakgl and that akgl_game_init() would normally
* have done -- the object pools, the name registries, and the camera. Nothing in
* the interpreter needed any of them until sprites, because text and the drawing
* primitives go straight at the renderer; an actor goes through all three.
*
* Asserted by building one actor by hand and drawing it, rather than by reading
* the globals back. Without akgl_registry_init() the actor cannot be named and
* akgl_actor_initialize raises AKERR_KEY; without akgl_heap_init() there is no
* pool to take it from; without a camera akgl_actor_render() dereferences NULL
* and the test does not report a failure so much as stop existing.
*/
static akerr_ErrorContext AKERR_NOIGNORE *test_host_can_draw_an_actor(void)
{
PREPARE_ERROR(errctx);
SDL_Surface *pattern = NULL;
SDL_Surface *shot = NULL;
SDL_Texture *texture = NULL;
akgl_SpriteSheet *sheet = NULL;
akgl_Sprite *sprite = NULL;
akgl_Character *basechar = NULL;
akgl_Actor *actor = NULL;
/*
* 128 bytes each, not string literals: akgl_sprite_initialize() and
* akgl_actor_initialize() memcpy a fixed AKGL_*_MAX_NAME_LENGTH from what
* they are handed, so a shorter buffer is read past its end.
*/
char sheetname[AKGL_SPRITE_SHEET_MAX_FILENAME_LENGTH];
char spritename[AKGL_SPRITE_MAX_NAME_LENGTH];
char charname[AKGL_SPRITE_MAX_NAME_LENGTH];
char actorname[AKGL_ACTOR_MAX_NAME_LENGTH];
PASS(errctx, start_frontend(NULL));
TEST_REQUIRE(camera != NULL, "the frontend should have pointed the camera somewhere");
TEST_REQUIRE_INT((int)camera->w, WINDOW_W);
TEST_REQUIRE_INT((int)camera->h, WINDOW_H);
TEST_REQUIRE(AKGL_REGISTRY_ACTOR != 0, "the frontend should have created the registries");
memset(sheetname, 0, sizeof(sheetname));
memset(spritename, 0, sizeof(spritename));
memset(charname, 0, sizeof(charname));
memset(actorname, 0, sizeof(actorname));
SDL_strlcpy(sheetname, "akbasic:test:sheet", sizeof(sheetname));
SDL_strlcpy(spritename, "akbasic:test:sprite", sizeof(spritename));
SDL_strlcpy(charname, "akbasic:test:character", sizeof(charname));
SDL_strlcpy(actorname, "akbasic:test:actor", sizeof(actorname));
/* A solid 8x8 white square, built in memory rather than loaded from a file. */
pattern = SDL_CreateSurface(8, 8, SDL_PIXELFORMAT_RGBA32);
TEST_REQUIRE(pattern != NULL, "could not create the pattern surface: %s", SDL_GetError());
TEST_REQUIRE(SDL_FillSurfaceRect(pattern, NULL,
SDL_MapSurfaceRGBA(pattern, 0xff, 0xff, 0xff, 0xff)),
"could not fill the pattern surface: %s", SDL_GetError());
texture = SDL_CreateTextureFromSurface(FRONTEND.renderer->sdl_renderer, pattern);
SDL_DestroySurface(pattern);
TEST_REQUIRE(texture != NULL, "could not upload the pattern: %s", SDL_GetError());
/*
* The sheet is assembled rather than initialized: akgl_spritesheet_initialize
* loads an image from a path, and there is no file here. Everything below it
* is the ordinary API.
*/
PASS(errctx, akgl_heap_next_spritesheet(&sheet));
memset(sheet, 0, sizeof(*sheet));
sheet->texture = texture;
SDL_strlcpy(sheet->name, sheetname, sizeof(sheet->name));
sheet->refcount = 1;
PASS(errctx, akgl_heap_next_sprite(&sprite));
PASS(errctx, akgl_sprite_initialize(sprite, spritename, sheet));
sprite->frames = 1;
sprite->frameids[0] = 0;
sprite->width = 8;
sprite->height = 8;
PASS(errctx, akgl_heap_next_character(&basechar));
PASS(errctx, akgl_character_initialize(basechar, charname));
PASS(errctx, akgl_character_sprite_add(basechar, sprite, 0));
PASS(errctx, akgl_heap_next_actor(&actor));
PASS(errctx, akgl_actor_initialize(actor, actorname));
PASS(errctx, akgl_actor_set_character(actor, charname));
actor->state = 0;
actor->visible = true;
actor->x = 40.0f;
actor->y = 24.0f;
/* The text layer paints every cell it owns, so draw the actor over it. */
PASS(errctx, akbasic_sink_akgl_render(&FRONTEND.akglsink));
PASS(errctx, akgl_actor_render(actor));
shot = SDL_RenderReadPixels(FRONTEND.renderer->sdl_renderer, NULL);
TEST_REQUIRE(shot != NULL, "could not read the render target back");
TEST_REQUIRE(anything_drawn(shot, 42, 26, 2, 2),
"the actor should have been drawn at its own coordinates");
SDL_DestroySurface(shot);
stop_frontend();
/*
* And the pools start over. akgl_heap_init() is not needed to make the
* *first* frontend work -- the pool arrays are file-scope and start zeroed --
* so the only thing that shows it ran is a second frontend handing back the
* slot the first one was still holding. Without it the actor above survives
* SDL_Quit with a live refcount, pointing at a texture that no longer exists,
* and the next program's sprites are allocated around the corpse.
*/
{
akgl_Actor *reused = NULL;
PASS(errctx, start_frontend(NULL));
PASS(errctx, akgl_heap_next_actor(&reused));
TEST_REQUIRE(reused == actor,
"a new frontend should hand back the released pool slot, not the one after it");
stop_frontend();
}
SUCCEED_RETURN(errctx);
}
/** @brief Every entry point validates its pointers before touching SDL. */
static akerr_ErrorContext AKERR_NOIGNORE *test_arguments_refused(void)
{
@@ -880,6 +1009,7 @@ int main(void)
CATCH(errctx, test_block_cursor_blinks());
CATCH(errctx, test_frame_owns_every_pixel());
CATCH(errctx, test_repl_session());
CATCH(errctx, test_host_can_draw_an_actor());
} CLEANUP {
stop_frontend();
} PROCESS(errctx) {