forked from breakintoprogram/agon-bbc-basic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec.asm
2712 lines (2704 loc) · 52.9 KB
/
exec.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
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;
; Title: BBC Basic Interpreter - Z80 version
; Statement Execution & Assembler Module - "EXEC"
; Author: (C) Copyright R.T.Russell 1984
; Modified By: Dean Belfield
; Created: 03/05/2022
; Last Updated: 20/09/2022
;
; Modinfo:
; 27/01/1984: Version 2.1
; 02/03/1987: Version 3.0
; 11/06/1987: Version 3.1
; 03/05/2022: Modified by Dean Belfield to assemble with ZDS
; 20/09/2022: Reformatted Z80 assembler defines to make it easier to read
.ASSUME ADL = 0
INCLUDE "equs.inc"
SEGMENT CODE
XDEF XEQ
XDEF CHAIN0
XDEF RUN
XDEF SYNTAX
XDEF ESCAPE
XDEF FN
XDEF USR
XDEF STORE5
XDEF STORE4
XDEF CHECK
XDEF TERMQ
XDEF FILL
XDEF X4OR5
XDEF MUL16
XDEF CHANEL
XREF AUTO
XREF DELETE
XREF LOAD
XREF LIST_
XREF NEW
XREF OLD
XREF RENUM
XREF SAVE
XREF SOUND
XREF CLG
XREF DRAW
XREF ENVEL
XREF GCOL
XREF MODE
XREF MOVE
XREF PLOT
XREF COLOUR
XREF EXPRS
XREF HIMEM
XREF LOAD0
XREF RANDOM
XREF CLEAR
XREF ERRTRP
XREF PAGE_
XREF DATAPTR
XREF ERRLIN
XREF TRAP
XREF NXT
XREF SETLIN
XREF CLOOP
XREF OSSHUT
XREF WARM
XREF TRACEN
XREF OUTCHR
XREF PBCDL
XREF OSCLI
XREF LISTON
XREF GETVAR
XREF PUTVAR
XREF DATPTR
XREF ERROR_
XREF EXPR
XREF CREATE
XREF EXPRI
XREF BRAKET
XREF FREE
XREF OSBPUT
XREF COUNT
XREF STR
XREF HEXSTR
XREF CRLF
XREF ITEMI
XREF FINDL
XREF TEST
XREF EXPRN
XREF DLOAD5
XREF LOADN
XREF FPP
XREF SWAP
XREF GETDEF
XREF ZERO
XREF OSBGET
XREF BUFFER
XREF CONS
XREF VAL0
XREF OSLINE
XREF CLRSCN
XREF TELL
XREF SAYLN
XREF REPORT
XREF PUTPTR
XREF PUTIME
XREF PUTIMS
XREF LOMEM
XREF WIDTH
XREF OSWRCH
XREF COMMA
XREF OSCALL
XREF SFIX
XREF LOAD4
XREF PUSHS
XREF POPS
XREF LOADS
XREF PUTCSR
XREF OUT_
;
; List of token values used in this module
;
TAND: EQU 80H
TOR: EQU 84H
TERROR: EQU 85H
LINE_: EQU 86H
OFF_: EQU 87H
STEP: EQU 88H
SPC: EQU 89H
TAB: EQU 8AH
ELSE_: EQU 8BH
THEN: EQU 8CH
LINO: EQU 8DH
TO: EQU B8H
TCMD: EQU C6H
TCALL: EQU D6H
DATA_: EQU DCH
DEF_: EQU DDH
TGOSUB: EQU E4H
TGOTO: EQU E5H
TON: EQU EEH
TPROC: EQU F2H
TSTOP: EQU FAH
;
; The command table
; Commands are tokens from C6H onwards; this lookup table is used to
; run the corresponding function; Note that DATA and DEF both use the same
; code as REM
;
CMDTAB: DW AUTO ; C6H
DW DELETE ; C7H
DW LOAD ; C8H
DW LIST_ ; C9H
DW NEW ; CAH
DW OLD ; CBH
DW RENUM ; CCH
DW SAVE ; CDH
DW PUT ; CEH
DW PTR ; CFH
DW PAGEV ; D0H
DW TIMEV ; D1H
DW LOMEMV ; D2H
DW HIMEMV ; D3H
DW SOUND ; D4H
DW BPUT ; D5H
DW CALL_ ; D6H
DW CHAIN ; D7H
DW CLR ; D8H
DW CLOSE ; D9H
DW CLG ; DAH
DW CLS ; DBH
DW REM ; DCH: DATA
DW REM ; DDH: DEF
DW DIM ; DEH
DW DRAW ; DFH
DW END_ ; E0H
DW ENDPRO ; E1H
DW ENVEL ; E2H
DW FOR ; E3H
DW GOSUB ; E4H
DW GOTO ; E5H
DW GCOL ; E6H
DW IF_ ; E7H
DW INPUT ; E8H
DW LET ; E9H
DW LOCAL_ ; EAH
DW MODE ; EBH
DW MOVE ; ECH
DW NEXT ; EDH
DW ON_ ; EEH
DW VDU ; EFH
DW PLOT ; F0H
DW PRINT_ ; F1H
DW PROC ; F2H
DW READ ; F3H
DW REM ; F4H
DW REPEAT ; F5H
DW REPOR ; F6H
DW RESTOR ; F7H
DW RETURN ; F8H
DW RUN ; F9H
DW STOP ; FAH
DW COLOUR ; FBH
DW TRACE ; FCH
DW UNTIL ; FDH
DW WIDTHV ; FEH
DW CLI ; FFH: OSCLI
;
; RUN
; RUN "filename"
;
RUN: CALL TERMQ ; Standalone RUN command?
JR Z,RUN0 ; Yes, so just RUN the code
;
; CHAIN "filename"
;
CHAIN: CALL EXPRS ; Get the filename
LD A,CR ; Terminate it with a CR
LD (DE),A
CHAIN0: LD SP,(HIMEM) ; Reset SP
CALL LOAD0 ; And load the file in
;
RUN0: LD SP,(HIMEM) ; Prepare for RUN
LD IX,RANDOM ; Pointer to the RANDOM sysvar
$$: LD A, R ; Use the R register to seed the random number generator
JR Z, $B ; Loop unti we get a non-zero value in A
RLCA ; Rotate it
RLCA
LD (IX+3),A ; And store
SBC A,A ; Depending upon the C flag, this will either be 00h or FFh
LD (IX+4),A ; And store
CALL CLEAR
LD HL,0 ; Clear the error trap sysvar
LD (ERRTRP),HL
LD HL,(PAGE_) ; Load HL with the start of program memory (PAGE)
LD A,DATA_ ; The DATA token value
CALL SEARCH ; Search for the first DATA token in the tokenised listing
LD (DATPTR),HL ; Set data pointer
LD IY,(PAGE_) ; Load IY with the start of program memory
;
XEQ0: CALL NEWLIN
XEQ: LD (ERRLIN),IY ; Error pointer
CALL TRAP ; Check keyboard
XEQ1: CALL NXT
INC IY
CP ':' ; Seperator
JR Z,XEQ1
CP CR
JR Z,XEQ0 ; New program line
SUB TCMD
JR C,LET0 ; Implied "LET"
ADD A,A
LD C,A
LD B,0
LD HL,CMDTAB
ADD HL,BC
LD A,(HL) ; Table entry
INC HL
LD H,(HL)
LD L,A
CALL NXT
JP (HL) ; Execute the statement
;
;END
;
END_: CALL SETLIN ;FIND CURRENT LINE
LD A,H
OR L ;DIRECT?
JP Z,CLOOP
LD E,0
CALL OSSHUT ;CLOSE ALL FILES
JP WARM ;"Ready"
;
NEWLIN: LD A,(IY+0) ;A=LINE LENGTH
LD BC,3
ADD IY,BC
OR A
JR Z,END_ ;LENGTH=0, EXIT
LD HL,(TRACEN)
LD A,H
OR L
RET Z
LD D,(IY-1) ;DE = LINE NUMBER
LD E,(IY-2)
SBC HL,DE
RET C
EX DE,HL
LD A,'[' ;TRACE
CALL OUTCHR
CALL PBCDL
LD A,']'
CALL OUTCHR
LD A,' '
JP OUTCHR
;
;ROUTINES FOR EACH STATEMENT:
;
;OSCLI
;
CLI: CALL EXPRS
LD A,CR
LD (DE),A
LD HL,ACCS
CALL OSCLI
JR XEQ
;
;REM, *
;
EXT: PUSH IY
POP HL
CALL OSCLI
REM: PUSH IY
POP HL
LD A,CR
LD B,A
CPIR ;FIND LINE END
PUSH HL
POP IY
JP XEQ0
;
;[LET] var = expr
;
LET0: CP ELSE_-TCMD
JR Z,REM
CP ('*'-TCMD) & 0FFH
JR Z,EXT
CP ('='-TCMD) & 0FFH
JR Z,FNEND
CP ('['-TCMD) & 0FFH
JR Z,ASM
DEC IY
LET: CALL ASSIGN
JP Z,XEQ
JR C,SYNTAX ;"Syntax error"
PUSH AF ;SAVE STRING TYPE
CALL EQUALS
PUSH HL
CALL EXPRS
POP IX
POP AF
CALL STACCS
XEQR: JP XEQ
;
ASM0: CALL NEWLIN
ASM: LD (ERRLIN),IY
CALL TRAP
CALL ASSEM
JR C,SYNTAX
CP CR
JR Z,ASM0
LD HL,LISTON
LD A,(HL)
AND 0FH
OR 30H
LD (HL),A
JR XEQR
;
VAR_: CALL GETVAR
RET Z
JP NC,PUTVAR
SYNTAX: LD A,16 ;"Syntax error"
DB 21H
ESCAPE: LD A,17 ;"Escape"
ERROR0: JP ERROR_
;
;=
;
FNEND: CALL EXPR ;FUNCTION RESULT
LD B,E
EX DE,HL
EXX ;SAVE RESULT
EX DE,HL ; IN DEB'C'D'E'
FNEND5: POP BC
LD HL,LOCCHK
OR A
SBC HL,BC
JR Z,FNEND0 ;LOCAL VARIABLE
LD HL,FNCHK
OR A
SBC HL,BC
LD A,7
JR NZ,ERROR0 ;"No FN"
POP IY
LD (ERRLIN),IY ;IN CASE OF ERROR
EX DE,HL
EXX
EX DE,HL
LD DE,ACCS
LD E,B
EX AF,AF'
RET
;
FNEND0: POP IX
POP BC
LD A,B
OR A
JP M,FNEND1 ;STRING
POP HL
EXX
POP HL
EXX
CALL STORE
JR FNEND5
FNEND1: LD HL,0
ADD HL,SP
PUSH DE
LD E,C
CALL STORES
POP DE
LD SP,HL
JR FNEND5
;
;DIM var(dim1[,dim2[,...]])[,var(...]
;DIM var expr[,var expr...]
;
DIM: CALL GETVAR ;VARIABLE
JR C,BADDIM
JP Z,DIM4
CALL CREATE
PUSH HL
POP IX
LD A,(IY)
CP '('
LD A,D
JR NZ,DIM4
PUSH HL
PUSH AF ;SAVE TYPE
LD DE,1
LD B,D ;DIMENSION COUNTER
DIM1: INC IY
PUSH BC
PUSH DE
PUSH IX
CALL EXPRI ;DIMENSION SIZE
BIT 7,H
JR NZ,BADDIM
EXX
INC HL
POP IX
INC IX
LD (IX),L ;SAVE SIZE
INC IX
LD (IX),H
POP BC
CALL MUL16 ;HL=HL*BC
JR C,NOROOM ;TOO LARGE
EX DE,HL ;DE=PRODUCT
POP BC
INC B ;DIMENSION COUNTER
LD A,(IY)
CP ',' ;ANOTHER
JR Z,DIM1
CALL BRAKET ;CLOSING BRACKET
POP AF ;RESTORE TYPE
INC IX
EX (SP),IX
LD (IX),B ;NO. OF DIMENSIONS
CALL X4OR5 ;DE=DE*n
POP HL
JR C,NOROOM
DIM3: ADD HL,DE
JR C,NOROOM
PUSH HL
INC H
JR Z,NOROOM
SBC HL,SP
JR NC,NOROOM ;OUT OF SPACE
POP HL
LD (FREE),HL
DIM2: LD A,D
OR E
JR Z,DIM5
DEC HL
LD (HL),0 ;INITIALISE ARRAY
DEC DE
JR DIM2
DIM5: CALL NXT
CP ',' ;ANOTHER VARIABLE?
JP NZ,XEQ
INC IY
CALL NXT
JR DIM
;
BADDIM: LD A,10 ;"Bad DIM"
DB 21H
NOROOM: LD A,11 ;"DIM space"
ERROR1: JP ERROR_
;
DIM4: OR A
JR Z,BADDIM
JP M,BADDIM
LD B,A
LD A,(IY-1)
CP ')'
LD A,B
JR Z,BADDIM
LD HL,(FREE)
EXX
LD HL,0
LD C,H
CALL STORE ;RESERVED AREA
CALL EXPRI
EXX
INC HL
EX DE,HL
LD HL,(FREE)
JR DIM3
;
;PRINT list...
;PRINT #channel,list...
;
PRINT_: CP '#'
JR NZ,PRINT0
CALL CHNL ;CHANNEL NO. = E
PRNTN1: CALL NXT
CP ','
JP NZ,XEQ
INC IY
PUSH DE
CALL EXPR ;ITEM TO PRINT
EX AF,AF'
JP M,PRNTN2 ;STRING
POP DE
PUSH BC
EXX
LD A,L
EXX
CALL OSBPUT
EXX
LD A,H
EXX
CALL OSBPUT
LD A,L
CALL OSBPUT
LD A,H
CALL OSBPUT
POP BC
LD A,C
CALL OSBPUT
JR PRNTN1
PRNTN2: LD C,E
POP DE
LD HL,ACCS
INC C
PRNTN3: DEC C
JR Z,PRNTN4
LD A,(HL)
INC HL
PUSH BC
CALL OSBPUT
POP BC
JR PRNTN3
PRNTN4: LD A,CR
CALL OSBPUT
JR PRNTN1
;
PRINT6: LD B,2
JR PRINTC
PRINT8: LD BC,100H
JR PRINTC
PRINT9: LD HL,STAVAR
XOR A
CP (HL)
JR Z,PRINT0
LD A,(COUNT)
OR A
JR Z,PRINT0
PRINTA: SUB (HL)
JR Z,PRINT0
JR NC,PRINTA
NEG
CALL FILL
PRINT0: LD A,(STAVAR)
LD C,A ;PRINTS
LD B,0 ;PRINTF
PRINTC: CALL TERMQ
JR Z,PRINT4
RES 0,B
INC IY
CP '~'
JR Z,PRINT6
CP ';'
JR Z,PRINT8
CP ','
JR Z,PRINT9
CALL FORMAT ;SPC, TAB, '
JR Z,PRINTC
DEC IY
PUSH BC
CALL EXPR ;VARIABLE TYPE
EX AF,AF'
JP M,PRINT3 ;STRING
POP DE
PUSH DE
BIT 1,D
PUSH AF
CALL Z,STR ;DECIMAL
POP AF
CALL NZ,HEXSTR ;HEX
POP BC
PUSH BC
LD A,C
SUB E
CALL NC,FILL ;RIGHT JUSTIFY
PRINT3: POP BC
CALL PTEXT ;PRINT
JR PRINTC
PRINT4: BIT 0,B
CALL Z,CRLF
JP XEQ
;
;
ONERR: INC IY ;SKIP "ERROR"
LD HL,0
LD (ERRTRP),HL
CALL NXT
CP OFF_
INC IY
JP Z,XEQ
DEC IY
LD (ERRTRP),IY
JP REM
;
;ON expr GOTO line[,line...] [ELSE statement]
;ON expr GOTO line[,line...] [ELSE line]
;ON expr GOSUB line[,line...] [ELSE statement]
;ON expr GOSUB line[,line...] [ELSE line]
;ON expr PROCone [,PROCtwo..] [ELSE PROCotherwise]
;ON ERROR statement [:statement...]
;ON ERROR OFF
;
ON_: CP TERROR
JR Z,ONERR ;"ON ERROR"
CALL EXPRI
LD A,(IY)
INC IY
LD E,',' ;SEPARATOR
CP TGOTO
JR Z,ON1
CP TGOSUB
JR Z,ON1
LD E,TPROC
CP E
LD A,39
JR NZ,ERROR2 ;"ON syntax"
ON1: LD D,A
EXX
PUSH HL
EXX
POP BC ;ON INDEX
LD A,B
OR H
OR L
JR NZ,ON4 ;OUT OF RANGE
OR C
JR Z,ON4
DEC C
JR Z,ON3 ;INDEX=1
ON2: CALL TERMQ
JR Z,ON4 ;OUT OF RANGE
INC IY ;SKIP DELIMITER
CP E
JR NZ,ON2
DEC C
JR NZ,ON2
ON3: LD A,E
CP TPROC
JR Z,ONPROC
PUSH DE
CALL ITEMI ;LINE NUMBER
POP DE
LD A,D
CP TGOTO
JR Z,GOTO2
CALL SPAN ;SKIP REST OF LIST
JR GOSUB1
;
ON4: LD A,(IY)
INC IY
CP ELSE_
JP Z,IF1 ;ELSE CLAUSE
CP CR
JR NZ,ON4
LD A,40
ERROR2: JP ERROR_ ;"ON range"
;
ONPROC: LD A,TON
JP PROC
;
;GOTO line
;
GOTO: CALL ITEMI ;LINE NUMBER
GOTO1: CALL TERMQ
JP NZ,SYNTAX
GOTO2: EXX
CALL FINDL
PUSH HL
POP IY
JP Z,XEQ0
LD A,41
JR ERROR2 ;"No such line"
;
;GOSUB line
;
GOSUB: CALL ITEMI ;LINE NUMBER
GOSUB1: PUSH IY ;TEXT POINTER
CALL CHECK ;CHECK ROOM
CALL GOTO1 ;SAVE MARKER
GOSCHK: EQU $
;
;RETURN
;
RETURN: POP DE ;MARKER
LD HL,GOSCHK
OR A
SBC HL,DE
POP IY
JP Z,XEQ
LD A,38
JR ERROR2 ;"No GOSUB"
;
;REPEAT
;
REPEAT: PUSH IY
CALL CHECK
CALL XEQ
REPCHK: EQU $
;
;UNTIL expr
;
UNTIL: POP BC
PUSH BC
LD HL,REPCHK
OR A
SBC HL,BC
LD A,43
JR NZ,ERROR2 ;"No REPEAT"
CALL EXPRI
CALL TEST
POP BC
POP DE
JR NZ,XEQ2 ;TRUE
PUSH DE
PUSH BC
PUSH DE
POP IY
XEQ2: JP XEQ
;
;FOR var = expr TO expr [STEP expr]
;
FORVAR: LD A,34
JR ERROR2 ;"FOR variable"
;
FOR: CALL ASSIGN
JR NZ,FORVAR ;"FOR variable"
PUSH AF ;SAVE TYPE
LD A,(IY)
CP TO
LD A,36
JR NZ,ERROR2 ;"No TO"
INC IY
PUSH IX
CALL EXPRN ;LIMIT
POP IX
POP AF
LD B,A ;TYPE
PUSH BC ;SAVE ON STACK
PUSH HL
LD HL,0
LD C,H
EXX
PUSH HL
LD HL,1 ;PRESET STEP
EXX
LD A,(IY)
CP STEP
JR NZ,FOR1
INC IY
PUSH IX
CALL EXPRN ;STEP
POP IX
FOR1: PUSH BC
PUSH HL
EXX
PUSH HL
EXX
PUSH IY ;SAVE TEXT POINTER
PUSH IX ;LOOP VARIABLE
CALL CHECK
CALL XEQ
FORCHK: EQU $
;
;NEXT [var[,var...]]
;
NEXT: POP BC ;MARKER
LD HL,FORCHK
OR A
SBC HL,BC
LD A,32
JR NZ,ERROR3 ;"No FOR"
CALL TERMQ
POP HL
PUSH HL
PUSH BC
PUSH HL
CALL NZ,GETVAR ;VARIABLE
POP DE
EX DE,HL
OR A
NEXT0: SBC HL,DE
JR NZ,NEXT1
PUSH DE
LD IX,6+2
ADD IX,SP
CALL DLOAD5 ;STEP
LD A,(IX+11) ;TYPE
POP IX
CALL LOADN ;LOOP VARIABLE
BIT 7,D ;SIGN?
PUSH AF
LD A,'+' & 0FH
CALL FPP ;ADD STEP
JR C,ERROR3
POP AF ;RESTORE TYPE
PUSH AF
CALL STORE ;UPDATE VARIABLE
LD IX,12+2
ADD IX,SP
CALL DLOAD5 ;LIMIT
POP AF
CALL Z,SWAP
LD A,0+('<'-4) & 0FH
CALL FPP ;TEST AGAINST LIMIT
JR C,ERROR3
INC H
JR NZ,LOOP_ ;KEEP LOOPING
LD HL,18
ADD HL,SP
LD SP,HL
CALL NXT
CP ','
JP NZ,XEQ
INC IY
JR NEXT
;
LOOP_: POP BC
POP DE
POP IY
PUSH IY
PUSH DE
PUSH BC
JP XEQ
;
NEXT1: LD HL,18
ADD HL,SP
LD SP,HL ;"POP" THE STACK
POP BC
LD HL,FORCHK
SBC HL,BC
POP HL ;VARIABLE POINTER
PUSH HL
PUSH BC
JR Z,NEXT0
LD A,33
ERROR3: JP ERROR_ ;"Can't match FOR"
;
;FNname
;N.B. ENTERED WITH A <> TON
;
FN: PUSH AF ;MAKE SPACE ON STACK
CALL PROC1
FNCHK: EQU $
;
;PROCname
;N.B. ENTERED WITH A = ON PROC FLAG
;
PROC: PUSH AF ;MAKE SPACE ON STACK
CALL PROC1
PROCHK: EQU $
PROC1: CALL CHECK
DEC IY
PUSH IY
CALL GETDEF
POP BC
JR Z,PROC4
LD A,30
JR C,ERROR3 ;"Bad call"
PUSH BC
LD HL,(PAGE_)
PROC2: LD A,DEF_
CALL SEARCH ;LOOK FOR "DEF"
JR C,PROC3
PUSH HL
POP IY
INC IY ;SKIP DEF
CALL NXT
CALL GETDEF
PUSH IY
POP DE
JR C,PROC6
CALL NZ,CREATE
PUSH IY
POP DE
LD (HL),E
INC HL
LD (HL),D ;SAVE ADDRESS
PROC6: EX DE,HL
LD A,CR
LD B,A
CPIR ;SKIP TO END OF LINE
JR PROC2
PROC3: POP IY ;RESTORE TEXT POINTER
CALL GETDEF
LD A,29
JR NZ,ERROR3 ;"No such FN/PROC"
PROC4: LD E,(HL)
INC HL
LD D,(HL) ;GET ADDRESS
LD HL,2
ADD HL,SP
CALL NXT ;ALLOW SPACE BEFORE (
PUSH DE ;EXCHANGE DE,IY
EX (SP),IY
POP DE
CP '(' ;ARGUMENTS?
JR NZ,PROC5
CALL NXT ;ALLOW SPACE BEFORE (
CP '('
JP NZ,SYNTAX ;"Syntax error"
PUSH IY
POP BC ;SAVE IY IN BC
EXX
CALL SAVLOC ;SAVE DUMMY VARIABLES
CALL BRAKET ;CLOSING BRACKET
EXX
PUSH BC
POP IY ;RESTORE IY
PUSH HL
CALL ARGUE ;TRANSFER ARGUMENTS
POP HL
PROC5: LD (HL),E ;SAVE "RETURN ADDRESS"
INC HL
LD A,(HL)
LD (HL),D
CP TON ;WAS IT "ON PROC" ?
JP NZ,XEQ
PUSH DE
EX (SP),IY
CALL SPAN ;SKIP REST OF ON LIST
EX (SP),IY
POP DE
LD (HL),D
DEC HL
LD (HL),E
JP XEQ
;
;LOCAL var[,var...]
;
LOCAL_: POP BC
PUSH BC
LD HL,FNCHK
OR A
SBC HL,BC
JR Z,LOCAL1
LD HL,PROCHK
OR A
SBC HL,BC
JR Z,LOCAL1
LD HL,LOCCHK
OR A
SBC HL,BC
LD A,12
JP NZ,ERROR_ ;"Not LOCAL"
LOCAL1: PUSH IY
POP BC
EXX
DEC IY
CALL SAVLOC
EXX
PUSH BC
POP IY
LOCAL2: CALL GETVAR
JP NZ,SYNTAX
OR A ;TYPE
EX AF,AF'
CALL ZERO
EX AF,AF'
PUSH AF
CALL P,STORE ;ZERO
POP AF
LD E,C
CALL M,STORES
CALL NXT
CP ','
JP NZ,XEQ
INC IY