Files
akbasic/src/runtime_trap.c

190 lines
7.6 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> Co-Authored-By: Andrew Kesterson <andrew@aklabs.net>
2026-07-31 21:50:37 -04:00
/**
* @file runtime_trap.c
* @brief The group C verbs: TRAP, RESUME, and the ERR() message lookup.
*
* Error trapping, built on the interrupt machinery COLLISION already uses:
* #AKBASIC_INTERRUPT_ERROR was declared with it and armed by nothing until now.
* A trapped error is a GOSUB the program did not write, entered at a line
* boundary, and `RESUME` is its `RETURN`.
*
* **`ER` and `EL` are ordinary global variables here, spelled `ER#` and `EL#`.**
* A C128 exposes them as bare reserved names, which this dialect cannot do: an
* identifier carries its type in a suffix and a bare name is a *label*
* (`LABEL DONE` / `GOTO DONE`). Writing them into the global scope costs nothing,
* needs no new syntax, and `PRINT ER#` reads the way the rest of the language
* does. Recorded in TODO.md section 5.
*/
#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 )
akerr_ErrorContext *akbasic_trap_set_error_variables(akbasic_Runtime *obj, int status, int64_t line)
{
PREPARE_ERROR(errctx);
akbasic_Variable *variable = NULL;
int64_t zerosubscript[1] = { 0 };
FAIL_ZERO_RETURN(errctx, (obj != NULL), AKERR_NULLPOINTER,
"NULL runtime in set_error_variables");
/*
* The *global* scope, not the active one. A trapped error is reported from
* wherever it happened -- inside a GOSUB body, inside a loop -- and a
* handler that reads ER# has usually been entered through a scope of its
* own. akbasic_runtime_global() walks to the root, which is the only place
* both can see.
*/
PASS(errctx, akbasic_runtime_global(obj, "ER#", &variable));
PASS(errctx, akbasic_variable_set_integer(variable, (int64_t)status, zerosubscript, 1));
PASS(errctx, akbasic_runtime_global(obj, "EL#", &variable));
PASS(errctx, akbasic_variable_set_integer(variable, line, zerosubscript, 1));
SUCCEED_RETURN(errctx);
}
/* ------------------------------------------------------------------ TRAP -- */
akerr_ErrorContext *akbasic_cmd_trap(akbasic_Runtime *obj, akbasic_ASTLeaf *expr, akbasic_Value *lval, akbasic_Value *rval, akbasic_Value **dest)
{
PREPARE_ERROR(errctx);
akbasic_ASTLeaf *target = NULL;
akbasic_Value *value = NULL;
(void)lval; (void)rval;
FAIL_ZERO_RETURN(errctx, (obj != NULL && dest != NULL), AKERR_NULLPOINTER,
"NULL argument in TRAP");
target = (expr != NULL ? expr->right : NULL);
if ( target == NULL ) {
/* Bare TRAP disarms, which is how a C128 turns trapping back off. */
PASS(errctx, akbasic_runtime_disarm_interrupt(obj, AKBASIC_INTERRUPT_ERROR));
SUCCEED_TRUE(obj, dest);
SUCCEED_RETURN(errctx);
}
/*
* A label is taken by name and resolved when the trap fires, exactly as
* COLLISION's handler is: an error handler sits on a line normal flow does
* not fall into.
*/
if ( target->leaftype == AKBASIC_LEAF_IDENTIFIER &&
akbasic_leaf_first_subscript(target) == NULL ) {
PASS(errctx, akbasic_runtime_arm_interrupt(obj, AKBASIC_INTERRUPT_ERROR,
0, target->identifier));
SUCCEED_TRUE(obj, dest);
SUCCEED_RETURN(errctx);
}
PASS(errctx, akbasic_runtime_evaluate(obj, target, &value));
FAIL_NONZERO_RETURN(errctx, (value->valuetype != AKBASIC_TYPE_INTEGER), AKBASIC_ERR_TYPE,
"TRAP expected a line number or a label");
PASS(errctx, akbasic_runtime_arm_interrupt(obj, AKBASIC_INTERRUPT_ERROR,
value->intval, NULL));
SUCCEED_TRUE(obj, dest);
SUCCEED_RETURN(errctx);
}
/* ---------------------------------------------------------------- RESUME -- */
akerr_ErrorContext *akbasic_cmd_resume(akbasic_Runtime *obj, akbasic_ASTLeaf *expr, akbasic_Value *lval, akbasic_Value *rval, akbasic_Value **dest)
{
PREPARE_ERROR(errctx);
akbasic_ASTLeaf *target = NULL;
akbasic_Value *value = NULL;
akbasic_Variable *variable = NULL;
int64_t zerosubscript[1] = { 0 };
int64_t resumeline = 0;
(void)lval; (void)rval;
FAIL_ZERO_RETURN(errctx, (obj != NULL && dest != NULL), AKERR_NULLPOINTER,
"NULL argument in RESUME");
FAIL_ZERO_RETURN(errctx, (obj->handlerenv != NULL), AKBASIC_ERR_STATE,
"RESUME outside the context of a TRAP handler");
FAIL_ZERO_RETURN(errctx, (obj->environment == obj->handlerenv), AKBASIC_ERR_STATE,
"RESUME must return from the handler itself, not from a GOSUB inside it");
FAIL_ZERO_RETURN(errctx, (obj->environment->parent != NULL), AKBASIC_ERR_ENVIRONMENT,
"RESUME from an orphaned environment");
/* Where the error happened, which EL# is holding. */
PASS(errctx, akbasic_runtime_global(obj, "EL#", &variable));
PASS(errctx, akbasic_variable_get_subscript(variable, zerosubscript, 1, &value));
resumeline = value->intval;
target = (expr != NULL ? expr->right : NULL);
if ( target == NULL ) {
/*
* Bare RESUME retries the line that failed. That is a loop unless the
* handler fixed whatever was wrong, which is the program's business and
* is exactly what the verb is for.
*/
obj->environment->parent->nextline = resumeline;
} else if ( target->leaftype == AKBASIC_LEAF_COMMAND &&
strcmp(target->identifier, "NEXT") == 0 ) {
/* RESUME NEXT carries on at the line after the one that failed. */
obj->environment->parent->nextline = resumeline + 1;
} else {
PASS(errctx, akbasic_runtime_evaluate(obj, target, &value));
FAIL_NONZERO_RETURN(errctx, (value->valuetype != AKBASIC_TYPE_INTEGER),
AKBASIC_ERR_TYPE, "RESUME expected NEXT, a line number or a label");
obj->environment->parent->nextline = value->intval;
}
obj->handlerenv = NULL;
PASS(errctx, akbasic_runtime_prev_environment(obj));
SUCCEED_TRUE(obj, dest);
SUCCEED_RETURN(errctx);
}
/* ------------------------------------------------------------------- ERR -- */
akerr_ErrorContext *akbasic_fn_err(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;
akbasic_Value *out = NULL;
const char *name = NULL;
int64_t status = 0;
(void)lval; (void)rval;
FAIL_ZERO_RETURN(errctx, (obj != NULL && expr != NULL && dest != NULL), AKERR_NULLPOINTER,
"NULL argument in ERR");
arg = akbasic_leaf_first_argument(expr);
FAIL_ZERO_RETURN(errctx, (arg != NULL), AKBASIC_ERR_SYNTAX, "ERR expected an error number");
PASS(errctx, akbasic_runtime_evaluate(obj, arg, &value));
FAIL_NONZERO_RETURN(errctx, (value->valuetype == AKBASIC_TYPE_STRING), AKBASIC_ERR_TYPE,
"ERR expected a number");
status = (value->valuetype == AKBASIC_TYPE_FLOAT)
? (int64_t)value->floatval : value->intval;
PASS(errctx, akbasic_environment_new_value(obj->environment, &out));
PASS(errctx, akbasic_value_zero(out));
out->valuetype = AKBASIC_TYPE_STRING;
/*
* The name libakerror registered for the code, which is where every status
* in this process is named -- akbasic's own band, libakgl's, and the errno
* values underneath. It answers for an unregistered code too, with "Unknown
* Error", so there is nothing to add here; the NULL guard is for a libakerror
* that ever decides otherwise.
*/
name = akerr_name_for_status((int)status, NULL);
snprintf(out->stringval, sizeof(out->stringval), "%s",
(name != NULL ? name : "Unknown Error"));
*dest = out;
SUCCEED_RETURN(errctx);
}