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>
33 lines
1.0 KiB
QBasic
33 lines
1.0 KiB
QBasic
10 REM A strict pointer is a distinct declared kind. Assignment always copies;
|
|
20 REM POINT is the only way to share, so a program that never writes it can
|
|
30 REM never be surprised by aliasing.
|
|
40 TYPE NODE
|
|
50 COUNT#
|
|
60 TAIL@ AS PTR TO NODE
|
|
70 END TYPE
|
|
80 DIM N1@ AS NODE
|
|
90 DIM N2@ AS NODE
|
|
100 DIM N3@ AS NODE
|
|
110 N1@.COUNT# = 10
|
|
120 N2@.COUNT# = 20
|
|
130 N3@.COUNT# = 30
|
|
140 REM A TYPE may refer to itself only through PTR TO, which is what makes a
|
|
150 REM list possible at all -- by value it would have no finite size.
|
|
160 POINT N1@.TAIL@ AT N2@
|
|
170 POINT N2@.TAIL@ AT N3@
|
|
180 DIM WALK@ AS PTR TO NODE
|
|
190 POINT WALK@ AT N1@
|
|
200 PRINT WALK@->COUNT#
|
|
210 WALK@ = WALK@->TAIL@
|
|
220 PRINT WALK@->COUNT#
|
|
230 WALK@ = WALK@->TAIL@
|
|
240 PRINT WALK@->COUNT#
|
|
250 REM An unbound pointer is NOTHING, not a crash.
|
|
260 PRINT WALK@->TAIL@
|
|
270 REM Writing through a pointer changes what it points at.
|
|
280 POINT WALK@ AT N1@
|
|
290 WALK@->COUNT# = 99
|
|
300 PRINT N1@.COUNT#
|
|
310 REM And the whole list renders, following pointers as it goes.
|
|
320 PRINT N1@
|