Add strict pointers: AS PTR TO, POINT ... AT, and the -> operator

A pointer is a distinct declared kind rather than a mode a structure can be in,
which is what "strict" means here: `.` requires a structure on its left and `->`
requires a pointer, neither stands in for the other, and both refusals name the
operator the program should have used. So a reader always knows from the
spelling whether the thing on the left is their own copy or somebody else's
data.

Assignment still copies. POINT is the only way to share, so a program that never
writes it can never be surprised by aliasing -- and `P@ = A@` is refused with a
message saying to POINT it instead, rather than quietly becoming the one
assignment in the language that does not copy.

PTR TO is also the only way a TYPE may refer to itself, since by value it would
have no finite size. That is what makes a linked list possible, and
tests/language/structures/pointers.bas builds one, walks it and renders it.

Rendering follows pointers, so it needs a depth bound where copying does not:
copy stops at a pointer by construction, but two nodes pointing at each other is
easy to write and PRINT would not come back. Four levels, chosen so the bound
bites before the 256-byte render buffer does -- otherwise a cycle would stop
because it ran out of room rather than because it was told to.

Three things the work turned up, all now pinned by tests:

A freshly DIMmed record printed `(UNDEFINED STRING REPRESENTATION FOR 0)` for
every field. Slots now take the type their field declared, so it reads as zeros.

Adding POINT as a verb makes POINT unusable as a type name, and the parser
reported that as "Expected expression or literal" pointing at the line rather
than the problem. The prescan now refuses a reserved word as a type name and
says so.

Field names follow the same reserved-word rule as variable names, enforced by
the loader's own scan -- `TO@` is a bad field name for exactly the reason `TO#`
is a bad variable name. Recorded rather than worked around.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-08-01 11:48:09 -04:00
parent 6a7c8cd920
commit 2a3f68d2c4
17 changed files with 488 additions and 17 deletions

View File

