2026-07-31 12:07:50 -04:00
|
|
|
/**
|
|
|
|
|
* @file runtime_housekeeping.c
|
|
|
|
|
* @brief The housekeeping verbs: NEW, CLR, CONT, TRON, TROFF, SWAP and HELP.
|
|
|
|
|
*
|
|
|
|
|
* TODO.md section 4 group B. None of these is in the Go reference -- they are
|
|
|
|
|
* on its own "What Isn't Implemented" list -- so what they mean here is a
|
|
|
|
|
* decision rather than a transcription, and each decision is recorded at its
|
|
|
|
|
* verb. Where a C128's answer needs machine state this interpreter does not
|
|
|
|
|
* have, the verb does the part that means something and says so.
|
|
|
|
|
*
|
|
|
|
|
* None of them needs a device, and none is in the golden corpus's way: they all
|
|
|
|
|
* act on interpreter state a program can see through PRINT.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
|
|
|
|
|
#include <akerror.h>
|
Port onto libakstdlib 2b79aca and convert the eight bool predicates
akbasic's src/ now calls libakstdlib 313 times and raw libc 7 -- 2.2%
bypassed, against 86.4% on the same tree before this. The submodule bump
669b2b3 -> 2b79aca needed no source change of its own: the release is
drop-in for what akbasic already used.
Seven of the eight sites the earlier port left on raw libc change their own
signature rather than swallowing an error, per andrew's ruling on
libakstdlib#38. word_is, the is_waiting_for pair, the scanner's is_at_end,
peek, peek_next and match_next_char, format.c's overflow, and sink_akgl's
scroll/newline/putchar_at/echo_line/edit_key chain all return an
akerr_ErrorContext * and hand the answer back through an out parameter.
is_waiting_for and is_waiting_for_any are a public header change; every
call site that used one as a term in a condition hoists it into a
statement first.
verb_compare is the eighth and stays on strcmp. bsearch(3) fixes the
comparator's signature, so there is no out parameter to report through --
which is what libakstdlib#38 concluded. It carries a comment saying so and
saying why the bypass is safe there.
Six snprintf sites stay raw because they want truncation as an answer
rather than an error, and aksl_snprintf cannot express that until
libakstdlib#34 hands the required length back. Each of the six says so at
the site. Two of them, in host.c, are a latent defect rather than a
decision: a host type name over 31 characters truncates silently and two
sharing a prefix then collide, where structtype.c refuses the same case.
DLOAD leaked a file descriptor. Its read loop sat inside an ATTEMPT and the
PASS in it returned past CLEANUP, so a scan error left the file open.
Hoisting the loop into its own helper to convert fgets fixes it.
Refs libakstdlib#26, libakstdlib#38
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-03 15:41:49 -04:00
|
|
|
#include <akstdlib.h>
|
2026-07-31 12:07:50 -04:00
|
|
|
|
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>
Co-Authored-By: Andrew Kesterson <andrew@aklabs.net>
2026-07-31 21:50:37 -04:00
|
|
|
#include <akbasic/args.h>
|
2026-07-31 12:07:50 -04:00
|
|
|
#include <akbasic/error.h>
|
|
|
|
|
#include <akbasic/runtime.h>
|
|
|
|
|
|
|
|
|
|
#include "verbs.h"
|
|
|
|
|
|
|
|
|
|
/* Most verbs answer "did something happen"; this is that answer. */
|
|
|
|
|
#define SUCCEED_TRUE(__obj, __dest) \
|
|
|
|
|
do { \
|
|
|
|
|
*(__dest) = &(__obj)->staticTrueValue; \
|
|
|
|
|
} while ( 0 )
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Drop every variable and function, and hand their storage back.
|
|
|
|
|
*
|
|
|
|
|
* Shared by NEW and CLR, which differ only in whether the program text goes
|
|
|
|
|
* too. The value pool is a bump allocator (see akbasic_ValuePool), so this is
|
|
|
|
|
* the only place anything it handed out ever comes back -- re-initializing the
|
|
|
|
|
* pool is what makes `CLR` in a loop viable where re-DIMming in one is not.
|
|
|
|
|
*/
|
|
|
|
|
static akerr_ErrorContext AKERR_NOIGNORE *clear_variables(akbasic_Runtime *obj)
|
|
|
|
|
{
|
|
|
|
|
PREPARE_ERROR(errctx);
|
|
|
|
|
akbasic_Environment *root = NULL;
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
|
|
for ( i = 0; i < AKBASIC_MAX_VARIABLES; i++ ) {
|
|
|
|
|
obj->variables[i].used = false;
|
|
|
|
|
}
|
|
|
|
|
for ( i = 0; i < AKBASIC_MAX_FUNCTIONS; i++ ) {
|
|
|
|
|
obj->functions[i].used = false;
|
|
|
|
|
}
|
|
|
|
|
PASS(errctx, akbasic_valuepool_init(&obj->valuepool));
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Unwind to the root before emptying its symbol tables. A CLR inside a
|
|
|
|
|
* GOSUB would otherwise leave child environments holding pointers into the
|
|
|
|
|
* variable pool it just released, and the parent chain is the only handle
|
|
|
|
|
* on them.
|
|
|
|
|
*/
|
|
|
|
|
while ( obj->environment != NULL && obj->environment->parent != NULL ) {
|
|
|
|
|
PASS(errctx, akbasic_runtime_prev_environment(obj));
|
|
|
|
|
}
|
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>
Co-Authored-By: Andrew Kesterson <andrew@aklabs.net>
2026-07-31 21:50:37 -04:00
|
|
|
/*
|
|
|
|
|
* That unwind may have released the environment an interrupt handler was
|
|
|
|
|
* running in. Leaving the pointer behind would wedge interrupts off for the
|
|
|
|
|
* rest of the session, since nothing would ever pop the environment it
|
|
|
|
|
* compares against again.
|
|
|
|
|
*/
|
|
|
|
|
obj->handlerenv = NULL;
|
2026-07-31 12:07:50 -04:00
|
|
|
root = obj->environment;
|
|
|
|
|
FAIL_ZERO_RETURN(errctx, (root != NULL), AKERR_NULLPOINTER, "Runtime has no root environment");
|
|
|
|
|
PASS(errctx, akbasic_symtab_init(&root->variables, AKBASIC_MAX_VARIABLES));
|
|
|
|
|
PASS(errctx, akbasic_symtab_init(&root->functions, AKBASIC_MAX_FUNCTIONS));
|
Enter a TRAP handler when the variable table is full
Two separable faults, both on the path a program takes when it is already in
trouble.
`akbasic_trap_set_error_variables()` reached `ER#` and `EL#` through
`akbasic_runtime_global()`, which *creates* a name the program never used -- and
creating one takes a variable slot. So a program that had filled the 128-slot
table could not have its handler entered at all, and because the failure
happened inside the error path rather than raising, nothing was reported and the
program carried on with the failing statement's effect quietly missing. A wrong
answer delivered as a right one, which is worse than an abort.
`akbasic_runtime_reserve_globals()` now creates both at runtime init, where
there is always room, and `clear_variables()` puts them back after `CLR` and
`NEW` empty the table.
Second: `report_and_reraise()` used a plain `PASS` around the report, so a
failure while reporting *replaced* the error the program had actually made --
"Maximum runtime variables reached" in place of the subscript that was out of
range. The secondary failure is now logged and the original is re-raised, which
is what a user needs to hear.
**The reduction in TODO.md no longer reproduces, and not because of this.** The
value-pool fix in the previous commit made a scalar free, so the pool can no
longer be emptied by creating names. The defect was still live through the
variable table: 124 names and a TRAP armed, and the handler was silently
skipped. That is what the new test in tests/trap_verbs.c pins, together with the
invariant -- both globals present before a program runs.
Verified by reverting the fix against the new tests: both fail, and the second
prints the log line the swallow used to eat, "could not report a BASIC error 515
(Out Of Bounds): Maximum runtime variables reached".
TODO.md section 6 item 33, struck.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Andrew Kesterson <andrew@aklabs.net>
2026-08-01 23:48:56 -04:00
|
|
|
/*
|
|
|
|
|
* Put the reserved globals back. They were in the table this just emptied,
|
|
|
|
|
* and the `TRAP` dispatch relies on never having to create them -- so a CLR
|
|
|
|
|
* that left them gone would reopen item 33 for the rest of the session.
|
|
|
|
|
*/
|
|
|
|
|
PASS(errctx, akbasic_runtime_reserve_globals(obj));
|
2026-07-31 12:07:50 -04:00
|
|
|
SUCCEED_RETURN(errctx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------- NEW --- */
|
|
|
|
|
|
|
|
|
|
akerr_ErrorContext *akbasic_cmd_new(akbasic_Runtime *obj, akbasic_ASTLeaf *expr, akbasic_Value *lval, akbasic_Value *rval, akbasic_Value **dest)
|
|
|
|
|
{
|
|
|
|
|
PREPARE_ERROR(errctx);
|
|
|
|
|
int64_t i = 0;
|
|
|
|
|
|
|
|
|
|
(void)expr; (void)lval; (void)rval;
|
|
|
|
|
FAIL_ZERO_RETURN(errctx, (obj != NULL && dest != NULL), AKERR_NULLPOINTER, "NULL argument in NEW");
|
|
|
|
|
|
|
|
|
|
for ( i = 0; i < AKBASIC_MAX_SOURCE_LINES; i++ ) {
|
|
|
|
|
obj->source[i].code[0] = '\0';
|
|
|
|
|
obj->source[i].lineno = 0;
|
Give a loaded line a number when it arrives without one
A script written against LABEL and GOTO NAME never names a line number, so
the numbers it had to carry were decoration. akbasic_runtime_load(),
RUNSTREAM and DLOAD now file an unnumbered line one slot after the last one
filed; a numbered line is filed under its number and moves the cursor, so
the two mix. akbasic_runtime_file_line() is the one implementation of that
rule, so the three paths cannot drift.
The prompt is untouched. A line typed without a number is still direct mode
and still runs now -- that is the only thing separating program text from a
statement at a REPL, and it is why this is a loading feature.
What this replaces was silent data loss: an unnumbered line was filed under
the cursor unchanged, on top of the line before it. A blank line therefore
erased whatever preceded it, and RUNSTREAM did not skip blank lines the way
the other two paths did.
That moves one golden file, and the reference had the same defect.
language/arithmetic/integer.bas has four PRINT statements, an expectation
with three values, and a trailing blank line that erased 40 PRINT 4 - 2
before the program ran. The expectation is now 4 4 2 2.
tests/reference/README.md records the divergence and TODO.md section 5 item
63 says why.
akbasic_SourceLine grows a `numbered` flag so an assigned number can be told
from a written one. RENUMBER sets it on every line it touches; NEW, DELETE
and DLOAD clear it. hadlinenumber moves to akbasic_scanner_scan(), so it
always describes the line just scanned rather than only the REPL's.
Two lines carrying the same written number still keep the last, as they
always have. That is a separate decision.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Andrew Kesterson <andrew@aklabs.net>
2026-08-01 16:33:27 -04:00
|
|
|
obj->source[i].numbered = false;
|
2026-07-31 12:07:50 -04:00
|
|
|
}
|
|
|
|
|
PASS(errctx, clear_variables(obj));
|
|
|
|
|
PASS(errctx, akbasic_symtab_init(&obj->environment->labels, AKBASIC_MAX_LABELS));
|
|
|
|
|
|
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>
Co-Authored-By: Andrew Kesterson <andrew@aklabs.net>
2026-07-31 21:50:37 -04:00
|
|
|
/* A new program does not inherit the old one's open files. */
|
|
|
|
|
PASS(errctx, akbasic_disk_close_all(&obj->disk_state));
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Disarm everything. The handlers were lines of the program that has just
|
|
|
|
|
* been deleted, so an interrupt left armed would send the *next* program
|
|
|
|
|
* into whatever happens to be at that line number.
|
|
|
|
|
*/
|
|
|
|
|
for ( i = 0; i < AKBASIC_MAX_INTERRUPTS; i++ ) {
|
|
|
|
|
PASS(errctx, akbasic_runtime_disarm_interrupt(obj, (akbasic_InterruptSource)i));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* And the sprites go back to undefined. The patterns belonged to the program
|
|
|
|
|
* that has just been deleted. The *device* still holds them -- there is no
|
|
|
|
|
* verb that undefines a sprite and so no entry point to say so -- but every
|
|
|
|
|
* one of them is hidden, which is the observable half.
|
|
|
|
|
*/
|
|
|
|
|
PASS(errctx, akbasic_sprite_state_init(&obj->sprite_state));
|
|
|
|
|
if ( obj->sprites != NULL && obj->sprites->show != NULL ) {
|
|
|
|
|
for ( i = 0; i < AKBASIC_MAX_SPRITES; i++ ) {
|
|
|
|
|
PASS(errctx, obj->sprites->show(obj->sprites, (int)i + 1, false));
|
|
|
|
|
}
|
|
|
|
|
}
|
Collide sprites with rectangles that are not sprites
`SOLID id, x1, y1, x2, y2` registers static collision geometry; `SOLID id`
retires one and a bare `SOLID` retires them all, the way `TRAP`, `COLLISION` and
`DCLOSE` all read absence. `COLLISION 2` and `BUMP(2)` stop being refused and
mean *sprite met static geometry*.
**This is the thing eight sprite slots made impossible.** A wall of bricks wants
sixty, so until now a program could only collide with one by doing the
arithmetic itself against its own array -- which is exactly what both breakout
listings do, at about two hundred lines between them. A rectangle costs no sprite
slot.
The id is the **program's own number**, 1 to 64, not a minted handle. That is the
whole trick for "which brick did I hit": the id comes back out again, so a wall
built as `SOLID I#, ...` maps onto `B#(I#)` with no lookup, and retiring a broken
brick is `SOLID I#`.
`COLLISION 2` was refused with "sprite-to-background collision needs the screen
read back every frame", which was true of the question a C128 asks -- a sprite
against the bitmap's set pixels. `SOLID` gives this interpreter a background made
of rectangles instead, which is the same question in a form it can answer. Same
move `SPRSAV` made when it learned to take an image path.
`AKBASIC_INTERRUPT_BACKGROUND` has been sitting in the interrupt table commented
"COLLISION 2 -- sprite met background; refused" the whole time. Its accumulator is
separate, so a sprite hitting a wall never sets a bit in `BUMP(1)`.
**There is no `akgl_CollisionWorld` here, and that is deliberate.** libakgl's
uniform grid keeps its cell heads, cell size and origin in file-scope statics, so
it is one index per process -- and `akgl_collision_world_init()` ends in a
`reset()` that memsets those heads *and* calls
`akgl_heap_init_collision_cells()`. An interpreter embedded in a game with its
own collision world would have destroyed every registration that game had made,
on the first `SOLID` a script ran. So the geometry is indexed by an ordinary
array here and pairs go straight to `akgl_collision_test()`, which needs no
world. At sixty-four rectangles that is the right answer anyway; libakgl's own
numbers put a naive sweep at 0.7% of a frame at sixty-four objects.
**The scan now short-circuits when nothing has moved**, and that is what makes
any of it affordable. Its inputs are the sprites' boxes, which slots are
collidable, and the static geometry; if none changed the answer cannot have. A
frame runs one full scan and 255 cached ones. Eight sprites against sixty-four
rectangles is five hundred and twelve tests -- fine once a frame, ruinous 256
times.
The benchmark was rewritten to say which path it is timing, because with the
cache in place a loop that only calls the scan measures the short circuit and
nothing else. Breakout now costs 590.6 ns for its one full scan plus 255 cached
at 40.0, which is 10.8 us against a 1.19 ms frame -- **0.91%, less than the 2.0%
it cost before any of this work**, with static geometry and contacts added on
top.
`NEW` retires the rectangles, where it cannot undefine a sprite pattern: there
*is* an entry point for this one, so leaving them would be a choice, and the
wrong one -- a rectangle is invisible, so one left behind by a deleted program is
an unexplainable collision in the next. `CLR` leaves them alone.
`tests/sprite_verbs.c` gains the whole second path against the mock and its
`COLLISION 2` case is rewritten: it pinned the refusal, and now pins that type 2
arms its own handler without disturbing type 1's. `tests/akgl_backends.c` gains
the end-to-end version, including a full sixty-four-rectangle wall so the proxy
budget is exercised at its ceiling and the pool has to come back intact, and the
sixty-fifth refused by name.
A bare `SOLID` needed `akbasic_parse_optional_arglist` rather than
`akbasic_parse_arglist`, which `DCLOSE` already uses for the same shape.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EwxGB6TdoVvZ11KQQME9cL
Co-Authored-By: Andrew Kesterson <andrew@aklabs.net>
2026-08-02 10:25:35 -04:00
|
|
|
/*
|
|
|
|
|
* **Static geometry does go back to the device**, where a sprite pattern
|
|
|
|
|
* cannot. There is an entry point that retires one, so leaving them would be
|
|
|
|
|
* a choice rather than a limitation -- and the wrong choice: a wall is
|
|
|
|
|
* invisible, so one left behind by a deleted program is an unexplainable
|
|
|
|
|
* collision in the next.
|
|
|
|
|
*/
|
|
|
|
|
if ( obj->sprites != NULL && obj->sprites->solid != NULL ) {
|
|
|
|
|
for ( i = 0; i < AKBASIC_MAX_SOLIDS; i++ ) {
|
|
|
|
|
PASS(errctx, obj->sprites->solid(obj->sprites, (int)i + 1, false, 0.0, 0.0, 0.0, 0.0));
|
|
|
|
|
}
|
|
|
|
|
}
|
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>
Co-Authored-By: Andrew Kesterson <andrew@aklabs.net>
2026-07-31 21:50:37 -04:00
|
|
|
|
Give BASIC menus, dialogs and HUD labels over libakgl's UI helpers
Group K, and the first verbs to reach the akgl_ui subsystem 0.9.0 brought
in: MENU and GETMENU and RMENU, DIALOG, HUD and UISTYLE. A program that
wanted a title screen had to draw one out of CHAR and GETKEY, which is
what both breakout tutorials make a reader do.
The interesting part is the impedance mismatch. libakgl's UI is immediate
mode -- widgets are re-declared inside a frame bracket every frame and
clay borrows their text until the bracket closes -- and a BASIC program
says MENU 1, "START" on line 100 and expects it up on line 900, several
hundred frames later. So src/ui_akgl.c is retained on this side and
immediate on that one: the record's entry points are setters that copy
into akbasic_AkglUi, and akbasic_ui_akgl_render() replays the whole set
once a frame from the host's pump. No BASIC string, which lives in the
per-line value pool, is ever what clay is handed.
The shapes are borrowed rather than invented. MENU retires the way SOLID
does -- no entries retires one, no arguments retire them all. GETMENU
holds the step loop the way GETKEY does, so parking is not blocking: the
step still returns, the host keeps its frame rate, and the sprite, audio
and collision services keep running underneath because they run before
the blocking checks. RMENU(n,1) reads and clears the way BUMP() does.
Withdrawing the device or retiring the menu releases a holding GETMENU
with 0 rather than wedging the script, which is akbasic_input_service()'s
rule for a withdrawn keyboard.
One thing a program has to know, and docs/19-user-interface.md says it
twice: a menu that is up owns the cursor keys and Return. It has to, and
retiring it gives them back -- forget the MENU n before an INPUT and the
INPUT never sees the Return that ends it.
akbasic_runtime_set_ui() is its own function rather than a fifth argument
to akbasic_runtime_set_devices(), whose signature has twenty-eight call
sites in tests and documentation that are about something else.
deps/libakgl is not touched. akgl_UiAnchor has the four corners and dead
centre, so HUD offers exactly those five; TODO.md records what a
top-centre and bottom-centre would cost upstream, along with the three
other things this deliberately leaves out. No new error code either --
DEVICE, BOUNDS, SYNTAX and TYPE cover the group, and 520 stays free.
tools/screenshot.c had to learn that "needs a font" and "draws the text
grid" are two questions. They were one, and a UI figure came out black:
the text layer owns every pixel of the rows it covers and painted over
the widgets. The new ui=1 fence attribute asks for the first without the
second; MAINTENANCE.md documents it.
112/112 in both configurations, 112/112 under ASan and UBSan, coverage
94.1% against the 90% gate with src/runtime_ui.c at 99% of lines and
100% of functions, doxygen clean, and the four new figures byte-identical
on a re-render. TODO.md section 8's gate table was stale on several
counts besides these and is refreshed with measured numbers.
Co-Authored-By: Tachikoma (Claude Code Opus 5 1M) <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EwxGB6TdoVvZ11KQQME9cL
Co-Authored-By: Andrew Kesterson <andrew@aklabs.net>
2026-08-02 18:37:10 -04:00
|
|
|
/*
|
|
|
|
|
* And every widget comes down. Unlike a sprite there *is* an entry point
|
|
|
|
|
* that undefines these, so this one is complete rather than half: a menu the
|
|
|
|
|
* deleted program put up would otherwise sit on screen eating the cursor
|
|
|
|
|
* keys, with nothing left running that knows how to retire it.
|
|
|
|
|
*/
|
|
|
|
|
PASS(errctx, akbasic_ui_state_init(&obj->ui_state));
|
|
|
|
|
if ( obj->ui != NULL && obj->ui->clear != NULL ) {
|
|
|
|
|
PASS(errctx, obj->ui->clear(obj->ui));
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-31 12:07:50 -04:00
|
|
|
/*
|
|
|
|
|
* The line counter goes home too. Without this a NEW typed at the REPL
|
|
|
|
|
* leaves the next entered line filed under wherever the last program
|
|
|
|
|
* stopped, which is a surprising place for a fresh program to start.
|
|
|
|
|
*/
|
|
|
|
|
obj->environment->lineno = 0;
|
|
|
|
|
obj->environment->nextline = 0;
|
|
|
|
|
obj->stopped = false;
|
|
|
|
|
obj->stoppedline = 0;
|
|
|
|
|
obj->errorline = 0;
|
|
|
|
|
SUCCEED_TRUE(obj, dest);
|
|
|
|
|
SUCCEED_RETURN(errctx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------- CLR --- */
|
|
|
|
|
|
|
|
|
|
akerr_ErrorContext *akbasic_cmd_clr(akbasic_Runtime *obj, akbasic_ASTLeaf *expr, akbasic_Value *lval, akbasic_Value *rval, akbasic_Value **dest)
|
|
|
|
|
{
|
|
|
|
|
PREPARE_ERROR(errctx);
|
|
|
|
|
|
|
|
|
|
(void)expr; (void)lval; (void)rval;
|
|
|
|
|
FAIL_ZERO_RETURN(errctx, (obj != NULL && dest != NULL), AKERR_NULLPOINTER, "NULL argument in CLR");
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Variables and functions, not the program. A C128's CLR also closes open
|
|
|
|
|
* files and empties the GOSUB and FOR stacks; the stacks *are* emptied here,
|
|
|
|
|
* because unwinding to the root is how the variable pool is made safe to
|
|
|
|
|
* release, and there are no open files to close -- DLOAD and DSAVE open,
|
|
|
|
|
* transfer and close within the verb.
|
|
|
|
|
*/
|
|
|
|
|
PASS(errctx, clear_variables(obj));
|
|
|
|
|
SUCCEED_TRUE(obj, dest);
|
|
|
|
|
SUCCEED_RETURN(errctx);
|
|
|
|
|
}
|
|
|
|
|
|
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>
Co-Authored-By: Andrew Kesterson <andrew@aklabs.net>
2026-07-31 21:50:37 -04:00
|
|
|
/* ------------------------------------------------------------ RENUMBER --- */
|
|
|
|
|
|
|
|
|
|
akerr_ErrorContext *akbasic_cmd_renumber(akbasic_Runtime *obj, akbasic_ASTLeaf *expr, akbasic_Value *lval, akbasic_Value *rval, akbasic_Value **dest)
|
|
|
|
|
{
|
|
|
|
|
PREPARE_ERROR(errctx);
|
|
|
|
|
double args[3];
|
|
|
|
|
int count = 0;
|
|
|
|
|
int64_t newstart = 10;
|
|
|
|
|
int64_t increment = 10;
|
|
|
|
|
int64_t oldstart = 0;
|
|
|
|
|
|
|
|
|
|
(void)lval; (void)rval;
|
|
|
|
|
FAIL_ZERO_RETURN(errctx, (obj != NULL && dest != NULL), AKERR_NULLPOINTER,
|
|
|
|
|
"NULL argument in RENUMBER");
|
|
|
|
|
if ( expr != NULL && akbasic_leaf_first_argument(expr) != NULL ) {
|
|
|
|
|
PASS(errctx, akbasic_args_numbers(obj, expr, "RENUMBER", args, 3, &count));
|
|
|
|
|
}
|
|
|
|
|
if ( count >= 1 ) {
|
|
|
|
|
newstart = (int64_t)args[0];
|
|
|
|
|
}
|
|
|
|
|
if ( count >= 2 ) {
|
|
|
|
|
increment = (int64_t)args[1];
|
|
|
|
|
}
|
|
|
|
|
if ( count >= 3 ) {
|
|
|
|
|
oldstart = (int64_t)args[2];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PASS(errctx, akbasic_renumber(obj, newstart, increment, oldstart));
|
|
|
|
|
/*
|
|
|
|
|
* The labels and the DATA items were filed against the old numbering, so
|
|
|
|
|
* both have to be built again. akbasic_runtime_set_mode() does it on the way
|
|
|
|
|
* into RUN, but a program renumbered and then CONTinued would otherwise
|
|
|
|
|
* branch on a stale map.
|
|
|
|
|
*/
|
|
|
|
|
PASS(errctx, akbasic_runtime_scan_labels(obj));
|
|
|
|
|
PASS(errctx, akbasic_data_scan(obj));
|
|
|
|
|
obj->stopped = false;
|
|
|
|
|
obj->stoppedline = 0;
|
|
|
|
|
SUCCEED_TRUE(obj, dest);
|
|
|
|
|
SUCCEED_RETURN(errctx);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-31 12:07:50 -04:00
|
|
|
/* ---------------------------------------------------------------- CONT --- */
|
|
|
|
|
|
|
|
|
|
akerr_ErrorContext *akbasic_cmd_cont(akbasic_Runtime *obj, akbasic_ASTLeaf *expr, akbasic_Value *lval, akbasic_Value *rval, akbasic_Value **dest)
|
|
|
|
|
{
|
|
|
|
|
PREPARE_ERROR(errctx);
|
|
|
|
|
|
|
|
|
|
(void)expr; (void)lval; (void)rval;
|
|
|
|
|
FAIL_ZERO_RETURN(errctx, (obj != NULL && dest != NULL), AKERR_NULLPOINTER, "NULL argument in CONT");
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Refused rather than treated as a RUN. "CAN'T CONTINUE" is what a C128
|
|
|
|
|
* says, and it says it for a reason: continuing a program that never
|
|
|
|
|
* started would run it from line zero with whatever variables happened to be
|
|
|
|
|
* lying about, which is not what anybody typing CONT wanted.
|
|
|
|
|
*/
|
|
|
|
|
FAIL_ZERO_RETURN(errctx, obj->stopped, AKBASIC_ERR_STATE, "CAN'T CONTINUE");
|
|
|
|
|
|
|
|
|
|
obj->environment->nextline = obj->stoppedline;
|
|
|
|
|
obj->stopped = false;
|
|
|
|
|
PASS(errctx, akbasic_runtime_set_mode(obj, AKBASIC_MODE_RUN));
|
|
|
|
|
SUCCEED_TRUE(obj, dest);
|
|
|
|
|
SUCCEED_RETURN(errctx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------- TRON / TROFF --- */
|
|
|
|
|
|
|
|
|
|
akerr_ErrorContext *akbasic_cmd_tron(akbasic_Runtime *obj, akbasic_ASTLeaf *expr, akbasic_Value *lval, akbasic_Value *rval, akbasic_Value **dest)
|
|
|
|
|
{
|
|
|
|
|
PREPARE_ERROR(errctx);
|
|
|
|
|
|
|
|
|
|
(void)expr; (void)lval; (void)rval;
|
|
|
|
|
FAIL_ZERO_RETURN(errctx, (obj != NULL && dest != NULL), AKERR_NULLPOINTER, "NULL argument in TRON");
|
|
|
|
|
obj->trace = true;
|
|
|
|
|
SUCCEED_TRUE(obj, dest);
|
|
|
|
|
SUCCEED_RETURN(errctx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
akerr_ErrorContext *akbasic_cmd_troff(akbasic_Runtime *obj, akbasic_ASTLeaf *expr, akbasic_Value *lval, akbasic_Value *rval, akbasic_Value **dest)
|
|
|
|
|
{
|
|
|
|
|
PREPARE_ERROR(errctx);
|
|
|
|
|
|
|
|
|
|
(void)expr; (void)lval; (void)rval;
|
|
|
|
|
FAIL_ZERO_RETURN(errctx, (obj != NULL && dest != NULL), AKERR_NULLPOINTER, "NULL argument in TROFF");
|
|
|
|
|
obj->trace = false;
|
|
|
|
|
SUCCEED_TRUE(obj, dest);
|
|
|
|
|
SUCCEED_RETURN(errctx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------- SWAP --- */
|
|
|
|
|
|
|
|
|
|
akerr_ErrorContext *akbasic_cmd_swap(akbasic_Runtime *obj, akbasic_ASTLeaf *expr, akbasic_Value *lval, akbasic_Value *rval, akbasic_Value **dest)
|
|
|
|
|
{
|
|
|
|
|
PREPARE_ERROR(errctx);
|
|
|
|
|
akbasic_ASTLeaf *first = NULL;
|
|
|
|
|
akbasic_ASTLeaf *second = NULL;
|
|
|
|
|
akbasic_Variable *a = NULL;
|
|
|
|
|
akbasic_Variable *b = NULL;
|
|
|
|
|
akbasic_Variable swap;
|
|
|
|
|
char namea[AKBASIC_MAX_STRING_LENGTH];
|
|
|
|
|
char nameb[AKBASIC_MAX_STRING_LENGTH];
|
|
|
|
|
|
|
|
|
|
(void)lval; (void)rval;
|
|
|
|
|
FAIL_ZERO_RETURN(errctx, (obj != NULL && expr != NULL && dest != NULL), AKERR_NULLPOINTER,
|
|
|
|
|
"NULL argument in SWAP");
|
|
|
|
|
|
|
|
|
|
first = akbasic_leaf_first_argument(expr);
|
|
|
|
|
second = (first != NULL ? first->next : NULL);
|
|
|
|
|
FAIL_ZERO_RETURN(errctx, (first != NULL && second != NULL), AKBASIC_ERR_SYNTAX,
|
|
|
|
|
"Expected SWAP VARIABLE, VARIABLE");
|
|
|
|
|
FAIL_ZERO_RETURN(errctx,
|
|
|
|
|
(akbasic_leaf_is_identifier(first) && akbasic_leaf_is_identifier(second)),
|
|
|
|
|
AKBASIC_ERR_SYNTAX, "Expected SWAP VARIABLE, VARIABLE");
|
|
|
|
|
/*
|
|
|
|
|
* Refused across types, which BASIC 7.0 also does. A# and B$ hold different
|
|
|
|
|
* things and swapping them would leave two variables whose names no longer
|
|
|
|
|
* describe their contents -- and the type suffix is the only type
|
|
|
|
|
* declaration this language has.
|
|
|
|
|
*/
|
|
|
|
|
FAIL_NONZERO_RETURN(errctx, (first->leaftype != second->leaftype), AKBASIC_ERR_TYPE,
|
|
|
|
|
"SWAP needs two variables of the same type");
|
|
|
|
|
|
|
|
|
|
PASS(errctx, akbasic_environment_get(obj->environment, first->identifier, &a));
|
|
|
|
|
PASS(errctx, akbasic_environment_get(obj->environment, second->identifier, &b));
|
|
|
|
|
FAIL_ZERO_RETURN(errctx, (a != NULL && b != NULL), AKBASIC_ERR_UNDEFINED,
|
|
|
|
|
"SWAP could not reach %s or %s", first->identifier, second->identifier);
|
|
|
|
|
if ( a == b ) {
|
|
|
|
|
/* SWAP A#, A# is legal and does nothing, rather than corrupting itself. */
|
|
|
|
|
SUCCEED_TRUE(obj, dest);
|
|
|
|
|
SUCCEED_RETURN(errctx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* The whole record except the name, so an array swaps its dimensions and its
|
|
|
|
|
* storage pointer along with its contents -- SWAP is documented as exchanging
|
|
|
|
|
* the *variables*, not their scalar values, and on a C128 it is O(1) for
|
|
|
|
|
* exactly this reason. The names stay put: they are what the symbol table
|
|
|
|
|
* points at.
|
|
|
|
|
*/
|
Port onto libakstdlib 2b79aca and convert the eight bool predicates
akbasic's src/ now calls libakstdlib 313 times and raw libc 7 -- 2.2%
bypassed, against 86.4% on the same tree before this. The submodule bump
669b2b3 -> 2b79aca needed no source change of its own: the release is
drop-in for what akbasic already used.
Seven of the eight sites the earlier port left on raw libc change their own
signature rather than swallowing an error, per andrew's ruling on
libakstdlib#38. word_is, the is_waiting_for pair, the scanner's is_at_end,
peek, peek_next and match_next_char, format.c's overflow, and sink_akgl's
scroll/newline/putchar_at/echo_line/edit_key chain all return an
akerr_ErrorContext * and hand the answer back through an out parameter.
is_waiting_for and is_waiting_for_any are a public header change; every
call site that used one as a term in a condition hoists it into a
statement first.
verb_compare is the eighth and stays on strcmp. bsearch(3) fixes the
comparator's signature, so there is no out parameter to report through --
which is what libakstdlib#38 concluded. It carries a comment saying so and
saying why the bypass is safe there.
Six snprintf sites stay raw because they want truncation as an answer
rather than an error, and aksl_snprintf cannot express that until
libakstdlib#34 hands the required length back. Each of the six says so at
the site. Two of them, in host.c, are a latent defect rather than a
decision: a host type name over 31 characters truncates silently and two
sharing a prefix then collide, where structtype.c refuses the same case.
DLOAD leaked a file descriptor. Its read loop sat inside an ATTEMPT and the
PASS in it returned past CLEANUP, so a scan error left the file open.
Hoisting the loop into its own helper to convert fgets fixes it.
Refs libakstdlib#26, libakstdlib#38
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-03 15:41:49 -04:00
|
|
|
PASS(errctx, aksl_memcpy(namea, a->name, sizeof(namea)));
|
|
|
|
|
PASS(errctx, aksl_memcpy(nameb, b->name, sizeof(nameb)));
|
|
|
|
|
PASS(errctx, aksl_memcpy(&swap, a, sizeof(swap)));
|
|
|
|
|
PASS(errctx, aksl_memcpy(a, b, sizeof(*a)));
|
|
|
|
|
PASS(errctx, aksl_memcpy(b, &swap, sizeof(*b)));
|
|
|
|
|
PASS(errctx, aksl_memcpy(a->name, namea, sizeof(a->name)));
|
|
|
|
|
PASS(errctx, aksl_memcpy(b->name, nameb, sizeof(b->name)));
|
Stop a scalar created inside a scope costing value-pool slots
A scalar now lives in the variable record (`akbasic_Variable::inlinevalue`)
rather than drawing from the value pool, so a `GOSUB` local, a `FOR` counter and
a `DEF` parameter cost nothing at all.
The pool is a bump allocator with no free, and its comment justified that with
"nothing in BASIC destroys a variable". Scope exit does: it marks the variable
slot unused, `new_variable()` memsets the slot it hands back -- clearing
`values` -- and `variable_init()` therefore took *fresh* slots for a variable
whose old ones were still counted. Every scope that created a local leaked, with
no diagnostic until the pool ran dry on whichever line happened to be unlucky.
Six thousand `GOSUB`s creating one local used to die on the 4091st at `LOC# = 1`
with "Array of 1 elements does not fit in the 0 remaining value slots". They now
run. A `DEF` called eight thousand times used to die between the four and five
thousandth -- the leaking slot was the call scope's parameter, which is a scalar
-- and both forms now run. A game creating one name per tick was dead in half a
minute; the Breakout in examples/ was, after twenty-five seconds.
**A `@` name is the one exclusion, and it is the whole of it.** A structure or a
pointer to one keeps pool storage, because a pointer into a record outlives the
scope that DIMmed it -- docs/16-structures.md says nothing is reclaimed and
`prev_environment()` relies on it. The name suffix is the right test rather than
`structtype`, which the DIM path sets *after* calling `variable_init()`. A local
array therefore still leaks, deliberately, and is now the narrow rule the
tutorial teaches.
`SWAP` needed the other half: it copies whole variable records, so the `values`
pointer that came over named the other variable's inline slot -- which by then
held this variable's own old value -- and SWAP silently did nothing. Caught by
tests/language/housekeeping/verbs.bas, which is the golden corpus earning its
keep.
tests/value_pool.c is the new coverage. It asserts the mechanism as well as the
consequence: a later change that moved arrays inline too would pass every
behavioural case and quietly break the pointer guarantee. The sharpest case
takes the pool's whole 4096 slots in four arrays after two hundred scope
entries, so one leaked slot has nowhere to go.
Chapter 17 Step 3 taught "declare every name at the top" and no longer needs to.
It now teaches what is still true -- a name first seen inside a subroutine dies
at RETURN, so a routine cannot answer its caller through one -- and its
demonstration is the array case, which still fails.
TODO.md section 6 item 30 and section 9 item 1, both struck.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Andrew Kesterson <andrew@aklabs.net>
2026-08-01 23:47:49 -04:00
|
|
|
/*
|
|
|
|
|
* A scalar's storage is *inside* the record, so the pointer that came over
|
|
|
|
|
* with it names the other variable's inline slot -- which now holds this
|
|
|
|
|
* variable's own old value. Left alone, each side reads back what it started
|
|
|
|
|
* with and SWAP silently does nothing. Rebind each to its own.
|
|
|
|
|
*/
|
|
|
|
|
if ( a->values == &b->inlinevalue ) {
|
|
|
|
|
a->values = &a->inlinevalue;
|
|
|
|
|
}
|
|
|
|
|
if ( b->values == &a->inlinevalue ) {
|
|
|
|
|
b->values = &b->inlinevalue;
|
|
|
|
|
}
|
2026-07-31 12:07:50 -04:00
|
|
|
|
|
|
|
|
SUCCEED_TRUE(obj, dest);
|
|
|
|
|
SUCCEED_RETURN(errctx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------- HELP --- */
|
|
|
|
|
|
|
|
|
|
akerr_ErrorContext *akbasic_cmd_help(akbasic_Runtime *obj, akbasic_ASTLeaf *expr, akbasic_Value *lval, akbasic_Value *rval, akbasic_Value **dest)
|
|
|
|
|
{
|
|
|
|
|
PREPARE_ERROR(errctx);
|
|
|
|
|
char line[AKBASIC_MAX_LINE_LENGTH * 2];
|
Port onto libakstdlib 2b79aca and convert the eight bool predicates
akbasic's src/ now calls libakstdlib 313 times and raw libc 7 -- 2.2%
bypassed, against 86.4% on the same tree before this. The submodule bump
669b2b3 -> 2b79aca needed no source change of its own: the release is
drop-in for what akbasic already used.
Seven of the eight sites the earlier port left on raw libc change their own
signature rather than swallowing an error, per andrew's ruling on
libakstdlib#38. word_is, the is_waiting_for pair, the scanner's is_at_end,
peek, peek_next and match_next_char, format.c's overflow, and sink_akgl's
scroll/newline/putchar_at/echo_line/edit_key chain all return an
akerr_ErrorContext * and hand the answer back through an out parameter.
is_waiting_for and is_waiting_for_any are a public header change; every
call site that used one as a term in a condition hoists it into a
statement first.
verb_compare is the eighth and stays on strcmp. bsearch(3) fixes the
comparator's signature, so there is no out parameter to report through --
which is what libakstdlib#38 concluded. It carries a comment saying so and
saying why the bypass is safe there.
Six snprintf sites stay raw because they want truncation as an answer
rather than an error, and aksl_snprintf cannot express that until
libakstdlib#34 hands the required length back. Each of the six says so at
the site. Two of them, in host.c, are a latent defect rather than a
decision: a host type name over 31 characters truncates silently and two
sharing a prefix then collide, where structtype.c refuses the same case.
DLOAD leaked a file descriptor. Its read loop sat inside an ATTEMPT and the
PASS in it returned past CLEANUP, so a scan error left the file open.
Hoisting the loop into its own helper to convert fgets fixes it.
Refs libakstdlib#26, libakstdlib#38
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-03 15:41:49 -04:00
|
|
|
int written = 0;
|
2026-07-31 12:07:50 -04:00
|
|
|
|
|
|
|
|
(void)expr; (void)lval; (void)rval;
|
|
|
|
|
FAIL_ZERO_RETURN(errctx, (obj != NULL && dest != NULL), AKERR_NULLPOINTER, "NULL argument in HELP");
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* A C128 re-lists the line the last error happened on and highlights the
|
|
|
|
|
* part it choked on. The line is reproduced here; the highlight is not,
|
|
|
|
|
* because the error is reported by the verb that raised it rather than by
|
|
|
|
|
* something holding a token offset, so there is nothing that knows which
|
|
|
|
|
* part to mark. Printing the line without pretending to know more is the
|
|
|
|
|
* honest half of the verb.
|
|
|
|
|
*/
|
|
|
|
|
if ( obj->errorline == 0 || obj->source[obj->errorline].code[0] == '\0' ) {
|
|
|
|
|
PASS(errctx, akbasic_runtime_println(obj, "NO ERROR TO HELP WITH"));
|
|
|
|
|
SUCCEED_TRUE(obj, dest);
|
|
|
|
|
SUCCEED_RETURN(errctx);
|
|
|
|
|
}
|
Port onto libakstdlib 2b79aca and convert the eight bool predicates
akbasic's src/ now calls libakstdlib 313 times and raw libc 7 -- 2.2%
bypassed, against 86.4% on the same tree before this. The submodule bump
669b2b3 -> 2b79aca needed no source change of its own: the release is
drop-in for what akbasic already used.
Seven of the eight sites the earlier port left on raw libc change their own
signature rather than swallowing an error, per andrew's ruling on
libakstdlib#38. word_is, the is_waiting_for pair, the scanner's is_at_end,
peek, peek_next and match_next_char, format.c's overflow, and sink_akgl's
scroll/newline/putchar_at/echo_line/edit_key chain all return an
akerr_ErrorContext * and hand the answer back through an out parameter.
is_waiting_for and is_waiting_for_any are a public header change; every
call site that used one as a term in a condition hoists it into a
statement first.
verb_compare is the eighth and stays on strcmp. bsearch(3) fixes the
comparator's signature, so there is no out parameter to report through --
which is what libakstdlib#38 concluded. It carries a comment saying so and
saying why the bypass is safe there.
Six snprintf sites stay raw because they want truncation as an answer
rather than an error, and aksl_snprintf cannot express that until
libakstdlib#34 hands the required length back. Each of the six says so at
the site. Two of them, in host.c, are a latent defect rather than a
decision: a host type name over 31 characters truncates silently and two
sharing a prefix then collide, where structtype.c refuses the same case.
DLOAD leaked a file descriptor. Its read loop sat inside an ATTEMPT and the
PASS in it returned past CLEANUP, so a scan error left the file open.
Hoisting the loop into its own helper to convert fgets fixes it.
Refs libakstdlib#26, libakstdlib#38
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-03 15:41:49 -04:00
|
|
|
/*
|
|
|
|
|
* aksl_snprintf treats truncation as an error, and this destination is twice
|
|
|
|
|
* AKBASIC_MAX_LINE_LENGTH against a stored line that is at most one of them
|
|
|
|
|
* plus a line number, so the fit is a property of the buffer rather than
|
|
|
|
|
* something to check for.
|
|
|
|
|
*/
|
|
|
|
|
PASS(errctx, aksl_snprintf(&written, line, sizeof(line), "%" PRId64 " %s",
|
|
|
|
|
obj->errorline, obj->source[obj->errorline].code));
|
2026-07-31 12:07:50 -04:00
|
|
|
PASS(errctx, akbasic_runtime_println(obj, line));
|
|
|
|
|
SUCCEED_TRUE(obj, dest);
|
|
|
|
|
SUCCEED_RETURN(errctx);
|
|
|
|
|
}
|