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:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user