@@ -568,6 +568,45 @@ akerr_ErrorContext *akbasic_parse_label(akbasic_Parser *parser, akbasic_ASTLeaf
* Making them reserved words would break any existing program with a variable
* called `AS#`, and they are only meaningful in this one position.
*/
/**
* @brief `POINT P@ AT target`.
*
* Two expressions with a word between them rather than a comma, because that is
* how it reads: the verb says what is being done and `AT` says which way round
* the two operands go. `POINT P@, A@` would read equally well in either
* direction, and getting it backwards would silently point the wrong thing.
*
* The two are chained through `.next`, the argument link, so the runtime reaches
* them with akbasic_leaf_first_argument() like any other verb's operands.
*/
akerr_ErrorContext *akbasic_parse_point(akbasic_Parser *parser, akbasic_ASTLeaf **dest)
{
PREPARE_ERROR(errctx);
akbasic_ASTLeaf *pointer = NULL;
akbasic_ASTLeaf *target = NULL;
akbasic_ASTLeaf *arglist = NULL;
akbasic_ASTLeaf *command = NULL;
akbasic_Token *word = NULL;
PASS(errctx, akbasic_parser_expression(parser, &pointer));
FAIL_ZERO_RETURN(errctx, akbasic_parser_match1(parser, AKBASIC_TOK_IDENTIFIER),
AKBASIC_ERR_SYNTAX, "Expected POINT <pointer> AT <structure>");
PASS(errctx, akbasic_parser_previous(parser, &word));
FAIL_ZERO_RETURN(errctx, (strcasecmp(word->lexeme, "AT") == 0), AKBASIC_ERR_SYNTAX,
"Expected AT after POINT <pointer>, not %s", word->lexeme);
PASS(errctx, akbasic_parser_expression(parser, &target));
pointer->next = target;
PASS(errctx, akbasic_parser_new_leaf(parser, &arglist));
arglist->leaftype = AKBASIC_LEAF_ARGUMENTLIST;
arglist->operator_ = AKBASIC_TOK_FUNCTION_ARGUMENT;
arglist->right = pointer;
PASS(errctx, akbasic_parser_new_leaf(parser, &command));
PASS(errctx, akbasic_leaf_new_command(command, "POINT", arglist));
*dest = command;
SUCCEED_RETURN(errctx);
}
/**
* @brief `TYPE NAME`, whose body was already read by the prescan.
*
@@ -605,7 +644,14 @@ akerr_ErrorContext *akbasic_parse_dim(akbasic_Parser *parser, akbasic_ASTLeaf **
PASS(errctx, akbasic_parser_previous(parser, &word));
if ( strcasecmp(word->lexeme, "PTR") == 0 ) {
command->literal_int = 1;
FAIL_ZERO_RETURN(errctx, akbasic_parser_match1(parser, AKBASIC_TOK_IDENTIFIER),
/*
* `TO` is already a verb -- FOR ... TO owns it -- so it arrives as a
* command token rather than an identifier. Matched by lexeme here for
* the same reason AS and PTR are: these three words mean something only
* in this one position, and reserving them would break any program with
* a variable called TO#.
*/
FAIL_ZERO_RETURN(errctx, akbasic_parser_match1(parser, AKBASIC_TOK_COMMAND),
AKBASIC_ERR_SYNTAX, "Expected TO after PTR");
PASS(errctx, akbasic_parser_previous(parser, &word));
FAIL_ZERO_RETURN(errctx, (strcasecmp(word->lexeme, "TO") == 0), AKBASIC_ERR_SYNTAX,

View File

@@ -317,7 +317,15 @@ static akerr_ErrorContext *dim_structure(akbasic_Runtime *obj, akbasic_ASTLeaf *
variable->values[0].valuetype = AKBASIC_TYPE_POINTER;
variable->values[0].structtype = typeindex;
variable->values[0].structbase = NULL;
SUCCEED_RETURN(errctx);
}
/*
* Give every slot the type its field declared, so a record that has just
* been DIMmed reads as zeros rather than as undefined values. A program can
* PRINT one before assigning anything, and `RECT(W#=0, H#=0)` is an answer
* where `(UNDEFINED STRING REPRESENTATION FOR 0)` is a puzzle.
*/
PASS(errctx, akbasic_struct_init_slots(obj, typeindex, variable->values));
SUCCEED_RETURN(errctx);
}

View File

@@ -253,3 +253,128 @@ akerr_ErrorContext *akbasic_cmd_type(akbasic_Runtime *obj, akbasic_ASTLeaf *expr
SUCCEED_TRUE(obj, dest);
SUCCEED_RETURN(errctx);
}
/* ---------------------------------------------------------------- POINT --- */
/**
* @brief Find the slot a pointer *lives in*, which is not the same as its value.
*
* `POINT` writes a reference into a pointer, so it needs the storage rather than
* a copy of what is in it -- the same distinction `POKE` and `POINTER()` already
* make with `eval_clone_identifiers`. Both spellings of a pointer are accepted:
* a variable declared `AS PTR TO`, and a field declared the same way.
*/
static akerr_ErrorContext *pointer_slot(akbasic_Runtime *obj, akbasic_ASTLeaf *leaf,
akbasic_Value **slotdest, int *typedest)
{
PREPARE_ERROR(errctx);
akbasic_Variable *variable = NULL;
akbasic_StructField *field = NULL;
akbasic_Value *slot = NULL;
FAIL_ZERO_RETURN(errctx, (leaf != NULL), AKERR_NULLPOINTER, "NULL leaf in pointer_slot");
if ( leaf->leaftype == AKBASIC_LEAF_FIELD ) {
PASS(errctx, akbasic_struct_resolve(obj, leaf, &field, &slot));
FAIL_ZERO_RETURN(errctx, (field->kind == AKBASIC_FIELD_POINTER), AKBASIC_ERR_TYPE,
"%s is not a pointer; only a field declared PTR TO can be POINTed",
field->name);
*slotdest = slot;
*typedest = field->typeindex;
SUCCEED_RETURN(errctx);
}
FAIL_ZERO_RETURN(errctx, (leaf->leaftype == AKBASIC_LEAF_IDENTIFIER_STRUCT),
AKBASIC_ERR_TYPE, "POINT expects a pointer on its left");
PASS(errctx, akbasic_environment_get(obj->environment, leaf->identifier, &variable));
FAIL_ZERO_RETURN(errctx, (variable != NULL), AKBASIC_ERR_UNDEFINED,
"Identifier %s is undefined", leaf->identifier);
FAIL_ZERO_RETURN(errctx, (variable->ispointer), AKBASIC_ERR_TYPE,
"%s was not DIMmed AS PTR TO a type, so it cannot point at anything",
leaf->identifier);
*slotdest = &variable->values[0];
*typedest = variable->structtype;
SUCCEED_RETURN(errctx);
}
akerr_ErrorContext *akbasic_cmd_point(akbasic_Runtime *obj, akbasic_ASTLeaf *expr, akbasic_Value *lval, akbasic_Value *rval, akbasic_Value **dest)
{
PREPARE_ERROR(errctx);
akbasic_ASTLeaf *pointerleaf = NULL;
akbasic_ASTLeaf *targetleaf = NULL;
akbasic_Value *slot = NULL;
akbasic_Value *target = NULL;
int pointertype = -1;
(void)lval; (void)rval;
FAIL_ZERO_RETURN(errctx, (obj != NULL && expr != NULL && dest != NULL), AKERR_NULLPOINTER,
"NULL argument in POINT");
pointerleaf = akbasic_leaf_first_argument(expr);
FAIL_ZERO_RETURN(errctx, (pointerleaf != NULL && pointerleaf->next != NULL),
AKBASIC_ERR_SYNTAX, "Expected POINT <pointer> AT <structure>");
targetleaf = pointerleaf->next;
PASS(errctx, pointer_slot(obj, pointerleaf, &slot, &pointertype));
PASS(errctx, akbasic_runtime_evaluate(obj, targetleaf, &target));
/*
* The target must be a structure, not another pointer. Pointing one pointer
* at another is spelled `Q@ = P@` -- assignment copies a reference, which is
* the one place in the language where copying does not deep-copy, and having
* two ways to write it would blur exactly the line this feature draws.
*/
FAIL_ZERO_RETURN(errctx, (target->valuetype == AKBASIC_TYPE_STRUCT), AKBASIC_ERR_TYPE,
(target->valuetype == AKBASIC_TYPE_POINTER
? "POINT ... AT takes a structure; to copy one pointer to another, assign it"
: "POINT ... AT takes a structure"));
FAIL_ZERO_RETURN(errctx, (target->structbase != NULL), AKBASIC_ERR_VALUE,
"That structure has no storage; DIM it AS a type first");
FAIL_ZERO_RETURN(errctx, (target->structtype == pointertype), AKBASIC_ERR_TYPE,
"This pointer is to %s and cannot be pointed at a %s",
obj->structtypes.types[pointertype].name,
obj->structtypes.types[target->structtype].name);
PASS(errctx, akbasic_value_zero(slot));
slot->valuetype = AKBASIC_TYPE_POINTER;
slot->structtype = target->structtype;
slot->structbase = target->structbase;
SUCCEED_TRUE(obj, dest);
SUCCEED_RETURN(errctx);
}
akerr_ErrorContext *akbasic_struct_init_slots(akbasic_Runtime *obj, int typeindex, akbasic_Value *base)
{
PREPARE_ERROR(errctx);
akbasic_StructType *type = NULL;
int i = 0;
FAIL_ZERO_RETURN(errctx, (obj != NULL && base != NULL), AKERR_NULLPOINTER,
"NULL argument in struct init_slots");
FAIL_ZERO_RETURN(errctx, (typeindex >= 0 && typeindex < obj->structtypes.count),
AKBASIC_ERR_BOUNDS, "Structure type index %d is out of range", typeindex);
type = &obj->structtypes.types[typeindex];
for ( i = 0; i < type->fieldcount; i++ ) {
akbasic_StructField *field = &type->fields[i];
akbasic_Value *slot = base + field->offset;
switch ( field->kind ) {
case AKBASIC_FIELD_STRUCT:
/* Recurses over the type graph, which is finite: a TYPE cannot
contain itself by value, so this cannot run away. */
PASS(errctx, akbasic_struct_init_slots(obj, field->typeindex, slot));
break;
case AKBASIC_FIELD_POINTER:
PASS(errctx, akbasic_value_zero(slot));
slot->valuetype = AKBASIC_TYPE_POINTER;
slot->structtype = field->typeindex;
slot->structbase = NULL;
break;
default:
PASS(errctx, akbasic_value_zero(slot));
slot->valuetype = field->valuetype;
break;
}
}
SUCCEED_RETURN(errctx);
}

View File

@@ -28,6 +28,8 @@
#include <akbasic/runtime.h>
#include <akbasic/structtype.h>
#include "verbs.h"
/** @brief Step over spaces and tabs. */
static const char *skip_space(const char *cursor)
{
@@ -209,6 +211,7 @@ static akerr_ErrorContext *scan_names(akbasic_Runtime *obj)
int64_t i = 0;
int open = -1;
int existing = 0;
const akbasic_Verb *verb = NULL;
for ( i = 0; i < AKBASIC_MAX_SOURCE_LINES; i++ ) {
if ( obj->source[i].code[0] == '\0' ) {
@@ -240,6 +243,16 @@ static akerr_ErrorContext *scan_names(akbasic_Runtime *obj)
PASS(errctx, akbasic_structtype_find(table, name, &existing));
FAIL_NONZERO_RETURN(errctx, (existing >= 0), AKBASIC_ERR_VALUE,
"TYPE %s is declared twice", name);
/*
* A type name is a bare word, and so is every verb, so the two share a
* namespace whether we like it or not. Refused here with a message that
* says so -- left to the parser it comes out as "Expected expression or
* literal", pointing at the line rather than at the problem. This is the
* same check the scanner already makes for variable names.
*/
PASS(errctx, akbasic_verb_lookup(name, &verb));
FAIL_NONZERO_RETURN(errctx, (verb != NULL), AKBASIC_ERR_VALUE,
"TYPE %s: %s is a reserved word and cannot name a type", name, name);
FAIL_ZERO_RETURN(errctx, (table->count < AKBASIC_MAX_STRUCT_TYPES), AKBASIC_ERR_BOUNDS,
"More than %d TYPE declarations", AKBASIC_MAX_STRUCT_TYPES);

View File

@@ -120,6 +120,7 @@ static const akbasic_Verb VERBS[] = {
{ "PAINT", AKBASIC_TOK_COMMAND, -1, akbasic_parse_arglist, akbasic_cmd_paint },
{ "PEEK", AKBASIC_TOK_FUNCTION, 1, NULL, akbasic_fn_peek },
{ "PLAY", AKBASIC_TOK_COMMAND, -1, NULL, akbasic_cmd_play },
{ "POINT", AKBASIC_TOK_COMMAND, -1, akbasic_parse_point, akbasic_cmd_point },
{ "POINTER", AKBASIC_TOK_FUNCTION, 1, NULL, akbasic_fn_pointer },
{ "POINTERVAR", AKBASIC_TOK_FUNCTION, 1, NULL, akbasic_fn_pointervar },
{ "POKE", AKBASIC_TOK_COMMAND, -1, akbasic_parse_poke, akbasic_cmd_poke },

View File

@@ -172,6 +172,8 @@ akerr_ErrorContext AKERR_NOIGNORE *akbasic_cmd_paint(struct akbasic_Runtime *obj
akerr_ErrorContext AKERR_NOIGNORE *akbasic_cmd_scale(struct akbasic_Runtime *obj, akbasic_ASTLeaf *expr, akbasic_Value *lval, akbasic_Value *rval, akbasic_Value **dest);
akerr_ErrorContext AKERR_NOIGNORE *akbasic_cmd_sshape(struct akbasic_Runtime *obj, akbasic_ASTLeaf *expr, akbasic_Value *lval, akbasic_Value *rval, akbasic_Value **dest);
/* Structures -- src/runtime_struct.c and src/parser_commands.c */
akerr_ErrorContext AKERR_NOIGNORE *akbasic_parse_point(struct akbasic_Parser *parser, akbasic_ASTLeaf **dest);
akerr_ErrorContext AKERR_NOIGNORE *akbasic_cmd_point(struct akbasic_Runtime *obj, akbasic_ASTLeaf *expr, akbasic_Value *lval, akbasic_Value *rval, akbasic_Value **dest);
akerr_ErrorContext AKERR_NOIGNORE *akbasic_parse_type(struct akbasic_Parser *parser, akbasic_ASTLeaf **dest);
akerr_ErrorContext AKERR_NOIGNORE *akbasic_cmd_type(struct akbasic_Runtime *obj, akbasic_ASTLeaf *expr, akbasic_Value *lval, akbasic_Value *rval, akbasic_Value **dest);