comparison src/interp.s @ 132:917b4893bb3d

Checkpoint before redoing a bunch of code for clarity
author William Astle <lost@l-w.ca>
date Mon, 24 Jun 2024 23:44:39 -0600
parents 95f174bf459b
children e49bd0493baf
comparison
equal deleted inserted replaced
131:95f174bf459b 132:917b4893bb3d
77 ldx #linebuff ; point to start of line input buffer 77 ldx #linebuff ; point to start of line input buffer
78 immediate0a lda ,x ; do we have anything at all? 78 immediate0a lda ,x ; do we have anything at all?
79 beq immediate0 ; brif not - just read another line 79 beq immediate0 ; brif not - just read another line
80 cmpa #0x20 ; space? 80 cmpa #0x20 ; space?
81 bne immediate0c ; brif not 81 bne immediate0c ; brif not
82 immediate0b leax 1,x ; move past the 82 immediate0b leax 1,x ; move past the space
83 bra immediate0a ; keep looking for the start of input 83 bra immediate0a ; keep looking for the start of input
84 immediate0c bsr setcifdigit ; do we have a line number? 84 immediate0c bsr setcifdigit ; do we have a line number?
85 bcs immediate1 ; brif so - go handle program editing 85 bcs immediate1 ; brif so - go handle program editing
86 clrb ; flag to do actual parsing 86 clrb ; flag to do actual parsing
87 jsr parse ; go parse the line 87 jsr parse ; go parse the line
88 bcs immediatee ; brif there was a parse error 88 bcs immediatee ; brif there was a parse error
89 bra * 89 bra *
90 jsr interpretline ; go interpret the tokenized line 90 jsr interpretline ; go interpret the tokenized line
91 bra immediate ; go handle another line 91 bra immediate ; go handle another line
92 immediate1 bsr parse_lineno ; parse the line number 92 immediate1 bsr parse_lineno ; parse the line number
93 bsr prog_findline ; go see if the line is in the program 93 jsr prog_remove ; remove the line from the program if it exists
94 bne immediate3 ; brif not - no need to delete it
95 ldu ,x ; get next line pointer which is where we start the copy from
96 leay ,x ; use temp pointer for copying
97 immediate2 lda ,u+ ; get source byte
98 sta ,y+ ; stash it
99 cmpu vartab ; did we reach the end of the program text?
100 blo immediate2 ; brif not
101 sty vartab ; save new end of program
102 immediate3 jsr curchar ; skip any spaces after line number 94 immediate3 jsr curchar ; skip any spaces after line number
103 tsta ; is it the end of input (don't test for colon) 95 tsta ; is it the end of input (don't test for colon)
104 beq immediate6 ; brif so - we don't need to insert a line 96 beq immediate6 ; brif so - we don't need to insert a line
105 pshs x ; save program insert location and line number
106 ldx inputptr ; point to line text 97 ldx inputptr ; point to line text
107 jsr parse ; tokenize line, get length to D 98 jsr parse ; tokenize line, get length to D
108 leay ,x ; save tokenized line pointer 99 ldy binval ; get the line number
109 addd #4 ; account for next line pointer and line number 100 jsr prog_insert ; insert the encoded line at X into program as line Y
110 ldx vartab ; get start of copy location 101 immediate6 ldx vartab ; clear out variables
111 leau d,x ; set destination copy location D bytes further up
112 stu vartab ; save new end of program
113 immediate4 lda ,-x ; get byte from program
114 sta ,-u ; stash it above the empty space
115 cmpx ,s ; did we reach the insertion point?
116 bne immediate4 ; brif not - keep going
117 leas 2,s ; clear insertion location
118 stu ,x++ ; set next line pointer to not null
119 ldd binval ; set the line number for the program
120 std ,x++
121 immediate5 lda ,y+ ; get byte from tokenized line
122 sta ,x+ ; stash it in the program
123 bne immediate5 ; brif not at end of tokenized line (see note for fixlineptrs)
124 immediate6 bsr prog_fixlineptrs ; fix up line pointers (all of them)
125 ldx vartab ; clear out variables
126 stx objecttab 102 stx objecttab
127 stx freestart 103 stx freestart
128 bra immediate0 ; go handle more input 104 bra immediate0 ; go handle more input
129 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 105 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
130 ; Fix up next line pointers. Enter at prog_fixlineptrs to do the entire program. Enter at prog_fixlineptrsx to start 106 ; Find line number table entry
131 ; at the line pointered to by X, which MUST NOT point to the end of the program. 107 ;
132 ; 108 ; Entry:
133 ; Works by simply scanning for a NUL in the program text after a line header (pointer to next line and line number) 109 ; D: the desired line number
134 ; and uses that as the new next line pointer. A NULL next line pointer flags the end of the program. 110 ;
135 ; 111 ; Exit:
136 ; Observation: if the program text format is changed such that it can include NULs embedded within a line, this routine 112 ; U: pointer to line number table entry
137 ; will need to be updated to grok that. 113 ; CC.C: clear
138 prog_fixlineptrs 114 ;
139 ldx progtext ; point to start of program 115 ; Error:
140 prog_fixlineptrsx 116 ; CC.C: set
141 ldu ,x ; are we at the end of the program? 117 ;
142 beq prog_findline2 ; brif not (borrow RTS from findline) 118 ; This works by doing a binary search through the line number table.
143 leau 4,x ; point to line text (past pointer and line number) 119 prog_findline ldu prog_linetab ; point to program line table
144 prog_fixlineptrs1 120 ldx prog_linetabp ; get end of table
145 lda ,u+ ; are we at the end of this line? 121 leax -prog_lineentl,u ; move back to the start of the last entry
146 bne prog_fixlineptrs1 ; brif not 122 pshs x,u ; save "high" at 0,s and "low" at 2,s
147 stu ,x ; set the next pointer for the previous line 123 tfr d,x ; save line number for later comparisons
148 leax ,u ; move to the next line 124 prog_findline1 ldd ,s ; get high pointer
149 bra prog_fixlineptrsx ; go handle the next line 125 subd 2,s ; get different with low pointer
150 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 126 bcs prog_findline2 ; brif high is below low - we didn't find it
151 ; Find a line in the program. Returns with C set and Z clear if no match and C clear and Z set if a match is found. X 127 lsra ; find half way
152 ; will point to either the exact matched line *or* the line that would be immediately after the desired line number if 128 rorb
153 ; the line had been present, which could be the end of the program. D and U are clobbered. Enter at prog_findlinex to 129 andb #0b11111100 ; round down for 4 bytes per entry
154 ; start searching at the line pointed to by X. Enter at prog_findline to start at the beginning of the program. Enter 130 addd prog_linetab ; offset into line table
155 ; with the desired line number in binval. 131 tfr d,u ; move to a pointer
156 prog_findlinecl ldx curline ; get current line pointer 132 cmpx linetabent_num,u ; is the desired number less, equal, or greater?
157 beq prog_findline ; brif immediate mode 133 beq prog_findline2 ; brif match
158 ldd binval ; get desired line number 134 blo prog_findline3 ; brif desired line is lower
159 cmpd 2,x ; is the desired line number >= current line? 135 leau prog_lineentl,u ; skip past this non-matching item
160 beq prog_findline2 ; brif this is the right line (optimizes goto self) 136 stu 2,s ; save new low pointer
161 bhi prog_findlinex ; brif desired line higher: start here instead of program start 137 bra prog_findline1 ; go do another iteration
162 prog_findline ldx progtext ; point to start of program 138 prog_findline2 leas 4,s ; clean up the temporaries (C clear from compare above)
163 prog_findlinex ldu binval ; get line number to search for 139 rts
164 prog_findline0 ldd ,x ; end of program? 140 prog_findline3 leau -prog_lineentl,u ; move before this non-matching entry
165 beq prog_findline1 ; brif not 141 stu ,s ; save new top entry pointer
166 cmpu 2,x ; does line number match? Z set if so, clear if not; C set not found 142 bra prog_findline1 ; go do another iteration
167 bls prog_findline2 143 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
168 ldx ,x ; move to next line 144 ; Delete a line from the program:
169 bra prog_findline0 ; see if we found the line yet 145 ;
170 prog_findline1 coma ; set carry for not found; also clears Z because D is zero from above 146 ; Entry:
171 prog_findline2 rts 147 ; D: the line number to delete
148 ;
149 ; This routine removes a line from the program. This works by deallocating the line data and moving all subsequent
150 ; line data forward to close the gap. The line table pointer will also be removed and the subsequent line table
151 ; entries will also be brought forward to fill the gap. While closing the gap in the line table, the line data
152 ; pointers will be adjusted to account for the relocation of the line data following the deleted line. The line number
153 ; table size allocation will not be adjusted.
154 prog_delline bsr prog_findline ; get a pointer to the desired line table entry
155 bcs prog_delline3 ; brif the line wasn't in the program - we have nothing to do
156 ldd linetabent_size+linetabent_ptr,u ; get pointer to next line data
157 subd linetabent_ptr,u ; now D is the length of the line data to collapse out
158 pshs d ; save the calculated length - we need it for later
159 ldy linetabent_ptr,u ; get pointer to the line data to delete
160 leax d,y ; point to data to move
161 bra prog_delline1 ; go handle the loop, including the case where we copy nothing
162 prog_delline0 lda ,x+ ; copy a byte down
163 sta ,y+
164 prog_delline1 cmpx vartab ; at the end of the program?
165 blo prog_delline0 ; brif not
166 sty vartab ; save new variable table location
167 prog_delline2 ldx linetabent_size+linetabent_num,u ; get number of next line
168 ldd linetabent_size+linetabent_ptr,u ; get pointer for next line
169 subd ,s ; adjust for length removed in the line data
170 std ,u++ ; save in the vacated entry
171 stx ,u++
172 cmpu prog_linetabp ; at the end of allocated table entries?
173 blo prog_delline2 ; brif not
174 leau -linetabent_size,u ; move back to the last actual entry
175 stu prog_linetabp ; update the line table pointer
176 leas 2,s ; clear out the temp we no longer need
177 jsr cmd_newvars ; clear the variables out
178 prog_delline3 rts
179 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
180 ; prog_shrinklt: shrink the line table to its minimum possible size, but keep it a multiple of linetab_stride entries
181 prog_shrinklt ldd prog_linetabp ; get the end of the table entries
182 subd prog_linetab ; now we have the length of the table
183 andb #(linetabent_size*linetab_stride)-1 ; is there a remainder?
184 beq prog_shrinklt0 ; brif not
185 addd #linetabent_size*linetab_strider
186 prog_shrinklt0 tfr d,u ; put in a pointer register
187 leau linetabent_size,x ; move to the end of the phantom entry
188 cmpu prog_text ; anything to do?
189 beq prog_shrinklt2 ; brif not
190 ldx prog_text ; point to source copy point
191 prog_shrinklt1 lda ,x+ ; copy a byte down
192 sta ,u+
193 cmpx vartab ; end of program?
194 blo prog_shrinklt1 ; brif not
195 stu vartab ; save new end of program
196 jmp cmd_newvars ; clear variables
197 prog_shrinklt2 rts
198
199 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
200 ; Insert a line into the program
201 ;
202 ; Entry:
203 ; D: length of line to insert
204 ; X: pointer to line data to insert
205 ; U: the line number we're adding
206 prog_addline pshs d,x,u ; save length and pointer
207 ldx prog_linetabp ; get line table pointer
208 leax linetabent_size,x ; add in space for new entry
209 cmpx prog_text ; did we run into the program?
210 blo prog_addline0 ; brif not
211 addd #linetabent_size*linetab_stride ; add in space for expanded line table
212 prog_addline0 addd vartab ; calculate the new end of program data
213 jsr checkmem_addr ; verify there is enough memory
214 cmpx prog_text ; do we need to expand the line number table?
215 blo prog_addline3 ; brif not
216 ldx vartab ; point to byte past end of program
217 leau linetab_stride*linetabent_size,x ; set up destination pointer
218 stu vartab ; set up new end of program text
219 prog_addline1 lda ,-x ; copy a byte up
220 sta ,-u
221 cmpx prog_text ; did we hit the start of the program?
222 bne prog_addline1 ; brif not
223 ldx prog_linetab ; point to start of line table
224 prog_addline2 ldd linetabent_ptr,x ; get pointer to this line
225 addd #linetab_stride*linetabent_size ; adjust offset for the expanded table
226 std linetabent_ptr,x
227 leax linetabent_size,x ; move to next entry
228 cmpx prog_linetabp ; at end of table?
229 bls prog_addline2 ; brif we're at the end of the table
230 prog_addline3 ldx prog_linetabp ; repoint to first "free" table entry
231 ldd linetabent_ptr,x ; get pointer for the end of the program
232 std linetabent_ptr+linetabent_size,x ; move it a slot forward
233 ldd 4,s ; get desired line number
234 std linetabent_num,x ; put line number in
235 bra prog_addline5 ; brif so - this is where we add it
236 prog_linetab4 cmpd -linetabent_size+linetabent_num,x ; is our line number less than previous entry?
237 bhs prog_addline6 ; brif not - we're at the right place
238 leax -linetabent_size,x ; move back an entry
239 ldu linetabent_num,x ; move line number to next entry
240 stu linetabent_num+linetabent_size,x
241 ldu linetabent_ptr,x ; and move the pointer
242 stu linetabent_ptr+linetabent_size,x
243 prog_linetab5 cmpx prog_linetab ; at the start of the table?
244 bhi prog_addline4 ; brif not
245 prog_linetab6 ldu vartab ; point to end of program data
246 ldd ,s ; get length of line
247 leay d,u ; Y points to the destination of the move
248 sty vartab ; save new end of program text
249 bra prog_linetab8 ; jump into loop in case nothing to copy
250 prog_linetab7 lda ,-u ; copy a byte up
251 sta ,-y
252 prog_linetab8 cmpu linetabent_ptr,x ; finished the copy?
253 bne prog_linetab7 ; brif not
254 prog_linetab9 leax linetabent_size,x ; move to next entry
255 ldd linetabent_ptr,x ; adjust the pointer for the newly inserted line
256 addd ,s
257 std linetabent_ptr,x
258 cmpx prog_linetabp ; run through the whole table?
259 blo prog_linetab9 ; brif not
260 puls y ; get copy length to counter
261 puls x ; get pointer to line data
262 prog_linetab10 lda ,x+ ; copy a byte into the program data
263 sta ,u+
264 leay -1,y ; done all of it?
265 bne prog_linetab10 ; brif not
266 leas 2,s ; lose line number
267 jmp cmd_newvar ; erase variables
172 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 268 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
173 ; Parse a line number and return it in binval; raise syntax error if the line number overflows 16 bits unsigned. 269 ; Parse a line number and return it in binval; raise syntax error if the line number overflows 16 bits unsigned.
174 ; Preserves; registers except D. This will accept the entire 16 bit unsigned number range which is why there is 270 ; Preserves; registers except D. This will accept the entire 16 bit unsigned number range which is why there is
175 ; a BCS after every shift or add. Enter with the input pointer pointing to the number to parse. 271 ; a BCS after every shift or add. Enter with the input pointer pointing to the number to parse.
176 parse_lineno ldd zero ; clear out accumlator but preserve carry flag 272 parse_lineno ldd zero ; clear out accumlator but preserve carry flag