-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic.asm
5266 lines (3988 loc) · 87.7 KB
/
basic.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
; GOOD COMPUTER: BASIC interpreter
; Copyright (c) 2020 Rob Norris
; This Source Code Form is subject to the terms of the Mozilla Public
; License, v. 2.0. If a copy of the MPL was not distributed with this
; file, You can obtain one at https://mozilla.org/MPL/2.0/.
.include "m88def.inc"
; XXX I wonder if there's a better way to set up a memory map
; location in internal memory for current linemap page (high byte)
.equ linemap_buffer_h = high(0x100)
; same for current varmap page
.equ varmap_buffer_h = high(0x200)
; string buffer (temp space for strings being created)
.equ string_buffer = 0x0300
.equ string_buffer_end = 0x03af
; op buffer
.equ op_buffer = 0x03b0
; gosub stack
.equ gosub_stack = 0x03e0
.equ gosub_stack_end = 0x03ff
; for/next state
.equ for_buffer = 0x0400
.equ for_buffer_end = 0x041f
; input buffer
.equ input_buffer = 0x0420
.equ input_buffer_end = 0x049f
; expression stack
.equ expr_stack = 0x04a0
.equ expr_stack_end = 0x04af
; external ram tracking
.equ opmem_top_l = 0x04b0 ; top of opmem (position of next instruction)
.equ opmem_top_h = 0x04b1
.equ varmem_top_l = 0x04b2 ; top of varmem (position of next value)
.equ varmem_top_h = 0x04b3
; rng state
.equ rand_l = 0x04b4
.equ rand_h = 0x04b5
; stack top
.equ stack_top = 0x04ff
; location in external memory of op lists (bank 0)
; long list of length, ops, pointed at by the linemap
.equ opmem_base = 0x0000
; location in external memory of linemap (bank 0)
; map of line number (2 bytes) -> pointer to op buffer (2 bytes)
; only high byte gets considered, so must be on an even page boundary
.equ linemap_base = 0xf000
.equ linemap_top = 0x0000
; location in external memory of var values (bank 1)
; just the raw values, pointed at by the varmap
.equ varmem_base = 0x0000
; location in external memory of the var map (bank 1)
; array of variable name (1 byte), value length (1 byte), pointer to varmem (2 bytes)
; only high byte gets considered, so must be on an even page boundary
.equ varmap_base = 0xf000
.equ varmap_top = 0x0000
; global registers
.def r_error = r25 ; last error code
.def r_flags = r24 ; global state flags
.def r_gosub_sp = r15 ; low byte of top of gosub stack
; flag bits
.equ f_immediate = 0
.equ f_abort_line = 1
.equ f_jump = 2
.equ f_end = 3
; error codes
.equ error_number_out_of_range = 1
.equ error_expected_number = 2
.equ error_expected_variable = 3
.equ error_expected_statement = 4
.equ error_expected_comparator = 5
.equ error_expected_expression = 6
.equ error_expected_operand = 7
.equ error_expected_string = 8
.equ error_mismatched_parens = 9
.equ error_incorrect_arguments = 10
.equ error_unterminated_string = 11
.equ error_return_without_gosub = 12
.equ error_if_without_then = 13
.equ error_for_without_to = 14
.equ error_next_without_for = 15
.equ error_out_of_memory = 16
.equ error_overflow = 17
.equ error_no_such_line = 18
.equ error_type_mismatch = 19
.equ error_division_by_zero = 20
.equ error_invalid_immediate = 21
.equ error_invalid_program = 22
.equ error_transfer_error = 23
.equ error_break = 24
; expression ops
.equ expr_op_number = 1
.equ expr_op_string = 2
.equ expr_op_variable = 3
.equ expr_op_add_number = 4
.equ expr_op_add_string = 5
.equ expr_op_subtract = 6
.equ expr_op_multiply = 7
.equ expr_op_divide = 8
.equ expr_op_abs = 9
.equ expr_op_rnd = 10
.equ expr_op_left = 11
.equ expr_op_right = 12
.equ expr_op_mid = 13
.equ expr_op_len = 14
.equ expr_op_inkey = 15
.equ expr_op_LAST = 16
; expression type modifiers
.equ expr_op_type_mask = 0x40
.equ expr_op_type_number = 0x00
.equ expr_op_type_string = 0x40
; expression args modifiers
.equ expr_op_args_mask = 0x3
.equ expr_op_args_0 = 0x0
.equ expr_op_args_1 = 0x1
.equ expr_op_args_2 = 0x2
.equ expr_op_args_3 = 0x3
; expression arg type modifier
.equ expr_op_argtype_mask = 0x1c
.equ expr_op_argtype_1_string = 0x10
.equ expr_op_argtype_1_number = 0x0
.equ expr_op_argtype_2_string = 0x08
.equ expr_op_argtype_2_number = 0x0
.equ expr_op_argtype_3_string = 0x04
.equ expr_op_argtype_3_number = 0x0
.cseg
.org 0x0000
rjmp reset
reti
reti
reti
reti
reti
reti
reti
reti
reti
reti
reti
reti
reti
reti
reti
reti
reti
reti
reset:
; clear reset state and disable watchdog
cli
wdr
in r16, MCUSR
cbr r16, (1<<WDRF)
out MCUSR, r16
lds r16, WDTCSR
lds r16, WDTCSR
sbr r16, (1<<WDCE) | (1<<WDE)
sts WDTCSR, r16
cbr r16, (1<<WDE)
sts WDTCSR, r16
sei
; XXX DEBUG clear memory
ldi XL, low(SRAM_START)
ldi XH, high(SRAM_START)
ldi r16, low(RAMEND)
ldi r17, high(RAMEND)
ldi r18, 0x55
ldi r19, 0xaa
st X+, r18
st X+, r19
cp XL, r16
cpc XH, r17
brlo PC-4
; setup stack pointer
ldi r16, low(stack_top)
ldi r17, high(stack_top)
out SPL, r16
out SPH, r17
; usart tx/rx enable
ldi r16, (1<<RXEN0) | (1<<TXEN0)
sts UCSR0B, r16
; usart frame format: 8N1 (8 data bits => UCSZ2:0 = 011, no parity => UPM1:0 = 00, 1 stop bit => USBS = 0)
ldi r16, (1<<UCSZ00) | (1<<UCSZ01)
sts UCSR0C, r16
; usart 38400 baud at 16MHz => UBRR = 25
ldi r16, 25
ldi r17, 0
sts UBRR0L, r16
sts UBRR0H, r17
; output: PB0 = error LED, PB1 = user LED
; PB2 = SPI /SS (SRAM /CS), PB3 = SPI MOSI, PB5 = SPI SCK
; input: PB4 = SPI MISO
; don't care: PB7
ldi r16, (1<<PB0) | (1<<PB1) | (1<<PB2) | (1<<PB3) | (1<<PB5)
out DDRB, r16
; drive SPI /SS high to disable it
ldi r16, (1<<PB2)
out PORTB, r16
; enable SPI, master mode, clock rate fck/4 (4MHz)
ldi r16, (1<<SPE) | (1<<MSTR)
out SPCR, r16
; SPI clock double (8MHz)
ldi r16, (1<<SPI2X)
out SPSR, r16
; setup
rcall op_new
main:
; say hello
ldi ZL, low(text_newline*2)
ldi ZH, high(text_newline*2)
rcall usart_print_static
ldi ZL, low(text_banner*2)
ldi ZH, high(text_banner*2)
rcall usart_print_static
ldi ZL, low(text_newline*2)
ldi ZH, high(text_newline*2)
rcall usart_print_static
main_loop:
; clear last error
clr r_error
; show the prompt
ldi ZL, low(text_newline*2)
ldi ZH, high(text_newline*2)
rcall usart_print_static
ldi ZL, low(text_prompt*2)
ldi ZH, high(text_prompt*2)
rcall usart_print_static
; read a line
rcall usart_line_input
; and do stuff on it
rcall handle_line_input
; error check
tst r_error
breq main_loop
; show error text
clt ; immediate
rcall handle_error
rjmp main_loop
; output error info
; inputs:
; r_error: error code
; T: if set, include "IN LINE XXXXX"
; r16:r17: if T set, line number
handle_error:
; if T set, push r16:r17 for later
brtc PC+3
push r16
push r17
; all error text starts with '?', oldschool
ldi r16, '?'
rcall usart_tx_byte
; make zero-based, and multiple for program memory lookup
dec r_error
lsl r_error
; point Z to error table entry
ldi ZL, low(error_lookup_table*2)
ldi ZH, high(error_lookup_table*2)
add ZL, r_error
brcc PC+2
inc ZH
; done with error code, clear it
clr r_error
; load error text location from table
lpm XL, Z+
lpm XH, Z+
; print it
movw ZL, XL
rcall usart_print_static
; do line number
brtc error_newline
ldi ZL, low(text_at_line*2)
ldi ZH, high(text_at_line*2)
rcall usart_print_static
pop r17
pop r16
ldi YL, low(input_buffer)
ldi YH, high(input_buffer)
rcall format_number
; trailing null
clr r16
st Y+, r16
ldi ZL, low(input_buffer)
ldi ZH, high(input_buffer)
rcall usart_print
error_newline:
; and the newline
ldi ZL, low(text_newline*2)
ldi ZH, high(text_newline*2)
rjmp usart_print_static
error_lookup_table:
.dw text_error_number_out_of_range*2
.dw text_error_expected_number*2
.dw text_error_expected_variable*2
.dw text_error_expected_statement*2
.dw text_error_expected_comparator*2
.dw text_error_expected_expression*2
.dw text_error_expected_operand*2
.dw text_error_expected_string*2
.dw text_error_mismatched_parens*2
.dw text_error_incorrect_arguments*2
.dw text_error_unterminated_string*2
.dw text_error_return_without_gosub*2
.dw text_error_if_without_then*2
.dw text_error_for_without_to*2
.dw text_error_next_without_for*2
.dw text_error_out_of_memory*2
.dw text_error_overflow*2
.dw text_error_no_such_line*2
.dw text_error_type_mismatch*2
.dw text_error_division_by_zero*2
.dw text_error_invalid_immediate*2
.dw text_error_invalid_program*2
.dw text_error_transfer_error*2
.dw text_error_break*2
; move X forward until there's no whitespace under it
skip_whitespace:
ld r16, X
cpi r16, 0x20
breq PC+2
ret
adiw XL, 1
rjmp skip_whitespace
; consider the line buffer and do the right thing
handle_line_input:
; start of buffer
ldi XL, low(input_buffer)
ldi XH, high(input_buffer)
rcall skip_whitespace
; return if buffer is empty
ld r16, X
tst r16
brne PC+2
ret
; parse the line number
rcall parse_number
tst r_error
breq PC+2
ret
; negative?
tst r3
brpl PC+3
; no negative numbers for line numbers sorrt
ldi r_error, error_number_out_of_range
ret
; copy line number so we can safely trample it in statement parsing
movw r8, r2
; save T flag so we can test immediate mode later
bld r_flags, f_immediate
; parse the line back into the immediate buffer
ldi YL, low(op_buffer)
ldi YH, high(op_buffer)
rcall skip_whitespace
; end of buffer check. if its empty, then it has to be because we got a line
; number and nothing else, otherwise we'd hit the trap above. proceed
; immediately to "store"
ld r16, X
tst r16
breq find_instruction_location
; stuff! parse it
rcall parse_statement_list
; XXX skip remaining whitespace and look for end of buffer, error if not
; bail on parse error
tst r_error
breq PC+2
ret
brts PC+3
ldi r_error, error_expected_statement
ret
; if we have a line number, store it
sbrc r_flags, f_immediate
rjmp find_instruction_location
; no line number, this is immediate mode and we can just execute it
ldi XL, low(op_buffer)
ldi XH, high(op_buffer)
rjmp execute_statement
find_instruction_location:
; we have a line number, so we need to add it to the program
rcall op_clear
; Y currently pointing at end of op we just parsed. subtract start of buffer
; to find length
mov r23, YL
ldi r16, low(op_buffer)
sub r23, r16
; set to write opbuffer out to opmem
; XXX check if there's opmem room
lds r16, opmem_top_l
lds r17, opmem_top_h
movw XL, r16 ; save for further down
ldi r18, 0x0 ; bank 0
rcall ram_write_start
; write length
mov r16, r23
rcall ram_write_byte
; write buffer
ldi ZL, low(op_buffer)
ldi ZH, high(op_buffer)
rcall ram_write_bytes
rcall ram_end
; now we need to update the linemap with this line
; T flag indicates whether this is the last instruction in the program. if
; so, we will set the empty end-of-program instruction after we store the new
; instruction. setting it for the most common case of adding the line to the
; end of the program
set
; setup ref to current linemap page (high byte)
ldi r20, high(linemap_base)
; load the first page of the linemap
clr r16
mov r17, r20
ldi r18, 0x0 ; bank 0
rcall ram_read_start
linemap_load:
clr ZL
ldi ZH, linemap_buffer_h
clr r16
rcall ram_read_bytes
; holding ram active, so we can easily load in the next page
; reset to start of buffer
clr ZL
ldi ZH, linemap_buffer_h
; walk the linemap, looking for the right place to put this
linemap_next:
; load the stored line number
ld r16, Z+
ld r17, Z+
; if we've found line zero, then this is where we put it
tst r16
brne PC+3
tst r17
breq append_line
; compare the line number we're looking with the one we just parsed
cp r8, r16
cpc r9, r17
; if its the same number, we're replacing it
breq replace_line
; if its lower than us, then we belong here and need to push forward
brlo insert_line
; its higher than us, so we need to move along and try the next one
; advancing; skip the opmem pointer
adiw ZL, 2
; see if we've gone off the end of the page
tst ZL
brne linemap_next
; yep, load the next page
inc r20
rjmp linemap_load
insert_line:
; XXX open a gap, put this line in it
sbi PORTB, PB0
rjmp PC
replace_line:
; XXX consider length and remove line if we're replacing with nothing
; not changing the end marker, otherwise its the same as append
clt
append_line:
; finish read
rcall ram_end
; if we're at the last entry of the last linemap page, then we can't add any more
cpi r20, high(linemap_top-1)
brne PC+5
cpi ZL, 0xfe ; Z is two bytes into last entry
brne PC+3
ldi r_error, error_out_of_memory
ret
; move Z back to line number position
sbiw ZL, 2
; prep for ram write; low byte is from Z, high byte from current page
mov r16, ZL
mov r17, r20
ldi r18, 0x0 ; bank 0
rcall ram_write_start
; write line number
movw r16, r8
rcall ram_write_pair
; write opmem pointer
movw r16, XL
rcall ram_write_pair
; advance opmem top pointer past the #r23+1 bytes of opmem
adiw XL, 1
add XL, r23
brcc PC+2
inc XH
sts opmem_top_l, XL
sts opmem_top_h, XH
; if T is set, we just appended, and need to update the end marker
brts PC+2
rjmp ram_end
; write end-of-linemap marker
clr r16
clr r17
rcall ram_write_pair
; write done!
rjmp ram_end
; parse an ascii number
; inputs:
; X: pointer to ascii digit sequence, will be moved
; outputs:
; r2: low byte of number
; r3: high byte of number
; T: set if we actually parsed something
parse_number:
; store X in case we need to rewind
movw r6, XL
; clear result flag
clt
; accumulator
clr r2
clr r3
ldi r18, 10 ; for multiplying by 10 repeatedly
clr r19
; check first char for negation
ld r20, X
cpi r20, '-'
brne parse_number_loop
; skip it, leave it in r20 to test at the end
adiw XL, 1
parse_number_loop:
; get input char and check range
ld r17, X
cpi r17, 0x30
brlo PC+3
cpi r17, 0x3a
brlo digit_valid
; out of range, this is the end
; did we read anything?
brts PC+3
; no, just roll X back and abort
movw XL, r6
ret
; see if we need to negate
cpi r20, '-'
brne PC+5
; do so!
com r3
neg r2
ldi r17, 0xff
sbc r3, r17
ret
digit_valid:
; multiply accumulator low byte by 10
mul r2, r18
movw r4, r0
; multiply accumulator high byte by 10
mul r3, r18
add r5, r0
brcc PC+2
inc r1
cp r1, r19
brne parse_number_overflow
; actually read something, flag this
set
; take char
adiw XL, 1
; reload the accumulator after multiplying
movw r2, r4
; convert digit to value, and add
andi r17, 0xf
add r2, r17
brcc parse_number_loop
inc r3
brpl parse_number_loop
parse_number_overflow:
; overflowed, abort
ldi r_error, error_number_out_of_range
ret
parse_statement_list:
; count number of statements parse
clr r16
push r16
statement_next:
; start of keyword table
ldi ZL, low(keyword_table*2)
ldi ZH, high(keyword_table*2)
; try to parse one
rcall parse_token
brts statement_got
statement_last:
; no, set T if we got any at all
clt
pop r16
tst r16
breq PC+2
set
ret
statement_got:
; got the keyword, subparse
rcall skip_whitespace
; store the opcode
st Y+, r17
; set up for rest of statement parse
ldi ZL, low(keyword_subparser_table)
ldi ZH, high(keyword_subparser_table)
; add opcode to get the parser vector
add ZL, r17
brcc PC+2
inc ZH
; call keyword subparser
icall
; error check
tst r_error
breq PC+3
pop r16
ret
; bump the count
pop r16
inc r16
push r16
rcall skip_whitespace
; following byte
ld r16, X
; end of input, leave it there and get out of here
tst r16
brne PC+3
; store the zero as an end-of-statement marker
st Y+, r16
rjmp statement_last
; statement separator
cpi r16, ':'
breq PC+4
; something weird, bail
pop r16
ldi r_error, error_expected_statement
ret
; take it
adiw XL, 1
; store it
st Y+, r16
; go again
rcall skip_whitespace
rjmp statement_next
keyword_table:
.db "PRINT", 0x01, \
"IF", 0x02, \
"GOTO", 0x03, \
"INPUT", 0x04, \
"LET", 0x05, \
"GOSUB", 0x06, \
"RETURN", 0x07, \
"FOR", 0x08, \
"NEXT", 0x09, \
"NEW", 0x0a, \
"CLEAR", 0x0b, \
"LIST", 0x0c, \
"RUN", 0x0d, \
"END", 0x0e, \
\
"ON", 0x0f, \
"OFF", 0x10, \
"SLEEP", 0x11, \
"RESET", 0x12, \
"CLS", 0x13, \
\
"XLOAD", 0x14, \
\
"DUMP", 0x15, \
\
"HWINFO", 0x16, \
\
0
keyword_subparser_table:
.dw 0 ; 0x00 [reserved]
rjmp st_parse_print ; 0x01 PRINT expr-list
rjmp st_parse_if ; 0x02 IF expression relop expression THEN statement
rjmp st_parse_goto ; 0x03 GOTO expression
rjmp st_parse_input ; 0x04 INPUT var-list
rjmp st_parse_let ; 0x05 LET var = expression
rjmp st_parse_goto ; 0x06 GOSUB expression
rjmp invalid_immediate ; 0x07 RETURN
rjmp st_parse_for ; 0x08 FOR var = expression TO expression
rjmp st_parse_next ; 0x09 NEXT var
rjmp invalid_program ; 0x0a NEW
rjmp invalid_program ; 0x0b CLEAR
rjmp invalid_program ; 0x0c LIST
rjmp invalid_program ; 0x0d RUN
rjmp invalid_immediate ; 0x0e END
ret ; 0x0f [ON]
ret ; 0x10 [OFF]
ret ; 0x11 [SLEEP]
ret ; 0x12 [RESET]
ret ; 0x13 [CLS]
rjmp invalid_program ; 0x14 [XLOAD]
rjmp invalid_program ; 0x15 [DUMP]
rjmp invalid_program ; 0x16 [HWINFO]
invalid_immediate:
sbrs r_flags, f_immediate
ldi r_error, error_invalid_immediate
ret
invalid_program:
sbrc r_flags, f_immediate
ldi r_error, error_invalid_program
ret
st_parse_print:
rcall skip_whitespace
; check for end of input, no expression is valid
ld r16, X
tst r16
breq st_parse_print_done
; tread unadorned colon as end of input as well. need this because we can't
; rely on their being an explicit separator between expressions, so we can't
; reliably know when to expect end-of-statenent in the face of a valid
; (weird) statement like 'PRINT "A" "B"'
cpi r16, ':'
brne st_parse_print_try
st_parse_print_done:
; nothing to parse, record null and eject
clr r16
st Y+, r16
; parsed stuff
set
ret
st_parse_print_try:
rcall parse_expression
tst r_error
breq PC+2
ret
brts st_parse_print_sep
; who knows
ldi r_error, error_expected_expression
ret
st_parse_print_sep:
rcall skip_whitespace
; look for separators
ld r16, X
cpi r16, ';'
breq PC+4
cpi r16, ','
breq PC+2
; no separator, loop for next expression/string/nothing
rjmp st_parse_print
; found one, take it
adiw XL, 1
; store it
st Y+, r16
; and go again
rjmp st_parse_print
st_parse_if:
rcall skip_whitespace
rcall parse_expression
tst r_error
breq PC+2
ret
brts PC+3
ldi r_error, error_expected_expression
ret
push r16
rcall skip_whitespace
; = 0x01
; < 0x02
; <= 0x04
; <> 0x08
; > 0x10
; >= 0x20
ld r16, X+
cpi r16, '='
breq comparator_equal
cpi r16, '<'
breq comparator_left
cpi r16, '>'
breq comparator_right
comparator_unknown:
pop r16
ldi r_error, error_expected_comparator
ret
comparator_left:
ld r16, X+
cpi r16, '='
brne PC+3
ldi r16, 0x04
rjmp comparator_store
cpi r16, '>'
brne PC+3
ldi r16, 0x08
rjmp comparator_store
sbiw XL, 1
ldi r16, 0x02
rjmp comparator_store
comparator_right:
ld r16, X+
cpi r16, '='
brne PC+3
ldi r16, 0x20
rjmp comparator_store
sbiw XL, 1
ldi r16, 0x10
rjmp comparator_store
comparator_equal:
ldi r16, 0x01
comparator_store: