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:
148
src/value.c
148
src/value.c
@@ -25,14 +25,34 @@
|
||||
* numeric payload the right operand is carrying" -- is legible, and so there is
|
||||
* one place to change when TODO.md section 12 item 5 is fixed.
|
||||
*/
|
||||
/*
|
||||
* Selected on the type, not summed. The reference adds both numeric fields --
|
||||
* `rval.intval + int64(rval.floatval)` -- which happens to give the right answer
|
||||
* only because whichever field is unused is always zero (TODO.md section 6
|
||||
* item 5). Nothing enforces that: a value that ever carries both, or one reused
|
||||
* from the pool without being zeroed, silently computes the sum of the two.
|
||||
*/
|
||||
static int64_t rval_as_int(akbasic_Value *rval)
|
||||
{
|
||||
return rval->intval + (int64_t)rval->floatval;
|
||||
if ( rval->valuetype == AKBASIC_TYPE_FLOAT ) {
|
||||
return (int64_t)rval->floatval;
|
||||
}
|
||||
if ( rval->valuetype == AKBASIC_TYPE_BOOLEAN ) {
|
||||
return rval->boolvalue;
|
||||
}
|
||||
return rval->intval;
|
||||
}
|
||||
|
||||
/** @brief The float counterpart of rval_as_int(); same reasoning. */
|
||||
static double rval_as_float(akbasic_Value *rval)
|
||||
{
|
||||
return rval->floatval + (double)rval->intval;
|
||||
if ( rval->valuetype == AKBASIC_TYPE_FLOAT ) {
|
||||
return rval->floatval;
|
||||
}
|
||||
if ( rval->valuetype == AKBASIC_TYPE_BOOLEAN ) {
|
||||
return (double)rval->boolvalue;
|
||||
}
|
||||
return (double)rval->intval;
|
||||
}
|
||||
|
||||
/* Copy a string into a value's inline buffer. Truncation is an error. */
|
||||
@@ -173,6 +193,35 @@ bool akbasic_value_is_true(akbasic_Value *self)
|
||||
return (self->boolvalue == AKBASIC_TRUE);
|
||||
}
|
||||
|
||||
bool akbasic_value_is_truthy(akbasic_Value *self)
|
||||
{
|
||||
if ( self == NULL ) {
|
||||
return false;
|
||||
}
|
||||
/*
|
||||
* Nonzero is true, which is Commodore's rule rather than a convenience.
|
||||
* BASIC 7.0 has no separate boolean type: a comparison yields -1 or 0, `AND`
|
||||
* and `OR` are the bitwise operators, and `IF A THEN` is legal for any
|
||||
* numeric A. Testing only for the boolean type would make `IF A# THEN` and
|
||||
* `IF A = 1 OR B = 2 THEN` -- whose OR yields an integer -- both silently
|
||||
* false.
|
||||
*
|
||||
* A string is never true. A C128 raises a type mismatch instead; that is a
|
||||
* stricter answer this interpreter could adopt later, and false is the
|
||||
* conservative one meanwhile.
|
||||
*/
|
||||
switch ( self->valuetype ) {
|
||||
case AKBASIC_TYPE_BOOLEAN:
|
||||
return (self->boolvalue != 0);
|
||||
case AKBASIC_TYPE_INTEGER:
|
||||
return (self->intval != 0);
|
||||
case AKBASIC_TYPE_FLOAT:
|
||||
return (self->floatval != 0.0);
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/* Shared prologue for the unary operators: validate, clone into scratch. */
|
||||
static akerr_ErrorContext *unary_prologue(akbasic_Value *self, akbasic_Value *scratch, akbasic_Value **dest)
|
||||
{
|
||||
@@ -200,6 +249,31 @@ static akerr_ErrorContext *binary_prologue(akbasic_Value *self, akbasic_Value *r
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief The integer a bitwise operator should use for @p value.
|
||||
*
|
||||
* BOOLEAN counts as an integer here, and that is what makes `AND` and `OR`
|
||||
* double as logical operators. Commodore BASIC has no separate logical pair: it
|
||||
* represents true as -1, every bit set, precisely so that `A = 1 AND B = 2`
|
||||
* works out bit by bit and lands on -1 or 0. Refusing a boolean operand would
|
||||
* make the commonest conditional in the language a type error.
|
||||
*/
|
||||
static bool bitwise_operand(akbasic_Value *value, int64_t *dest)
|
||||
{
|
||||
if ( value == NULL ) {
|
||||
return false;
|
||||
}
|
||||
if ( value->valuetype == AKBASIC_TYPE_INTEGER ) {
|
||||
*dest = value->intval;
|
||||
return true;
|
||||
}
|
||||
if ( value->valuetype == AKBASIC_TYPE_BOOLEAN ) {
|
||||
*dest = value->boolvalue;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
akerr_ErrorContext *akbasic_value_invert(akbasic_Value *self, akbasic_Value *scratch, akbasic_Value **dest)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
@@ -216,12 +290,21 @@ akerr_ErrorContext *akbasic_value_invert(akbasic_Value *self, akbasic_Value *scr
|
||||
akerr_ErrorContext *akbasic_value_bitwise_not(akbasic_Value *self, akbasic_Value *scratch, akbasic_Value **dest)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
int64_t a = 0;
|
||||
|
||||
FAIL_ZERO_RETURN(errctx, (self != NULL), AKERR_NULLPOINTER, "NULL value in bitwise not");
|
||||
FAIL_ZERO_RETURN(errctx, (self->valuetype == AKBASIC_TYPE_INTEGER),
|
||||
AKBASIC_ERR_TYPE, "Cannot only perform bitwise operations on integers");
|
||||
FAIL_ZERO_RETURN(errctx, bitwise_operand(self, &a), AKBASIC_ERR_TYPE,
|
||||
"Can only perform bitwise operations on integers and truth values");
|
||||
PASS(errctx, unary_prologue(self, scratch, dest));
|
||||
(*dest)->intval = ~(self->intval);
|
||||
/*
|
||||
* A truth value inverts to a truth value. On a C128 true is -1 -- every bit
|
||||
* set -- so `NOT` of it is 0 and `NOT` of 0 is -1 either way you compute it;
|
||||
* carrying the type through is what keeps `IF NOT (A = 9) THEN` reading as a
|
||||
* condition rather than as an integer that happens to be nonzero.
|
||||
*/
|
||||
(*dest)->valuetype = self->valuetype;
|
||||
(*dest)->intval = ~a;
|
||||
(*dest)->boolvalue = ~a;
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
@@ -261,40 +344,54 @@ akerr_ErrorContext *akbasic_value_shift_right(akbasic_Value *self, int64_t bits,
|
||||
akerr_ErrorContext *akbasic_value_bitwise_and(akbasic_Value *self, akbasic_Value *rval, akbasic_Value *scratch, akbasic_Value **dest)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
int64_t a = 0;
|
||||
int64_t b = 0;
|
||||
|
||||
FAIL_ZERO_RETURN(errctx, (self != NULL), AKERR_NULLPOINTER, "NULL value in bitwise and");
|
||||
FAIL_ZERO_RETURN(errctx, (rval != NULL), AKERR_NULLPOINTER, "nil rval");
|
||||
FAIL_ZERO_RETURN(errctx, (self->valuetype == AKBASIC_TYPE_INTEGER),
|
||||
AKBASIC_ERR_TYPE, "Cannot perform bitwise operations on string or float");
|
||||
FAIL_ZERO_RETURN(errctx, (bitwise_operand(self, &a) && bitwise_operand(rval, &b)),
|
||||
AKBASIC_ERR_TYPE,
|
||||
"Can only perform bitwise operations on integers and truth values");
|
||||
PASS(errctx, binary_prologue(self, rval, scratch, dest));
|
||||
(*dest)->intval = self->intval & rval->intval;
|
||||
(*dest)->valuetype = AKBASIC_TYPE_INTEGER;
|
||||
(*dest)->intval = a & b;
|
||||
(*dest)->boolvalue = (*dest)->intval;
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
akerr_ErrorContext *akbasic_value_bitwise_or(akbasic_Value *self, akbasic_Value *rval, akbasic_Value *scratch, akbasic_Value **dest)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
int64_t a = 0;
|
||||
int64_t b = 0;
|
||||
|
||||
FAIL_ZERO_RETURN(errctx, (self != NULL), AKERR_NULLPOINTER, "NULL value in bitwise or");
|
||||
FAIL_ZERO_RETURN(errctx, (rval != NULL), AKERR_NULLPOINTER, "nil rval");
|
||||
FAIL_ZERO_RETURN(errctx, (self->valuetype == AKBASIC_TYPE_INTEGER),
|
||||
AKBASIC_ERR_TYPE, "Can only perform bitwise operations on integers");
|
||||
FAIL_ZERO_RETURN(errctx, (bitwise_operand(self, &a) && bitwise_operand(rval, &b)),
|
||||
AKBASIC_ERR_TYPE,
|
||||
"Can only perform bitwise operations on integers and truth values");
|
||||
PASS(errctx, binary_prologue(self, rval, scratch, dest));
|
||||
(*dest)->intval = self->intval | rval->intval;
|
||||
(*dest)->valuetype = AKBASIC_TYPE_INTEGER;
|
||||
(*dest)->intval = a | b;
|
||||
(*dest)->boolvalue = (*dest)->intval;
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
akerr_ErrorContext *akbasic_value_bitwise_xor(akbasic_Value *self, akbasic_Value *rval, akbasic_Value *scratch, akbasic_Value **dest)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
int64_t a = 0;
|
||||
int64_t b = 0;
|
||||
|
||||
FAIL_ZERO_RETURN(errctx, (self != NULL), AKERR_NULLPOINTER, "NULL value in bitwise xor");
|
||||
FAIL_ZERO_RETURN(errctx, (rval != NULL), AKERR_NULLPOINTER, "nil rval");
|
||||
FAIL_ZERO_RETURN(errctx,
|
||||
(self->valuetype == AKBASIC_TYPE_INTEGER && rval->valuetype == AKBASIC_TYPE_INTEGER),
|
||||
AKBASIC_ERR_TYPE, "Can only perform bitwise operations on integers");
|
||||
FAIL_ZERO_RETURN(errctx, (bitwise_operand(self, &a) && bitwise_operand(rval, &b)),
|
||||
AKBASIC_ERR_TYPE,
|
||||
"Can only perform bitwise operations on integers and truth values");
|
||||
PASS(errctx, binary_prologue(self, rval, scratch, dest));
|
||||
(*dest)->intval = self->intval ^ rval->intval;
|
||||
(*dest)->valuetype = AKBASIC_TYPE_INTEGER;
|
||||
(*dest)->intval = a ^ b;
|
||||
(*dest)->boolvalue = (*dest)->intval;
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
@@ -310,17 +407,18 @@ akerr_ErrorContext *akbasic_value_math_plus(akbasic_Value *self, akbasic_Value *
|
||||
FAIL_ZERO_RETURN(errctx, (dest != NULL), AKERR_NULLPOINTER, "NULL destination in math plus");
|
||||
|
||||
/*
|
||||
* The asymmetry with every other operator is deliberate and load-bearing:
|
||||
* mathPlus mutates self in place when self is mutable, and CommandNEXT's
|
||||
* loop increment relies on that to advance the loop variable. TODO.md
|
||||
* section 12 item 4.
|
||||
* Always a clone, like every other operator. The reference mutates `self` in
|
||||
* place when it happens to be mutable, so `A# + 1` modified `A#` whenever
|
||||
* the left operand came from a variable rather than from a literal --
|
||||
* TODO.md section 6 item 4, and the highest-blast-radius entry on that list.
|
||||
*
|
||||
* It was load-bearing: NEXT advanced its counter by calling this and letting
|
||||
* the mutation land in the variable. NEXT now writes the result back itself,
|
||||
* which is what made this safe to change. tests/for_next.c is the coverage
|
||||
* that had to exist first.
|
||||
*/
|
||||
if ( !self->mutable_ ) {
|
||||
PASS(errctx, akbasic_value_clone(self, scratch));
|
||||
out = scratch;
|
||||
} else {
|
||||
out = self;
|
||||
}
|
||||
PASS(errctx, akbasic_value_clone(self, scratch));
|
||||
out = scratch;
|
||||
|
||||
if ( self->valuetype == AKBASIC_TYPE_INTEGER ) {
|
||||
out->intval = self->intval + rval_as_int(rval);
|
||||
|
||||
Reference in New Issue
Block a user