forked from jkotlinski/durexforth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpreter.asm
743 lines (644 loc) · 11.3 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
;{{{ The MIT License
;
;Copyright (c) 2008 Johan Kotlinski
;
;Permission is hereby granted, free of charge, to any person obtaining a copy
;of this software and associated documentation files (the "Software"), to deal
;in the Software without restriction, including without limitation the rights
;to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
;copies of the Software, and to permit persons to whom the Software is
;furnished to do so, subject to the following conditions:
;
;The above copyright notice and this permission notice shall be included in
;all copies or substantial portions of the Software.
;
;THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
;THE SOFTWARE. }}}
; QUIT INTERPRET FIND FIND-NAME >CFA PARSE-NAME WORD EXECUTE EVALUATE ' ABORT /STRING
restore_handler
cli
jmp QUIT
quit_reset
sei
lda #<restore_handler
sta $318
lda #>restore_handler
sta $319
cli
; 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
stx READ_EOF
jsr CHKIN
pla
tax
rts
+BACKLINK
!byte 4
!text "quit"
QUIT
jsr quit_reset
; resets the return stack
txa
INIT_S = * + 1
ldx #0
txs
tax
interpret_loop
jsr REFILL
jsr interpret_tib
jmp interpret_loop
+BACKLINK
!byte 3
!text "bye"
ldx INIT_S
txs
pla
sta $319
pla
sta $318
pla
sta 1
rts
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
lda SOURCE_ID_LSB
beq +
rts
+ lda #'o'
jsr PUTCHR
lda #'k'
jsr PUTCHR
lda #$d
jmp PUTCHR
.on_stack_underflow
lda #$12 ; reverse on
jsr PUTCHR
lda #'e'
jsr PUTCHR
lda #'r'
jsr PUTCHR
jmp .stop_error_print
+BACKLINK
!byte 7
!text "execute"
EXECUTE
lda LSB, x
sta W
lda MSB, x
sta W + 1
inx
jmp (W)
+BACKLINK
!byte 9
!text "interpret"
INTERPRET
jsr PARSE_NAME
lda LSB,x
bne +
inx
inx
rts
+
jsr FIND_NAME ; replace string with dictionary ptr
lda LSB, x
bne .found_word
inx ; drop
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 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.
inx
lda MSB-1, x
and STATE
beq EXECUTE
; OK, this word should be compiled...
jmp COMPILE_COMMA
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
!byte 1
!text "'"
jsr PARSE_NAME
jsr FIND_NAME
inx
lda LSB-1,x
beq print_word_not_found_error
rts
+BACKLINK
!byte 4
!text "find"
FIND
jsr DUP
jsr TO_R
jsr COUNT
jsr FIND_NAME
lda LSB,x
beq +
jsr R_TO
inx
rts
+ inx
inx
inx
jsr R_TO
jmp ZERO
+BACKLINK
!byte 9
!text "find-name"
FIND_NAME ; ( caddr u -- caddr u 0 | xt 1 | xt -1 )
txa
pha
lda LSB,x
beq .find_failed
sta .findlen + 1
sta .findlen2 + 1
lda MSB+1,x
sta W2+1
lda LSB+1,x
sta W2
lda W2
bne +
dec W2+1
+ dec W2
lda W2
bne +
dec W2+1
+ dec W2
ldx _LATEST
lda _LATEST + 1
.examine_word
sta W + 1
stx W
; W now contains new dictionary word.
ldy #2
lda (W), y ; get string length of dictionary word
and #STRLEN_MASK | F_HIDDEN ; include hidden flag... so we don't find the hidden words.
.findlen
cmp #0
beq .string_compare
.word_not_equal
; no match, advance the linked list.
ldy #0
lax (W), y
iny
lda (W), y
; Is word null? If not, examine it.
bne .examine_word
; It is null - give up.
.find_failed
pla
tax
jmp ZERO
.string_compare ; y = 2
; equal strlen, now compare strings...
.findlen2
lda #0
sta .strlen
- lda (W2), y ; find string
jsr CHAR_TO_LOWERCASE
iny
cmp (W), y ; dictionary string
bne .word_not_equal
dec .strlen
beq .word_is_equal
jmp -
.strlen !byte 0
.word_is_equal
; return address to dictionary word
pla
tax
inx
lda W
sta LSB, x
lda W + 1
sta MSB, x
jsr TO_XT
dex
ldy #2
lda (W), y
and #F_NO_TAIL_CALL_ELIMINATION | F_IMMEDIATE
sta FOUND_WORD_WITH_NO_TCE
lda (W), y ; a contains string length + mask
and #F_IMMEDIATE
beq .not_immed
dey
sty LSB, x ; 1
dey
sty MSB, x ; 0
rts
.not_immed
lda #$ff
sta LSB, x
sta MSB, x
rts
+BACKLINK
!byte 3
!text ">xt"
TO_XT
lda MSB, x
sta W + 1
lda LSB, x
sta W
; W contains pointer to word
ldy #2
lda (W), y ; a contains string length + mask
and #STRLEN_MASK
clc
adc #3 ; offset for link + string length
adc LSB, x
sta LSB, x
bcc +
inc MSB, x
+ 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
!byte 10
!text "parse-name"
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
!byte 4
!text "word"
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
!byte 8
!text "evaluate" ; ( addr size -- )
EVALUATE
jsr SAVE_INPUT
lda LSB + 1, x
sta TIB_PTR
lda MSB + 1, x
sta TIB_PTR + 1
jsr PLUS
lda LSB, x
sta .bufend
lda MSB, x
sta .bufend + 1
inx
jsr evaluate_get_new_line
ldy #$ff
sty SOURCE_ID_LSB
sty SOURCE_ID_MSB
.eval_loop
lda TIB_PTR + 1
cmp .bufend + 1
bcc +
lda TIB_PTR
cmp .bufend
bcc +
jmp RESTORE_INPUT ; exit
+
jsr interpret_tib
jsr REFILL
jmp .eval_loop
evaluate_get_new_line
ldy #0
sty TO_IN_W
sty TO_IN_W + 1
; Determines TIB_SIZE.
lda TIB_PTR
sta W
lda TIB_PTR + 1
sta W + 1
.findtibsizeloop
lda .bufend + 1
cmp W + 1
bcc .foundeol
bne +
lda W
cmp .bufend
bcs .foundeol
+
ldy #0
lda (W),y
cmp #K_RETURN
beq .foundeol
inc W
bne +
inc W + 1
+
jmp .findtibsizeloop
.foundeol
lda W
sec
sbc TIB_PTR
sta TIB_SIZE
lda W + 1
sbc TIB_PTR + 1
sta TIB_SIZE + 1
rts
evaluate_consume_tib
lda TIB_PTR
clc
adc TIB_SIZE
sta TIB_PTR
lda TIB_PTR + 1
adc TIB_SIZE + 1
sta TIB_PTR + 1
inc TIB_PTR ; skip cr
bne +
inc TIB_PTR + 1
+ rts
.bufend
!word 0
+BACKLINK
!byte 5
!text "abort"
ABORT
ldx #X_INIT ; reset stack
jmp QUIT
+BACKLINK
!byte 7
!text "/string"
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