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:
@@ -61,6 +61,32 @@ akerr_ErrorContext *akbasic_cmd_print(akbasic_Runtime *obj, akbasic_ASTLeaf *exp
|
||||
SUCCEED_TRUE(obj, dest);
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
/*
|
||||
* PRINT USING: the parse handler leaves an argument list of exactly two --
|
||||
* the format and the value -- where a plain PRINT leaves a single
|
||||
* expression. The list is the only thing that tells them apart.
|
||||
*/
|
||||
{
|
||||
akbasic_ASTLeaf *arg = akbasic_leaf_first_argument(expr);
|
||||
|
||||
if ( arg != NULL && arg->next != NULL ) {
|
||||
akbasic_Value *format = NULL;
|
||||
akbasic_Value *value = NULL;
|
||||
char formatted[AKBASIC_MAX_STRING_LENGTH];
|
||||
|
||||
PASS(errctx, akbasic_runtime_evaluate(obj, arg, &format));
|
||||
FAIL_NONZERO_RETURN(errctx, (format->valuetype != AKBASIC_TYPE_STRING),
|
||||
AKBASIC_ERR_TYPE, "PRINT USING expected a format string");
|
||||
PASS(errctx, akbasic_runtime_evaluate(obj, arg->next, &value));
|
||||
PASS(errctx, akbasic_format_using(&obj->format_state, format->stringval, value,
|
||||
formatted, sizeof(formatted)));
|
||||
PASS(errctx, akbasic_runtime_println(obj, formatted));
|
||||
SUCCEED_TRUE(obj, dest);
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
}
|
||||
|
||||
PASS(errctx, akbasic_runtime_evaluate(obj, expr->right, dest));
|
||||
PASS(errctx, akbasic_value_to_string(*dest, rendered, sizeof(rendered)));
|
||||
PASS(errctx, akbasic_runtime_println(obj, rendered));
|
||||
@@ -131,6 +157,14 @@ akerr_ErrorContext *akbasic_cmd_return(akbasic_Runtime *obj, akbasic_ASTLeaf *ex
|
||||
"RETURN from an orphaned environment");
|
||||
obj->environment->parent->nextline = obj->environment->gosubReturnLine;
|
||||
PASS(errctx, akbasic_value_clone(result, &obj->environment->returnValue));
|
||||
/*
|
||||
* Leaving an interrupt handler re-arms interrupts. Compared by identity
|
||||
* rather than by a depth counter so that a GOSUB the handler itself makes
|
||||
* returns without re-arming: it is *this* environment that has to pop.
|
||||
*/
|
||||
if ( obj->environment == obj->handlerenv ) {
|
||||
obj->handlerenv = NULL;
|
||||
}
|
||||
PASS(errctx, akbasic_runtime_prev_environment(obj));
|
||||
*dest = result;
|
||||
SUCCEED_RETURN(errctx);
|
||||
@@ -677,6 +711,23 @@ akerr_ErrorContext *akbasic_cmd_next(akbasic_Runtime *obj, akbasic_ASTLeaf *expr
|
||||
|
||||
obj->environment->loopExitLine = obj->environment->lineno + 1;
|
||||
|
||||
/*
|
||||
* An EXIT sent us here. The loop is over whatever the counter says, and it
|
||||
* is over for *this* loop -- EXIT leaves the innermost one, and the wait it
|
||||
* armed is on the innermost environment, so the first NEXT reached is the
|
||||
* right one whether or not it names the same variable.
|
||||
*/
|
||||
if ( obj->environment->exiting ) {
|
||||
obj->environment->exiting = false;
|
||||
PASS(errctx, akbasic_environment_stop_waiting(obj->environment, "NEXT"));
|
||||
FAIL_ZERO_RETURN(errctx, (obj->environment->parent != NULL), AKBASIC_ERR_ENVIRONMENT,
|
||||
"NEXT in an orphaned environment");
|
||||
obj->environment->parent->nextline = obj->environment->loopExitLine;
|
||||
PASS(errctx, akbasic_runtime_prev_environment(obj));
|
||||
*dest = &obj->staticFalseValue;
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
/*
|
||||
* A NEXT for someone else's loop variable: this environment is done, hand
|
||||
* the line back to the parent and pop. That is how nested loops unwind.
|
||||
@@ -707,12 +758,15 @@ akerr_ErrorContext *akbasic_cmd_next(akbasic_Runtime *obj, akbasic_ASTLeaf *expr
|
||||
}
|
||||
|
||||
/*
|
||||
* Advance the counter. The stored value is mutable, so math_plus updates it
|
||||
* in place -- see TODO.md section 12 item 4. Changing that without changing
|
||||
* this breaks every FOR loop.
|
||||
* Advance the counter, then store it. math_plus clones like every other
|
||||
* operator, so the sum lands in `scratch` and this is what puts it back in
|
||||
* the variable. The reference relied on math_plus mutating the counter in
|
||||
* place instead, which meant `A# + 1` anywhere in a program could modify
|
||||
* `A#` -- TODO.md section 6 item 4.
|
||||
*/
|
||||
PASS(errctx, akbasic_value_math_plus(counter, &obj->environment->forStepValue,
|
||||
&scratch, &updated));
|
||||
PASS(errctx, akbasic_variable_set_subscript(nextvar, updated, zerosubscript, 1));
|
||||
obj->environment->nextline = obj->environment->loopFirstLine;
|
||||
SUCCEED_TRUE(obj, dest);
|
||||
SUCCEED_RETURN(errctx);
|
||||
@@ -723,20 +777,31 @@ akerr_ErrorContext *akbasic_cmd_exit(akbasic_Runtime *obj, akbasic_ASTLeaf *expr
|
||||
PREPARE_ERROR(errctx);
|
||||
|
||||
(void)expr; (void)lval; (void)rval;
|
||||
/*
|
||||
* EXIT leaves whichever kind of loop it is standing in, so which verb it
|
||||
* skips forward to depends on that: NEXT for a FOR, LOOP for a DO.
|
||||
*/
|
||||
FAIL_NONZERO_RETURN(errctx,
|
||||
(obj->environment->forToValue.valuetype == AKBASIC_TYPE_UNDEFINED),
|
||||
AKBASIC_ERR_STATE, "EXIT outside the context of FOR");
|
||||
(obj->environment->forToValue.valuetype == AKBASIC_TYPE_UNDEFINED &&
|
||||
!obj->environment->isDoLoop),
|
||||
AKBASIC_ERR_STATE, "EXIT outside the context of FOR or DO");
|
||||
FAIL_ZERO_RETURN(errctx, (obj->environment->parent != NULL), AKBASIC_ERR_ENVIRONMENT,
|
||||
"EXIT in an orphaned environment");
|
||||
obj->environment->parent->nextline = obj->environment->loopExitLine;
|
||||
/*
|
||||
* The reference pops without clearing the wait, which leaves the parent
|
||||
* waiting for a NEXT that will never arrive (TODO.md section 12 item 8). The
|
||||
* wait is cleared here first: leaving it set would hang the interpreter
|
||||
* rather than merely misbehave, and no golden case depends on the hang.
|
||||
* Skip forward to the loop's NEXT rather than jumping past it. The reference
|
||||
* jumps to loopExitLine, which is only ever written by NEXT itself -- so an
|
||||
* EXIT on the first pass through the loop, which is the ordinary case, jumps
|
||||
* to line 0 and restarts the program. Each restart enters FOR again and
|
||||
* takes another environment and another variable, so what a reader sees is
|
||||
* "Maximum runtime variables reached" reported against the FOR.
|
||||
*
|
||||
* The wait does the work instead: nothing between here and the NEXT
|
||||
* executes, the NEXT sees `exiting` and pops rather than looping, and
|
||||
* neither this verb nor that one has to know where the loop ends.
|
||||
*/
|
||||
PASS(errctx, akbasic_environment_stop_waiting(obj->environment, "NEXT"));
|
||||
PASS(errctx, akbasic_runtime_prev_environment(obj));
|
||||
obj->environment->exiting = true;
|
||||
PASS(errctx, akbasic_environment_wait_for_command(obj->environment,
|
||||
(obj->environment->isDoLoop ? "LOOP" : "NEXT")));
|
||||
SUCCEED_TRUE(obj, dest);
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
@@ -744,14 +809,93 @@ akerr_ErrorContext *akbasic_cmd_exit(akbasic_Runtime *obj, akbasic_ASTLeaf *expr
|
||||
akerr_ErrorContext *akbasic_cmd_read(akbasic_Runtime *obj, akbasic_ASTLeaf *expr, akbasic_Value *lval, akbasic_Value *rval, akbasic_Value **dest)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
akbasic_Environment *env = obj->environment;
|
||||
akbasic_ASTLeaf *identifier = NULL;
|
||||
akbasic_ASTLeaf assign;
|
||||
akbasic_ASTLeaf literal;
|
||||
akbasic_Value value;
|
||||
akbasic_Value *unused = NULL;
|
||||
int i = 0;
|
||||
|
||||
(void)expr; (void)lval; (void)rval;
|
||||
/*
|
||||
* READ does not read: it declares that the next DATA line should fill these
|
||||
* identifiers, and skips forward until one appears.
|
||||
* READ now reads. The reference records its identifiers, sets the scope
|
||||
* waiting for a DATA verb and lets execution skip forward until one turns up
|
||||
* -- which never finds a DATA line written *above* the READ, and leaves
|
||||
* nothing for RESTORE to reset. Every DATA item is pre-scanned into one list
|
||||
* before the program runs (see akbasic_data_scan), and this walks a cursor
|
||||
* along it. TODO.md section 6.
|
||||
*/
|
||||
PASS(errctx, akbasic_environment_wait_for_command(obj->environment, "DATA"));
|
||||
obj->environment->readIdentifierIdx = 0;
|
||||
for ( i = 0; i < AKBASIC_MAX_LEAVES; i++ ) {
|
||||
identifier = env->readIdentifierLeaves[i];
|
||||
if ( identifier == NULL ) {
|
||||
break;
|
||||
}
|
||||
PASS(errctx, akbasic_data_next(obj, akbasic_leaf_identifier_type(identifier), &value));
|
||||
|
||||
/*
|
||||
* Build the assignment by hand rather than through the parser: the
|
||||
* identifier leaf is a stored copy and the value came from the item
|
||||
* list, so there is no source text to re-parse.
|
||||
*/
|
||||
PASS(errctx, akbasic_leaf_init(&literal, AKBASIC_LEAF_LITERAL_INT));
|
||||
if ( value.valuetype == AKBASIC_TYPE_STRING ) {
|
||||
literal.leaftype = AKBASIC_LEAF_LITERAL_STRING;
|
||||
snprintf(literal.literal_string, sizeof(literal.literal_string), "%s", value.stringval);
|
||||
} else if ( value.valuetype == AKBASIC_TYPE_FLOAT ) {
|
||||
literal.leaftype = AKBASIC_LEAF_LITERAL_FLOAT;
|
||||
literal.literal_float = value.floatval;
|
||||
} else {
|
||||
literal.literal_int = value.intval;
|
||||
}
|
||||
PASS(errctx, akbasic_leaf_init(&assign, AKBASIC_LEAF_BINARY));
|
||||
assign.left = identifier;
|
||||
assign.right = &literal;
|
||||
assign.operator_ = AKBASIC_TOK_ASSIGNMENT;
|
||||
PASS(errctx, akbasic_runtime_evaluate(obj, &assign, &unused));
|
||||
}
|
||||
env->readIdentifierIdx = 0;
|
||||
SUCCEED_TRUE(obj, dest);
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
akerr_ErrorContext *akbasic_cmd_restore(akbasic_Runtime *obj, akbasic_ASTLeaf *expr, akbasic_Value *lval, akbasic_Value *rval, akbasic_Value **dest)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
akbasic_Value *target = NULL;
|
||||
int64_t line = 0;
|
||||
int i = 0;
|
||||
|
||||
(void)lval; (void)rval;
|
||||
FAIL_ZERO_RETURN(errctx, (obj != NULL && dest != NULL), AKERR_NULLPOINTER,
|
||||
"NULL argument in RESTORE");
|
||||
|
||||
if ( expr == NULL || expr->right == NULL ) {
|
||||
/* Bare RESTORE goes back to the first item, which is what a C128 does. */
|
||||
obj->data_state.cursor = 0;
|
||||
SUCCEED_TRUE(obj, dest);
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
PASS(errctx, akbasic_runtime_evaluate(obj, expr->right, &target));
|
||||
FAIL_NONZERO_RETURN(errctx, (target->valuetype != AKBASIC_TYPE_INTEGER), AKBASIC_ERR_TYPE,
|
||||
"RESTORE expected a line number or a label");
|
||||
line = target->intval;
|
||||
|
||||
/*
|
||||
* The first item at or after that line. "At or after" rather than "on",
|
||||
* because RESTORE 100 in a program whose DATA is on line 110 should find it
|
||||
* -- a program names the line it wants to start reading *from*.
|
||||
*/
|
||||
for ( i = 0; i < obj->data_state.count; i++ ) {
|
||||
if ( obj->data_state.items[i].lineno >= line ) {
|
||||
obj->data_state.cursor = i;
|
||||
SUCCEED_TRUE(obj, dest);
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
}
|
||||
/* Nothing at or after it: the next READ is out of data, which is honest. */
|
||||
obj->data_state.cursor = obj->data_state.count;
|
||||
SUCCEED_TRUE(obj, dest);
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
@@ -759,48 +903,16 @@ akerr_ErrorContext *akbasic_cmd_read(akbasic_Runtime *obj, akbasic_ASTLeaf *expr
|
||||
akerr_ErrorContext *akbasic_cmd_data(akbasic_Runtime *obj, akbasic_ASTLeaf *expr, akbasic_Value *lval, akbasic_Value *rval, akbasic_Value **dest)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
akbasic_Environment *env = obj->environment;
|
||||
akbasic_ASTLeaf *literal = NULL;
|
||||
akbasic_ASTLeaf *identifier = NULL;
|
||||
akbasic_ASTLeaf assign;
|
||||
akbasic_Value *unused = NULL;
|
||||
|
||||
(void)lval; (void)rval;
|
||||
FAIL_ZERO_RETURN(errctx, (expr != NULL && expr->right != NULL), AKERR_NULLPOINTER,
|
||||
"NIL expression or argument list");
|
||||
|
||||
for ( literal = expr->right->right; literal != NULL; literal = literal->next ) {
|
||||
if ( env->readIdentifierIdx >= AKBASIC_MAX_LEAVES ) {
|
||||
break;
|
||||
}
|
||||
identifier = env->readIdentifierLeaves[env->readIdentifierIdx];
|
||||
if ( identifier == NULL ) {
|
||||
break;
|
||||
}
|
||||
/*
|
||||
* Build the assignment by hand rather than through the parser: the
|
||||
* identifier leaf is a stored copy and the literal belongs to this
|
||||
* line's pool, so there is no source text to re-parse.
|
||||
*/
|
||||
PASS(errctx, akbasic_leaf_init(&assign, AKBASIC_LEAF_BINARY));
|
||||
assign.left = identifier;
|
||||
assign.right = literal;
|
||||
assign.operator_ = AKBASIC_TOK_ASSIGNMENT;
|
||||
PASS(errctx, akbasic_runtime_evaluate(obj, &assign, &unused));
|
||||
env->readIdentifierIdx += 1;
|
||||
}
|
||||
|
||||
if ( literal == NULL &&
|
||||
env->readIdentifierIdx < AKBASIC_MAX_LEAVES &&
|
||||
env->readIdentifierLeaves[env->readIdentifierIdx] != NULL ) {
|
||||
/* Out of DATA with READ items outstanding: stay in waiting mode. */
|
||||
SUCCEED_TRUE(obj, dest);
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
PASS(errctx, akbasic_environment_stop_waiting(env, "DATA"));
|
||||
env->lineno = env->readReturnLine;
|
||||
env->readIdentifierIdx = 0;
|
||||
(void)expr; (void)lval; (void)rval;
|
||||
FAIL_ZERO_RETURN(errctx, (obj != NULL && dest != NULL), AKERR_NULLPOINTER,
|
||||
"NULL argument in DATA");
|
||||
/*
|
||||
* Nothing to do. DATA is declaration, not execution: every item was
|
||||
* collected into the item list before the program started, so reaching the
|
||||
* statement means walking past it. The reference did the assigning here,
|
||||
* which is why READ had to skip forward to find one.
|
||||
*/
|
||||
SUCCEED_TRUE(obj, dest);
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user