-
Notifications
You must be signed in to change notification settings - Fork 0
/
TinyBasic.asm
5065 lines (4840 loc) · 87.6 KB
/
TinyBasic.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
;;
; Copyright Jacques Deschênes 2019,2022,2023
; This file is part of stm8_tbi
;
; stm8_tbi is free software: you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version.
;
; stm8_tbi is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with stm8_tbi. If not, see <http://www.gnu.org/licenses/>.
;;
;--------------------------------------
; Implementation of Tiny BASIC
; REF: https://en.wikipedia.org/wiki/Li-Chen_Wang#Palo_Alto_Tiny_BASIC
; Palo Alto BASIC is 4th version of TinyBasic
; DATE: 2019-12-17
;
;--------------------------------------------------
; implementation information
;
; * integer are 24 bits in registers they are
; kept as A:X (A being most signifant byte)
;
; * register Y is used as basicptr
;
; IMPORTANT: when a routine use Y it must preserve
; its content and restore it at exit.
; This hold only when BASIC is running
;
; * BASIC function return their value registers
; A for character
; X for address
; A:X for integer
;
; * variables return their value in A:X
;
;---------------------------------------------------
SEPARATE=0
.if SEPARATE
.module TINY_BASIC
.include "config.inc"
.area CODE
.endif
;-------------------------------------
; retrun string length
; input:
; X .asciz pointer
; output:
; X not affected
; A length
;-------------------------------------
strlen::
pushw x
clr a
1$: tnz (x)
jreq 9$
inc a
incw x
jra 1$
9$: popw x
ret
;------------------------------------
; compare 2 strings
; input:
; X char* first string
; Y char* second string
; output:
; Z flag 0 != | 1 ==
;-------------------------------------
strcmp:
ld a,(x)
jreq 5$
cp a,(y)
jrne 9$
incw x
incw y
jra strcmp
5$: ; end of first string
cp a,(y)
9$: ret
;---------------------------------------
; copy src string to dest
; input:
; X dest
; Y src
; output:
; X dest
;----------------------------------
strcpy::
push a
pushw x
1$: ld a,(y)
jreq 9$
ld (x),a
incw x
incw y
jra 1$
9$: clr (x)
popw x
pop a
ret
;---------------------------------------
; move memory block
; input:
; X destination
; Y source
; acc16 bytes count
; output:
; none
;--------------------------------------
INCR=1 ; incrament high byte
LB=2 ; increment low byte
VSIZE=2
move::
push a
_vars VSIZE
clr (INCR,sp)
clr (LB,sp)
pushw y
cpw x,(1,sp) ; compare DEST to SRC
popw y
jreq move_exit ; x==y
jrmi move_down
move_up: ; start from top address with incr=-1
addw x,acc16
addw y,acc16
cpl (INCR,sp)
cpl (LB,sp) ; increment = -1
jra move_loop
move_down: ; start from bottom address with incr=1
decw x
decw y
inc (LB,sp) ; incr=1
move_loop:
_ldaz acc16
or a, acc8
jreq move_exit
addw x,(INCR,sp)
addw y,(INCR,sp)
ld a,(y)
ld (x),a
pushw x
_ldxz acc16
decw x
ldw acc16,x
popw x
jra move_loop
move_exit:
_drop VSIZE
pop a
ret
;-----------------------
; display system
; information
;-----------------------
MAJOR=5
MINOR=0
REV=8
software: .asciz "\n\nTiny BASIC for STM8\nCopyright, Jacques Deschenes 2019,2022,2023\nversion "
board:
.if NUCLEO_8S208RB
.asciz "NUCLEO-8S208RB"
.else
.asciz "NUCLEO-8S207K8"
.endif
system_information:
push base
ldw x,#software
call puts
mov base, #10
ld a,#MAJOR
callr prt_i8
ld a,#'.
call putc
ld a,#MINOR
callr prt_i8
ld a,#'R
call putc
ld a,#REV
callr prt_i8
ld a,#CR
call putc
ldw x,#board
call puts
;call test
pop base
ret
;------------------------
; print int8
; input:
; A int8
; output:
; none
;-----------------------
prt_i8:
clrw x
rlwa x
call prt_i16
ret
warm_init:
ldw x,#uart_putc
ldw out,x ; standard output
_clrz flags
mov base,#10
ldw x,#free_ram
ldw x,#tib
ldw end_free_ram,x
subw x,dvar_end
ld a,#INT_SIZE
div x,a
_strxz array_size
ret
;-------------------------
; erase app_sign
;-------------------------
clear_autorun:
ld a,#'X
clr farptr
ldw x,#app_sign
_strxz ptr16
clrw x
call write_byte
call write_byte
ret
;------------------------------------
; set all variables to zero
; input:
; none
; output:free_ram
; none
;------------------------------------
clear_vars:
pushw x
push a
ldw x,#vars
ld a,#CELL_SIZE*26
1$: clr (x)
incw x
dec a
jrne 1$
pop a
popw x
ret
;---------------------------
; reset BASIC text variables
; and clear variables
;---------------------------
clear_basic:
pushw x
ldw x,#free_ram
ldw txtbgn,x
ldw txtend,x
ldw dvar_bgn,x
ldw dvar_end,x
call clear_vars
_clrz chain_level
popw x
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Tiny BASIC error messages
;; addresses indexed table
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; macro to create err_msg table
.macro _err_entry msg_addr, error_code
.word msg_addr
error_code==ERR_IDX
ERR_IDX=ERR_IDX+1
.endm
ERR_IDX=0
; array of pointers to
; error_messages strings table.
err_msg_idx:
_err_entry 0,ERR_NONE
_err_entry err_mem_full,ERR_MEM_FULL
_err_entry err_syntax,ERR_SYNTAX
_err_entry err_math_ovf,ERR_MATH_OVF
_err_entry err_div0,ERR_DIV0
_err_entry err_no_line,ERR_NO_LINE
_err_entry err_run_only,ERR_RUN_ONLY
_err_entry err_cmd_only,ERR_CMD_ONLY
_err_entry err_duplicate,ERR_DUPLICATE
_err_entry err_not_file,ERR_NOT_FILE
_err_entry err_bad_value,ERR_BAD_VALUE
_err_entry err_no_access,ERR_NO_ACCESS
_err_entry err_no_data,ERR_NO_DATA
_err_entry err_no_prog,ERR_NO_PROG
_err_entry err_no_fspace,ERR_NO_FSPACE
_err_entry err_buf_full,ERR_BUF_FULL
_err_entry err_read_only,ERR_READ_ONLY
_err_entry err_not_program,ERR_NOT_PROGRAM
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; error messages strngs table
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
error_messages:
err_mem_full: .asciz "Rejected, memory full\n"
err_syntax: .asciz "syntax error\n"
err_math_ovf: .asciz "math operation overflow\n"
err_div0: .asciz "division by 0\n"
err_no_line: .asciz "invalid line number.\n"
err_run_only: .asciz "run time only usage.\n"
err_cmd_only: .asciz "command line only usage.\n"
err_duplicate: .asciz "duplicate name.\n"
err_not_file: .asciz "File not found.\n"
err_bad_value: .asciz "bad value.\n"
err_no_access: .asciz "File in extended memory, can't be run from there.\n"
err_no_data: .asciz "No data found.\n"
err_no_prog: .asciz "No program in RAM!\n"
err_no_fspace: .asciz "File system full.\n"
err_buf_full: .asciz "Buffer full\n"
err_read_only: .asciz "constant can't be modified\n"
err_not_program: .asciz "no program at this address\n"
;-------------------------------------
rt_msg: .asciz "\nrun time error, "
comp_msg: .asciz "\ncompile error, "
syntax_error::
ld a,#ERR_SYNTAX
tb_error::
btjt flags,#FCOMP,1$
push a
.if 0 ; DEBUG
call dump_prog
.endif
ldw x, #rt_msg
call puts
0$: pop a
callr print_err_msg
subw y, line.addr
ld a,yl
sub a,#LINE_HEADER_SIZE
_ldxz line.addr
call prt_basic_line
jra 6$
1$:
push a
ldw x,#comp_msg
call puts
pop a
callr print_err_msg
ldw x,#tib
call puts
ld a,#CR
call putc
_ldxz in.w
call spaces
ld a,#'^
call putc
6$: ldw x,#STACK_EMPTY
ldw sp,x
jra warm_start
;------------------------
; print error message
; input:
; A error code
; output:
; none
;------------------------
print_err_msg:
clrw x
ld xl,a
sllw x
addw x,#err_msg_idx
ldw x,(x)
call puts
ret
warm_start:
call warm_init
;----------------------------
; BASIC interpreter
;----------------------------
cmd_line: ; user interface
ld a,#CR
call putc
ld a,#'>
call putc
call readln
tnz count
jreq cmd_line
call compile
tnz a
jreq cmd_line ; not direct command
;; direct command
;; interpret
;;;;;;;;;;;;;;;;;;;;;;
;; dump compiled BASIC
.if 0 ; DEBUG
ldw x,#pad
_clrz farptr
ldw ptr16,x
clrw x
_ldaz count
ld xl,a
call hex_dump
.endif
;;;;;;;;;;;;;;;;;;;;;;
; jra interp_loop
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; This is the interpreter loop
;; for each BASIC code line.
;; 12 cycles
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
do_nothing:
interp_loop:
_next_cmd ; command bytecode, 2 cy
.if 0; DEBUG
_tp 'I
_jp_code
.else
_jp_code ; 8 cy + 2 cy for jump back to interp_loop
.endif
;---------------------
; BASIC: REM | '
; skip comment to end of line
;----------------------
kword_remark::
ldw y,line.addr
ld a,(2,y) ; line length
_straz in
addw y,in.w
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; move basicptr to first token
; of next line
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
next_line:
btjf flags,#FRUN,0$
cpw y,txtend
jrmi 1$
0$:
jp kword_end
1$:
_stryz line.addr
addw y,#LINE_HEADER_SIZE
btjf flags,#FTRACE,2$
call prt_line_no
2$:
_next
;------------------------
; when TRACE is active
; print line number to
; be executed by VM
;------------------------
prt_line_no:
ldw x,[line.addr]
call prt_i16
ld a,#CR
call putc
ret
;-------------------------
; skip .asciz in BASIC line
; name
; input:
; x * string
; output:
; none
;-------------------------
skip_string:
ld a,(y)
jreq 8$
1$: incw y
ld a,(y)
jrne 1$
8$: incw y
ret
;--------------------
; skip label name
;--------------------
skip_label:
ld a,(y) ; label length
add a,#2 ; skip count and 0 terminal
push a
push #0
addw y,(1,sp)
_drop 2
ret
;-------------------
; needed for new
; model using
; _jp_code instead
; of _call_code
;--------------------
jump_label:
ldw x,y
call skip_label
ld a,(y)
cp a,#REL_EQU_IDX
jrne 9$
ldw y,x
jp do_dvar
9$:
_next
;--------------------
; extract int24_t
; value from BASIC
; code
; input:
; Y *integer
; output:
; A:X int24
;--------------------
get_int24:
ld a,(2,y) ; 1 cy
ld xl,a ; 1 cy
ld a,(1,y) ; 1 cy
ld xh,a ; 1 cy
ld a,(y) ; 1 cy
addw y,#INT_SIZE ; 2 cy
ret ; 4cy = 11cy
;-------------------------------
; called when an intger token
; is expected. can be LIT_IDX
; or LITW_IDX
; program fail if not integer
;------------------------------
expect_integer:
_next_token
cp a,#LIT_IDX
jreq get_int24
cp a,#LITW_IDX
jreq 0$
jp syntax_error
0$: _get_word
ret
;-----------------------------------
; print a 16 bit integer
; using variable 'base' as conversion
; format.
; input:
; X integer to print
; 'base' conversion base
; output:
; terminal
;-----------------------------------
prt_i16:
_clrz acc24
ldw acc16,x
ld a,#16
cp a,base
jreq prt_acc24
btjf acc16,#7,prt_acc24
cpl acc24 ; sign extend
;------------------------------------
; print integer in acc24
; input:
; acc24 integer to print
; 'base' numerical base for conversion
; A signed||unsigned conversion
; output:
; A string length
;------------------------------------
prt_acc24:
ld a,#255 ; signed conversion
call itoa ; conversion entier en .asciz
push a
call puts
pop a
ret
;------------------------------------
; convert integer in acc24 to string
; input:
; 'base' conversion base
; acc24 integer to convert
; A 0=unsigned, else signed
; output:
; X pointer to first char of string
; A string length
;------------------------------------
SIGN=1 ; 1 byte, integer sign
LEN=2 ; 1 byte, string length
VSIZE=2 ;locals size
itoa::
_vars VSIZE
clr (LEN,sp) ; string length
clr (SIGN,sp) ; sign
tnz A
jreq 1$ ; unsigned conversion
_ldaz base
cp a,#10
jrne 1$
; base 10 string display with negative sign if bit 23==1
btjf acc24,#7,1$
cpl (SIGN,sp)
call neg_acc24
1$:
; initialize string pointer
; build string at end of pad
ldw x,#pad
addw x,#PAD_SIZE
decw x
clr (x)
itoa_loop:
_ldaz base
call divu24_8 ; acc24/A
add a,#'0 ; remainder of division
cp a,#'9+1
jrmi 2$
add a,#7
2$:
decw x
ld (x),a
inc (LEN,sp)
; if acc24==0 conversion done
_ldaz acc24
or a,acc16
or a,acc8
jrne itoa_loop
;conversion done, next add '$' or '-' as required
_ldaz base
cp a,#16
jreq 8$
ld a,(SIGN,sp)
jreq 10$
ld a,#'-
jra 9$
8$:
ld a,#'$
9$: decw x
ld (x),a
inc (LEN,sp)
10$:
ld a,(LEN,sp)
_drop VSIZE
ret
;------------------------------------
; convert alpha to uppercase
; input:
; a character to convert
; output:
; a uppercase character
;------------------------------------
to_upper::
cp a,#'a
jrpl 1$
0$: ret
1$: cp a,#'z
jrugt 0$
sub a,#32
ret
;------------------------------------
; convert pad content in integer
; input:
; x * .asciz to convert
; output:
; A:X int24_t
; acc24 int24_t
;------------------------------------
; local variables
N=1 ; 3 bytes
SIGN=N+INT_SIZE ; 1 byte,
BASE=SIGN+1 ; 1 byte, numeric base used in conversion
DIGIT=BASE+1 ; 1 byte, temporary storage
PSTR=DIGIT+1 ; 2 bytes, preserve X
VSIZE=8 ; bytes reserved for local storage
atoi24::
_vars VSIZE
ldw (PSTR,sp),x ; pointer to string
; conversion made on stack
clr a
clrw x
_i24_store N
clr (SIGN,sp)
ld a,#10
ld (BASE,sp),a ; default base decimal
ldw x,(PSTR,sp)
ld a,(x)
jreq 9$ ; completed if 0
cp a,#'-
jrne 1$
cpl (SIGN,sp)
jra 2$
1$:
cp a,#'$
jrne 3$
ld a,#16
ld (BASE,sp),a
2$: incw x
ldw (PSTR,sp),x
ld a,(x)
3$: ; char to digit
call to_upper
sub a,#'0
jrmi 9$
cp a,#10
jrmi 5$
cp a,#17
jrmi 9$
sub a,#7
cp a,(BASE,sp)
jrpl 9$
5$: ld (DIGIT,sp),a
ld a,(BASE,sp)
call mulu24_8
rrwa x
add a,(DIGIT,SP)
jrnc 6$
incw x
6$: rlwa x
_i24_store N
ldw x,(PSTR,sp)
jra 2$
9$: tnz (SIGN,sp)
jreq 10$
call neg24
10$:
_i24_fetch N
ldw y,(PSTR,sp)
_drop VSIZE
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; TINY BASIC operators,
;; commands and functions
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;---------------------------------
; dictionary search
; input:
; X dictionary entry point, name field
; y .asciz name to search
; output:
; A TOKEN_IDX
;---------------------------------
NLEN=1 ; cmd length
XSAVE=NLEN+2
YSAVE=XSAVE+2
VSIZE=YSAVE+1
search_dict::
_vars VSIZE
clr (NLEN,sp)
ldw (YSAVE,sp),y
search_next:
ldw (XSAVE,sp),x
; get name length in dictionary
ld a,(x)
ld (NLEN+1,sp),a
ldw y,(YSAVE,sp) ; name pointer
incw x
call strcmp
jreq str_match
no_match:
ldw x,(XSAVE,sp)
subw x,#2 ; move X to link field
ld a,#NONE_IDX
ldw x,(x) ; next word link
jreq search_exit ; not found
;try next
jra search_next
str_match:
ldw x,(XSAVE,sp)
addw x,(NLEN,sp)
; move x to token field
addw x,#2 ; to skip length byte and 0 at end
ld a,(x) ; token index
search_exit:
_drop VSIZE
ret
;--------------------------------
; called by command that should
; be invoked only from command
; line.
; Display an error if
; invoked from program.
;---------------------------------
cmd_line_only:
btjf flags,#FRUN,0$
ld a,#ERR_CMD_ONLY
jp tb_error
0$: ret
;---------------------
; check if next token
; is of expected type
; input:
; A expected token attribute
; ouput:
; none if fail call syntax_error
;--------------------
expect:
push a
_next_token
cp a,(1,sp)
jreq 1$
jp syntax_error
1$: pop a
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;
; parse arguments list
; between ()
;;;;;;;;;;;;;;;;;;;;;;;;;;
func_args:
_next_token
cp a,#LPAREN_IDX
jreq arg_list
jp syntax_error
; expected to continue in arg_list
; caller must check for RPAREN_IDX
;-------------------------------
; parse embedded BASIC routines
; arguments list.
; arg_list::= expr[','expr]*
; all arguments are of int24_t type
; and pushed on stack
; input:
; none
; output:
; stack{n} arguments pushed on stack
; A number of arguments pushed on stack
;--------------------------------
ARGN=4
ARG_SIZE=INT_SIZE
arg_list:
push #0 ; arguments counter
1$:
pop a
popw x
sub sp, #ARG_SIZE
pushw x
inc a
push a
call expression
_i24_store ARGN
_next_token
cp a,#COMMA_IDX
jreq 1$
cp a,#RPAREN_IDX
jreq 7$
_unget_token
7$:
pop a
ret
;--------------------------------
; BASIC commnands
;--------------------------------
;--------------------------------
; arithmetic and relational
; routines
; operators precedence
; highest to lowest
; operators on same row have
; same precedence and are executed
; from left to right.
; '*','/','%'
; '-','+'
; '=','>','<','>=','<=','<>','><'
; '<>' and '><' are equivalent for not equal.
;--------------------------------
;---------------------
; return array element
; address from @(expr)
; input:
; A ARRAY_IDX
; output:
; X element address
;----------------------
get_array_element:
call func_args
cp a,#1
jreq 1$
jp syntax_error
1$: _i24_pop
; ignore A, index < 65536 in any case
; check for bounds
cpw x,array_size
jrule 3$
; bounds {1..array_size}
2$: ld a,#ERR_BAD_VALUE
jp tb_error
3$:
tnzw x
jreq 2$
ld a,#INT_SIZE
mul x,a
ldw acc16,x
ldw x,end_free_ram ; array start at this point
subw x,acc16
ret
;***********************************
; expression parse,execute
;***********************************
;-----------------------------------
; factor ::= ['+'|'-'|e] var | @ |
; integer | function |
; '('expression')'
; output:
; A:X factr
; ---------------------------------
NEG=1
VSIZE=1
factor:
_vars VSIZE
clr (NEG,sp)
_next_token
cp a,#CMD_END
jrugt 1$
jp syntax_error
1$:
cp a,#ADD_IDX
jreq 2$
cp a,#SUB_IDX
jrne 4$
cpl (NEG,sp)
2$:
_next_token
4$:
cp a,#LITW_IDX
jrne 5$
_get_word
jra 18$
5$:
cp a,#LIT_IDX
jrne 6$
call get_int24
jra 18$
6$:
cp a,#VAR_IDX
jrne 7$
_get_addr
jra 71$
7$:
cp a,#ARRAY_IDX
jrne 8$
call get_array_element
71$: ; put value in A:X
ld a,(x)
ldw x,(1,x)
jra 18$
8$:
cp a,#LABEL_IDX
jrne 9$
ldw x,y
call skip_label
ld a,(x)
incw x
call search_name
tnzw x
jrne 82$
jp syntax_error
82$:
call get_value ; in A:X
jra 18$
9$: