-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathword.asm
806 lines (741 loc) · 11.9 KB
/
word.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
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
;;; -*- TI-Asm -*-
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Word Manipulation Routines
;;;
;; NewString:
;;
;; Create a new string object. The contents will be uninitialized.
;;
;; Input:
;; - BC = length of string
;;
;; Output:
;; - HL = reference to string
;; - DE = address of first character
;;
;; Destroys:
;; - AF
NewString:
ld h,b
ld l,c
inc hl
inc hl
push bc
ld a,T_STRING<<2
call NewObject
pop bc
ld (hl),c
inc hl
ld (hl),b
inc hl
ex de,hl
ret
;; NewSymbol:
;;
;; Create a new symbol object. Its value will be undefined by
;; default, and it will not be added to the obarray.
;;
;; Input:
;; - BC = length of symbol's name
;;
;; Output:
;; - HL = reference to symbol
;; - DE = address of first character
;;
;; Destroys:
;; - AF
NewSymbol:
ld hl,10
add hl,bc
push bc
ld a,T_SYMBOL<<2
call NewObject
ld (hl),low voidNode ;
inc hl ; procedure
ld (hl),high voidNode ;
inc hl
ld (hl),low voidNode ;
inc hl ; variable
ld (hl),high voidNode ;
inc hl
ld (hl),low voidNode ;
inc hl ; plist
ld (hl),high voidNode ;
inc hl
ld (hl),0 ;
inc hl ; next pointer
ld (hl),0 ;
inc hl
pop bc
ld (hl),c
inc hl
ld (hl),b
inc hl
ex de,hl
ret
;; NewChar:
;;
;; Create a new character atom.
;;
;; Input:
;; - A = character value
;;
;; Output:
;; - HL = reference to character
;;
;; Destroys:
;; - AF, BC, DE
NewChar:
ld bc,T_CHAR<<2
ld l,a
ld h,b
jp NewAtom
;; NewQuote:
;;
;; Create a new quote-prefixed word.
;;
;; Input:
;; - HL = word
;;
;; Output:
;; - HL = word with quote prepended
;;
;; Destroys:
;; - AF
NewQuote:
push bc
push de
ld bc,T_QUOTE<<2
call NewAtom
pop de
pop bc
ret
;; NewColon:
;;
;; Create a new colon-prefixed word.
;;
;; Input:
;; - HL = word
;;
;; Output:
;; - HL = word with colon prepended
;;
;; Destroys:
;; - AF
NewColon:
push bc
push de
ld bc,T_COLON<<2
call NewAtom
pop de
pop bc
ret
;; IsWord:
;;
;; Determine if a value is a word.
;;
;; Input:
;; - HL = value
;;
;; Output:
;; - CF set if value is not a word
;;
;; Destroys:
;; - A
IsWord:
ld a,h
and 80h
ret z
push hl
call RefToPointer
ld a,(hl)
pop hl
rrca
ccf
ret nc
rrca
ret c
cp T_SYMBOL ; all types >= T_SYMBOL are currently
; defined to be words
ret
;; GetWordSize:
;;
;; Calculate the length (number of characters) in a word.
;;
;; Input:
;; - HL = word value (as determined by IsWord)
;;
;; Output:
;; - HL = number of bytes in word
;;
;; Destroys:
;; - AF, B
GetWordSize:
call IsWord
jp c,TypeAssertionFailed
GetWordSize_nc:
bit 7,h
jr z,GetWordSize_Integer
push de
call GetNodeContents
cp T_SYMBOL<<2
jr z,GetWordSize_Symbol
cp T_CHAR<<2
jr z,GetWordSize_Char
cp T_STRING<<2
jr z,GetWordSize_String
cp T_QUOTE<<2
jr z,GetWordSize_Quote
cp T_COLON<<2
jr z,GetWordSize_Colon
; rrca
; jr c,GetWordSize_Float
BCALL _ErrDataType
;; UNREACHABLE
GetWordSize_Symbol:
ld de,8
ADD_BHL_DE
GetWordSize_String:
LOAD_HL_iBHL
pop de
ret
GetWordSize_Char:
pop de
ld hl,1
ret
GetWordSize_Quote:
GetWordSize_Colon:
pop de
WARNING "FIXME: unsafe recursion"
call GetWordSize
inc hl
ret
GetWordSize_Integer:
;; Determine number of digits
ld a,h
ld h,0
or a
jr z,GetWordSize_Integer_0_255
cp 3 ; < 768 ?
jr c,GetWordSize_Integer_256_767
jr z,GetWordSize_Integer_768_1023
cp 27h ; < 9984 ?
jr c,GetWordSize_Integer_1024_9983
jr z,GetWordSize_Integer_9984_10239
ld l,5
ret
GetWordSize_Integer_0_255:
ld a,l
ld l,1
cp 10
ret c
inc l
cp 100
ret c
inc l
ret
GetWordSize_Integer_256_767:
ld l,3
ret
GetWordSize_Integer_768_1023:
ld a,l
ld l,3
cp 0E8h
ret c
inc l
ret
GetWordSize_Integer_1024_9983:
ld l,4
ret
GetWordSize_Integer_9984_10239:
ld a,l
ld l,4
cp 10h
ret c
inc l
ret
;; GetWordChar:
;;
;; Retrieve a given character from a word.
;;
;; (For some types of words, this may be implemented using
;; GetWordChars; but use whichever routine is more appropriate for
;; whatever you're doing. Also note that calling GetWordSize
;; immediately prior to GetWordChar or GetWordChars may speed things
;; up.)
;;
;; Input:
;; - HL = word value (as determined by IsWord)
;; - DE = character offset from start (i.e., 0 for the first
;; character)
;;
;; Output:
;; - A = desired character
;; - CF set if index invalid
;;
;; Destroys:
;; - F, BC, DE, HL
GetWordChar:
call IsWord
jp c,TypeAssertionFailed
GetWordChar_nc:
bit 7,h
jr z,GetWordChar_Integer
push de
call GetNodeContents
cp T_SYMBOL<<2
jr z,GetWordChar_Symbol
cp T_CHAR<<2
jr z,GetWordChar_Char
cp T_STRING<<2
jr z,GetWordChar_String
cp T_QUOTE<<2
jr z,GetWordChar_Quote
cp T_COLON<<2
jr z,GetWordChar_Colon
; rrca
; jr c,GetWordChar_Float
BCALL _ErrDataType
;; UNREACHABLE
GetWordChar_Symbol:
ld de,8
ADD_BHL_DE
GetWordChar_String:
push hl
push bc
LOAD_HL_iBHL ; HL = length of string
pop af
pop bc ; ABC = address of start
pop de ; DE = offset
inc de
or a
sbc hl,de
ret c
inc de
ld h,b
ld l,c
ld b,a
ADD_BHL_DE
LOAD_A_iBHL
or a
ret
GetWordChar_Colon:
ld b,Lcolon
jr GetWordChar_Prefix
GetWordChar_Quote:
ld b,Lquote
GetWordChar_Prefix:
pop de
dec de ; if DE = 0, then return the prefix;
; otherwise, return the character
; (DE-1) of the quoted word
ld a,d
and e
inc a
jr nz,GetWordChar
ld a,b
ret
GetWordChar_Char:
pop de
ld a,d
or e
scf
ret nz
or l
ret
GetWordChar_Integer:
push de
ld de,OP1
push de
call GetWordChars_Integer
pop hl
pop bc
add hl,bc
ret c
sbc hl,de
ccf
ret c
add hl,de
or a
ld a,(hl)
ret
;; GetWordChars:
;;
;; Retrieve all of the characters in a word. Call GetWordSize first
;; to ensure that your buffer is large enough. Result will not be
;; zero-terminated.
;;
;; (For some types of words, this may be implemented using
;; GetWordChar; but use whichever routine is more appropriate for
;; whatever you're doing. Also note that calling GetWordSize
;; immediately prior to GetWordChar or GetWordChars may speed things
;; up.)
;;
;; Input:
;; - HL = word value (as determined by IsWord)
;; - DE = address of buffer to store characters
;;
;; Output:
;; - DE = address of byte after the last in the string
;;
;; Destroys:
;; - AF, BC, HL
GetWordChars:
call IsWord
jp c,TypeAssertionFailed
GetWordChars_nc:
bit 7,h
jr z,GetWordChars_Integer
push de
call GetNodeContents
cp T_SYMBOL<<2
jr z,GetWordChars_Symbol
cp T_CHAR<<2
jr z,GetWordChars_Char
cp T_STRING<<2
jr z,GetWordChars_String
cp T_QUOTE<<2
jr z,GetWordChars_Quote
cp T_COLON<<2
jr z,GetWordChars_Colon
; rrca
; jr c,GetWordChars_Float
BCALL _ErrDataType
;; UNREACHABLE
GetWordChars_Symbol:
ld de,8
ADD_BHL_DE
GetWordChars_String:
push hl
push bc
LOAD_HL_iBHL
pop bc
ex de,hl
pop hl
INC_BHL
INC_BHL
ld a,b
ld b,d
ld c,e
pop de
FLASH_TO_RAM
ret
GetWordChars_Char:
ld a,l
pop de
ld (de),a
inc de
ret
GetWordChars_Colon:
ld a,Lcolon
jr GetWordChars_Prefix
GetWordChars_Quote:
ld a,Lquote
GetWordChars_Prefix:
pop de
ld (de),a
inc de
jr GetWordChars
GetWordChars_Integer:
or a
ld bc,-10000
call GetWordChars_Integer_Digit
ld bc,-1000
call GetWordChars_Integer_Digit
ld bc,-100
call GetWordChars_Integer_Digit
ld bc,-10
call GetWordChars_Integer_Digit
ld a,l
add a,L0
ld (de),a
inc de
ret
GetWordChars_Integer_Digit:
push af
ld a,L0-1
GetWordChars_Integer_DigitLoop:
add hl,bc
inc a
jr c,GetWordChars_Integer_DigitLoop
sbc hl,bc
pop bc
rr c
jr c,GetWordChars_Integer_NoHide0
cp L0
ret z
scf
GetWordChars_Integer_NoHide0:
ld (de),a
inc de
ret
;; WordToSymbol:
;;
;; Convert given word into a symbol.
;;
;; Input:
;; - HL = word
;;
;; Output:
;; - HL = symbol
;; - CF set for conversion error
;;
;; Destroys:
;; - AF
WordToSymbol:
call IsWord
ret c
WordToSymbol_nc:
call GetType
cp T_SYMBOL
ret z
push de
push bc
cp T_INT
jr z,WordToSymbol_Short
cp T_REAL
jr z,WordToSymbol_Short
cp T_COMPLEX
jr z,WordToSymbol_Short
cp T_CHAR
jr z,WordToSymbol_Short
cp T_STRING
jr z,WordToSymbol_String
warning "FIXME: how to quickly symbolize a quote, colon, or paged string?"
; cp T_QUOTE
; jr z,WordToSymbol_Quote
; cp T_COLON
; jr z,WordToSymbol_Colon
WordToSymbol_Error:
pop bc
pop de
scf
ret
WordToSymbol_Short:
ld de,OP3
push de
call GetWordChars
ex de,hl
pop de
or a
sbc hl,de
ld b,h
ld c,l
ex de,hl
call GetNamedSymbol
pop bc
pop de
or a
ret
WordToSymbol_String:
call GetNodeContents
ifndef NO_PAGED_MEM
ld a,b
or a
jr nz,WordToSymbol_Error
endif
ld c,(hl)
inc hl
ld b,(hl)
inc hl
call GetNamedSymbol
pop bc
pop de
or a
ret
;; GetSymbolProcedure:
;;
;; Get the procedure definition (if any) associated with a symbol.
;;
;; Input:
;; - HL = symbol
;;
;; Output:
;; - HL = procedure definition, or void if undefined
;;
;; Destroys:
;; - AF, BC, DE
GetSymbolProcedure:
bit 7,h
jp z,TypeAssertionFailed
GetSymbolProcedure_nc:
call GetNodeContents
cp T_SYMBOL<<2
jp nz,TypeAssertionFailed
LOAD_HL_iBHL
ret
;; GetSymbolVariable:
;;
;; Get the variable value (if any) associated with a symbol.
;;
;; Input:
;; - HL = symbol
;;
;; Output:
;; - HL = variable value, or void if undefined
;;
;; Destroys:
;; - AF, BC, DE
GetSymbolVariable:
bit 7,h
jp z,TypeAssertionFailed
GetSymbolVariable_nc:
call GetNodeContents
cp T_SYMBOL<<2
jp nz,TypeAssertionFailed
INC_BHL
INC_BHL
LOAD_HL_iBHL
ret
;; SetSymbolProcedure:
;;
;; Set the procedure definition associated with a symbol.
;;
;; Input:
;; - HL = symbol
;; - DE = new value
;;
;; Destroys:
;; - AF, BC, DE, HL
SetSymbolProcedure:
bit 7,h
jp z,TypeAssertionFailed
ex de,hl
call GetType
cp T_SUBR
jr z,SetSymbolProcedure_OK
cp T_LIST
jp nz,TypeAssertionFailed
push hl
call GetListFirst
call IsList
pop hl
jp c,TypeAssertionFailed
SetSymbolProcedure_OK:
ex de,hl
SetSymbolProcedure_nc:
push de
call GetNodeContents
pop de
cp T_SYMBOL<<2
jp nz,TypeAssertionFailed
LOAD_iBHL_DE
ret
;; SetSymbolVariable:
;;
;; Set the variable value associated with a symbol.
;;
;; Input:
;; - HL = symbol
;; - DE = new value
;;
;; Destroys:
;; - AF, BC, DE, HL
SetSymbolVariable:
bit 7,h
jp z,TypeAssertionFailed
SetSymbolVariable_nc:
push de
call GetNodeContents
pop de
cp T_SYMBOL<<2
jp nz,TypeAssertionFailed
INC_BHL
INC_BHL
LOAD_iBHL_DE
ret
;; GetNamedSymbol:
;;
;; Find (or create) the symbol with the given name.
;;
;; Input:
;; - HL = address of first character of name
;; - BC = number of characters in name
;;
;; Output:
;; - HL = symbol
;;
;; Destroys:
;; - AF, BC, DE
GetNamedSymbol:
ld (symbolSearchMPtr),hl
ld (symbolSearchLen),bc
ld hl,(firstSymbol)
push af
push hl
jr GetNamedSymbol_NextSymbol
GetNamedSymbol_NextSymbol1:
pop af
GetNamedSymbol_NextSymbol:
pop hl
pop af
ld a,h
or l
jr z,GetNamedSymbol_Failed
push hl ; current symbol
call GetNodeContents
cp T_SYMBOL<<2
jp nz,TypeAssertionFailed1
ld de,6
ADD_BHL_DE
LOAD_DE_iBHL
push de ; next symbol
INC_BHL
LOAD_DE_iBHL ; DE = length of current symbol
INC_BHL
;; Check if length is the same
push hl
ld hl,(symbolSearchLen)
or a
sbc hl,de
pop hl
jr nz,GetNamedSymbol_NextSymbol
ld c,d
ld a,e ; CA = length
ld de,(symbolSearchMPtr) ; DE -> search string, BHL -> symbol
GetNamedSymbol_CompareLoop:
push af
or c
jr z,GetNamedSymbol_Done
LOAD_A_iBHL
ex de,hl
cp (hl)
jr nz,GetNamedSymbol_NextSymbol1
ex de,hl
inc de
INC_BHL
pop af
sub 1
jr nc,GetNamedSymbol_CompareLoop
dec c
jr GetNamedSymbol_CompareLoop
GetNamedSymbol_Done:
pop af
pop hl
pop hl
ret
GetNamedSymbol_Failed:
;; Create a new symbol
ld bc,(symbolSearchLen)
call NewSymbol
push hl
push de
dec de
dec de
dec de
ld hl,(firstSymbol)
ex de,hl
ld (hl),d
dec hl
ld (hl),e
pop de
ld hl,(symbolSearchMPtr)
ld a,b
or c
jr z,GetNamedSymbol_Empty
ldir
GetNamedSymbol_Empty:
pop hl
ld (firstSymbol),hl
ret