Files
akbasic/src/runtime_machine.c

133 lines
5.2 KiB
C
Raw Normal View History

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>
2026-07-31 21:50:37 -04:00
/**
* @file runtime_machine.c
* @brief The group J verbs: FETCH, STASH and SYS.
*
* Machine-level verbs on a machine that is not a Commodore 128. `POKE`, `PEEK`
* and `POINTER` already treat a BASIC integer as a real address in this
* process's memory -- that decision was made when they were ported -- and these
* follow it, because a `FETCH` that meant something different from the `PEEK`
* beside it would be worse than either answer on its own.
*
* `SYS` does not follow it, and the difference is the point: reading and writing
* a byte is something this process can do, and jumping to one is not.
*/
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
#include <akerror.h>
#include <akbasic/args.h>
#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 The copy behind both FETCH and STASH.
*
* On a C128 the two differ by which side is expansion RAM: `STASH` writes out to
* the RAM Expansion Unit and `FETCH` reads back from it. There is no expansion
* unit here and no banks, so both are the same byte copy and the only honest
* thing to do is say so rather than invent a distinction.
*
* @param obj Runtime to evaluate against.
* @param expr The command leaf.
* @param verb Name to use in a diagnostic.
* @return `NULL` on success, otherwise an error context owned by the caller.
*/
static akerr_ErrorContext *copy_bytes(akbasic_Runtime *obj, akbasic_ASTLeaf *expr, const char *verb)
{
PREPARE_ERROR(errctx);
double args[4];
int count = 0;
int64_t length = 0;
PASS(errctx, akbasic_args_numbers(obj, expr, verb, args, 4, &count));
FAIL_ZERO_RETURN(errctx, (count >= 3), AKBASIC_ERR_SYNTAX,
"Expected %s (count), (source address), (destination address)", verb);
length = (int64_t)args[0];
FAIL_ZERO_RETURN(errctx, (length >= 0), AKBASIC_ERR_VALUE,
"%s cannot copy a negative number of bytes", verb);
FAIL_ZERO_RETURN(errctx, (args[1] != 0.0 && args[2] != 0.0), AKBASIC_ERR_VALUE,
"%s will not touch address zero", verb);
if ( length == 0 ) {
SUCCEED_RETURN(errctx);
}
/*
* memmove rather than memcpy: a program shifting a buffer along by a few
* bytes overlaps its own source, and that is a normal thing to ask for.
*
* There is no bounds check, and there cannot be one -- the same is true of
* POKE and PEEK. A BASIC integer here is a real address, so a wrong one is a
* segmentation fault rather than an error message. See TODO.md section 5.
*/
memmove((void *)(uintptr_t)args[2], (const void *)(uintptr_t)args[1], (size_t)length);
SUCCEED_RETURN(errctx);
}
akerr_ErrorContext *akbasic_cmd_fetch(akbasic_Runtime *obj, akbasic_ASTLeaf *expr, akbasic_Value *lval, akbasic_Value *rval, akbasic_Value **dest)
{
PREPARE_ERROR(errctx);
(void)lval; (void)rval;
FAIL_ZERO_RETURN(errctx, (obj != NULL && dest != NULL), AKERR_NULLPOINTER,
"NULL argument in FETCH");
PASS(errctx, copy_bytes(obj, expr, "FETCH"));
SUCCEED_TRUE(obj, dest);
SUCCEED_RETURN(errctx);
}
akerr_ErrorContext *akbasic_cmd_stash(akbasic_Runtime *obj, akbasic_ASTLeaf *expr, akbasic_Value *lval, akbasic_Value *rval, akbasic_Value **dest)
{
PREPARE_ERROR(errctx);
(void)lval; (void)rval;
FAIL_ZERO_RETURN(errctx, (obj != NULL && dest != NULL), AKERR_NULLPOINTER,
"NULL argument in STASH");
PASS(errctx, copy_bytes(obj, expr, "STASH"));
SUCCEED_TRUE(obj, dest);
SUCCEED_RETURN(errctx);
}
akerr_ErrorContext *akbasic_cmd_sys(akbasic_Runtime *obj, akbasic_ASTLeaf *expr, akbasic_Value *lval, akbasic_Value *rval, akbasic_Value **dest)
{
PREPARE_ERROR(errctx);
double args[4];
int count = 0;
(void)lval; (void)rval;
FAIL_ZERO_RETURN(errctx, (obj != NULL && dest != NULL), AKERR_NULLPOINTER,
"NULL argument in SYS");
/*
* Parsed, then refused. Parsing first is deliberate: a program is told that
* SYS is not implemented rather than that its arguments are wrong, and a
* listing containing SYS still LISTs and still RENUMBERs.
*/
PASS(errctx, akbasic_args_numbers(obj, expr, "SYS", args, 4, &count));
FAIL_ZERO_RETURN(errctx, (count >= 1), AKBASIC_ERR_SYNTAX, "Expected SYS (address)");
/*
* There is no 6502. SYS calls machine code at an address, and the machine
* code a C128 program would call is ROM that does not exist here -- so there
* is nothing to jump to and nothing sensible to emulate. Jumping to a real
* address in this process would be a way to crash it on purpose.
*
* Refused by name rather than ignored, because a program whose SYS silently
* did nothing would carry on as though it had worked. This is the same
* reasoning BANK, FAST and MONITOR are out of scope on, with one difference:
* those are refused at the table and this one has a handler, because SYS is
* common enough in published listings to be worth a specific message.
*/
FAIL_RETURN(errctx, AKBASIC_ERR_DEVICE,
"SYS %d: there is no 6502 here, and no ROM to call. Machine code cannot be run by this interpreter",
(int)args[0]);
}