-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathinterpreter.asm
747 lines (648 loc) · 11.8 KB
/
interpreter.asm
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
; QUIT EXECUTE INTERPRET NOTFOUND ' FIND FIND-NAME >XT PARSE-NAME WORD EVALUATE
; ABORT /STRING DOWORDS
restore_handler
pha ; save a
txa ; copy x
pha ; save x
tya ; copy y
pha ; save y
lda #$7f ; disable all CIA 2 interrupts
sta $dd0d ;
ldy $dd0d ; save CIA 2 interrupt control register for kernal_nmi
bpl brk_handler ; CIA 2 is not the NMI source if the most significant bit is not set.
kernal_nmi
jmp $fe72 ; all CIA 2 NMI's fall through to the Kernals' RS-232 routines
brk_handler ; all non-CIA NMI (RESTORE key) and brk instructions- via IRQ vector end up here.
pla ; drop y -the return stack will be reset by QUIT anyway
pla ; pull x
tax ; restore parameter stack pointer for QUIT
jmp QUIT ; already under sei from NMI stub in Kernal or from IRQ to brk_handler
quit_reset
sei ; goes here from QUIT and program start
lda #<restore_handler
sta $318
lda #>restore_handler
sta $319
lda #<brk_handler
sta $316
lda #>brk_handler
sta $317
cli ; still have to
; lores
lda #$9b
sta $d011
lda #$17
sta $dd00
sta $d018
txa
pha
ldx #0
stx $d020
stx $d021
lda #>TIB
sta TIB_PTR + 1
lda #$36 ; ram + i/o + kernal
sta 1
; Yellow text.
lda #7
sta $286
; Clears color area.
- sta $d800, x
sta $d900, x
sta $da00, x
sta $db00, x
dex
bne -
stx STATE
stx TIB_SIZE
stx TIB_SIZE + 1
stx TIB_PTR
stx TO_IN_W
stx TO_IN_W + 1
stx SOURCE_ID_LSB
stx SOURCE_ID_MSB
stx SAVE_INPUT_STACK_DEPTH
; Used registers: A, X, Y
close_all_logical_files:
jsr CLRCHN
ldx #0
- txa
pha
jsr CLOSE
pla
tax
dex
bne -
pla
tax
rts
+BACKLINK "quit", 4
QUIT
jsr quit_reset
; resets the return stack
txa
INIT_S = * + 1
ldx #0
txs
tax
interpret_loop
jsr REFILL_OR_CLOSE
jsr interpret_tib
jmp interpret_loop
interpret_tib
jsr INTERPRET
cpx #X_INIT+1
bpl .on_stack_underflow
lda TO_IN_W
cmp TIB_SIZE
bne interpret_tib
lda TO_IN_W + 1
cmp TIB_SIZE + 1
bne interpret_tib
; 0 - keyboard, -1 evaluate, else file
lda SOURCE_ID_LSB
beq +
rts
+ lda LATEST_LSB
sec
sbc HERE_LSB
lda LATEST_MSB
sbc HERE_MSB
beq .on_data_underflow
lda STATE
bne +
lda #'o'
jsr PUTCHR
lda #'k'
jsr PUTCHR
lda #$d
jmp PUTCHR
+ rts
.on_stack_underflow
lda #$12 ; reverse on
jsr PUTCHR
lda #'e'
jsr PUTCHR
lda #'r'
jsr PUTCHR
jmp .stop_error_print
.on_data_underflow
lda #$12 ; reverse on
jsr PUTCHR
lda #'f'
jsr PUTCHR
lda #'u'
jsr PUTCHR
lda #'l'
jsr PUTCHR
lda #$d
jmp PUTCHR
+BACKLINK "execute", 7
EXECUTE
lda LSB, x
sta W
lda MSB, x
sta W + 1
inx
jmp (W)
+BACKLINK "interpret", 9
INTERPRET
jsr PARSE_NAME
lda LSB,x
bne +
inx
inx
rts
+
jsr TWODUP
jsr FIND_NAME ; ( caddr u 0 | caddr u nt )
lda MSB, x
bne .found_word
inx
jsr READ_NUMBER
beq .was_number
jmp print_word_not_found_error
; yep, it's a number...
.was_number
lda STATE ; are we compiling?
bne +
rts
+ ; yes, compile the number
sta curr_word_no_tail_call_elimination
jmp LITERAL
.found_word
; OK, we found a word...
lda MSB, x
ldy LSB, x
inx
sta MSB, x
sty LSB, x
sta MSB+1, x
sty LSB+1, x
jsr TO_XT
jsr SWAP
jsr GET_IMMED ; ( xt 1 | xt -1 )
inx
lda curr_word_no_tail_call_elimination
sta last_word_no_tail_call_elimination
FOUND_WORD_WITH_NO_TCE = * + 1
lda #0
sta curr_word_no_tail_call_elimination
; Executes the word if it is immediate, or interpreting.
lda MSB-1, x
and STATE
beq EXECUTE
; OK, this word should be compiled...
jmp COMPILE_COMMA
+BACKLINK "notfound",8
print_word_not_found_error ; ( caddr u -- )
lda #$12 ; reverse on
jsr PUTCHR
jsr TYPE
lda #'?'
.stop_error_print
jsr PUTCHR
lda #$d ; cr
jsr PUTCHR
jmp ABORT
+BACKLINK "'", 1
jsr PARSE_NAME
jsr TWODUP
jsr FIND_NAME
lda MSB,x
beq print_word_not_found_error
+ jsr TO_XT
jsr NIP
jmp NIP
+BACKLINK "find", 4
FIND ; ( xt -1 | xt 1 | caddr 0 )
jsr DUP
jsr TO_R
jsr COUNT
jsr FIND_NAME
lda MSB, x
beq +
jsr DUP
jsr TO_XT
jsr SWAP
jsr GET_IMMED
jsr R_TO
inx
rts
+ inx
jsr R_TO
jmp ZERO
FIND_BUFFER = $33c
FIND_BUFFER_SIZE = 31
+BACKLINK "find-name", 9
FIND_NAME ; ( caddr u -- nt | 0 )
inx
lda LSB-1,x
tay
beq .find_failed
cmp #FIND_BUFFER_SIZE+1
bcs .find_failed
sta .findlen + 1
lda MSB,x
sta W2+1
lda LSB,x
sta W2
dey
- lda (W2),y
jsr CHAR_TO_LOWERCASE
sta FIND_BUFFER,y
dey
bpl -
lda LATEST_LSB
sta W
lda LATEST_MSB
sta W + 1
; W now contains new dictionary pointer.
ldy #0
lda (W), y ; get string length of dictionary word
.examine_word
and #STRLEN_MASK
.findlen
cmp #$ff ; overwritten
beq .string_compare
.string_compare_failed
; no match, advance the dp
clc
adc #3
adc W
sta W
bcc +
inc W + 1
+ lda (W), y
; Is word null? If not, examine it.
bne .examine_word
; It is null - give up.
.find_failed
inx
jmp ZERO
.string_compare
; equal strlen, now compare strings...
tay
- lda (W), y
cmp FIND_BUFFER - 1, y
bne .word_not_equal
dey
bne -
; word is equal!
; return address to dictionary word
ldy #0
lda (W), y
; Immediate words are exempt from TCE because custom compile-time behavior.
; (E.g. DROP compiles inx instead of jsr DROP.)
and #F_NO_TAIL_CALL_ELIMINATION | F_IMMEDIATE
sta FOUND_WORD_WITH_NO_TCE
lda W
sta LSB, x
lda W + 1
sta MSB, x
rts
.word_not_equal
ldy #0
lda (W), y
and #STRLEN_MASK
jmp .string_compare_failed
GET_IMMED ; ( nt -- 1 | -1 )
lda MSB, x
sta W + 1
lda LSB, x
sta W
ldy #0
lda (W), y ; a contains string length + mask
and #F_IMMEDIATE
beq .not_immed
sty MSB, x ; 0
iny
sty LSB, x ; 1
rts
.not_immed
lda #$ff
sta LSB, x
sta MSB, x
rts
+BACKLINK ">xt", 3
TO_XT
lda MSB, x
sta W + 1
lda LSB, x
sta W
; W contains pointer to word
ldy #0
lda (W), y ; a contains string length + mask
and #STRLEN_MASK
clc
adc #1 ; offset for char + string length
adc LSB, x
sta LSB, x
bcc +
inc MSB, x
+ jsr FETCH
rts
IS_SPACE ; ( c -- f )
ldy #1
lda LSB,x
cmp #' ' | 0x80
beq .is_space
lda #' '
cmp LSB,x
bcs .is_space
dey
.is_space:
sty LSB,x
sty MSB,x
rts
IS_NOT_SPACE ; ( c -- f )
jsr IS_SPACE
jmp ZEQU
XT_SKIP ; ( addr n xt -- addr n )
; skip all chars satisfying xt
jsr TO_R
- jsr DUP
jsr ZBRANCH
!word .done
jsr OVER
jsr FETCHBYTE
jsr R_FETCH
jsr EXECUTE
jsr ZBRANCH
!word .done
jsr ONE
jsr SLASH_STRING
jmp -
.done
jsr R_TO
inx
rts
+BACKLINK "parse-name", 10
PARSE_NAME ; ( name -- addr u )
jsr SOURCE
jsr TO_IN
jsr FETCH
jsr SLASH_STRING
jsr LIT
!word IS_SPACE
jsr XT_SKIP
jsr OVER
jsr TO_R
jsr LIT
!word IS_NOT_SPACE
jsr XT_SKIP
jsr TWODUP
jsr ONE
jsr MIN
jsr PLUS
jsr SOURCE
inx
jsr MINUS
jsr TO_IN
jsr STORE
inx
jsr R_TO
jsr TUCK
jmp MINUS
; WORD ( delim -- strptr )
+BACKLINK "word", 4
WORD
jsr ZERO
jsr HERE
jsr STOREBYTE
; skips initial delimiters.
- jsr GET_CHAR_FROM_TIB
beq .word_end
jsr .is_delim
beq -
jmp .append
- jsr GET_CHAR_FROM_TIB
beq .word_end
jsr .is_delim
beq .word_end
.append
jsr pushya
jsr HERE
jsr FETCHBYTE
jsr ONEPLUS
jsr HERE
jsr STOREBYTE
jsr HERE
jsr HERE
jsr FETCHBYTE
jsr PLUS
jsr STOREBYTE
jmp -
.word_end
inx
jmp HERE
.is_delim
; a == delim?
cmp LSB,x
beq + ; yes
; delim == space?
ldy LSB,x
cpy #K_SPACE
bne + ; no
; compare with nonbreaking space, too
cmp #K_SPACE | $80
+ rts
+BACKLINK "evaluate", 8
EVALUATE
jsr PUSH_INPUT_SOURCE
lda LSB + 1, x
sta TIB_PTR
lda MSB + 1, x
sta TIB_PTR + 1
lda LSB, x
sta TIB_SIZE
lda MSB, x
sta TIB_SIZE + 1
inx
inx
ldy #0
sty TO_IN_W
sty TO_IN_W + 1
dey
sty SOURCE_ID_LSB
sty SOURCE_ID_MSB
jmp interpret_tib
+BACKLINK "abort", 5
ABORT
ldx #X_INIT ; reset stack
jmp QUIT
+BACKLINK "/string", 7
SLASH_STRING ; ( addr u n -- addr u )
jsr DUP
jsr TO_R
jsr MINUS
jsr SWAP
jsr R_TO
jsr PLUS
jmp SWAP
apply_base
sta BASE
dec .chars_to_process
inc W3
bne +
inc W3+1
+ lda (W3),y
rts
; Z = success, NZ = fail
; success: ( caddr u -- number )
; fail: ( caddr u -- caddr u )
READ_NUMBER
lda LSB,x
sta .chars_to_process
lda MSB+1,x
sta W3+1
lda LSB+1,x
sta W3
lda BASE
sta OLD_BASE
ldy #0
sty .negate
dex
dex
sty LSB+1,x
sty MSB+1,x
sty MSB,x
lda (W3), y
cmp #"'"
beq .parse_char
cmp #"#"
bne .check_decimal
lda #10
jsr apply_base
.check_decimal
cmp #"$"
bne .check_binary
lda #16
jsr apply_base
.check_binary
cmp #"%"
bne .check_negate
lda #2
jsr apply_base
.check_negate
cmp #"-"
bne .loop_entry
inc .negate
jmp .prepare_next_char
.next_digit
; number *= BASE
lda BASE
sta LSB,x
jsr U_M_STAR
lda LSB,x
bne .parse_failed ; overflow!
inc W3
bne +
inc W3+1
+ lda (W3), y
.loop_entry
jsr CHAR_TO_LOWERCASE
clc
adc #-$30 ; petscii 0-9 -> 0-9
cmp #10 ; within 0-9?
bcc +
clc
adc #-$7 ; a-f..?
cmp #10
bcc .parse_failed
+ cmp BASE
bcs .parse_failed
adc LSB+1,x
sta LSB+1,x
bcc .prepare_next_char
inc MSB+1,x
beq .parse_failed
.prepare_next_char
dec .chars_to_process
bne .next_digit
.parse_done
OLD_BASE = * + 1
lda #0
sta BASE
lda LSB+1,x
sta LSB+3,x
lda MSB+1,x
sta MSB+3,x
inx
inx
inx
.negate = * + 1
lda #0
beq +
jsr NEGATE
tya ; clear Z flag
+ rts
.parse_char
lda .chars_to_process
cmp #3
bne .parse_failed
ldy #2
lda (W3),y
cmp #"'"
bne .parse_failed
dey
lda (W3),y
sta LSB+1,x
lda #0
sta MSB+1,x
jmp .parse_done
.parse_failed
inx
inx ; Z flag set
rts
.chars_to_process
!byte 0
+BACKLINK "dowords", 7 ; ( xt -- )
; to be useful, nothing must be left on stack before execute
; so that there is no distance between nt and the rest of the stack
lda LSB,x
sta .xt
lda MSB, x
sta .xt + 1
inx
lda LATEST_LSB
sta .dowords_nametoken
lda LATEST_MSB
sta .dowords_nametoken + 1
.dowords_lambda
lda .dowords_nametoken
sta W
lda .dowords_nametoken + 1
sta W + 1
ldy #0
lda (W), y
bne +
- rts
+ and #STRLEN_MASK
pha
dex
lda .dowords_nametoken
sta LSB, x
lda .dowords_nametoken + 1
sta MSB, x
.xt = * + 1
jsr PLACEHOLDER_ADDRESS
inx
lda MSB-1, x
ora LSB-1, x
beq .cancel
pla
clc
adc #3 ; guaranteed carry clear
adc .dowords_nametoken
sta .dowords_nametoken
lda .dowords_nametoken + 1
adc #0
sta .dowords_nametoken + 1
jmp .dowords_lambda
; using a word here in case the lambda trashes Ws
.dowords_nametoken
!word 0
.cancel
pla
rts