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:
32
tests/language/structures/pointers.bas
Normal file
32
tests/language/structures/pointers.bas
Normal file
@@ -0,0 +1,32 @@
|
||||
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@
|
||||
6
tests/language/structures/pointers.txt
Normal file
6
tests/language/structures/pointers.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
10
|
||||
20
|
||||
30
|
||||
NOTHING
|
||||
99
|
||||
NODE(COUNT#=99, TAIL@=NODE(COUNT#=20, TAIL@=NODE(COUNT#=30, TAIL@=NOTHING)))
|
||||
@@ -1,12 +1,12 @@
|
||||
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 POINT
|
||||
30 TYPE COORD
|
||||
40 X#
|
||||
50 Y#
|
||||
60 END TYPE
|
||||
70 TYPE SHAPE
|
||||
80 NAME$
|
||||
90 ORIGIN@ AS POINT
|
||||
90 ORIGIN@ AS COORD
|
||||
100 AREA%
|
||||
110 END TYPE
|
||||
120 DIM A@ AS SHAPE
|
||||
@@ -22,7 +22,7 @@
|
||||
220 PRINT A@
|
||||
230 PRINT B@
|
||||
240 REM A whole nested record copies as a unit too.
|
||||
250 DIM P@ AS POINT
|
||||
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.
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
SHAPE(NAME$=CHANGED, ORIGIN@=POINT(X#=99, Y#=20), AREA%=2.500000)
|
||||
SHAPE(NAME$=FIRST, ORIGIN@=POINT(X#=10, Y#=20), AREA%=2.500000)
|
||||
POINT(X#=10, Y#=20)
|
||||
SHAPE(NAME$=CHANGED, ORIGIN@=COORD(X#=99, Y#=20), AREA%=2.500000)
|
||||
SHAPE(NAME$=FIRST, ORIGIN@=COORD(X#=10, Y#=20), AREA%=2.500000)
|
||||
COORD(X#=10, Y#=20)
|
||||
30
|
||||
|
||||
9
tests/language/structures/reserved_names.bas
Normal file
9
tests/language/structures/reserved_names.bas
Normal file
@@ -0,0 +1,9 @@
|
||||
10 REM A type name and a field name are bare words, and so is every verb, so
|
||||
20 REM they share a namespace whether we like it or not. Both are refused with
|
||||
30 REM the same rule the scanner already applies to variable names -- and a type
|
||||
40 REM name is refused by the prescan, which can say so plainly rather than
|
||||
50 REM leaving the parser to report "Expected expression or literal".
|
||||
60 TYPE POINT
|
||||
70 X#
|
||||
80 END TYPE
|
||||
90 PRINT 1
|
||||
2
tests/language/structures/reserved_names.txt
Normal file
2
tests/language/structures/reserved_names.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
? 90 : PARSE ERROR TYPE POINT: POINT is a reserved word and cannot name a type
|
||||
|
||||
Reference in New Issue
Block a user