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>
122 lines
4.8 KiB
C
122 lines
4.8 KiB
C
/**
|
|
* @file runtime_format.c
|
|
* @brief The group D verbs: PRINT USING, PUDEF, WIDTH and CHAR.
|
|
*
|
|
* Formatting and text placement. The field rendering itself lives in
|
|
* src/format.c, which is a pure function of a format string and a value and is
|
|
* tested as one; what is here is the verbs that reach it.
|
|
*/
|
|
|
|
#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 )
|
|
|
|
/* ------------------------------------------------------------------ PUDEF -- */
|
|
|
|
akerr_ErrorContext *akbasic_cmd_pudef(akbasic_Runtime *obj, akbasic_ASTLeaf *expr, akbasic_Value *lval, akbasic_Value *rval, akbasic_Value **dest)
|
|
{
|
|
PREPARE_ERROR(errctx);
|
|
akbasic_Value *value = NULL;
|
|
|
|
(void)lval; (void)rval;
|
|
FAIL_ZERO_RETURN(errctx, (obj != NULL && dest != NULL), AKERR_NULLPOINTER,
|
|
"NULL argument in PUDEF");
|
|
FAIL_ZERO_RETURN(errctx, (expr != NULL && expr->right != NULL), AKBASIC_ERR_SYNTAX,
|
|
"Expected PUDEF \"characters\"");
|
|
PASS(errctx, akbasic_runtime_evaluate(obj, expr->right, &value));
|
|
FAIL_NONZERO_RETURN(errctx, (value->valuetype != AKBASIC_TYPE_STRING), AKBASIC_ERR_TYPE,
|
|
"PUDEF expected a string");
|
|
/*
|
|
* No device needed. The fill characters are the program's state, the same
|
|
* way COLOR's source bindings are, and a runtime with no output device is
|
|
* still entitled to remember what it was told.
|
|
*/
|
|
PASS(errctx, akbasic_format_pudef(&obj->format_state, value->stringval));
|
|
SUCCEED_TRUE(obj, dest);
|
|
SUCCEED_RETURN(errctx);
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ WIDTH -- */
|
|
|
|
akerr_ErrorContext *akbasic_cmd_width(akbasic_Runtime *obj, akbasic_ASTLeaf *expr, akbasic_Value *lval, akbasic_Value *rval, akbasic_Value **dest)
|
|
{
|
|
PREPARE_ERROR(errctx);
|
|
double args[1];
|
|
int count = 0;
|
|
|
|
(void)lval; (void)rval;
|
|
FAIL_ZERO_RETURN(errctx, (obj != NULL && dest != NULL), AKERR_NULLPOINTER,
|
|
"NULL argument in WIDTH");
|
|
PASS(errctx, akbasic_args_numbers(obj, expr, "WIDTH", args, 1, &count));
|
|
FAIL_ZERO_RETURN(errctx, (count == 1), AKBASIC_ERR_SYNTAX, "Expected WIDTH (1 or 2)");
|
|
/*
|
|
* BASIC 7.0's WIDTH sets the thickness of the lines the *graphics* verbs
|
|
* draw, not a text column count -- a common confusion, and the reason this
|
|
* verb lives with the drawing state rather than with PRINT.
|
|
*/
|
|
FAIL_ZERO_RETURN(errctx, (args[0] == 1.0 || args[0] == 2.0), AKBASIC_ERR_BOUNDS,
|
|
"WIDTH is 1 or 2, not %d", (int)args[0]);
|
|
obj->gfx.linewidth = (int)args[0];
|
|
SUCCEED_TRUE(obj, dest);
|
|
SUCCEED_RETURN(errctx);
|
|
}
|
|
|
|
/* ------------------------------------------------------------------- CHAR -- */
|
|
|
|
akerr_ErrorContext *akbasic_cmd_char(akbasic_Runtime *obj, akbasic_ASTLeaf *expr, akbasic_Value *lval, akbasic_Value *rval, akbasic_Value **dest)
|
|
{
|
|
PREPARE_ERROR(errctx);
|
|
akbasic_ASTLeaf *arg = NULL;
|
|
akbasic_Value *value = NULL;
|
|
char text[AKBASIC_MAX_STRING_LENGTH];
|
|
double coords[3];
|
|
int count = 0;
|
|
|
|
(void)lval; (void)rval;
|
|
FAIL_ZERO_RETURN(errctx, (obj != NULL && dest != NULL), AKERR_NULLPOINTER,
|
|
"NULL argument in CHAR");
|
|
FAIL_ZERO_RETURN(errctx, (obj->sink != NULL), AKBASIC_ERR_DEVICE,
|
|
"CHAR needs a text device and this runtime has none");
|
|
FAIL_ZERO_RETURN(errctx, (obj->sink->moveto != NULL), AKBASIC_ERR_DEVICE,
|
|
"CHAR needs a text device that can position its cursor, and this one cannot");
|
|
|
|
/*
|
|
* CHAR colour, column, row, "text". The colour argument is accepted and
|
|
* ignored: this interpreter's text sink draws in one colour, chosen by the
|
|
* host when it built the sink, and a per-call colour would have to become
|
|
* part of the sink interface. Recorded in TODO.md section 5.
|
|
*/
|
|
arg = akbasic_leaf_first_argument(expr);
|
|
for ( count = 0; count < 3 && arg != NULL; count++, arg = arg->next ) {
|
|
PASS(errctx, akbasic_runtime_evaluate(obj, arg, &value));
|
|
FAIL_NONZERO_RETURN(errctx, (value->valuetype == AKBASIC_TYPE_STRING), AKBASIC_ERR_TYPE,
|
|
"CHAR expected a number in argument %d", count + 1);
|
|
coords[count] = (value->valuetype == AKBASIC_TYPE_FLOAT)
|
|
? value->floatval : (double)value->intval;
|
|
}
|
|
FAIL_ZERO_RETURN(errctx, (count == 3 && arg != NULL), AKBASIC_ERR_SYNTAX,
|
|
"Expected CHAR (colour), (column), (row), (string)");
|
|
|
|
PASS(errctx, akbasic_runtime_evaluate(obj, arg, &value));
|
|
PASS(errctx, akbasic_value_to_string(value, text, sizeof(text)));
|
|
|
|
PASS(errctx, obj->sink->moveto(obj->sink, (int)coords[1], (int)coords[2]));
|
|
PASS(errctx, obj->sink->write(obj->sink, text));
|
|
SUCCEED_TRUE(obj, dest);
|
|
SUCCEED_RETURN(errctx);
|
|
}
|