57
|
1 \ This is the file kernel.4, included by the cross compiler.
|
|
2 \ created 1994 by L.C. Benschop.
|
|
3 \ copyleft (c) 1994-2014 by the sbc09 team, see AUTHORS for more details.
|
|
4 \ license: GNU General Public License version 2, see LICENSE for more details.
|
|
5
|
|
6 \ It is excessively commented as it must serve as an introduction to the
|
|
7 \ construction of Forth compilers.
|
|
8
|
|
9 \ Lines starting with \G are comments that are included in the glossary.
|
|
10
|
|
11 ALSO TRANSIENT DEFINITIONS
|
|
12 FORWARD THROW
|
|
13 FORWARD COLD
|
|
14 FORWARD WARM
|
|
15 PREVIOUS DEFINITIONS
|
|
16
|
|
17 ALSO ASSEMBLER DEFINITIONS
|
|
18
|
|
19 : NEXT
|
|
20 \ JMP $300 \ For tracing/debugging.
|
|
21 PULU PC \ For normal use.
|
|
22 ;
|
|
23
|
|
24 PREVIOUS DEFINITIONS
|
|
25
|
|
26 ASSEMBLE HEX
|
|
27
|
|
28 ORIGIN ORG
|
|
29 7E C, TRANSIENT COLD ASSEMBLER
|
|
30 7E C, TRANSIENT WARM ASSEMBLER \ Jumps to cold and warm entry points.
|
|
31
|
|
32 ENDASM
|
|
33
|
|
34 DECIMAL
|
|
35 CROSS-COMPILE
|
|
36
|
|
37 LABEL DOCON
|
|
38 LDD 0 ,S [] \ Get constant.
|
|
39 STD 0 ,S \ Store it on stack.
|
|
40 LABEL DOVAR
|
|
41 NEXT
|
|
42
|
|
43 LABEL DOCOL
|
|
44 STU ,--Y \ Save IP on return stack.
|
|
45 LDU ,S++ \ Pop IP from stack where it is left by JSR DOCOL.
|
|
46 NEXT
|
|
47
|
|
48 LABEL DODEFER
|
|
49 LDX ,S++ [] \ Get jump address.
|
|
50 JMP 0 ,X
|
|
51 ENDASM
|
|
52
|
|
53 CODE LIT ( --- n)
|
|
54 LDD ,U++ \ Get literal from instruction stream.
|
|
55 STD ,--S
|
|
56 NEXT
|
|
57 END-CODE
|
|
58
|
|
59 CODE BRANCH
|
|
60 LABEL BR
|
|
61 LDU 0 ,U
|
|
62 NEXT
|
|
63 END-CODE
|
|
64
|
|
65 CODE ?BRANCH ( f ---)
|
|
66 LDD ,S++
|
|
67 BEQ BR \ Bracnh if TOS is zero.
|
|
68 LEAU 2 ,U \ Skip branch address.
|
|
69 NEXT
|
|
70 END-CODE
|
|
71
|
|
72 CODE EXECUTE ( a ---)
|
|
73 RTS
|
|
74 END-CODE
|
|
75
|
|
76 CODE EXIT
|
|
77 LDU ,Y++
|
|
78 NEXT
|
|
79 END-CODE
|
|
80
|
|
81 CODE UNNEST
|
|
82 LDU ,Y++
|
|
83 NEXT
|
|
84 END-CODE
|
|
85
|
|
86 CODE (DO) ( l s ---)
|
|
87 LDD ,S++
|
|
88 LABEL DO1
|
|
89 SUBD 0 ,S
|
|
90 EORA # $80 \ Now START-LIMIT-$8000 Initial value for counter.
|
|
91 LDX ,S++
|
|
92 STX ,--Y \ Push limit value.
|
|
93 STD ,--Y
|
|
94 NEXT
|
|
95 END-CODE
|
|
96
|
|
97 CODE (?DO) ( l s ---)
|
|
98 LDD ,S++
|
|
99 CMPD 0 ,S
|
|
100 0<> IF
|
|
101 LEAU 2 ,U \ Skip branch address.
|
|
102 BRA DO1
|
|
103 THEN
|
|
104 LEAS 2 ,S
|
|
105 BRA BR
|
|
106 END-CODE
|
|
107
|
|
108 CODE (LOOP)
|
|
109 LDD 0 ,Y
|
|
110 ADDD # 1
|
|
111 LABEL LOOP1
|
|
112 VC IF
|
|
113 STD 0 ,Y
|
|
114 LDU 0 ,U
|
|
115 NEXT
|
|
116 THEN
|
|
117 LEAY 4 ,Y \ Discard parameters from return stack.
|
|
118 LEAU 2 ,U \ Skip branch address.
|
|
119 NEXT
|
|
120 END-CODE
|
|
121
|
|
122 CODE (+LOOP) ( n ---)
|
|
123 LDD 0 ,Y
|
|
124 ADDD ,S++
|
|
125 BRA LOOP1
|
|
126 END-CODE
|
|
127
|
|
128 CODE (LEAVE)
|
|
129 LABEL LEAV1
|
|
130 LDU 0 ,U
|
|
131 LEAY 4 ,Y
|
|
132 NEXT
|
|
133 END-CODE
|
|
134
|
|
135 CODE (?LEAVE) ( f ---)
|
|
136 LDD ,S++
|
|
137 BEQ LEAV1
|
|
138 LEAU 2 ,U
|
|
139 NEXT
|
|
140 END-CODE
|
|
141
|
|
142 CODE I ( --- n)
|
|
143 LDD 0 ,Y
|
|
144 EORA # $80
|
|
145 ADDD 2 ,Y
|
|
146 STD ,--S
|
|
147 NEXT
|
|
148 END-CODE
|
|
149
|
|
150 CODE I' ( ---n)
|
|
151 LDD 2 ,Y
|
|
152 STD ,--S
|
|
153 NEXT
|
|
154 END-CODE
|
|
155
|
|
156 CODE J ( ---n)
|
|
157 LDD 4 ,Y
|
|
158 EORA # $80
|
|
159 ADDD 6 ,Y
|
|
160 STD ,--S
|
|
161 NEXT
|
|
162 END-CODE
|
|
163
|
|
164 CODE UNLOOP
|
|
165 LEAY 4 ,Y
|
|
166 NEXT
|
|
167 END-CODE
|
|
168
|
|
169 CODE R@ ( --- n)
|
|
170 LDD 0 ,Y
|
|
171 STD ,--S
|
|
172 NEXT
|
|
173 END-CODE
|
|
174
|
|
175 CODE >R ( n ---)
|
|
176 LDD ,S++
|
|
177 STD ,--Y
|
|
178 NEXT
|
|
179 END-CODE
|
|
180
|
|
181 CODE R> ( --- n)
|
|
182 LDD ,Y++
|
|
183 STD ,--S
|
|
184 NEXT
|
|
185 END-CODE
|
|
186
|
|
187 CODE RP@ ( --- addr)
|
|
188 PSHS Y
|
|
189 NEXT
|
|
190 END-CODE
|
|
191
|
|
192 CODE RP! ( addr --- )
|
|
193 PULS Y
|
|
194 NEXT
|
|
195 END-CODE
|
|
196
|
|
197 CODE SP@ ( --- addr)
|
|
198 TFR S, D
|
|
199 STD ,--S
|
|
200 NEXT
|
|
201 END-CODE
|
|
202
|
|
203 CODE SP! ( addr ---)
|
|
204 LDD ,S++
|
|
205 TFR D, S
|
|
206 NEXT
|
|
207 END-CODE
|
|
208
|
|
209 CODE UM* ( u1 u2 --- ud)
|
|
210 LEAS -4 ,S \ Create room for result.
|
|
211 LDA 7 ,S
|
|
212 LDB 5 ,S
|
|
213 MUL \ Multiply least significant bytes.
|
|
214 STD 2 ,S
|
|
215 LDA 7 ,S
|
|
216 LDB 4 ,S
|
|
217 MUL \ Multiply LSB of n1 and MSB of n2.
|
|
218 ADDB 2 ,S
|
|
219 ADCA # 0
|
|
220 STD 1 ,S
|
|
221 LDA 6 ,S
|
|
222 LDB 5 ,S
|
|
223 MUL \ Multiply LSB of n2 and MSB of n1.
|
|
224 ADDD 1 ,S
|
|
225 STD 1 ,S
|
|
226 LDA # 0
|
|
227 ADCA # 0
|
|
228 STA 0 ,S
|
|
229 LDA 6 ,S
|
|
230 LDB 4 ,S
|
|
231 MUL \ Multiply most significant bytes.
|
|
232 ADDD 0 ,S
|
|
233 STD 4 ,S
|
|
234 LDD 2 ,S
|
|
235 STD 6 ,S \ Move result to position of numbers.
|
|
236 LEAS 4 ,S \ Reclaim extra space for result.
|
|
237 NEXT
|
|
238 END-CODE
|
|
239
|
|
240 CODE UM/MOD ( ud u --- rem quot)
|
|
241 LEAS -1 ,S \ Create room for iteration counter.
|
|
242 LDA # 16
|
|
243 STA 0 ,S
|
|
244 BEGIN
|
|
245 ASL 6 ,S
|
|
246 ROL 5 ,S
|
|
247 ROL 4 ,S
|
|
248 ROL 3 ,S
|
|
249 LDD 3 ,S
|
|
250 U< IF \ Account for extra bit shifted out, perform subtraction anyway.
|
|
251 SUBD 1 ,S
|
|
252 STD 3 ,S
|
|
253 INC 6 ,S
|
|
254 ELSE
|
|
255 SUBD 1 ,S \ Perform trial subtraction.
|
|
256 U>= IF
|
|
257 STD 3 ,S
|
|
258 INC 6 ,S \ Add 1-bit to quotient.
|
|
259 THEN
|
|
260 THEN
|
|
261 DEC 0 ,S
|
|
262 0= UNTIL
|
|
263 LEAS 3 ,S
|
|
264 LDD 2 ,S
|
|
265 LDX 0 ,S
|
|
266 STX 2 ,S
|
|
267 STD 0 ,S
|
|
268 NEXT
|
|
269 END-CODE
|
|
270
|
|
271 CODE + ( n1 n2 ---n3)
|
|
272 LDD ,S++
|
|
273 ADDD 0 ,S
|
|
274 STD 0 ,S
|
|
275 NEXT
|
|
276 END-CODE
|
|
277
|
|
278 CODE - ( n1 n2 ---n3)
|
|
279 LDD 2 ,S
|
|
280 SUBD ,S++
|
|
281 STD 0 ,S
|
|
282 NEXT
|
|
283 END-CODE
|
|
284
|
|
285 CODE NEGATE ( n1 --- n2)
|
|
286 CLRA
|
|
287 CLRB
|
|
288 SUBD 0 ,S
|
|
289 STD 0 ,S
|
|
290 NEXT
|
|
291 END-CODE
|
|
292
|
|
293 CODE AND ( n1 n2 ---n3)
|
|
294 LDD 2 ,S
|
|
295 ANDA ,S+
|
|
296 ANDB ,S+
|
|
297 STD 0 ,S
|
|
298 NEXT
|
|
299 END-CODE
|
|
300
|
|
301 CODE OR ( n1 n2 ---n3)
|
|
302 LDD 2 ,S
|
|
303 ORA ,S+
|
|
304 ORB ,S+
|
|
305 STD 0 ,S
|
|
306 NEXT
|
|
307 END-CODE
|
|
308
|
|
309 CODE XOR ( n1 n2 ---n3)
|
|
310 LDD 2 ,S
|
|
311 EORA ,S+
|
|
312 EORB ,S+
|
|
313 STD 0 ,S
|
|
314 NEXT
|
|
315 END-CODE
|
|
316
|
|
317 CODE 1+ ( n1 --- n2)
|
|
318 INC 1 ,S
|
|
319 0= IF INC 0 ,S THEN
|
|
320 NEXT
|
|
321 END-CODE
|
|
322
|
|
323 CODE 1- ( n1 --- n2)
|
|
324 LDD 0 ,S
|
|
325 SUBD # 1
|
|
326 STD 0 ,S
|
|
327 NEXT
|
|
328 END-CODE
|
|
329
|
|
330 CODE 2+ ( n1 --- n2)
|
|
331 LDD 0 ,S
|
|
332 ADDD # 2
|
|
333 STD 0 ,S
|
|
334 NEXT
|
|
335 END-CODE
|
|
336
|
|
337 CODE 2- ( n1 --- n2)
|
|
338 LDD 0 ,S
|
|
339 SUBD # 2
|
|
340 STD 0 ,S
|
|
341 NEXT
|
|
342 END-CODE
|
|
343
|
|
344 CODE 2* ( n1 --- n2)
|
|
345 LDD 0 ,S
|
|
346 ASLB
|
|
347 ROLA
|
|
348 STD 0 ,S
|
|
349 NEXT
|
|
350 END-CODE
|
|
351
|
|
352 CODE 2/ ( n1 --- n2)
|
|
353 LDD 0 ,S
|
|
354 ASRA
|
|
355 RORB
|
|
356 STD 0 ,S
|
|
357 NEXT
|
|
358 END-CODE
|
|
359
|
|
360 CODE D+ ( d1 d2 --- d3)
|
|
361 LDD 6 ,S
|
|
362 ADDD 2 ,S
|
|
363 STD 6 ,S
|
|
364 LDD 4 ,S
|
|
365 ADCB 1 ,S
|
|
366 ADCA 0 ,S
|
|
367 STD 4 ,S
|
|
368 LEAS 4 ,S
|
|
369 NEXT
|
|
370 END-CODE
|
|
371
|
|
372 CODE DNEGATE ( d1 --- d2)
|
|
373 CLRA
|
|
374 CLRB
|
|
375 SUBD 2 ,S
|
|
376 STD 2 ,S
|
|
377 LDD # 0
|
|
378 SBCB 1 ,S
|
|
379 SBCA 0 ,S
|
|
380 STD 0 ,S
|
|
381 NEXT
|
|
382 END-CODE
|
|
383
|
|
384 CODE LSHIFT ( u1 n1 --- u2)
|
|
385 PULS D
|
|
386 TSTB
|
|
387 0<> IF
|
|
388 BEGIN
|
|
389 ASL 1 ,S
|
|
390 ROL 0 ,S
|
|
391 DECB
|
|
392 0= UNTIL
|
|
393 THEN
|
|
394 NEXT
|
|
395 END-CODE
|
|
396
|
|
397 CODE RSHIFT ( u1 n1 --- u2)
|
|
398 PULS D
|
|
399 TSTB
|
|
400 0<> IF
|
|
401 BEGIN
|
|
402 LSR 0 ,S
|
|
403 ROR 1 ,S
|
|
404 DECB
|
|
405 0= UNTIL
|
|
406 THEN
|
|
407 NEXT
|
|
408 END-CODE
|
|
409
|
|
410 CODE DROP ( n --- )
|
|
411 LEAS 2 ,S
|
|
412 NEXT
|
|
413 END-CODE
|
|
414
|
|
415 CODE DUP ( n --- n n )
|
|
416 LDD 0 ,S
|
|
417 STD ,--S
|
|
418 NEXT
|
|
419 END-CODE
|
|
420
|
|
421 CODE SWAP ( n1 n2 --- n2 n1)
|
|
422 LDD 0 ,S
|
|
423 LDX 2 ,S
|
|
424 STX 0 ,S
|
|
425 STD 2 ,S
|
|
426 NEXT
|
|
427 END-CODE
|
|
428
|
|
429 CODE OVER ( n1 n2 --- n1 n2 n1)
|
|
430 LDD 2 ,S
|
|
431 STD ,--S
|
|
432 NEXT
|
|
433 END-CODE
|
|
434
|
|
435 CODE ROT ( n1 n2 n3 --- n2 n3 n1)
|
|
436 LDD 4 ,S
|
|
437 LDX 0 ,S
|
|
438 STD 0 ,S
|
|
439 LDD 2 ,S
|
|
440 STX 2 ,S
|
|
441 STD 4 ,S
|
|
442 NEXT
|
|
443 END-CODE
|
|
444
|
|
445 CODE -ROT ( n1 n2 n3 --- n3 n1 n2)
|
|
446 LDD 4 ,S
|
|
447 LDX 2 ,S
|
|
448 STD 2 ,S
|
|
449 LDD 0 ,S
|
|
450 STX 0 ,S
|
|
451 STD 4 ,S
|
|
452 NEXT
|
|
453 END-CODE
|
|
454
|
|
455 CODE 2DROP ( d ---)
|
|
456 LEAS 4 ,S
|
|
457 NEXT
|
|
458 END-CODE
|
|
459
|
|
460 CODE 2DUP ( d --- d d )
|
|
461 LDX 2 ,S
|
|
462 LDD 0 ,S
|
|
463 PSHS X, D
|
|
464 NEXT
|
|
465 END-CODE
|
|
466
|
|
467 CODE 2SWAP ( d1 d2 --- d2 d1)
|
|
468 LDD 6 ,S
|
|
469 LDX 2 ,S
|
|
470 STD 2 ,S
|
|
471 STX 6 ,S
|
|
472 LDD 4 ,S
|
|
473 LDX 0 ,S
|
|
474 STD 0 ,S
|
|
475 STX 4 ,S
|
|
476 NEXT
|
|
477 END-CODE
|
|
478
|
|
479 CODE 2OVER ( d1 d2 --- d1 d2 d1)
|
|
480 LDX 6 ,S
|
|
481 LDD 4 ,S
|
|
482 PSHS X, D
|
|
483 NEXT
|
|
484 END-CODE
|
|
485
|
|
486 CODE PICK ( n1 --- n2)
|
|
487 LDD 0 ,S
|
|
488 ADDD ,S++
|
|
489 LDD D,S
|
|
490 STD ,--S
|
|
491 NEXT
|
|
492 END-CODE
|
|
493
|
|
494 CODE ROLL ( n1 ---)
|
|
495 LDD 0 ,S
|
|
496 LEAS -2 ,S \ Make room to store counter.
|
|
497 ADDD # 1
|
|
498 STD 0 ,S \ Store 1 plus the counter.
|
|
499 ADDD 2 ,S \ Double counter.
|
|
500 ADDD # 3
|
|
501 LEAX D,S \ Point past last elemtn to roll on stack.
|
|
502 LEAX 2 ,X
|
|
503 LDD D,S
|
|
504 STD 2 ,S \ Store element picked.
|
|
505 INC 0 ,S
|
|
506 BEGIN
|
|
507 BEGIN
|
|
508 LDD -4 ,X
|
|
509 STD ,--X
|
|
510 DEC 1 ,S
|
|
511 0= UNTIL
|
|
512 DEC 0 ,S
|
|
513 0= UNTIL
|
|
514 LEAS 4 ,S
|
|
515 NEXT
|
|
516 END-CODE
|
|
517
|
|
518 CODE C@ ( addr --- c)
|
|
519 LDB 0 ,S []
|
|
520 CLRA
|
|
521 STD 0 ,S
|
|
522 NEXT
|
|
523 END-CODE
|
|
524
|
|
525 CODE @ ( addr --- n)
|
|
526 LDD 0 ,S []
|
|
527 STD 0 ,S
|
|
528 NEXT
|
|
529 END-CODE
|
|
530
|
|
531 CODE C! ( c addr ---)
|
|
532 LDB 3 ,S
|
|
533 STB 0 ,S []
|
|
534 LEAS 4 ,S
|
|
535 NEXT
|
|
536 END-CODE
|
|
537
|
|
538 CODE ! ( n addr ---)
|
|
539 LDD 2 ,S
|
|
540 STD 0 ,S []
|
|
541 LEAS 4 ,S
|
|
542 NEXT
|
|
543 END-CODE
|
|
544
|
|
545 CODE +! ( n addr ---)
|
|
546 PULS X
|
|
547 PULS D
|
|
548 ADDD 0 ,X
|
|
549 STD 0 ,X
|
|
550 NEXT
|
|
551 END-CODE
|
|
552
|
|
553 CODE 2@ ( addr --- d)
|
|
554 LDX 0 ,S
|
|
555 LDD 0 ,X
|
|
556 LDX 2 ,X
|
|
557 STX 0 ,S
|
|
558 STD ,--S
|
|
559 NEXT
|
|
560 END-CODE
|
|
561
|
|
562 CODE 2! ( d addr ---)
|
|
563 LDX 0 ,S
|
|
564 LDD 2 ,S
|
|
565 STD 0 ,X
|
|
566 LDD 4 ,S
|
|
567 STD 2 ,X
|
|
568 LEAS 6 ,S
|
|
569 NEXT
|
|
570 END-CODE
|
|
571
|
|
572 LABEL YES \ Store a true flag on stack.
|
|
573 LEAX -1 ,X
|
|
574 STX 0 ,S
|
|
575 NEXT
|
|
576 ENDASM
|
|
577
|
|
578 CODE 0= ( n --- f)
|
|
579 LDX # 0
|
|
580 LDD 0 ,S
|
|
581 BEQ YES
|
|
582 STX 0 ,S
|
|
583 NEXT
|
|
584 END-CODE
|
|
585
|
|
586 CODE 0< ( n --- f)
|
|
587 LDX # 0
|
|
588 LDD 0 ,S
|
|
589 BMI YES
|
|
590 STX 0 ,S
|
|
591 NEXT
|
|
592 END-CODE
|
|
593
|
|
594 CODE < ( n1 n2 --- f)
|
|
595 LDX # 0
|
|
596 LDD 2 ,S
|
|
597 SUBD ,S++
|
|
598 BLT YES
|
|
599 STX 0 ,S
|
|
600 NEXT
|
|
601 END-CODE
|
|
602
|
|
603 CODE U< ( n1 n2 --- f)
|
|
604 LDX # 0
|
|
605 LDD 2 ,S
|
|
606 SUBD ,S++
|
|
607 BLO YES
|
|
608 STX 0 ,S
|
|
609 NEXT
|
|
610 END-CODE
|
|
611
|
|
612 CODE CMOVE ( addr1 addr2 n ---)
|
|
613 LDX 4 ,S
|
|
614 STY 4 ,S
|
|
615 LDY 2 ,S
|
|
616 LDD 0 ,S
|
|
617 0<> IF
|
|
618 INC 0 ,S
|
|
619 BEGIN
|
|
620 BEGIN
|
|
621 LDA ,X+
|
|
622 STA ,Y+
|
|
623 DECB
|
|
624 0= UNTIL
|
|
625 DEC 0 ,S
|
|
626 0= UNTIL
|
|
627 THEN
|
|
628 LDY 4 ,S
|
|
629 LEAS 6 ,S
|
|
630 NEXT
|
|
631 END-CODE
|
|
632
|
|
633 CODE CMOVE> ( addr1 addr2 n ---)
|
|
634 LDX 4 ,S
|
|
635 STY 4 ,S
|
|
636 LDY 2 ,S
|
|
637 LDD 0 ,S
|
|
638 LEAX D,X
|
|
639 LEAY D,Y
|
|
640 LDD 0 ,S
|
|
641 0<> IF
|
|
642 INC 0 ,S
|
|
643 BEGIN
|
|
644 BEGIN
|
|
645 LDA ,-X
|
|
646 STA ,-Y
|
|
647 DECB
|
|
648 0= UNTIL
|
|
649 DEC 0 ,S
|
|
650 0= UNTIL
|
|
651 THEN
|
|
652 LDY 4 ,S
|
|
653 LEAS 6 ,S
|
|
654 NEXT
|
|
655 END-CODE
|
|
656
|
|
657 CODE FILL ( addr n c ---)
|
|
658 LDX 4 ,S
|
|
659 LDD 2 ,S
|
|
660 0<> IF
|
|
661 INC 2 ,S
|
|
662 LDA 1 ,S
|
|
663 BEGIN
|
|
664 BEGIN
|
|
665 STA ,X+
|
|
666 DECB
|
|
667 0= UNTIL
|
|
668 DEC 2 ,S
|
|
669 0= UNTIL
|
|
670 THEN
|
|
671 LEAS 6 ,S
|
|
672 NEXT
|
|
673 END-CODE
|
|
674
|
|
675 CODE (FIND) ( word firstnfa --- cfa/word f )
|
|
676 LDX 0 ,S
|
|
677 0<> IF
|
|
678 STU 0 ,S
|
|
679 LDU 2 ,S
|
|
680 STY 2 ,S
|
|
681 PSHS U
|
|
682 BEGIN
|
|
683 TFR X, Y
|
|
684 LDA ,X+
|
|
685 ANDA # $1F
|
|
686 CMPA ,U+ \ Compare count bytes.
|
|
687 0= IF \ Do count bytes match?
|
|
688 BEGIN
|
|
689 DECA
|
|
690 LDB ,X+
|
|
691 CMPB ,U+
|
|
692 0<> UNTIL \ Compare strings until difference encountered.
|
|
693 INCA
|
|
694 0= IF
|
|
695 LEAS 2 ,S \ Yes, then word is found.
|
|
696 TFR Y, X
|
|
697 LDY 2 ,S
|
|
698 LDU 0 ,S
|
|
699 LDA 0 ,X
|
|
700 ANDA # $40
|
|
701 0= IF
|
|
702 LDD # -1
|
|
703 ELSE
|
|
704 LDD # 1 \ Make flag that indicates immediate bit.
|
|
705 THEN
|
|
706 STD 0 ,S
|
|
707 LDB ,X+
|
|
708 ANDB # $1F
|
|
709 ABX \ Compute CFA
|
|
710 STX 2 ,S
|
|
711 NEXT
|
|
712 THEN
|
|
713 THEN
|
|
714 LDU 0 ,S
|
|
715 LDX -2 ,Y \ Point to next word in linked list.
|
|
716 0= UNTIL
|
|
717 LEAS 2 ,S
|
|
718 LDY 2 ,S
|
|
719 STU 2 ,S
|
|
720 LDU 0 ,S
|
|
721 STX 0 ,S
|
|
722 THEN
|
|
723 NEXT
|
|
724 END-CODE
|
|
725
|
|
726 CODE SKIP ( addr1 len1 c --- addr2 len2 )
|
|
727 STU ,--Y
|
|
728 PULS D
|
|
729 PULS X
|
|
730 PULS U
|
|
731 LEAX 0 ,X
|
|
732 0<> IF
|
|
733 BEGIN
|
|
734 CMPB ,U+
|
|
735 0<> IF
|
|
736 LEAU -1 ,U
|
|
737 PSHS U
|
|
738 PSHS X
|
|
739 LDU ,Y++
|
|
740 NEXT
|
|
741 THEN
|
|
742 LEAX -1 ,X
|
|
743 0= UNTIL
|
|
744 THEN
|
|
745 PSHS U
|
|
746 PSHS X
|
|
747 LDU ,Y++
|
|
748 NEXT
|
|
749 END-CODE
|
|
750
|
|
751 CODE SCAN ( addr1 len1 c --- addr2 len2 )
|
|
752 STU ,--Y
|
|
753 PULS D
|
|
754 PULS X
|
|
755 PULS U
|
|
756 LEAX 0 ,X
|
|
757 0<> IF
|
|
758 BEGIN
|
|
759 CMPB ,U+
|
|
760 0= IF
|
|
761 LEAU -1 ,U
|
|
762 PSHS U
|
|
763 PSHS X
|
|
764 LDU ,Y++
|
|
765 NEXT
|
|
766 THEN
|
|
767 LEAX -1 ,X
|
|
768 0= UNTIL
|
|
769 THEN
|
|
770 PSHS U
|
|
771 PSHS X
|
|
772 LDU ,Y++
|
|
773 NEXT
|
|
774 END-CODE
|
|
775
|
|
776 CODE KEY ( --- c)
|
|
777 JSR 0
|
|
778 CLRA
|
|
779 STD ,--S
|
|
780 NEXT
|
|
781 END-CODE
|
|
782
|
|
783 CODE EMIT ( c ---)
|
|
784 LDD ,S++
|
|
785 JSR 3
|
|
786 NEXT
|
|
787 END-CODE
|
|
788
|
|
789 CODE KEY? ( --- f)
|
|
790 JSR 15
|
|
791 SEX
|
|
792 PSHS D
|
|
793 NEXT
|
|
794 END-CODE
|
|
795
|
|
796 CODE BYE
|
|
797 JMP $E400
|
|
798 NEXT
|
|
799 END-CODE
|
|
800
|
|
801 CODE CR
|
|
802 JSR 12
|
|
803 NEXT
|
|
804 END-CODE
|
|
805
|
|
806 CODE XOPENIN
|
|
807 JSR 18
|
|
808 NEXT
|
|
809 END-CODE
|
|
810
|
|
811 CODE XABORTIN
|
|
812 PSHS Y, U
|
|
813 JSR 24
|
|
814 PULS Y, U
|
|
815 NEXT
|
|
816 END-CODE
|
|
817
|
|
818 : NOOP ;
|
|
819
|
|
820 00 CONSTANT 0
|
|
821 01 CONSTANT 1
|
|
822 02 CONSTANT 2
|
|
823 -1 CONSTANT -1
|
|
824
|
|
825 \ PART 3: SIMPLE DEFINITIONS
|
|
826
|
|
827 \ This is a large class of words, which would be written in machine code
|
|
828 \ on most non-native code systems. Many contain just a few words, so they
|
|
829 \ are implemented as macros.
|
|
830
|
|
831 \ This category contains simple arithmetic and compare words, the runtime
|
|
832 \ parts of DO LOOP and string related words etc, many on which are
|
|
833 \ dependent on each other, so they are in a less than logical order to
|
|
834 \ avoid large numbers of forward references.
|
|
835
|
|
836 : = ( x1 x2 --- f)
|
|
837 \G f is true if and only if x1 is equal to x2.
|
|
838 - 0= ;
|
|
839
|
|
840 : <> ( x1 x2 --- f)
|
|
841 \G f is true if and only if x1 is not equal to x2.
|
|
842 = 0= ;
|
|
843
|
|
844 : > ( n1 n2 --- f)
|
|
845 \G f is true if and only if the signed number n1 is less than n2.
|
|
846 SWAP < ;
|
|
847
|
|
848 : 0> ( n --- f)
|
|
849 \G f is true if and only if n is greater than 0.
|
|
850 0 > ;
|
|
851
|
|
852 : U> ( u1 u2 --- f)
|
|
853 \G f is true if and only if the unsigned number u1 is greater than u2.
|
|
854 SWAP U< ;
|
|
855
|
|
856 VARIABLE S0 ( --- a-addr)
|
|
857 \G Variable that holds the bottom address of the stack.
|
|
858 -2 ALLOT-T
|
|
859 LABEL S0ADDR ENDASM
|
|
860 02 ALLOT-T
|
|
861
|
|
862 VARIABLE R0 ( --- a-addr)
|
|
863 \G Variable that holds the bottom address of the return stack.
|
|
864 -2 ALLOT-T
|
|
865 LABEL R0ADDR ENDASM
|
|
866 02 ALLOT-T
|
|
867
|
|
868 : DEPTH ( --- n )
|
|
869 \G n is the number of cells on the stack (before DEPTH was executed).
|
|
870 SP@ S0 @ SWAP - 2/ ;
|
|
871
|
|
872 : COUNT ( c-addr1 --- c-addr2 c)
|
|
873 \G c-addr2 is the next address after c-addr1 and c is the character
|
|
874 \G stored at c-addr1.
|
|
875 \G This word is intended to be used with 'counted strings' where the
|
|
876 \G first character indicates the length of the string.
|
|
877 DUP 1 + SWAP C@ ;
|
|
878
|
|
879 : TYPE ( c-addr1 u --- )
|
|
880 \G Output the string starting at c-addr and length u to the terminal.
|
|
881 DUP IF 0 DO DUP I + C@ EMIT LOOP DROP ELSE DROP DROP THEN ;
|
|
882
|
|
883 : ALIGNED ( c-addr --- a-addr )
|
|
884 \G a-addr is the first aligned address after c-addr.
|
|
885 ;
|
|
886
|
|
887 : (.") ( --- )
|
|
888 \G Runtime part of ."
|
|
889 \ This expects an in-line counted string.
|
|
890 R> COUNT OVER OVER TYPE + ALIGNED >R ;
|
|
891 : (S") ( --- c-addr u )
|
|
892 \G Runtime part of S"
|
|
893 \ It returns address and length of an in-line counted string.
|
|
894 R> COUNT OVER OVER + ALIGNED >R ;
|
|
895
|
|
896
|
|
897 00
|
|
898 CONSTANT FALSE ( --- 0)
|
|
899 \G Constant 0, indicates FALSE
|
|
900
|
|
901 -01
|
|
902 CONSTANT TRUE ( --- -1)
|
|
903 \G Constant -1, indicates TRUE
|
|
904
|
|
905 32
|
|
906 CONSTANT BL ( --- 32 )
|
|
907 \G Constant 32, the blank character
|
|
908
|
|
909 : OFF ( a-addr ---)
|
|
910 \G Store FALSE at a-addr.
|
|
911 0 SWAP ! ;
|
|
912
|
|
913 : ON ( a-addr ---)
|
|
914 \G Store TRUE at a-addr.
|
|
915 -1 SWAP ! ;
|
|
916
|
|
917 : INVERT ( x1 --- x2)
|
|
918 \G Invert all the bits of x1 (one's complement)
|
|
919 -1 XOR ;
|
|
920
|
|
921
|
|
922 \ The next few words manipulate addresses in a system-independent way.
|
|
923 \ Use CHAR+ instead of 1+ and it will be portable to systems where you
|
|
924 \ have to add something different from 1.
|
|
925
|
|
926 : CHAR+ ( c-addr1 --- c-addr2)
|
|
927 \G c-addr2 is the next character address after c-addr1.
|
|
928 1+ ;
|
|
929
|
|
930 : CHARS ( n1 --- n2)
|
|
931 \G n2 is the number of address units occupied by n1 characters.
|
|
932 ; \ A no-op.
|
|
933
|
|
934 : CHAR- ( c-addr1 --- c-addr2)
|
|
935 \G c-addr2 is the previous character address before c-addr1.
|
|
936 1- ;
|
|
937
|
|
938 : CELL+ ( a-addr1 --- a-addr2)
|
|
939 \G a-addr2 is the address of the next cell after a-addr2.
|
|
940 2+ ;
|
|
941
|
|
942 : CELLS ( n2 --- n1)
|
|
943 \G n2 is the number of address units occupied by n1 cells.
|
|
944 1 LSHIFT ;
|
|
945
|
|
946 : CELL- ( a-addr1 --- a-addr2)
|
|
947 \G a-addr2 is the address of the previous cell before a-addr1.
|
|
948 2- ;
|
|
949
|
|
950 : ?DUP ( n --- 0 | n n)
|
|
951 \G Duplicate the top cell on the stack, but only if it is nonzero.
|
|
952 DUP IF DUP THEN ;
|
|
953
|
|
954 : MIN ( n1 n2 --- n3)
|
|
955 \G n3 is the minimum of n1 and n2.
|
|
956 OVER OVER > IF SWAP THEN DROP ;
|
|
957
|
|
958 : MAX ( n1 n2 --- n3)
|
|
959 \G n3 is the maximum of n1 and n2.
|
|
960 OVER OVER < IF SWAP THEN DROP ;
|
|
961
|
|
962 : ABS ( n --- u)
|
|
963 \G u is the absolute value of n.
|
|
964 DUP 0< IF NEGATE THEN ;
|
|
965
|
|
966 : DABS ( d --- ud)
|
|
967 \G ud is the absolute value of d.
|
|
968 DUP 0< IF DNEGATE THEN ;
|
|
969
|
|
970 : SM/REM ( d n1 --- nrem nquot )
|
|
971 \G Divide signed double number d by single number n1, giving quotient and
|
|
972 \G remainder. Round towards zero, remainder has same sign as dividend.
|
|
973 2DUP XOR >R OVER >R \ Push signs of quot and rem.
|
|
974 ABS >R DABS R>
|
|
975 UM/MOD
|
|
976 SWAP R> 0< IF NEGATE THEN SWAP
|
|
977 R> 0< IF NEGATE THEN ;
|
|
978
|
|
979 : FM/MOD ( d n1 --- nrem nquot )
|
|
980 \G Divide signed double number d by single number n1, giving quotient and
|
|
981 \G remainder. Round always down (floored division),
|
|
982 \G remainder has same sign as divisor.
|
|
983 DUP >R OVER OVER XOR >R
|
|
984 SM/REM
|
|
985 OVER R> 0< AND IF SWAP R@ + SWAP 1 - THEN R> DROP ;
|
|
986
|
|
987 : M* ( n1 n2 --- d )
|
|
988 \G Multiply the signed numbers n1 and n2, giving the signed double number d.
|
|
989 2DUP XOR >R ABS SWAP ABS UM* R> 0< IF DNEGATE THEN ;
|
|
990
|
|
991 : * ( w1 w2 --- w3)
|
|
992 \G Multiply single numbers, signed or unsigned give the same result.
|
|
993 UM* DROP ;
|
|
994
|
|
995 : */MOD ( n1 n2 n3 --- nrem nquot)
|
|
996 \G Multiply signed numbers n1 by n2 and divide by n3, giving quotient and
|
|
997 \G remainder. Intermediate result is double.
|
|
998 >R M* R> FM/MOD ;
|
|
999
|
|
1000 : */ ( n1 n2 n3 --- n4 )
|
|
1001 \G Multiply signed numbers n1 by n2 and divide by n3, giving quotient n4.
|
|
1002 \G Intermediate result is double.
|
|
1003 */MOD SWAP DROP ;
|
|
1004
|
|
1005 : S>D ( n --- d)
|
|
1006 \G Convert single number to double number.
|
|
1007 DUP 0< ;
|
|
1008
|
|
1009 : /MOD ( n1 n2 --- nrem nquot)
|
|
1010 \G Divide signed number n1 by n2, giving quotient and remainder.
|
|
1011 SWAP S>D ROT FM/MOD ;
|
|
1012
|
|
1013 : / ( n1 n2 --- n3)
|
|
1014 \G n3 is n1 divided by n2.
|
|
1015 /MOD SWAP DROP ;
|
|
1016
|
|
1017 : MOD ( n1 n2 --- n3)
|
|
1018 \G n3 is the remainder of n1 and n2.
|
|
1019 /MOD DROP ;
|
|
1020
|
|
1021 : ?THROW ( f n --- )
|
|
1022 \G Perform n THROW if f is nonzero.
|
|
1023 SWAP IF THROW ELSE DROP THEN ;
|
|
1024
|
|
1025 \ PART 4: NUMERIC OUTPUT WORDS.
|
|
1026
|
|
1027 VARIABLE BASE ( --- a-addr)
|
|
1028 \G Variable that contains the numerical conversion base.
|
|
1029
|
|
1030 VARIABLE DP ( --- a-addr)
|
|
1031 \G Variable that contains the dictionary pointer. New space is allocated
|
|
1032 \G from the address in DP
|
|
1033
|
|
1034 VARIABLE HLD ( --- a-addr)
|
|
1035 \G Variable that holds the address of the numerical output conversion
|
|
1036 \G character.
|
|
1037
|
|
1038 VARIABLE DPL ( --- a-addr)
|
|
1039 \G Variable that holds the decimal point location for numerical conversion.
|
|
1040
|
|
1041 : DECIMAL ( --- )
|
|
1042 \G Set numerical conversion to decimal.
|
|
1043 10 BASE ! ;
|
|
1044
|
|
1045 : HEX ( --- )
|
|
1046 \G Set numerical conversion to hexadecimal.
|
|
1047 16 BASE ! ;
|
|
1048
|
|
1049 : SPACE ( ---)
|
|
1050 \G Output a space to the terminal.
|
|
1051 32 EMIT ;
|
|
1052
|
|
1053 : SPACES ( u --- )
|
|
1054 \G Output u spaces to the terminal.
|
|
1055 ?DUP IF 0 DO SPACE LOOP THEN ;
|
|
1056
|
|
1057 : HERE ( --- c-addr )
|
|
1058 \G The address of the dictionary pointer. New space is allocated here.
|
|
1059 DP @ ;
|
|
1060
|
|
1061 : PAD ( --- c-addr )
|
|
1062 \G The address of a scratch pad area. Right below this address there is
|
|
1063 \G the numerical conversion buffer.
|
|
1064 DP @ 84 + ;
|
|
1065
|
|
1066 : MU/MOD ( ud u --- urem udquot )
|
|
1067 \G Divide unsigned double number ud by u and return a double quotient and
|
|
1068 \G a single remainder.
|
|
1069 >R 0 R@ UM/MOD R> SWAP >R UM/MOD R> ;
|
|
1070
|
|
1071 \ The numerical conversion buffer starts right below PAD and grows down.
|
|
1072 \ Characters are added to it from right to left, as as the div/mod algorithm
|
|
1073 \ to convert numbers to an arbitrary base produces the digits from right to
|
|
1074 \ left.
|
|
1075
|
|
1076 : HOLD ( c ---)
|
|
1077 \G Insert character c into the numerical conversion buffer.
|
|
1078 1 NEGATE HLD +! HLD @ C! ;
|
|
1079
|
|
1080 : # ( ud1 --- ud2)
|
|
1081 \G Extract the rightmost digit of ud1 and put it into the numerical
|
|
1082 \G conversion buffer.
|
|
1083 BASE @ MU/MOD ROT DUP 9 > IF 7 + THEN 48 + HOLD ;
|
|
1084
|
|
1085 : #S ( ud --- 0 0 )
|
|
1086 \G Convert ud by repeated use of # until ud is zero.
|
|
1087 BEGIN # OVER OVER OR 0= UNTIL ;
|
|
1088
|
|
1089 : SIGN ( n ---)
|
|
1090 \G Insert a - sign in the numerical conversion buffer if n is negative.
|
|
1091 0< IF 45 HOLD THEN ;
|
|
1092
|
|
1093 : <# ( --- )
|
|
1094 \G Reset the numerical conversion buffer.
|
|
1095 PAD HLD ! ;
|
|
1096
|
|
1097 : #> ( ud --- addr u )
|
|
1098 \G Discard ud and give the address and length of the numerical conversion
|
|
1099 \G buffer.
|
|
1100 DROP DROP HLD @ PAD OVER - ;
|
|
1101
|
|
1102 : D. ( d --- )
|
|
1103 \G Type the double number d to the terminal.
|
|
1104 SWAP OVER DABS <# #S ROT SIGN #> TYPE SPACE ;
|
|
1105
|
|
1106 : U. ( u ---)
|
|
1107 \G Type the unsigned number u to the terminal.
|
|
1108 0 D. ;
|
|
1109
|
|
1110 : . ( n ---)
|
|
1111 \G Type the signed number n to the terminal.
|
|
1112 S>D D. ;
|
|
1113
|
|
1114 : MOVE ( c-addr1 c-addr2 u --- )
|
|
1115 \G Copy a block of u bytes starting at c-addr1 to c-addr2. Order is such
|
|
1116 \G that partially overlapping blocks are copied intact.
|
|
1117 >R OVER OVER U< IF R> CMOVE> ELSE R> CMOVE THEN ;
|
|
1118
|
|
1119
|
|
1120 CODE ACCEPT ( c-addr n1 --- n2 )
|
|
1121 \G Read a line from the terminal to a buffer starting at c-addr with
|
|
1122 \G length n1. n2 is the number of characters read,
|
|
1123 PULS X, D
|
|
1124 JSR 6
|
|
1125 CLRA
|
|
1126 PSHS D
|
|
1127 NEXT
|
|
1128 END-CODE
|
|
1129
|
|
1130
|
|
1131 $200 CONSTANT TIB ( --- addr)
|
|
1132 \G is the standard terminal input buffer.
|
|
1133
|
|
1134 VARIABLE SPAN ( --- addr)
|
|
1135 \G This variable holds the number of characters read by EXPECT.
|
|
1136
|
|
1137 VARIABLE #TIB ( --- addr)
|
|
1138 \G This variable holds the number of characters in the terminal input buffer.
|
|
1139
|
|
1140 VARIABLE >IN ( --- addr)
|
|
1141 \G This variable holds an index in the current input source where the next word
|
|
1142 \G will be parsed.
|
|
1143
|
|
1144 VARIABLE SID ( --- addr)
|
|
1145 \G This variable holds the source i.d. returned by SOURCE-ID.
|
|
1146
|
|
1147 VARIABLE SRC ( --- addr)
|
|
1148 \G This variable holds the address of the current input source.
|
|
1149
|
|
1150 VARIABLE #SRC ( --- addr)
|
|
1151 \G This variable holds the length of the current input source.
|
|
1152
|
|
1153 VARIABLE LOADLINE ( --- addr)
|
|
1154 \G This variable holds the line number in the file being included.
|
|
1155
|
|
1156
|
|
1157 : EXPECT ( c-addr u --- )
|
|
1158 \G Read a line from the terminal to a buffer at c-addr with length u.
|
|
1159 \G Store the length of the line in SPAN.
|
|
1160 ACCEPT SPAN ! ;
|
|
1161
|
|
1162 : QUERY ( --- )
|
|
1163 \G Read a line from the terminal into the terminal input buffer.
|
|
1164 TIB 128 ACCEPT #TIB ! ;
|
|
1165
|
|
1166 : SOURCE ( --- addr len)
|
|
1167 \G Return the address and length of the current input source.
|
|
1168 SRC @ #SRC @ ;
|
|
1169
|
|
1170 : SOURCE-ID ( --- sid)
|
|
1171 \G Return the i.d. of the current source i.d., 0 for terminal, -1
|
|
1172 \G for EVALUATE and positive number for INCLUDE file.
|
|
1173 SID @ ;
|
|
1174
|
|
1175 : REFILL ( --- f)
|
|
1176 \G Refill the current input source when it is exhausted. f is
|
|
1177 \G true if it was successfully refilled.
|
|
1178 SOURCE-ID -1 = IF
|
|
1179 0 \ Not refillable for EVALUATE
|
|
1180 ELSE
|
|
1181 QUERY #TIB @ #SRC ! 0 >IN ! -1 \ Always successful from terminal.
|
|
1182 1 LOADLINE +!
|
|
1183 THEN
|
|
1184 ;
|
|
1185
|
|
1186 : PARSE ( c --- addr len )
|
|
1187 \G Find a character sequence in the current source that is delimited by
|
|
1188 \G character c. Adjust >IN to 1 past the end delimiter character.
|
|
1189 >R SOURCE >IN @ - SWAP >IN @ + R> OVER >R >R SWAP
|
|
1190 R@ SKIP OVER R> SWAP >R SCAN IF 1 >IN +! THEN
|
|
1191 DUP R@ - R> SWAP
|
|
1192 ROT R> - >IN +! ;
|
|
1193
|
|
1194 : PLACE ( addr len c-addr --- )
|
|
1195 \G Place the string starting at addr with length len at c-addr as
|
|
1196 \G a counted string.
|
|
1197 OVER OVER C!
|
|
1198 1+ SWAP CMOVE ;
|
|
1199
|
|
1200 : WORD ( c --- addr )
|
|
1201 \G Parse a character sequence delimited by character c and return the
|
|
1202 \G address of a counted string that is a copy of it. The counted
|
|
1203 \G string is actually placed at HERE. The character after the counted
|
|
1204 \G string is set to a space.
|
|
1205 PARSE HERE PLACE HERE BL HERE COUNT + C! ;
|
|
1206
|
|
1207 VARIABLE CAPS ( --- a-addr)
|
|
1208 \G This variable contains a nonzero number if input is case insensitive.
|
|
1209
|
|
1210 : UPPERCASE? ( --- )
|
|
1211 \G Convert the parsed word to uppercase is CAPS is true.
|
|
1212 CAPS @ HERE C@ AND IF
|
|
1213 HERE COUNT 0 DO
|
|
1214 DUP I + C@ DUP 96 > SWAP 123 < AND IF DUP I + DUP C@ 32 - SWAP C! THEN
|
|
1215 LOOP DROP
|
|
1216 THEN
|
|
1217 ;
|
|
1218
|
|
1219
|
|
1220 \ PART 8: INTERPRETER HELPER WORDS
|
|
1221
|
|
1222 \ First we need FIND and related words.
|
|
1223
|
|
1224 \ Each word list consists of a number of linked list of definitions (number
|
|
1225 \ is a power of 2). Hashing
|
|
1226 \ is used to speed up dictionary search. All names in the dictionary
|
|
1227 \ are at aligned addresses and FIND is optimized to compare one 4-byte
|
|
1228 \ cell at a time.
|
|
1229
|
|
1230 \ Dictionary definitions are built as follows:
|
|
1231 \
|
|
1232 \ LINK field: 1 cell, aligned, contains name field of previous word in thread.
|
|
1233 \ NAME field: counted string of at most 31 characters.
|
|
1234 \ bits 5-7 of length byte have special meaning.
|
|
1235 \ 7 is always set to mark start of name ( for >NAME)
|
|
1236 \ 6 is set if the word is immediate.
|
|
1237 \ CODE field: first aligned address after name, is execution token for word.
|
|
1238 \ here the executable code for the word starts. (is 3 bytes for
|
|
1239 \ variables etc.)
|
|
1240 \ PARAMETER field: (body) Contains the data of constants and variables etc.
|
|
1241
|
|
1242 VARIABLE NAMEBUF ( --- a-addr)
|
|
1243 \G An aligned buffer that holds a copy of the name that is searched.
|
|
1244 30 ALLOT-T
|
|
1245
|
|
1246 VARIABLE FORTH-WORDLIST ( --- addr)
|
|
1247 4 CELLS-T ALLOT-T
|
|
1248 \G This array holds pointers to the last definition of each thread in the Forth
|
|
1249 \G word list.
|
|
1250
|
|
1251 VARIABLE LAST ( --- addr)
|
|
1252 \G This variable holds a pointer to the last definition created.
|
|
1253
|
|
1254 VARIABLE CONTEXT 28 ALLOT-T ( --- a-addr)
|
|
1255 \G This variable holds the addresses of up to 8 word lists that are
|
|
1256 \G in the search order.
|
|
1257
|
|
1258 VARIABLE #ORDER ( --- addr)
|
|
1259 \G This variable holds the number of word list that are in the search order.
|
|
1260
|
|
1261 VARIABLE CURRENT ( --- addr)
|
|
1262 \G This variable holds the address of the word list to which new definitions
|
|
1263 \G are added.
|
|
1264
|
|
1265 : HASH ( c-addr u #threads --- n)
|
|
1266 \G Compute the hash function for the name c-addr u with the indicated number
|
|
1267 \G of threads.
|
|
1268 >R OVER C@ 1 LSHIFT OVER 1 > IF ROT CHAR+ C@ 2 LSHIFT XOR ELSE ROT DROP
|
|
1269 THEN XOR
|
|
1270 R> 1- AND
|
|
1271 ;
|
|
1272
|
|
1273 : NAME>BUF ( c-addr u ---)
|
|
1274 \G Move the name c-addr u to the aligned buffer NAMEBUF.
|
|
1275 NAMEBUF 32 0 FILL 32 MIN NAMEBUF PLACE ;
|
|
1276
|
|
1277
|
|
1278 : SEARCH-WORDLIST ( c-addr u wid --- 0 | xt 1 xt -1)
|
|
1279 \G Search the wordlist with address wid for the name c-addr u.
|
|
1280 \G Return 0 if not found, the execution token xt and -1 for non-immediate
|
|
1281 \G words and xt and 1 for immediate words.
|
|
1282 ROT ROT
|
|
1283 NAME>BUF
|
|
1284 NAMEBUF COUNT 2 PICK @ HASH 1+ CELLS SWAP + @ \ Get the right thread.
|
|
1285 DUP IF
|
|
1286 NAMEBUF SWAP (FIND) DUP 0= IF DROP DROP 0 THEN EXIT
|
|
1287 THEN
|
|
1288 DROP 0 \ Not found.
|
|
1289 ;
|
|
1290
|
|
1291 : FIND ( c-addr --- c-addr 0| xt 1|xt -1 )
|
|
1292 \G Search all word lists in the search order for the name in the
|
|
1293 \G counted string at c-addr. If not found return the name address and 0.
|
|
1294 \G If found return the execution token xt and -1 if the word is non-immediate
|
|
1295 \G and 1 if the word is immediate.
|
|
1296 #ORDER @ DUP 1 > IF
|
|
1297 CONTEXT #ORDER @ 1- CELLS + DUP @ SWAP CELL- @ =
|
|
1298 ELSE 0 THEN
|
|
1299 IF 1- THEN \ If last wordlist is double, don't search it twice.
|
|
1300 BEGIN
|
|
1301 DUP
|
|
1302 WHILE
|
|
1303 1- >R
|
|
1304 DUP COUNT
|
|
1305 R@ CELLS CONTEXT + @ SEARCH-WORDLIST
|
|
1306 DUP
|
|
1307 IF
|
|
1308 R> DROP ROT DROP EXIT \ Exit if found.
|
|
1309 THEN
|
|
1310 DROP R>
|
|
1311 REPEAT
|
|
1312 ;
|
|
1313
|
|
1314 \ The following words are related to numeric input.
|
|
1315
|
|
1316 : DIGIT? ( c -- 0| c--- n -1)
|
|
1317 \G Convert character c to its digit value n and return true if c is a
|
|
1318 \G digit in the current base. Otherwise return false.
|
|
1319 48 - DUP 0< IF DROP 0 EXIT THEN
|
|
1320 DUP 9 > OVER 17 < AND IF DROP 0 EXIT THEN
|
|
1321 DUP 9 > IF 7 - THEN
|
|
1322 DUP BASE @ < 0= IF DROP 0 EXIT THEN
|
|
1323 -1
|
|
1324 ;
|
|
1325
|
|
1326 : >NUMBER ( ud1 c-addr1 u1 --- ud2 c-addr2 u2 )
|
|
1327 \G Convert the string at c-addr with length u1 to binary, multiplying ud1
|
|
1328 \G by the number in BASE and adding the digit value to it for each digit.
|
|
1329 \G c-addr2 u2 is the remainder of the string starting at the first character
|
|
1330 \G that is no digit.
|
|
1331 BEGIN
|
|
1332 DUP
|
|
1333 WHILE
|
|
1334 1 - >R
|
|
1335 COUNT DIGIT? 0=
|
|
1336 IF
|
|
1337 R> 1+ SWAP 1 - SWAP EXIT
|
|
1338 THEN
|
|
1339 SWAP >R
|
|
1340 >R
|
|
1341 SWAP BASE @ UM* ROT BASE @ * 0 SWAP D+ \ Multiply ud by base.
|
|
1342 R> 0 D+ \ Add new digit.
|
|
1343 R> R>
|
|
1344 REPEAT
|
|
1345 ;
|
|
1346
|
|
1347 : CONVERT ( ud1 c-addr1 --- ud2 c-addr2)
|
|
1348 \G Convert the string starting at c-addr1 + 1 to binary. c-addr2 is the
|
|
1349 \G address of the first non-digit. Digits are added into ud1 as in >NUMBER
|
|
1350 1 - -1 >NUMBER DROP ;
|
|
1351
|
|
1352 : NUMBER? ( c-addr ---- d f)
|
|
1353 \G Convert the counted string at c-addr to a double binary number.
|
|
1354 \G f is true if and only if the conversion was successful. DPL contains
|
|
1355 \G -1 if there was no point in the number, else the position of the point
|
|
1356 \G from the right. Special prefixes: # means decimal, $ means hex.
|
|
1357 -1 DPL !
|
|
1358 BASE @ >R
|
|
1359 COUNT
|
|
1360 OVER C@ 45 = DUP >R IF 1 - SWAP 1 + SWAP THEN \ Get any - sign
|
|
1361 OVER C@ 36 = IF 16 BASE ! 1 - SWAP 1 + SWAP THEN \ $ sign for hex.
|
|
1362 OVER C@ 35 = IF 10 BASE ! 1 - SWAP 1 + SWAP THEN \ # sign for decimal
|
|
1363 DUP 0 > 0= IF R> DROP R> BASE ! 0 EXIT THEN \ Length 0 or less?
|
|
1364 >R >R 0 0 R> R>
|
|
1365 BEGIN
|
|
1366 >NUMBER
|
|
1367 DUP IF OVER C@ 46 = IF 1 - DUP DPL ! SWAP 1 + SWAP ELSE \ handle point.
|
|
1368 R> DROP R> BASE ! 0 EXIT THEN \ Error if anything but point
|
|
1369 THEN
|
|
1370 DUP 0= UNTIL DROP DROP R> IF DNEGATE THEN
|
|
1371 R> BASE ! -1
|
|
1372 ;
|
|
1373
|
|
1374 \ PART 9: THE COMPILER
|
|
1375
|
|
1376 VARIABLE ERROR$ ( --- a-addr )
|
|
1377 \G Variable containing string address of ABORT" message.
|
|
1378
|
|
1379 VARIABLE HANDLER ( --- a-addr )
|
|
1380 \G Variable containing return stack address where THROW should return.
|
|
1381
|
|
1382 : (ABORT") ( f -- - )
|
|
1383 \G Runtime part of ABORT"
|
|
1384 IF R> ERROR$ ! -2 THROW
|
|
1385 ELSE R> COUNT + ALIGNED >R THEN ;
|
|
1386
|
|
1387 : THROW ( n --- )
|
|
1388 \G If n is nonzero, cause the corresponding CATCH to return with n.
|
|
1389 DUP IF
|
|
1390 HANDLER @ IF
|
|
1391 HANDLER @ RP!
|
|
1392 RP@ 4 + @ HANDLER ! \ point to previous exception frame.
|
|
1393 R> \ get old stack pointer.
|
|
1394 SWAP >R SP! DROP R> \ save throw code temp. on ret. stack set old sp.
|
|
1395 R> DROP \ remove address of handler.
|
|
1396 \ return stack points to return address of CATCH.
|
|
1397 ELSE
|
|
1398 WARM \ Warm start if no exception frame on stack.
|
|
1399 THEN
|
|
1400 ELSE
|
|
1401 DROP \ continue if zero.
|
|
1402 THEN
|
|
1403 ;
|
|
1404
|
|
1405 : CATCH ( xt --- n )
|
|
1406 \G Execute the word with execution token xt. If it returns normally, return
|
|
1407 \G 0. If it executes a THROW, return the throw parameter.
|
|
1408 HANDLER @ >R \ push handler on ret stack.
|
|
1409 SP@ >R \ push stack pointer on ret stack,
|
|
1410 RP@ HANDLER !
|
|
1411 EXECUTE
|
|
1412 RP@ 4 + @ HANDLER ! \ set handler to previous exception frame.
|
|
1413 R> DROP R> DROP \ remove exception frame.
|
|
1414 0 \ return 0
|
|
1415 ;
|
|
1416
|
|
1417 : ALLOT ( n --- )
|
|
1418 \G Allot n extra bytes of memory, starting at HERE to the dictionary.
|
|
1419 DP +! ;
|
|
1420
|
|
1421 : , ( x --- )
|
|
1422 \G Append cell x to the dictionary at HERE.
|
|
1423 HERE ! 1 CELLS ALLOT ;
|
|
1424
|
|
1425 : C, ( n --- )
|
|
1426 \G Append character c to the dictionary at HERE.
|
|
1427 HERE C! 1 ALLOT ;
|
|
1428
|
|
1429 : ALIGN ( --- )
|
|
1430 \G Add as many bytes to the dictionary as needed to align dictionary pointer.
|
|
1431 ;
|
|
1432
|
|
1433 : >NAME ( addr1 --- addr2 )
|
|
1434 \G Convert execution token addr1 (address of code) to address of name.
|
|
1435 BEGIN 1- DUP C@ 128 AND UNTIL ;
|
|
1436
|
|
1437 : NAME> ( addr1 --- addr2 )
|
|
1438 \G Convert address of name to address of code.
|
|
1439 COUNT 31 AND + ALIGNED ;
|
|
1440
|
|
1441 : HEADER ( --- )
|
|
1442 \G Create a header for a new definition without a code field.
|
|
1443 ALIGN 0 , \ Create link field.
|
|
1444 HERE LAST ! \ Set LAST so definition can be linked by REVEAL
|
|
1445 32 WORD UPPERCASE?
|
|
1446 DUP FIND IF ." Redefining: " HERE COUNT TYPE CR THEN DROP
|
|
1447 \ Give warning if existing word redefined.
|
|
1448 DUP COUNT CURRENT @ @ HASH 1+ CELLS CURRENT @ + @ HERE CELL- !
|
|
1449 \ Set link field to point to the right thread
|
|
1450 C@ 1+ HERE C@ 128 + HERE C! ALLOT ALIGN
|
|
1451 \ Allot the name and set bit 7 in length byte.
|
|
1452 ;
|
|
1453
|
|
1454 : JSR, $BD C, ;
|
|
1455
|
|
1456 : REVEAL ( --- )
|
|
1457 \G Add the last created definition to the CURRENT wordlist.
|
|
1458 LAST @ DUP COUNT 31 AND \ Get address and length of name
|
|
1459 CURRENT @ @ HASH \ compute hash code.
|
|
1460 1+ CELLS CURRENT @ + ! ;
|
|
1461
|
|
1462 : CREATE ( "ccc" --- )
|
|
1463 \G Create a definition that returns its parameter field address when
|
|
1464 \G executed. Storage can be added to it with ALLOT.
|
|
1465 HEADER REVEAL JSR, LIT DOVAR , ;
|
|
1466
|
|
1467 : VARIABLE ( "ccc" --- )
|
|
1468 \G Create a variable where 1 cell can be stored. When executed it
|
|
1469 \G returns the address.
|
|
1470 CREATE 0 , ;
|
|
1471
|
|
1472 : CONSTANT ( x "ccc" ---)
|
|
1473 \G Create a definition that returns x when executed.
|
|
1474 \ Definition contains lit & return in its code field.
|
|
1475 HEADER REVEAL JSR, LIT DOCON , , ;
|
|
1476
|
|
1477
|
|
1478 VARIABLE STATE ( --- a-addr)
|
|
1479 \G Variable that holds the compiler state, 0 is interpreting 1 is compiling.
|
|
1480
|
|
1481 : ] ( --- )
|
|
1482 \G Start compilation mode.
|
|
1483 1 STATE ! ;
|
|
1484
|
|
1485 : [ ( --- )
|
|
1486 \G Leave compilation mode.
|
|
1487 0 STATE ! ; IMMEDIATE
|
|
1488
|
|
1489 : LITERAL ( n --- )
|
|
1490 \G Add a literal to the current definition.
|
|
1491 POSTPONE LIT , ; IMMEDIATE
|
|
1492
|
|
1493 : COMPILE, ( xt --- )
|
|
1494 \G Add the execution semantics of the definition xt to the current definition.
|
|
1495 ,
|
|
1496 ;
|
|
1497
|
|
1498 VARIABLE CSP ( --- a-addr )
|
|
1499 \G This variable is used for stack checking between : and ;
|
|
1500
|
|
1501 VARIABLE 'LEAVE ( --- a-addr)
|
|
1502 \ This variable is used for LEAVE address resolution.
|
|
1503
|
|
1504 : !CSP ( --- )
|
|
1505 \G Store current stack pointer in CSP.
|
|
1506 SP@ CSP ! ;
|
|
1507
|
|
1508 : ?CSP ( --- )
|
|
1509 \G Check that stack pointer is equal to value contained in CSP.
|
|
1510 SP@ CSP @ - -22 ?THROW ;
|
|
1511
|
|
1512 : ; ( --- )
|
|
1513 \G Finish the current definition by adding a return to it, make it
|
|
1514 \G visible and leave compilation mode.
|
|
1515 POSTPONE UNNEST [
|
|
1516 ?CSP REVEAL
|
|
1517 ; IMMEDIATE
|
|
1518
|
|
1519 : (POSTPONE) ( --- )
|
|
1520 \G Runtime for POSTPONE.
|
|
1521 \ has inline argument.
|
|
1522 R> DUP @ SWAP CELL+ >R
|
|
1523 DUP >NAME C@ 64 AND IF EXECUTE ELSE COMPILE, THEN
|
|
1524 ;
|
|
1525
|
|
1526 : : ( "ccc" --- )
|
|
1527 \G Start a new definition, enter compilation mode.
|
|
1528 !CSP HEADER JSR, LIT DOCOL , ] ;
|
|
1529
|
|
1530 : BEGIN ( --- x )
|
|
1531 \G Start a BEGIN UNTIL or BEGIN WHILE REPEAT loop.
|
|
1532 HERE ; IMMEDIATE
|
|
1533
|
|
1534 : UNTIL ( x --- )
|
|
1535 \G Form a loop with matching BEGIN.
|
|
1536 \G Runtime: A flag is take from the stack
|
|
1537 \G each time UNTIL is encountered and the loop iterates until it is nonzero.
|
|
1538 POSTPONE ?BRANCH , ; IMMEDIATE
|
|
1539
|
|
1540 : IF ( --- x)
|
|
1541 \G Start an IF THEN or IF ELSE THEN construction.
|
|
1542 \G Runtime: At IF a flag is taken from
|
|
1543 \G the stack and if it is true the part between IF and ELSE is executed,
|
|
1544 \G otherwise the part between ELSE and THEN. If there is no ELSE, the part
|
|
1545 \G between IF and THEN is executed only if flag is true.
|
|
1546 POSTPONE ?BRANCH HERE 1 CELLS ALLOT ; IMMEDIATE
|
|
1547
|
|
1548 : THEN ( x ---)
|
|
1549 \G End an IF THEN or IF ELSE THEN construction.
|
|
1550 HERE SWAP ! ; IMMEDIATE
|
|
1551
|
|
1552 : ELSE ( x1 --- x2)
|
|
1553 \G part of IF ELSE THEN construction.
|
|
1554 POSTPONE BRANCH HERE 1 CELLS ALLOT SWAP POSTPONE THEN ; IMMEDIATE
|
|
1555
|
|
1556 : WHILE ( x1 --- x2 x1 )
|
|
1557 \G part of BEGIN WHILE REPEAT construction.
|
|
1558 \G Runtime: At WHILE a flag is taken from the stack. If it is false,
|
|
1559 \G the program jumps out of the loop, otherwise the part between WHILE
|
|
1560 \G and REPEAT is executed and the loop iterates to BEGIN.
|
|
1561 POSTPONE IF SWAP ; IMMEDIATE
|
|
1562
|
|
1563 : REPEAT ( x1 x2 --- )
|
|
1564 \G part of BEGIN WHILE REPEAT construction.
|
|
1565 POSTPONE BRANCH , POSTPONE THEN ; IMMEDIATE
|
|
1566
|
|
1567 VARIABLE POCKET ( --- a-addr )
|
|
1568 \G Buffer for S" strings that are interpreted.
|
|
1569 252 ALLOT-T
|
|
1570
|
|
1571 : ' ( "ccc" --- xt)
|
|
1572 \G Find the word with name ccc and return its execution token.
|
|
1573 32 WORD UPPERCASE? FIND 0= -13 ?THROW ;
|
|
1574
|
|
1575 : ['] ( "ccc" ---)
|
|
1576 \G Copile the execution token of the word with name ccc as a literal.
|
|
1577 ' LITERAL ; IMMEDIATE
|
|
1578
|
|
1579 : CHAR ( "ccc" --- c)
|
|
1580 \G Return the first character of "ccc".
|
|
1581 BL WORD 1 + C@ ;
|
|
1582
|
|
1583 : [CHAR] ( "ccc" --- )
|
|
1584 \G Compile the first character of "ccc" as a literal.
|
|
1585 CHAR LITERAL ; IMMEDIATE
|
|
1586
|
|
1587 : DO ( --- x)
|
|
1588 \G Start a DO LOOP.
|
|
1589 \G Runtime: ( n1 n2 --- ) start a loop with initial count n2 and
|
|
1590 \G limit n1.
|
|
1591 POSTPONE (DO) 'LEAVE @ HERE 0 'LEAVE ! ; IMMEDIATE
|
|
1592
|
|
1593 : ?DO ( --- x )
|
|
1594 \G Start a ?DO LOOP.
|
|
1595 \G Runtime: ( n1 n2 --- ) start a loop with initial count n2 and
|
|
1596 \G limit n1. Exit immediately if n1 = n2.
|
|
1597 POSTPONE (?DO) 'LEAVE @ HERE 'LEAVE ! 0 , HERE ; IMMEDIATE
|
|
1598
|
|
1599 : LEAVE ( --- )
|
|
1600 \G Runtime: leave the matching DO LOOP immediately.
|
|
1601 \ All places where a leave address for the loop is needed are in a linked
|
|
1602 \ list, starting with 'LEAVE variable, the other links in the cells where
|
|
1603 \ the leave addresses will come.
|
|
1604 POSTPONE (LEAVE) HERE 'LEAVE @ , 'LEAVE ! ; IMMEDIATE
|
|
1605
|
|
1606 : RESOLVE-LEAVE
|
|
1607 \G Resolve the references to the leave addresses of the loop.
|
|
1608 'LEAVE @
|
|
1609 BEGIN DUP WHILE DUP @ HERE ROT ! REPEAT DROP ;
|
|
1610
|
|
1611 : LOOP ( x --- )
|
|
1612 \G End a DO LOOP.
|
|
1613 \G Runtime: Add 1 to the count and if it is equal to the limit leave the loop.
|
|
1614 POSTPONE (LOOP) , RESOLVE-LEAVE 'LEAVE ! ; IMMEDIATE
|
|
1615
|
|
1616 : +LOOP ( x --- )
|
|
1617 \G End a DO +LOOP
|
|
1618 \G Runtime: ( n ---) Add n to the count and exit if this crosses the
|
|
1619 \G boundary between limit-1 and limit.
|
|
1620 POSTPONE (+LOOP) , RESOLVE-LEAVE 'LEAVE ! ; IMMEDIATE
|
|
1621
|
|
1622 : RECURSE ( --- )
|
|
1623 \G Compile a call to the current (not yet finished) definition.
|
|
1624 LAST @ NAME> COMPILE, ; IMMEDIATE
|
|
1625
|
|
1626 : ." ( "ccc<quote>" --- )
|
|
1627 \G Parse a string delimited by " and compile the following runtime semantics.
|
|
1628 \G Runtime: type that string.
|
|
1629 POSTPONE (.") 34 WORD C@ 1+ ALLOT ALIGN ; IMMEDIATE
|
|
1630
|
|
1631
|
|
1632 : S" ( "ccc<quote>" --- )
|
|
1633 \G Parse a string delimited by " and compile the following runtime semantics.
|
|
1634 \G Runtime: ( --- c-addr u) Return start address and length of that string.
|
|
1635 STATE @ IF POSTPONE (S") 34 WORD C@ 1+ ALLOT ALIGN
|
|
1636 ELSE 34 WORD COUNT POCKET PLACE POCKET COUNT THEN ; IMMEDIATE
|
|
1637
|
|
1638 : ABORT" ( "ccc<quote>" --- )
|
|
1639 \G Parse a string delimited by " and compile the following runtime semantics.
|
|
1640 \G Runtime: ( f --- ) if f is nonzero, print the string and abort program.
|
|
1641 POSTPONE (ABORT") 34 WORD C@ 1+ ALLOT ALIGN ; IMMEDIATE
|
|
1642
|
|
1643 : ABORT ( --- )
|
|
1644 \G Abort unconditionally without a message.
|
|
1645 -1 THROW ;
|
|
1646
|
|
1647 : POSTPONE ( "ccc" --- )
|
|
1648 \G Parse the next word delimited by spaces and compile the following runtime.
|
|
1649 \G Runtime: depending on immediateness EXECUTE or compile the execution
|
|
1650 \G semantics of the parsed word.
|
|
1651 POSTPONE (POSTPONE) ' , ; IMMEDIATE
|
|
1652
|
|
1653 : IMMEDIATE ( --- )
|
|
1654 \G Make last definition immediate, so that it will be executed even in
|
|
1655 \G compilation mode.
|
|
1656 LAST @ DUP C@ 64 OR SWAP C! ;
|
|
1657
|
|
1658 : ( ( "ccc<rparen>" --- )
|
|
1659 \G Comment till next ).
|
|
1660 41 PARSE DROP DROP ; IMMEDIATE
|
|
1661
|
|
1662 : \
|
|
1663 \G Comment till end of line.
|
|
1664 SOURCE >IN ! DROP ; IMMEDIATE
|
|
1665
|
|
1666 : >BODY ( xt --- a-addr)
|
|
1667 \G Convert execution token to parameter field address.
|
|
1668 3 + ;
|
|
1669
|
|
1670 : (;CODE) ( --- )
|
|
1671 \G Runtime for DOES>, exit calling definition and make last defined word
|
|
1672 \G execute the calling definition after (;CODE)
|
|
1673 R> LAST @ NAME> 1+ ! ;
|
|
1674
|
|
1675 : DOES> ( --- )
|
|
1676 \G Word that contains DOES> will change the behavior of the last created
|
|
1677 \G word such that it pushes its parameter field address onto the stack
|
|
1678 \G and then executes whatever comes after DOES>
|
|
1679 POSTPONE (;CODE)
|
|
1680 JSR, LIT DOCOL ,
|
|
1681 ; IMMEDIATE
|
|
1682
|
|
1683 \ PART 10: TOP LEVEL OF INTERPRETER
|
|
1684
|
|
1685 : ?STACK ( ---)
|
|
1686 \G Check for stack over/underflow and abort with an error if needed.
|
|
1687 DEPTH DUP 0< -4 ?THROW 10000 > -3 ?THROW ;
|
|
1688
|
|
1689 : INTERPRET ( ---)
|
|
1690 \G Interpret words from the current source until the input source is exhausted.
|
|
1691 BEGIN
|
|
1692 32 WORD UPPERCASE? DUP C@
|
|
1693 WHILE
|
|
1694 FIND DUP
|
|
1695 IF
|
|
1696 -1 = STATE @ AND
|
|
1697 IF
|
|
1698 COMPILE,
|
|
1699 ELSE
|
|
1700 EXECUTE
|
|
1701 THEN
|
|
1702 ELSE DROP
|
|
1703 NUMBER? 0= -13 ?THROW
|
|
1704 DPL @ 1+ IF
|
|
1705 STATE @ IF SWAP LITERAL LITERAL THEN
|
|
1706 ELSE
|
|
1707 DROP STATE @ IF LITERAL THEN
|
|
1708 THEN
|
|
1709 THEN ?STACK
|
|
1710 REPEAT DROP
|
|
1711 ;
|
|
1712
|
|
1713 : EVALUATE ( c-addr u --- )
|
|
1714 \G Evaluate the string c-addr u as if it were typed on the terminal.
|
|
1715 SID @ >R SRC @ >R #SRC @ >R >IN @ >R
|
|
1716 #SRC ! SRC ! 0 >IN ! -1 SID ! INTERPRET
|
|
1717 R> >IN ! R> #SRC ! R> SRC ! R> SID ! ;
|
|
1718
|
|
1719 VARIABLE ERRORS ( --- a-addr)
|
|
1720 \G This variable contains the head of a linked list of error messages.
|
|
1721
|
|
1722 : ERROR-SOURCE ( --- )
|
|
1723 \G Print location of error source.
|
|
1724 SID @ 0 > IF
|
|
1725 ." in line " LOADLINE @ .
|
|
1726 THEN
|
|
1727 HERE COUNT TYPE CR WARM
|
|
1728 ;
|
|
1729
|
|
1730 : QUIT ( --- )
|
|
1731 \G This word resets the return stack, resets the compiler state, the include
|
|
1732 \G buffer and then it reads and interprets terminal input.
|
|
1733 R0 @ RP! [
|
|
1734 TIB SRC ! 0 SID !
|
|
1735 BEGIN
|
|
1736 REFILL DROP ['] INTERPRET CATCH DUP 0= IF
|
|
1737 DROP STATE @ 0= IF ." OK" THEN CR
|
|
1738 ELSE \ throw occured.
|
|
1739 XABORTIN
|
|
1740 DUP -2 = IF
|
|
1741 ERROR$ @ COUNT TYPE SPACE
|
|
1742 ELSE
|
|
1743 ERRORS @
|
|
1744 BEGIN DUP WHILE
|
|
1745 OVER OVER @ = IF 4 + COUNT TYPE SPACE ERROR-SOURCE THEN CELL+ @
|
|
1746 REPEAT DROP
|
|
1747 ." Error " .
|
|
1748 THEN ERROR-SOURCE
|
|
1749 THEN
|
|
1750 0 UNTIL
|
|
1751 ;
|
|
1752
|
|
1753 : XLOAD
|
|
1754 XOPENIN 1 SID ! 0 LOADLINE ! ;
|
|
1755
|
|
1756 : WARM ( ---)
|
|
1757 \G This word is called when an error occurs. Clears the stacks, sets
|
|
1758 \G BASE to decimal, closes the files and resets the search order.
|
|
1759 R0 @ RP! S0 @ SP! DECIMAL
|
|
1760 2 #ORDER !
|
|
1761 FORTH-WORDLIST CONTEXT !
|
|
1762 FORTH-WORDLIST CONTEXT CELL+ !
|
|
1763 FORTH-WORDLIST CURRENT !
|
|
1764 0 HANDLER !
|
|
1765 ." Welcome to Forth" CR
|
|
1766 QUIT ;
|
|
1767
|
|
1768 CODE COLD ( --- )
|
|
1769 \G The first word that is called at the start of Forth.
|
|
1770 LDY # $8000
|
|
1771 STY R0ADDR
|
|
1772 LDS # $7C00
|
|
1773 STS S0ADDR A;
|
|
1774 $7E C, WARM
|
|
1775 END-CODE
|
|
1776
|
|
1777
|
|
1778 END-CROSS
|
|
1779
|
|
1780 \ PART 10: FINISHING AND SAVING THE TARGET IMAGE.
|
|
1781
|
|
1782 \ Resolve the forward references created by the cross compiler.
|
|
1783 RESOLVE DOCOL RESOLVE DOCON RESOLVE LIT RESOLVE BRANCH RESOLVE ?BRANCH
|
|
1784 RESOLVE (DO) RESOLVE DOVAR RESOLVE UNNEST
|
|
1785 RESOLVE (LOOP) RESOLVE (.")
|
|
1786 RESOLVE COLD RESOLVE WARM
|
|
1787 RESOLVE THROW
|
|
1788 RESOLVE (POSTPONE)
|
|
1789
|
|
1790 \ Store appropriate values into some of the new Forth's variables.
|
|
1791 : CELLS>TARGET
|
|
1792 0 DO OVER I CELLS + @ OVER I CELLS-T + !-T LOOP 2DROP ;
|
|
1793
|
|
1794 #THREADS T' FORTH-WORDLIST >BODY-T !-T
|
|
1795 TLINKS T' FORTH-WORDLIST >BODY-T 2 + #THREADS CELLS>TARGET
|
|
1796 THERE T' DP >BODY-T !-T
|
|
1797
|
|
1798 : TELLMEHOW BASE @ HEX
|
|
1799 ." Type SO" ORIGIN . ." ,then SS" IMAGE . ." ," THERE ORIGIN - .
|
|
1800 BYE ;
|