forked from vintagechips/ttbasic_lin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic.c
More file actions
1312 lines (1154 loc) · 22.4 KB
/
basic.c
File metadata and controls
1312 lines (1154 loc) · 22.4 KB
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
/*
TOYOSHIKI TinyBASIC V1.01
Linux edition
(C)2020 Gapstare
(C)2015 Tetsuya Suzuki
*/
// Compiler requires description
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdint.h>
// TOYOSHIKI TinyBASIC symbols
// TO-DO Rewrite defined values to fit your machine as needed
#define SIZE_LINE 78 //Command line buffer length + NULL
#define SIZE_IBUF 78 //i-code conversion buffer size
#define SIZE_LIST 1024 //List buffer size
#define SIZE_ARRY 64 //Array area size
#define SIZE_GSTK 6 //GOSUB stack size(2/nest)
#define SIZE_LSTK 15 //FOR stack size(5/nest)
// Depending on device functions
// TO-DO Rewrite these functions to fit your machine
#ifdef __linux__
#define STR_EDITION "LINUX"
#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
#define STR_EDITION "BSD"
#elif defined(_WIN32)
#define STR_EDITION "WINDOW$"
#else
#define STR_EDITION "UNKNOWN"
#endif
// Terminal control
#define c_putch(c) putchar(c)
char c_getch(){
struct termios b;
struct termios a;
char c;
tcgetattr(STDIN_FILENO, &b);
a = b;
a.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &a);
c = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &b);
return c;
}
char c_kbhit(void)
{
char c;
int f;
f = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, f | O_NONBLOCK);
c = c_getch();
fcntl(STDIN_FILENO, F_SETFL, f);
if (c != EOF) {
ungetc(c, stdin);
return 1;
}
return 0;
}
#define KEY_ENTER 10
void newline(void){
c_putch(KEY_ENTER); //LF
}
// Return random number
short getrnd(short value){
return(rand() % value) + 1;
}
// Prototypes (necessity minimum)
short iexp(void);
// Keyword table
const char* kwtbl[] = {
"GOTO", "GOSUB", "RETURN",
"FOR", "TO", "STEP", "NEXT",
"IF", "REM", "STOP",
"INPUT", "PRINT", "?", "LET",
",", ";",
"-", "+", "*", "/", "(", ")",
">=", "#", ">", "=", "<=", "<",
"@", "RND", "ABS", "SIZE",
"LIST", "RUN", "NEW", "QUIT"
};
// i-code(Intermediate code) assignment
enum{
I_GOTO, I_GOSUB, I_RETURN,
I_FOR, I_TO, I_STEP, I_NEXT,
I_IF, I_REM, I_STOP,
I_INPUT, I_PRINT, I_QMARK, I_LET,
I_COMMA, I_SEMI,
I_MINUS, I_PLUS, I_MUL, I_DIV, I_OPEN, I_CLOSE,
I_GTE, I_SHARP, I_GT, I_EQ, I_LTE, I_LT,
I_ARRAY, I_RND, I_ABS, I_SIZE,
I_LIST, I_RUN, I_NEW, I_QUIT,
I_NUM, I_VAR, I_STR,
I_EOL
};
// Keyword count
#define SIZE_KWTBL (sizeof(kwtbl) / sizeof(const char*))
// List formatting condition
// no space after
const unsigned char i_nsa[] = {
I_RETURN, I_STOP, I_COMMA,
I_MINUS, I_PLUS, I_MUL, I_DIV, I_OPEN, I_CLOSE,
I_GTE, I_SHARP, I_GT, I_EQ, I_LTE, I_LT,
I_ARRAY, I_RND, I_ABS, I_SIZE
};
// no space before (after numeric or variable only)
const unsigned char i_nsb[] = {
I_MINUS, I_PLUS, I_MUL, I_DIV, I_OPEN, I_CLOSE,
I_GTE, I_SHARP, I_GT, I_EQ, I_LTE, I_LT,
I_COMMA, I_SEMI, I_EOL
};
// exception search function
char sstyle(unsigned char code, const unsigned char *table, unsigned char count) {
while(count--)
if (code == table[count])
return 1;
return 0;
}
// exception search macro
#define nospacea(c) sstyle(c, i_nsa, sizeof(i_nsa))
#define nospaceb(c) sstyle(c, i_nsb, sizeof(i_nsb))
// Error messages
unsigned char err;// Error message index
const char* errmsg[] ={
"OK",
"Devision by zero",
"Overflow",
"Subscript out of range",
"Icode buffer full",
"List full",
"GOSUB too many nested",
"RETURN stack underflow",
"FOR too many nested",
"NEXT without FOR",
"NEXT without counter",
"NEXT mismatch FOR",
"FOR without variable",
"FOR without TO",
"LET without variable",
"IF without condition",
"Undefined line number",
"\'(\' or \')\' expected",
"\'=\' expected",
"Illegal command",
"Syntax error",
"Internal error",
"Abort by [ESC]"
};
// Error code assignment
enum{
ERR_OK,
ERR_DIVBY0,
ERR_VOF,
ERR_SOR,
ERR_IBUFOF, ERR_LBUFOF,
ERR_GSTKOF, ERR_GSTKUF,
ERR_LSTKOF, ERR_LSTKUF,
ERR_NEXTWOV, ERR_NEXTUM, ERR_FORWOV, ERR_FORWOTO,
ERR_LETWOV, ERR_IFWOC,
ERR_ULN,
ERR_PAREN, ERR_VWOEQ,
ERR_COM,
ERR_SYNTAX,
ERR_SYS,
ERR_ESC
};
// RAM mapping
char lbuf[SIZE_LINE]; //Command line buffer
unsigned char ibuf[SIZE_IBUF]; //i-code conversion buffer
short var[26]; //Variable area
short arr[SIZE_ARRY]; //Array area
unsigned char listbuf[SIZE_LIST]; //List area
unsigned char* clp; //Pointer current line
unsigned char* cip; //Pointer current Intermediate code
unsigned char* gstk[SIZE_GSTK]; //GOSUB stack
unsigned char gstki; //GOSUB stack index
unsigned char* lstk[SIZE_LSTK]; //FOR stack
unsigned char lstki; //FOR stack index
// Standard C libraly (about) same functions
char c_toupper(char c) {return(c <= 'z' && c >= 'a' ? c - 32 : c);}
char c_isprint(char c) {return(c >= 32 && c <= 126);}
char c_isspace(char c) {return(c == ' ' || (c <= 13 && c >= 9));}
char c_isdigit(char c) {return(c <= '9' && c >= '0');}
char c_isalpha(char c) {return ((c <= 'z' && c >= 'a') || (c <= 'Z' && c >= 'A'));}
void c_puts(const char *s) {while(*s) c_putch(*s++);}
void c_gets(){
char c;
unsigned char len;
len = 0;
while((c = c_getch()) != KEY_ENTER){
if( c == 9) c = ' '; // TAB exchange Space
if(((c == 8) || (c == 127)) && (len > 0)){ // Backspace manipulation
len--;
c_putch(8); c_putch(' '); c_putch(8);
} else
if(c_isprint(c) && (len < (SIZE_LINE - 1))){
lbuf[len++] = c;
c_putch(c);
}
}
newline();
lbuf[len] = 0; // Put NULL
if(len > 0){
while(c_isspace(lbuf[--len])); // Skip space
lbuf[++len] = 0; // Put NULL
}
}
// Print numeric specified columns
void putnum(short value, short d){
unsigned char i;
unsigned char sign;
if(value < 0){
sign = 1;
value = -value;
} else {
sign = 0;
}
lbuf[6] = 0;
i = 6;
do {
lbuf[--i] = (value % 10) + '0';
value /= 10;
} while(value > 0);
if(sign)
lbuf[--i] = '-';
//String length = 6 - i
while(6 - i < d){ // If short
c_putch(' '); // Fill space
d--;
}
c_puts(&lbuf[i]);
}
// Input numeric and return value
// Called by only INPUT statement
short getnum(){
short value, tmp;
char c;
unsigned char len;
unsigned char sign;
len = 0;
while((c = c_getch()) != KEY_ENTER){
if(((c == 8) || (c == 127)) && (len > 0)){ // Backspace manipulation
len--;
c_putch(8); c_putch(' '); c_putch(8);
} else
if( (len == 0 && (c == '+' || c == '-')) ||
(len < 6 && c_isdigit(c))){ // Numeric or sign only
lbuf[len++] = c;
c_putch(c);
}
}
newline();
lbuf[len] = 0;
switch(lbuf[0]){
case '-':
sign = 1;
len = 1;
break;
case '+':
sign = 0;
len = 1;
break;
default:
sign = 0;
len = 0;
break;
}
value = 0; // Initialize value
tmp = 0; // Temp value
while(lbuf[len]){
tmp = 10 * value + lbuf[len++] - '0';
if(value > tmp){ // It means overflow
err = ERR_VOF;
}
value = tmp;
}
if(sign)
return -value;
return value;
}
// Convert token to i-code
// Return byte length or 0
unsigned char toktoi() {
unsigned char i; // Loop counter(i-code sometime)
unsigned char len = 0; //byte counter
char* pkw = 0; // Temporary keyword pointer
char* ptok; // Temporary token pointer
char* s = lbuf; // Pointer to charactor at line buffer
char c; // Surround the string character, " or '
short value; //numeric
short tmp; //numeric for overflow check
while (*s) {
while (c_isspace(*s)) s++; // Skip space
//Try keyword conversion
for (i = 0; i < SIZE_KWTBL; i++) {
pkw = (char *)kwtbl[i]; // Point keyword
ptok = s; // Point top of command line
// Compare 1 keyword
while ((*pkw != 0) && (*pkw == c_toupper(*ptok))) {
pkw++;
ptok++;
}
if (*pkw == 0) {// Case success
if (len >= SIZE_IBUF - 1) {// List area full
err = ERR_IBUFOF;
return 0;
}
// i have i-code
ibuf[len++] = i;
s = ptok;
break;
}
}
// Case statement needs an argument except numeric, valiable, or strings
if(i == I_REM) {
while (c_isspace(*s)) s++; // Skip space
ptok = s;
for (i = 0; *ptok++; i++); // Get length
if (len >= SIZE_IBUF - 2 - i) {
err = ERR_IBUFOF;
return 0;
}
ibuf[len++] = i; // Put length
while (i--) { // Copy strings
ibuf[len++] = *s++;
}
break;
}
if (*pkw == 0)
continue;
ptok = s; // Point top of command line
// Try numeric conversion
if (c_isdigit(*ptok)) {
value = 0;
tmp = 0;
do {
tmp = 10 * value + *ptok++ - '0';
if (value > tmp) {
err = ERR_VOF;
return 0;
}
value = tmp;
} while (c_isdigit(*ptok));
if (len >= SIZE_IBUF - 3) {
err = ERR_IBUFOF;
return 0;
}
ibuf[len++] = I_NUM;
ibuf[len++] = value & 255;
ibuf[len++] = value >> 8;
s = ptok;
}
else
// Try string conversion
if (*s == '\"' || *s == '\'') {// If start of string
c = *s++;
ptok = s;
for (i = 0; (*ptok != c) && c_isprint(*ptok); i++) // Get length
ptok++;
if (len >= SIZE_IBUF - 1 - i) { // List area full
err = ERR_IBUFOF;
return 0;
}
ibuf[len++] = I_STR; // Put i-code
ibuf[len++] = i; // Put length
while (i--) { // Put string
ibuf[len++] = *s++;
}
if (*s == c) s++; // Skip " or '
}
else
// Try valiable conversion
if (c_isalpha(*ptok)) {
if (len >= SIZE_IBUF - 2) {
err = ERR_IBUFOF;
return 0;
}
if (len >= 4 && ibuf[len - 2] == I_VAR && ibuf[len - 4] == I_VAR) { // Case series of variables
err = ERR_SYNTAX; // Syntax error
return 0;
}
ibuf[len++] = I_VAR; // Put i-code
ibuf[len++] = c_toupper(*ptok) - 'A'; // Put index of valiable area
s++;
}
else
// Nothing mutch
{
err = ERR_SYNTAX;
return 0;
}
}
ibuf[len++] = I_EOL; // Put end of line
return len; // Return byte length
}
// Get line numbere by line pointer
short getlineno(unsigned char *lp) {
if(*lp == 0) //end of list
return 32767;// max line bumber
return *(lp + 1) | *(lp + 2) << 8;
}
// Search line by line number
unsigned char* getlp(short lineno) {
unsigned char *lp;
for (lp = listbuf; *lp; lp += *lp)
if (getlineno(lp) >= lineno)
break;
return lp;
}
// Return free memory size
short getsize() {
unsigned char* lp;
for (lp = listbuf; *lp; lp += *lp);
return listbuf + SIZE_LIST - lp - 1;
}
// Insert i-code to the list
// Preconditions to do *ibuf = len
void inslist() {
unsigned char *insp;
unsigned char *p1, *p2;
short len;
if (getsize() < *ibuf) {
err = ERR_LBUFOF; // List buffer overflow
return;
}
insp = getlp(getlineno(ibuf));
if (getlineno(insp) == getlineno(ibuf)) {// line number agree
p1 = insp;
p2 = p1 + *p1;
while (len = *p2) {
while (len--)
*p1++ = *p2++;
}
*p1 = 0;
}
// Case line number only
if (*ibuf == 4)
return;
// Make space
for (p1 = insp; *p1; p1 += *p1);
len = p1 - insp + 1;
p2 = p1 + *ibuf;
while (len--)
*p2-- = *p1--;
// Insert
len = *ibuf;
p1 = insp;
p2 = ibuf;
while (len--)
*p1++ = *p2++;
}
//Listing 1 line of i-code
void putlist(unsigned char* ip) {
unsigned char i;
while (*ip != I_EOL) {
// Case keyword
if (*ip < SIZE_KWTBL) {
c_puts(kwtbl[*ip]);
if (!nospacea(*ip))
c_putch(' ');
if (*ip == I_REM) {
ip++;
i = *ip++;
while (i--) {
c_putch(*ip++);
}
return;
}
ip++;
}
else
// Case numeric
if (*ip == I_NUM) {
ip++;
putnum(*ip | *(ip + 1) << 8, 0);
ip += 2;
if (!nospaceb(*ip)) c_putch(' ');
}
else
// Case variable
if (*ip == I_VAR) {
ip++;
c_putch(*ip++ + 'A');
if (!nospaceb(*ip)) c_putch(' ');
}
else
// Case string
if (*ip == I_STR) {
char c;
c = '\"';
ip++;
for (i = *ip; i; i--)
if (*(ip + i) == '\"') {
c = '\'';
break;
}
c_putch(c);
i = *ip++;
while (i--) {
c_putch(*ip++);
}
c_putch(c);
if (*ip == I_VAR)
c_putch(' ');
}
// Nothing match, I think, such case is impossible
else {
err = ERR_SYS;
return;
}
}
}
// Get argument in parenthesis
short getparam(){
short value;
if(*cip != I_OPEN){
err = ERR_PAREN;
return 0;
}
cip++;
value = iexp();
if(err) return 0;
if(*cip != I_CLOSE){
err = ERR_PAREN;
return 0;
}
cip++;
return value;
}
// Get value
short ivalue() {
short value;
switch (*cip) {
case I_NUM:
cip++;
value = *cip | *(cip + 1) << 8;
cip += 2;
break;
case I_PLUS:
cip++;
value = ivalue();
break;
case I_MINUS:
cip++;
value = 0 - ivalue();
break;
case I_VAR:
cip++;
value = var[*cip++];
break;
case I_OPEN:
value = getparam();
break;
case I_ARRAY:
cip++;
value = getparam();
if (err)
break;
if (value >= SIZE_ARRY) {
err = ERR_SOR;
break;
}
value = arr[value];
break;
case I_RND:
cip++;
value = getparam();
if (err)
break;
value = getrnd(value);
break;
case I_ABS:
cip++;
value = getparam();
if (err)
break;
if (value < 0)
value *= -1;
break;
case I_SIZE:
cip++;
if ((*cip != I_OPEN) || (*(cip + 1) != I_CLOSE)) {
err = ERR_PAREN;
break;
}
cip += 2;
value = getsize();
break;
default:
err = ERR_SYNTAX;
break;
}
return value;
}
// multiply or divide calculation
short imul() {
short value, tmp;
value = ivalue();
if (err)
return -1;
while (1)
switch (*cip) {
case I_MUL:
cip++;
tmp = ivalue();
value *= tmp;
break;
case I_DIV:
cip++;
tmp = ivalue();
if (tmp == 0) {
err = ERR_DIVBY0;
return -1;
}
value /= tmp;
break;
default:
return value;
}
}
// add or subtract calculation
short iplus() {
short value, tmp;
value = imul();
if (err)
return -1;
while (1)
switch (*cip) {
case I_PLUS:
cip++;
tmp = imul();
value += tmp;
break;
case I_MINUS:
cip++;
tmp = imul();
value -= tmp;
break;
default:
return value;
}
}
// The parser
short iexp() {
short value, tmp;
value = iplus();
if (err)
return -1;
// conditional expression
while (1)
switch (*cip) {
case I_EQ:
cip++;
tmp = iplus();
value = (value == tmp);
break;
case I_SHARP:
cip++;
tmp = iplus();
value = (value != tmp);
break;
case I_LT:
cip++;
tmp = iplus();
value = (value < tmp);
break;
case I_LTE:
cip++;
tmp = iplus();
value = (value <= tmp);
break;
case I_GT:
cip++;
tmp = iplus();
value = (value > tmp);
break;
case I_GTE:
cip++;
tmp = iplus();
value = (value >= tmp);
break;
default:
return value;
}
}
// PRINT handler
void iprint() {
short value;
short len;
unsigned char i;
len = 0;
while (*cip != I_SEMI && *cip != I_EOL) {
switch (*cip) {
case I_STR:
cip++;
i = *cip++;
while (i--)
c_putch(*cip++);
break;
case I_SHARP:
cip++;
len = iexp();
if (err)
return;
break;
default:
value = iexp();
if (err)
return;
putnum(value, len);
break;
}
if (*cip == I_COMMA) {
cip++;
if (*cip == I_SEMI || *cip == I_EOL)
return;
}
else {
if (*cip != I_SEMI && *cip != I_EOL) {
err = ERR_SYNTAX;
return;
}
}
}
newline();
}
// INPUT handler
void iinput() {
short value;
short index;
unsigned char i;
unsigned char prompt;
while (1) {
prompt = 1;
if (*cip == I_STR) {
cip++;
i = *cip++;
while (i--)
c_putch(*cip++);
prompt = 0;
}
switch (*cip) {
case I_VAR:
cip++;
if (prompt) {
c_putch(*cip + 'A');
c_putch(':');
}
value = getnum();
if (err)
return;
var[*cip++] = value;
break;
case I_ARRAY:
cip++;
index = getparam();
if (err)
return;
if (index >= SIZE_ARRY) {
err = ERR_SOR;
return;
}
if (prompt) {
c_puts("@(");
putnum(index, 0);
c_puts("):");
}
value = getnum();
if (err)
return;
arr[index] = value;
break;
default:
err = ERR_SYNTAX;
return;
}
switch (*cip) {
case I_COMMA:
cip++;
break;
case I_SEMI:
case I_EOL:
return;
default:
err = ERR_SYNTAX;
return;
}
}
}
// Variable assignment handler
void ivar() {
short value;
short index;
index = *cip++;
if (*cip != I_EQ) {
err = ERR_VWOEQ;
return;
}
cip++;
value = iexp();
if (err)
return;
var[index] = value;
}
// Array assignment handler
void iarray() {
short value;
short index;
index = getparam();
if (err)
return;
if (index >= SIZE_ARRY) {
err = ERR_SOR;
return;
}
if (*cip != I_EQ) {
err = ERR_VWOEQ;
return;
}
cip++;
value = iexp();
if (err)
return;
arr[index] = value;
}
// LET handler
void ilet() {
switch (*cip) {
case I_VAR:
cip++;
ivar(); // Variable assignment
break;
case I_ARRAY:
cip++;
iarray(); // Array assignment
break;
default:
err = ERR_LETWOV;
break;
}
}
// Execute a series of i-code
unsigned char* iexe() {
short lineno; //line number
unsigned char* lp; //temporary line pointer
short index, vto, vstep; // FOR-NEXT items
short condition; //IF condition
while (*cip != I_EOL) {
if (c_kbhit()) // check keyin
if (c_getch() == 27) { // ESC ?
err = ERR_ESC;
return NULL;
}
switch (*cip) {
case I_GOTO:
cip++;
lineno = iexp(); // get line number
if (err)
break;
lp = getlp(lineno); // search line
if (lineno != getlineno(lp)) { // if not found
err = ERR_ULN;
break;
}
clp = lp; // update line pointer
cip = clp + 3; // update i-code pointer
break;
case I_GOSUB:
cip++;
lineno = iexp(); // get line number
if (err)
break;
lp = getlp(lineno); // search line
if (lineno != getlineno(lp)) { // if not found
err = ERR_ULN;
break;
}
// push pointers
if (gstki >= SIZE_GSTK - 2) { // stack overflow ?
err = ERR_GSTKOF;
break;
}
gstk[gstki++] = clp; // push line pointer