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>
30 lines
754 B
QBasic
30 lines
754 B
QBasic
10 REM A TYPE declares its fields; each takes its own type from its own suffix,
|
|
20 REM which is the same rule every other name in this language follows.
|
|
30 TYPE COORD
|
|
40 X#
|
|
50 Y#
|
|
60 END TYPE
|
|
70 TYPE SHAPE
|
|
80 NAME$
|
|
90 ORIGIN@ AS COORD
|
|
100 AREA%
|
|
110 END TYPE
|
|
120 DIM A@ AS SHAPE
|
|
130 DIM B@ AS SHAPE
|
|
140 A@.NAME$ = "FIRST"
|
|
150 A@.ORIGIN@.X# = 10
|
|
160 A@.ORIGIN@.Y# = 20
|
|
170 A@.AREA% = 2.5
|
|
180 REM Assignment COPIES. B@ is its own record from here on.
|
|
190 B@ = A@
|
|
200 A@.NAME$ = "CHANGED"
|
|
210 A@.ORIGIN@.X# = 99
|
|
220 PRINT A@
|
|
230 PRINT B@
|
|
240 REM A whole nested record copies as a unit too.
|
|
250 DIM P@ AS COORD
|
|
260 P@ = B@.ORIGIN@
|
|
270 PRINT P@
|
|
280 REM And a structure renders with its type name and its fields.
|
|
290 PRINT B@.ORIGIN@.X# + B@.ORIGIN@.Y#
|