forked from source-solutions/sebasic4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05_preprocessor.asm
More file actions
337 lines (298 loc) · 9.99 KB
/
Copy path05_preprocessor.asm
File metadata and controls
337 lines (298 loc) · 9.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
; // SE Basic IV 4.2 Cordelia
; // Copyright (c) 1999-2024 Source Solutions, Inc.
; // SE Basic IV is free software: you can redistribute it and/or modify
; // it under the terms of the GNU General Public License as published by
; // the Free Software Foundation, either version 3 of the License, or
; // (at your option) any later version.
; //
; // SE Basic IV is distributed in the hope that it will be useful,
; // but WITHOUT ANY WARRANTY; without even the implied warranty o;
; // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; // GNU General Public License for more details.
; //
; // You should have received a copy of the GNU General Public License
; // along with SE Basic IV. If not, see <http://www.gnu.org/licenses/>.
;;
; // --- PRE-PROCESSOR -------------------------------------------------------
;;
:
; org $4600;
; // pre-processor check for a matching token
match_token:
ex de, hl; // swap pointers
match_loop:
ld a, (de); // next character to A
call alpha; // alpha?
jr nc, match_non_alpha; // jump if not
; res 5, a; // make upper case
and %11011111; // make upper case
match_non_alpha:
bit 7, (hl); // final character of a token?
jr nz, match_last; // jump if so
cp (hl); // compare with token
jr nz, match_exit; // jump if no match
inc de; // next character (in edit line)
inc hl; // next character (in token)
jr match_loop; // loop until done
match_last:
; set 7, a; // set bit 7
or %10000000; // set bit 7
cp (hl); // set zero flag if matched
match_exit:
ex de, hl; // restore pointers
ret; // return
; // skip spaces
skip_space:
inc hl; // next character
ld a, (hl); // get it in A
cp ' '; // is it space?
jr z, skip_space; // jump if so
ret; // return with first non-space character
; // make room and insert a comma
make_room_2_comma:
ld bc, 2; // two spaces
jr make_room_any_comma; // immediate jump
make_room_3_comma:
ld bc, 3; // three spaces
make_room_any_comma:
call make_room_inc_hl; // make room and incremet HL
ld (hl), ','; // insert comma
inc hl; // next character
ret; // done
; // make one space the increment HL
make_1_inc_hl:
ld bc, 1; // one character
make_room_inc_hl:
call make_room; // make space
inc hl; // next character
ret; // done
; // remove characters
remove_three:
call ed_backspace; // remove a character
remove_two:
call ed_backspace; // remove a character
jp ed_backspace; // remove a character and exit
; // substitute STR
sbst_str:
ld hl, (mem_5_1); // restore position
ld (hl), 'S'; // replace
inc hl; // three
ld (hl), 'T'; // characters
inc hl; // with
ld (hl), 'R'; // STR
ret; // and return
; // check for closing parenthesis
close_paren:
inc hl; // next character
ld a, (hl); // get it to A
cp ctrl_cr; // missing close parenthesis
jr z, pop_hl_exit_pp; // drop return address and exit
cp ')'; // close parenthesis
jr nz, close_paren; // jump until
ret; // found
; // check for opening parenthesis
open_paren:
inc hl; // next character
ld a, (hl); // get it in A
cp '('; // is it open parenthesis?
ret z; // return if so
pop_hl_exit_pp:
pop hl; // else drop return addres
; // restore character and exit
exit_pp:
ld hl, (mem_5_1); // restore position
ld a, (hl); // restore character
ret; // done
; // check for ELSE without leading colon
colon_else:
cp ' '; // is it space?
ret nz; // return if not
dec hl; // previous character
ld a, (hl); // get it
cp ':'; // colon?
inc hl; // current character
ld a, (hl); // restore current character
ret z; // return if there is already a colon
ld (mem_5_1), hl; // store position
inc hl; // next character
push de; // stack DE
ld de, tk_ptr_else; // point to token
call match_token; // check for match
pop de; // restore DE
ld hl, (mem_5_1); // restore position
ld a, (hl); // restore character
ret nz; // return if no match
ld (hl), ':'; // insert colon
ret; // done
colour:
push de; // stack DE
ld de, tk_ptr_colour; // point to token
call match_token; // check for match
pop de; // restore DE
call z, ed_backspace; // remove 'U' if match found
jr exit_pp; // done
; // check for FN without trailing space
fn_alpha:
push de; // stack DE
ld de, tk_ptr_fn; // point to token
call match_token; // check for match
pop de; // restore DE
jr nz, exit_pp; // jump if not
inc hl; // next character
ld a, (hl); // get it in A
call alpha; // alpha?
jp nc, exit_pp; // jump if not
call open_paren; // check for '('
dec hl; // back
dec hl; // two
ld (hl), tk_fn; // replace the N with the FN token
jp ed_backspace; // remove the F and exit
; // check for HEX$()
hex_str:
push de; // stack DE
ld de, tk_ptr_hex_str; // point to token
call match_token; // check for match
pop de; // restore DE
jr nz, exit_pp; // jump if not
call sbst_str; // substitute STR$
call close_paren; // find closing parenthesis
call make_room_3_comma; // make three spaces and insert a comma
ld (hl), '1'; // set
inc hl; // base
ld (hl), '6'; // 16
jr exit_pp; // done
; // check for OCT$()
oct_str:
push de; // stack DE
ld de, tk_ptr_oct_str; // point to token
call match_token; // check for match
pop de; // restore DE
jr nz, exit_pp; // jump if not
call sbst_str; // substitute STR$
call close_paren; // find closing parenthesis
call make_room_2_comma; // make two spaces and insert a comma
ld (hl), '8'; // base 8
jr exit_pp; // done
; // check for RND with trailing parameter
rnd_param:
push de; // stack DE
ld de, tk_ptr_rnd; // point to token
call match_token; // check for match
pop de; // restore DE
jp nz, exit_pp; // jump if not
call open_paren; // check for '('
inc hl; // point to number (0 or 1)
inc hl; // point to close parenthesis
inc hl; // point to next character
jp remove_three; // remove the (n))
; // check for SPACE$(n)
space_str:
push de; // stack DE
ld de, tk_ptr_space_str; // point to token
call match_token; // check for match
pop de; // restore DE
jp nz, exit_pp; // jump if not
call close_paren; // find closing parenthesis
call make_room_3_comma; // make three spaces and insert a comma
ld (hl), '3'; // insert decimal '3'
inc hl; // next character
ld (hl), '2'; // innsert decimal '2'
ld hl, (mem_5_1); // restore position
ld (hl), tk_string_str; // sunbstitute STR$
inc hl; // next character
inc hl; // skip 'P'
inc hl; // skip 'A'
inc hl; // skip 'C'
inc hl; // skip 'E'
inc hl; // skip '$''
call remove_three; // remove
jp remove_two; // five
; // check for THEN followed by a number
then_number:
ld (mem_5_1), hl; // store position
push de; // stack DE
ld de, tk_ptr_then; // point to token
call match_token; // check for match
pop de; // restore DE
jp nz, exit_pp; // jump if not
call skip_space; // skip any trailing spaces
call numeric; // digit?
jp c, exit_pp; // jump if not
call make_1_inc_hl; // make one space
ld (hl), tk_goto; // insert a quote mark
ret; // done
; // check for TROFF
troff:
push de; // stack DE
ld de, tk_ptr_troff; // point to token
try_tron:
call match_token; // check for match
pop de; // restore DE
jp nz, exit_pp; // jump if not
found_trace:
ld hl, (mem_5_1); // restore position
inc hl; // point to R
call ed_backspace; // delete T
ld (hl), tk_trace; // replace it with TRACE
ret; // exit to tokenize ON or OFF
; // check for TRON
tron:
push de; // stack DE
ld de, tk_ptr_tron; // point to token
jr try_tron; // immedaite jump
; // check for ><
sbst_ne:
push de; // stack DE
ld de, tk_ptr_ne; // point to token
call match_token; // check for match
pop de; // restore DE
jp nz, exit_pp; // jump if not
ld (hl), tk_ne; // replace with token
jp ed_backspace; // remove previous character and exit
; // check for =<
sbst_le:
push de; // stack DE
ld de, tk_ptr_le; // point to token
call match_token; // check for match
pop de; // restore DE
jp nz, exit_pp; // jump if not
ld (hl), tk_le; // replace with token
jp ed_backspace; // remove previous character and exit
; // check for =>
sbst_ge:
push de; // stack DE
ld de, tk_ptr_ge; // point to token
call match_token; // check for match
pop de; // restore DE
jp nz, exit_pp; // jump if not
ld (hl), tk_ge; // replace with token
jp ed_backspace; // remove previous character and exit
; // check for &H
hex:
push de; // stack DE
ld de, tk_ptr_hex; // point to token
call match_token; // check for match
pop de; // restore DE
jp nz, exit_pp; // jump if not
ld (hl), '$'; // replace with '$'
jp ed_backspace; // remove previous character and exit
; // check for &O
oct:
push de; // stack DE
ld de, tk_ptr_oct; // point to token
call match_token; // check for match
pop de; // restore DE
jp nz, exit_pp; // jump if not
ld (hl), '@'; // replace with '@'
jp ed_backspace; // remove previous character and exit
; // check for ATN()
atn:
push de; // stack DE
ld de, tk_ptr_atn; // point to token
call match_token; // check for match
pop de; // restore DE
jp nz, exit_pp; // jump if not
dec hl; // back one place
call make_1_inc_hl; // make one space
ld (hl), 'A'; // insert 'A'
jp exit_pp; // done