Files
akbasic/src/data.c
Andrew Kesterson 9845e77a5c 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

212 lines
5.8 KiB
C

/**
* @file data.c
* @brief Implements the `DATA` item list and the `READ` cursor.
*/
#include <ctype.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <akerror.h>
#include <akstdlib.h>
#include <akbasic/data.h>
#include <akbasic/error.h>
#include <akbasic/runtime.h>
akerr_ErrorContext *akbasic_data_state_init(akbasic_DataState *obj)
{
PREPARE_ERROR(errctx);
FAIL_ZERO_RETURN(errctx, (obj != NULL), AKERR_NULLPOINTER, "NULL data state in init");
obj->count = 0;
obj->cursor = 0;
SUCCEED_RETURN(errctx);
}
/**
* @brief Collect the items of one `DATA` statement.
*
* @param obj The list to append to.
* @param cursor Points just past the `DATA` keyword; advanced past what is read.
* @param lineno The line these items were written on.
* @return `NULL` on success, otherwise an error context owned by the caller.
* @throws AKBASIC_ERR_BOUNDS When the list is full or an item is too long.
*/
static akerr_ErrorContext *collect_items(akbasic_DataState *obj, const char **cursor, int64_t lineno)
{
PREPARE_ERROR(errctx);
const char *p = *cursor;
for ( ;; ) {
akbasic_DataItem *item = NULL;
size_t used = 0;
while ( *p == ' ' || *p == '\t' ) {
p += 1;
}
if ( *p == '\0' || *p == ':' ) {
break;
}
FAIL_ZERO_RETURN(errctx, (obj->count < AKBASIC_MAX_DATA_ITEMS), AKBASIC_ERR_BOUNDS,
"The program holds more than %d DATA items", AKBASIC_MAX_DATA_ITEMS);
item = &obj->items[obj->count];
item->lineno = lineno;
item->quoted = false;
if ( *p == '"' ) {
/*
* A quoted item runs to its closing quote and may hold anything --
* commas, colons, spaces. That is the only way to put a comma in a
* DATA item, and it is why the scan has to understand quotes at all.
*/
item->quoted = true;
p += 1;
while ( *p != '\0' && *p != '"' && used < sizeof(item->text) - 1 ) {
item->text[used] = *p;
used += 1;
p += 1;
}
if ( *p == '"' ) {
p += 1;
}
} else {
while ( *p != '\0' && *p != ',' && *p != ':' && used < sizeof(item->text) - 1 ) {
item->text[used] = *p;
used += 1;
p += 1;
}
/* Trailing spaces are not part of an unquoted item. */
while ( used > 0 && (item->text[used - 1] == ' ' || item->text[used - 1] == '\t') ) {
used -= 1;
}
}
item->text[used] = '\0';
obj->count += 1;
while ( *p == ' ' || *p == '\t' ) {
p += 1;
}
if ( *p != ',' ) {
break;
}
p += 1;
}
*cursor = p;
SUCCEED_RETURN(errctx);
}
/**
* @brief Scan one source line for `DATA` statements.
*
* Walks statement by statement, the way scan_line_labels() does, because `DATA`
* is a verb and a verb starts a statement -- and a string literal is the only
* thing that can hide a separator.
*/
static akerr_ErrorContext *scan_line(akbasic_DataState *obj, const char *code, int64_t lineno)
{
PREPARE_ERROR(errctx);
const char *cursor = code;
bool statementstart = true;
bool instring = false;
while ( *cursor != '\0' ) {
if ( instring ) {
instring = (*cursor != '"');
cursor += 1;
continue;
}
if ( *cursor == '"' ) {
instring = true;
statementstart = false;
cursor += 1;
continue;
}
if ( *cursor == ':' ) {
statementstart = true;
cursor += 1;
continue;
}
if ( isspace((unsigned char)*cursor) ) {
cursor += 1;
continue;
}
/* A stored line may still carry its own number; see scan_line_labels(). */
if ( statementstart && isdigit((unsigned char)*cursor) ) {
while ( isdigit((unsigned char)*cursor) ) {
cursor += 1;
}
continue;
}
if ( statementstart && strncasecmp(cursor, "DATA", 4) == 0
&& !isalnum((unsigned char)cursor[4]) ) {
cursor += 4;
PASS(errctx, collect_items(obj, &cursor, lineno));
statementstart = false;
continue;
}
statementstart = false;
cursor += 1;
}
SUCCEED_RETURN(errctx);
}
akerr_ErrorContext *akbasic_data_scan(akbasic_Runtime *obj)
{
PREPARE_ERROR(errctx);
int64_t i = 0;
FAIL_ZERO_RETURN(errctx, (obj != NULL), AKERR_NULLPOINTER, "NULL runtime in data_scan");
PASS(errctx, akbasic_data_state_init(&obj->data_state));
for ( i = 0; i < AKBASIC_MAX_SOURCE_LINES; i++ ) {
if ( obj->source[i].code[0] != '\0' ) {
PASS(errctx, scan_line(&obj->data_state, obj->source[i].code, i));
}
}
SUCCEED_RETURN(errctx);
}
akerr_ErrorContext *akbasic_data_next(akbasic_Runtime *obj, akbasic_Type type, akbasic_Value *dest)
{
PREPARE_ERROR(errctx);
akbasic_DataItem *item = NULL;
FAIL_ZERO_RETURN(errctx, (obj != NULL && dest != NULL), AKERR_NULLPOINTER,
"NULL argument in data_next");
FAIL_ZERO_RETURN(errctx, (obj->data_state.cursor < obj->data_state.count),
AKBASIC_ERR_STATE, "OUT OF DATA");
item = &obj->data_state.items[obj->data_state.cursor];
obj->data_state.cursor += 1;
PASS(errctx, akbasic_value_zero(dest));
if ( type == AKBASIC_TYPE_STRING ) {
dest->valuetype = AKBASIC_TYPE_STRING;
snprintf(dest->stringval, sizeof(dest->stringval), "%s", item->text);
SUCCEED_RETURN(errctx);
}
/*
* A quoted item is a string and reading it into a number is a mistake worth
* reporting -- `READ A#` against `DATA "TEXT"` would otherwise quietly yield
* zero. An *unquoted* item that is not a number is caught the same way, by
* the conversion refusing it.
*/
FAIL_NONZERO_RETURN(errctx, item->quoted, AKBASIC_ERR_TYPE,
"READ: DATA item \"%s\" on line %" PRId64 " is a string",
item->text, item->lineno);
if ( type == AKBASIC_TYPE_FLOAT ) {
dest->valuetype = AKBASIC_TYPE_FLOAT;
PASS(errctx, aksl_atof(item->text, &dest->floatval));
} else {
long long converted = 0;
dest->valuetype = AKBASIC_TYPE_INTEGER;
PASS(errctx, aksl_atoll(item->text, &converted));
dest->intval = (int64_t)converted;
}
SUCCEED_RETURN(errctx);
}