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

@@ -32,11 +32,14 @@
* deps/libakgl/tests/draw.c does.
*/
#include <akgl/game.h>
#include <akgl/heap.h>
#include <akgl/registry.h>
#include <akgl/renderer.h>
#include <akgl/text.h>
#include <akbasic/akgl.h>
#include <akbasic/error.h>
#include <akbasic/sprite.h>
#include <akbasic/runtime.h>
#include <akbasic/sink.h>
@@ -54,6 +57,8 @@ static akbasic_TextSink AKGLSINK;
static akbasic_GraphicsBackend GRAPHICS;
static akbasic_AkglGraphics GRAPHICSSTATE;
static akbasic_InputBackend INPUT;
static akbasic_SpriteBackend SPRITES;
static akbasic_AkglSprites SPRITESSTATE;
static TTF_Font *font = NULL;
static char OUTPUT[8192];
static FILE *OUT = NULL;
@@ -100,7 +105,8 @@ static akerr_ErrorContext AKERR_NOIGNORE *start_runtime(const char *source)
PASS(errctx, akbasic_sink_init_stdio(&SINK, &SINKSTATE, OUT, NULL));
PASS(errctx, akbasic_runtime_init(&RUNTIME, &SINK));
PASS(errctx, akbasic_graphics_init_akgl(&GRAPHICS, &GRAPHICSSTATE, renderer));
PASS(errctx, akbasic_runtime_set_devices(&RUNTIME, &GRAPHICS, NULL, NULL));
PASS(errctx, akbasic_sprite_init_akgl(&SPRITES, &SPRITESSTATE, renderer, &GRAPHICSSTATE));
PASS(errctx, akbasic_runtime_set_devices(&RUNTIME, &GRAPHICS, NULL, NULL, &SPRITES));
PASS(errctx, akbasic_runtime_load(&RUNTIME, source));
PASS(errctx, akbasic_runtime_start(&RUNTIME, AKBASIC_MODE_RUN));
PASS(errctx, akbasic_runtime_run(&RUNTIME, 0));
@@ -335,6 +341,130 @@ static akerr_ErrorContext AKERR_NOIGNORE *test_input_backend(void)
SUCCEED_RETURN(errctx);
}
/**
* @brief A sprite loaded from an image file lands on the target where MOVSPR put it.
*
* The one assertion in this repository that exercises the whole file path:
* a BASIC string that is not an SSHAPE handle, resolved by akgl_path_relative()
* against the working directory, decoded by SDL_image inside
* akgl_spritesheet_initialize(), and blitted through the actor's renderfunc.
*
* The fixture is 8x8 rather than 24x21 on purpose -- a sprite from a file takes
* the image's own size, and an assertion at (43, 33) rather than (40, 30) is
* what says the size came from the image rather than from a constant.
*/
static akerr_ErrorContext AKERR_NOIGNORE *test_sprite_from_file(void)
{
PREPARE_ERROR(errctx);
SDL_Surface *shot = NULL;
PASS(errctx, clear_target());
PASS(errctx, start_runtime("10 SPRSAV \"assets/sprite8x8.png\", 1\n"
"20 SPRITE 1, 1\n"
"30 MOVSPR 1, 40, 30\n"));
TEST_REQUIRE_STR(OUTPUT, "");
PASS(errctx, akbasic_sprite_akgl_render(&SPRITES));
shot = SDL_RenderReadPixels(renderer->sdl_renderer, NULL);
TEST_REQUIRE(shot != NULL, "could not read the render target back");
/*
* Magenta, not white: SPRITE's colour argument modulates the texture and
* this program never gave one, so the image's own colour comes through.
*/
TEST_REQUIRE(pixel_is(shot, 43, 33, 0xff, 0x00, 0xff),
"the loaded sprite should be drawn at MOVSPR's coordinate");
TEST_REQUIRE(pixel_is(shot, 47, 37, 0xff, 0x00, 0xff),
"the whole 8x8 image should have been drawn, not one pixel of it");
TEST_REQUIRE(!pixel_is(shot, 49, 39, 0xff, 0x00, 0xff),
"an 8x8 sprite should stop after eight pixels");
SDL_DestroySurface(shot);
/* A path that names nothing is the program's mistake, and is reported. */
stop_runtime();
PASS(errctx, start_runtime("10 SPRSAV \"assets/nosuchfile.png\", 1\n"));
TEST_REQUIRE(strstr(OUTPUT, "No such sprite image") != NULL,
"a missing image should be reported by name, got \"%s\"", OUTPUT);
stop_runtime();
SUCCEED_RETURN(errctx);
}
/**
* @brief A sprite defined from 63 pattern bytes reaches real pixels, in its colour.
*
* The DATA-driven path a type-in listing uses, end to end: an integer array, the
* bit unpacking, a surface built a pixel at a time, and a texture. The pattern
* is one set bit in the top-left corner, so what is asserted is both that the
* pixel is lit *and* that its neighbour is not -- which is what catches a bit
* order that came through reversed.
*/
static akerr_ErrorContext AKERR_NOIGNORE *test_sprite_from_pattern(void)
{
PREPARE_ERROR(errctx);
SDL_Surface *shot = NULL;
PASS(errctx, clear_target());
PASS(errctx, start_runtime("10 DIM P#(63)\n"
"20 P#(0) = 128\n"
"30 SPRSAV P#, 1\n"
"40 SPRITE 1, 1, 6\n"
"50 MOVSPR 1, 20, 20\n"));
TEST_REQUIRE_STR(OUTPUT, "");
PASS(errctx, akbasic_sprite_akgl_render(&SPRITES));
shot = SDL_RenderReadPixels(renderer->sdl_renderer, NULL);
TEST_REQUIRE(shot != NULL, "could not read the render target back");
/* Palette index 6 is green: 0x55, 0xa0, 0x49. */
TEST_REQUIRE(pixel_is(shot, 20, 20, 0x55, 0xa0, 0x49),
"the pattern's one set bit should be the sprite's top-left pixel");
TEST_REQUIRE(!pixel_is(shot, 21, 20, 0x55, 0xa0, 0x49),
"only one bit was set, so only one pixel should be lit");
TEST_REQUIRE(!pixel_is(shot, 43, 20, 0x55, 0xa0, 0x49),
"a reversed bit order would have lit the far end of the row");
SDL_DestroySurface(shot);
stop_runtime();
SUCCEED_RETURN(errctx);
}
/**
* @brief A region SSHAPE saved becomes a sprite, which is the shared-format claim.
*
* The C128 documents SPRSAV's string as the SSHAPE data format at a fixed 24x21,
* so reusing the shape pool here is faithful rather than a shortcut -- and this
* is what says the handle actually survives the trip through a BASIC string and
* into the sprite device.
*/
static akerr_ErrorContext AKERR_NOIGNORE *test_sprite_from_shape(void)
{
PREPARE_ERROR(errctx);
SDL_Surface *shot = NULL;
PASS(errctx, clear_target());
PASS(errctx, start_runtime("10 COLOR 1, 3\n"
"20 BOX 1, 0, 0, 10, 10\n"
"30 SSHAPE A$, 0, 0, 10, 10\n"
"40 SPRSAV A$, 1\n"
"50 SPRITE 1, 1\n"
"60 MOVSPR 1, 60, 60\n"));
TEST_REQUIRE_STR(OUTPUT, "");
PASS(errctx, akbasic_sprite_akgl_render(&SPRITES));
shot = SDL_RenderReadPixels(renderer->sdl_renderer, NULL);
TEST_REQUIRE(shot != NULL, "could not read the render target back");
/*
* Palette index 3 is red, which is what the BOX was outlined in. The corner
* of the box is the (0,0) pixel of the saved region, so it lands exactly
* where MOVSPR put the sprite -- and its interior does not, which is what
* says a region was copied rather than a rectangle drawn.
*/
TEST_REQUIRE(pixel_is(shot, 60, 60, 0x88, 0x39, 0x32),
"the saved region's corner should have been drawn as a sprite");
TEST_REQUIRE(!pixel_is(shot, 65, 65, 0x88, 0x39, 0x32),
"the region was an outline, so its middle should not be red");
SDL_DestroySurface(shot);
stop_runtime();
SUCCEED_RETURN(errctx);
}
int main(void)
{
PREPARE_ERROR(errctx);
@@ -366,6 +496,20 @@ int main(void)
*/
CATCH(errctx, akgl_render_bind2d(renderer));
/*
* The three things akgl_game_init() would have done and this file, being
* a host that deliberately never calls it, has to do itself: the object
* pools, the name registries, and a camera for akgl_actor_render() to
* dereference. src/frontend_akgl.c does the same, and says why at length.
*/
CATCH(errctx, akgl_heap_init());
CATCH(errctx, akgl_registry_init());
camera = &_akgl_camera;
camera->x = 0.0f;
camera->y = 0.0f;
camera->w = (float)TARGET_SIZE;
camera->h = (float)TARGET_SIZE;
/*
* libakgl's own monospaced fixture, borrowed rather than copied. Being
* monospaced is what makes a character grid meaningful to assert on.
@@ -381,6 +525,9 @@ int main(void)
CATCH(errctx, test_sink_grid_and_wrap());
CATCH(errctx, test_sink_renders());
CATCH(errctx, test_input_backend());
CATCH(errctx, test_sprite_from_file());
CATCH(errctx, test_sprite_from_pattern());
CATCH(errctx, test_sprite_from_shape());
} CLEANUP {
if ( font != NULL ) {
TTF_CloseFont(font);