forked from EtchedPixels/ubasic
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ubasic.c
1458 lines (1364 loc) · 36.8 KB
/
ubasic.c
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 (c) 2006, Adam Dunkels
* All rights reserved.
*
* Copyright (c) 2015, Alan Cox
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the author nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
#define DEBUG 0
#if DEBUG
#define DEBUG_PRINTF(...) printf(__VA_ARGS__)
#else
#define DEBUG_PRINTF(...)
#endif
#include <stdio.h> /* printf() */
#include <stdlib.h> /* exit() */
#include <stdint.h> /* Types */
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <setjmp.h>
#include "ubasic.h"
#include "tokenizer.h"
jmp_buf exception;
#define exit(x) longjmp(exception, x)
static char const *program_ptr;
#define MAX_GOSUB_STACK_DEPTH 10
static char const *gosub_stack[MAX_GOSUB_STACK_DEPTH];
static int gosub_stack_ptr;
struct for_state {
char const *resume_token; /* Token to resume execution at */
var_t for_variable;
value_t to;
value_t step;
};
#define MAX_FOR_STACK_DEPTH 4
static struct for_state for_stack[MAX_FOR_STACK_DEPTH];
static int for_stack_ptr;
struct line_index {
line_t line_number;
char const *program_text_position;
struct line_index *next;
};
struct line_index *line_index_head = NULL;
struct line_index *line_index_current = NULL;
#define MAX_VARNUM 26 * 11
#define MAX_SUBSCRIPT 2
#define MAX_STRING 26
#define MAX_ARRAY 26
static value_t variables[MAX_VARNUM];
static uint8_t *vararrays[MAX_ARRAY]; /* Could union with variables FIXME ?*/
static value_t variablesubs[MAX_ARRAY];
static value_t vardim[MAX_ARRAY][MAX_SUBSCRIPT];
static uint8_t *strings[MAX_STRING];
static value_t stringsubs[MAX_STRING];
static value_t stringdim[MAX_STRING][MAX_SUBSCRIPT];
static uint8_t nullstr[1] = { 0 };
static int ended;
static void expr(struct typevalue *val);
static void line_statements(void);
void statements(void);
static uint8_t statementgroup(void);
static uint8_t statement(void);
static void index_free(void);
peek_func peek_function = NULL;
poke_func poke_function = NULL;
line_t line_num;
static const char *data_position;
static int data_seek;
static unsigned int array_base = 0;
const char *_itoa(int v)
{
static char buf[16];
snprintf(buf, 16, "%d", v);
return buf;
}
const char *_uitoa(int v)
{
static char buf[16];
snprintf(buf, 16, "%u", v);
return buf;
}
/*---------------------------------------------------------------------------*/
void ubasic_init(const char *program)
{
int i;
program_ptr = program;
for_stack_ptr = gosub_stack_ptr = 0;
index_free();
tokenizer_init(program);
data_position = program_ptr;
data_seek = 1;
ended = 0;
for (i = 0; i < MAX_STRING; i++)
strings[i] = nullstr;
}
/*---------------------------------------------------------------------------*/
void ubasic_init_peek_poke(const char *program, peek_func peek, poke_func poke)
{
peek_function = peek;
poke_function = poke;
ubasic_init(program);
}
/*---------------------------------------------------------------------------*/
void ubasic_error(const char *err)
{
const char *p;
charout('\n', NULL);
if (line_num) {
p = _uitoa(line_num);
putstrz(p);
putstrz(": ");
}
putstrz(err);
putstrz(" error.\n");
exit(1);
}
static const char syntax[] = { "Syntax" };
static const char badtype[] = { "Type mismatch" };
static const char divzero[] = { "Division by zero" };
static const char outofmemory[] = { "Out of memory" };
static const char badsubscript[] = { "Subscript" };
static const char redimension[] = { "Redimension" };
static void syntax_error(void)
{
ubasic_error(syntax);
}
/* Call back from the tokenizer on error */
void ubasic_tokenizer_error(void)
{
syntax_error();
}
/*---------------------------------------------------------------------------*/
static uint8_t accept_tok(uint8_t token)
{
if(token != current_token) {
DEBUG_PRINTF("Token not what was expected (expected %d, got %d)\n",
token, current_token);
tokenizer_error_print();
exit(1);
}
DEBUG_PRINTF("Expected %d, got it\n", token);
tokenizer_next();
/* This saves lots of extra calls - return the new token */
return current_token;
}
/*---------------------------------------------------------------------------*/
static uint8_t accept_either(uint8_t tok1, uint8_t tok2)
{
uint8_t t = current_token;
if (t == tok2)
accept_tok(tok2);
else
accept_tok(tok1);
return t;
}
/*---------------------------------------------------------------------------*/
static int statement_end(void)
{
uint8_t t = current_token;
/* FIXME?? END OF FILE */
if (t == TOKENIZER_CR || t == TOKENIZER_COLON)
return 1;
return 0;
}
/*---------------------------------------------------------------------------*/
static void bracketed_expr(struct typevalue *v)
{
accept_tok(TOKENIZER_LEFTPAREN);
expr(v);
accept_tok(TOKENIZER_RIGHTPAREN);
}
/*---------------------------------------------------------------------------*/
static void typecheck_int(struct typevalue *v)
{
if (v->type != TYPE_INTEGER)
ubasic_error(badtype);
}
/*---------------------------------------------------------------------------*/
static void typecheck_string(struct typevalue *v)
{
if (v->type != TYPE_STRING)
ubasic_error(badtype);
}
/*---------------------------------------------------------------------------*/
static void typecheck_same(struct typevalue *l, struct typevalue *r)
{
if (l->type != r->type)
ubasic_error(badtype);
}
/*---------------------------------------------------------------------------*/
static void range_check(struct typevalue *v, value_t top)
{
typecheck_int(v);
if (v->d.i > top || v->d.i < array_base)
ubasic_error(badsubscript);
}
/*---------------------------------------------------------------------------*/
/* Temoporary implementation of string workspaces */
static uint8_t stringblob[512];
static uint8_t *nextstr;
static uint8_t *string_temp(int len)
{
uint8_t *p = nextstr;
if (len > 255)
ubasic_error("String too long");
nextstr += len + 1;
if (nextstr > stringblob + sizeof(stringblob))
ubasic_error("Out of temporary space");
*p = len;
return p;
}
/*---------------------------------------------------------------------------*/
static void string_temp_free(void)
{
nextstr = stringblob;
}
/*---------------------------------------------------------------------------*/
static void string_cut(struct typevalue *o, struct typevalue *t, value_t l, value_t n)
{
uint8_t *p = t->d.p;
int f = *p;
/* Strings start at 1 ... */
if (l > f) /* Nothing to cut */
o->d.p = string_temp(0);
else {
f -= l - 1;
if (f < n)
n = f;
o->d.p = string_temp(n);
memcpy(o->d.p+1, p + l, n);
}
o->type = TYPE_STRING;
}
/*---------------------------------------------------------------------------*/
static void string_cut_r(struct typevalue *o, struct typevalue *t, value_t r)
{
int f = *t->d.p;
f -= r;
if (f <= 0) {
o->d.p = string_temp(0);
o->type = TYPE_STRING;
} else
string_cut(o, t, f + 1, r);
}
/*---------------------------------------------------------------------------*/
static value_t string_val(struct typevalue *t)
{
uint8_t *p = t->d.p;
uint8_t l = *p++;
uint8_t neg = 0;
value_t n = 0;
if (*p == '-') {
neg = 1;
p++;
l--;
}
if (l == 0)
ubasic_error(badtype);
while(l) {
if (!isdigit(*p))
ubasic_error(badtype);
n = 10 * n + *p++ - '0';
l--;
}
return neg ? -n : n;
}
/*---------------------------------------------------------------------------*/
static value_t bracketed_intexpr(void)
{
struct typevalue v;
bracketed_expr(&v);
typecheck_int(&v);
return v.d.i;
}
/*---------------------------------------------------------------------------*/
static void funcexpr(struct typevalue *t, const char *f)
{
accept_tok(TOKENIZER_LEFTPAREN);
while(*f) {
expr(t);
if (*f != t->type)
ubasic_error(badtype);
if (*++f)
accept_tok(TOKENIZER_COMMA);
t++;
}
accept_tok(TOKENIZER_RIGHTPAREN);
}
/*---------------------------------------------------------------------------*/
static int parse_subscripts(struct typevalue *v)
{
accept_tok(TOKENIZER_LEFTPAREN);
expr(v);
if (accept_either(TOKENIZER_COMMA, TOKENIZER_RIGHTPAREN) == TOKENIZER_COMMA) {
expr(++v);
accept_tok(TOKENIZER_RIGHTPAREN);
return 2;
}
return 1;
}
/*---------------------------------------------------------------------------*/
static void varfactor(struct typevalue *v)
{
var_t var = tokenizer_variable_num();
struct typevalue s[MAX_SUBSCRIPT];
int n = 0;
/* Sinclair style A$(2 TO 5) would also need to be parsed here if added */
accept_either(TOKENIZER_INTVAR, TOKENIZER_STRINGVAR);
if (current_token == TOKENIZER_LEFTPAREN)
n = parse_subscripts(s);
ubasic_get_variable(var, v, n, s);
DEBUG_PRINTF("varfactor: obtaining %d from variable %d\n", v->d.i, tokenizer_variable_num());
}
/*---------------------------------------------------------------------------*/
static void factor(struct typevalue *v)
{
uint8_t t = current_token;
int len;
struct typevalue arg[3];
DEBUG_PRINTF("factor: token %d\n", current_token);
switch(t) {
case TOKENIZER_STRING:
v->type = TYPE_STRING;
len = tokenizer_string_len();
v->d.p = string_temp(len);
memcpy(v->d.p + 1, tokenizer_string(), len);
DEBUG_PRINTF("factor: string %p\n", v->d.p);
accept_tok(TOKENIZER_STRING);
break;
case TOKENIZER_NUMBER:
v->d.i = tokenizer_num();
v->type = TYPE_INTEGER;
DEBUG_PRINTF("factor: number %d\n", v->d.i);
accept_tok(TOKENIZER_NUMBER);
break;
case TOKENIZER_LEFTPAREN:
accept_tok(TOKENIZER_LEFTPAREN);
expr(v);
accept_tok(TOKENIZER_RIGHTPAREN);
break;
case TOKENIZER_INTVAR:
case TOKENIZER_STRINGVAR:
varfactor(v);
break;
default:
if (TOKENIZER_NUMEXP(t)) {
accept_tok(t);
switch(t) {
case TOKENIZER_PEEK:
funcexpr(arg,"I");
v->d.i = peek_function(arg[0].d.i);
break;
case TOKENIZER_ABS:
funcexpr(arg,"I");
v->d.i = arg[0].d.i;
if (v->d.i < 0)
v->d.i = -v->d.i;
break;
case TOKENIZER_INT:
funcexpr(arg,"I");
v->d.i = arg[0].d.i;
break;
case TOKENIZER_SGN:
funcexpr(arg,"I");
v->d.i = arg[0].d.i;
if (v->d.i > 1 ) v->d.i = 1;
if (v->d.i < 0) v->d.i = -1;
break;
case TOKENIZER_LEN:
funcexpr(arg,"S");
v->d.i = *arg[0].d.p;
break;
case TOKENIZER_CODE:
funcexpr(arg,"S");
if (*arg[0].d.p)
v->d.i = arg[0].d.p[1];
else
v->d.i = 0;
break;
case TOKENIZER_VAL:
funcexpr(arg,"S");
v->d.i = string_val(&arg[0]);
break;
default:
syntax_error();
}
v->type = TYPE_INTEGER;
}
else if (TOKENIZER_STRINGEXP(t)) {
accept_tok(t);
switch(t) {
case TOKENIZER_LEFTSTR:
funcexpr(arg, "SI");
string_cut(v, &arg[0], 1, arg[1].d.i);
break;
case TOKENIZER_RIGHTSTR:
funcexpr(arg, "SI");
string_cut_r(v, &arg[0], arg[1].d.i);
break;
case TOKENIZER_MIDSTR:
funcexpr(arg, "SII");
string_cut(v, &arg[0], arg[1].d.i, arg[2].d.i);
break;
case TOKENIZER_CHRSTR:
funcexpr(arg, "I");
v->d.p = string_temp(2);
v->d.p[1] = arg[0].d.i;
v->type = TYPE_STRING;
break;
default:
syntax_error();
}
}
else
syntax_error();
}
}
/*---------------------------------------------------------------------------*/
static void term(struct typevalue *v)
{
struct typevalue f2;
int op;
factor(v);
op = current_token;
DEBUG_PRINTF("term: token %d\n", op);
while(op == TOKENIZER_ASTR ||
op == TOKENIZER_SLASH ||
op == TOKENIZER_MOD) {
tokenizer_next();
factor(&f2);
typecheck_int(v);
typecheck_int(&f2);
DEBUG_PRINTF("term: %d %d %d\n", v->d.i, op, f2.d.i);
switch(op) {
case TOKENIZER_ASTR:
v->d.i *= f2.d.i;
break;
case TOKENIZER_SLASH:
if (f2.d.i == 0)
ubasic_error(divzero);
v->d.i /= f2.d.i;
break;
case TOKENIZER_MOD:
if (f2.d.i == 0)
ubasic_error(divzero);
v->d.i %= f2.d.i;
break;
}
op = current_token;
}
DEBUG_PRINTF("term: %d\n", v->d.i);
}
/*---------------------------------------------------------------------------*/
static void mathexpr(struct typevalue *v)
{
struct typevalue t2;
int op;
term(v);
op = current_token;
DEBUG_PRINTF("mathexpr: token %d\n", op);
while(op == TOKENIZER_PLUS ||
op == TOKENIZER_MINUS ||
op == TOKENIZER_BAND ||
op == TOKENIZER_BOR) {
tokenizer_next();
term(&t2);
if (op != TOKENIZER_PLUS)
typecheck_int(v);
typecheck_same(v, &t2);
DEBUG_PRINTF("mathexpr: %d %d %d\n", v->d.i, op, t2.d.i);
switch(op) {
case TOKENIZER_PLUS:
if (v->type == TYPE_INTEGER)
v->d.i += t2.d.i;
else {
uint8_t *p;
uint8_t l = *v->d.p;
p = string_temp(l + *t2.d.p);
memcpy(p + 1, v->d.p + 1, l);
memcpy(p + l + 1, t2.d.p + 1, *t2.d.p);
v->d.p = p;
}
break;
case TOKENIZER_MINUS:
v->d.i -= t2.d.i;
break;
case TOKENIZER_BAND:
v->d.i &= t2.d.i;
break;
case TOKENIZER_BOR:
v->d.i |= t2.d.i;
break;
}
op = current_token;
}
DEBUG_PRINTF("mathexpr: %d\n", v->d.i);
}
/*---------------------------------------------------------------------------*/
static void relation(struct typevalue *r1)
{
struct typevalue r2;
int op;
mathexpr(r1);
op = current_token;
DEBUG_PRINTF("relation: token %d\n", op);
/* FIXME: unclear the while is correct here. It's not correct in most
BASIC to write A > B > C, rather relations should be two part linked
with logic */
while(op == TOKENIZER_LT ||
op == TOKENIZER_GT ||
op == TOKENIZER_EQ ||
op == TOKENIZER_NE ||
op == TOKENIZER_LE ||
op == TOKENIZER_GE) {
tokenizer_next();
mathexpr(&r2);
typecheck_same(r1, &r2);
DEBUG_PRINTF("relation: %d %d %d\n", r1->d.i, op, r2.d.i);
if (r1->type == TYPE_INTEGER) {
switch(op) {
case TOKENIZER_LT:
r1->d.i = r1->d.i < r2.d.i;
break;
case TOKENIZER_GT:
r1->d.i = r1->d.i > r2.d.i;
break;
case TOKENIZER_EQ:
r1->d.i = r1->d.i == r2.d.i;
break;
case TOKENIZER_LE:
r1->d.i = r1->d.i <= r2.d.i;
break;
case TOKENIZER_GE:
r1->d.i = r1->d.i >= r2.d.i;
break;
case TOKENIZER_NE:
r1->d.i = r1->d.i != r2.d.i;
break;
}
} else {
int n =*r1->d.p;
if (*r2.d.p < n)
n = *r2.d.p;
n = memcmp(r1->d.p + 1, r2.d.p + 1, n);
if (n == 0) {
if (*r1->d.p > *r2.d.p)
n = 1;
else if (*r1->d.p < *r2.d.p)
n = -1;
}
switch(op) {
case TOKENIZER_LT:
n = (n == -1);
break;
case TOKENIZER_GT:
n = (n == 1);
break;
case TOKENIZER_EQ:
n = (n == 0);
break;
case TOKENIZER_LE:
n = (n != 1);
break;
case TOKENIZER_GE:
n = (n != -1);
break;
case TOKENIZER_NE:
n = (n != 0);
break;
}
r1->d.i = n;
}
op = current_token;
}
r1->type = TYPE_INTEGER;
}
/*---------------------------------------------------------------------------*/
static void expr(struct typevalue *r1)
{
struct typevalue r2;
int op;
relation(r1);
op = current_token;
DEBUG_PRINTF("logicrelation: token %d\n", op);
/* FIXME: unclear the while is correct here. It's not correct in most
BASIC to write A > B > C, rather relations should be two part linked
with logic */
while(op == TOKENIZER_AND ||
op == TOKENIZER_OR) {
tokenizer_next();
relation(&r2);
/* No type checks needed on relations */
DEBUG_PRINTF("logicrelation: %d %d %d\n", r1->d.i, op, r2.d.i);
switch(op) {
case TOKENIZER_AND:
r1->d.i = r1->d.i & r2.d.i;
break;
case TOKENIZER_OR:
r1->d.i = r1->d.i | r2.d.i;
break;
}
op = current_token;
}
r1->type = TYPE_INTEGER;
}
/*---------------------------------------------------------------------------*/
static value_t intexpr(void)
{
struct typevalue t;
expr(&t);
typecheck_int(&t);
return t.d.i;
}
/*---------------------------------------------------------------------------*/
static uint8_t *stringexpr(void)
{
struct typevalue t;
expr(&t);
typecheck_string(&t);
return t.d.p;
}
/*---------------------------------------------------------------------------*/
static void index_free(void) {
if(line_index_head != NULL) {
line_index_current = line_index_head;
do {
DEBUG_PRINTF("Freeing index for line %p.\n", (void *)line_index_current);
line_index_head = line_index_current;
line_index_current = line_index_current->next;
free(line_index_head);
} while (line_index_current != NULL);
line_index_head = NULL;
}
}
/*---------------------------------------------------------------------------*/
static char const *index_find(int linenum) {
#if DEBUG
int step = 0;
#endif
struct line_index *lidx;
lidx = line_index_head;
while(lidx != NULL && lidx->line_number != linenum) {
lidx = lidx->next;
#if DEBUG
if(lidx != NULL) {
DEBUG_PRINTF("index_find: Step %3d. Found index for line %d: %p.\n",
step, lidx->line_number,
lidx->program_text_position);
}
step++;
#endif
}
if(lidx != NULL && lidx->line_number == linenum) {
DEBUG_PRINTF("index_find: Returning index for line %d.\n", linenum);
return lidx->program_text_position;
}
DEBUG_PRINTF("index_find: Returning NULL.\n");
return NULL;
}
/*---------------------------------------------------------------------------*/
static void index_add(int linenum, char const* sourcepos) {
struct line_index *new_lidx;
if(line_index_head != NULL && index_find(linenum)) {
return;
}
new_lidx = malloc(sizeof(struct line_index));
new_lidx->line_number = linenum;
new_lidx->program_text_position = sourcepos;
new_lidx->next = NULL;
if(line_index_head != NULL) {
line_index_current->next = new_lidx;
line_index_current = line_index_current->next;
} else {
line_index_current = new_lidx;
line_index_head = line_index_current;
}
DEBUG_PRINTF("index_add: Adding index for line %d: %p.\n", linenum,
sourcepos);
}
/*---------------------------------------------------------------------------*/
static void
jump_linenum_slow(int linenum)
{
tokenizer_init(program_ptr);
while(tokenizer_num() != linenum) {
do {
do {
tokenizer_next();
} while(current_token != TOKENIZER_CR &&
current_token != TOKENIZER_ENDOFINPUT);
if(current_token == TOKENIZER_CR) {
tokenizer_next();
}
} while(current_token != TOKENIZER_NUMBER);
DEBUG_PRINTF("jump_linenum_slow: Found line %d\n", tokenizer_num());
}
}
/*---------------------------------------------------------------------------*/
static void
jump_linenum(int linenum)
{
char const* pos = index_find(linenum);
if(pos != NULL) {
DEBUG_PRINTF("jump_linenum: Going to line %d.\n", linenum);
tokenizer_goto(pos);
} else {
/* We'll try to find a yet-unindexed line to jump to. */
DEBUG_PRINTF("jump_linenum: Calling jump_linenum_slow %d.\n", linenum);
jump_linenum_slow(linenum);
}
}
/*---------------------------------------------------------------------------*/
static void go_statement(void)
{
int linenum;
uint8_t t;
t = accept_either(TOKENIZER_TO, TOKENIZER_SUB);
if (t == TOKENIZER_TO) {
linenum = intexpr();
DEBUG_PRINTF("go_statement: go to %d.\n", linenum);
if (!statement_end())
syntax_error();
DEBUG_PRINTF("go_statement: jumping.\n");
jump_linenum(linenum);
return;
}
linenum = intexpr();
if (!statement_end())
syntax_error();
if(gosub_stack_ptr < MAX_GOSUB_STACK_DEPTH) {
gosub_stack[gosub_stack_ptr] = tokenizer_pos();
gosub_stack_ptr++;
jump_linenum(linenum);
} else {
DEBUG_PRINTF("gosub_statement: gosub stack exhausted\n");
ubasic_error("Return without gosub");
}
}
/*---------------------------------------------------------------------------*/
#include "lib/textmode/textmode.h"
static int chpos = 0, Y=0;
extern uint8_t text_color;
char cursor;
void begin_input(void)
{
cursor = 1;
vram_attr[Y][chpos] = cursor;
}
void end_input(void)
{
cursor = 0;
}
void charout(char c, void *unused)
{
if (c == '\t') {
do {
charout(' ', NULL);
} while(chpos%8);
return;
}
if ((c == 8 || c== 127) && chpos)
{
chpos--;
vram[Y][chpos] = ' ';
}
else if (c == '\r' || c == '\n')
{
vram_attr[Y][chpos] = 0;
chpos = 0; Y++;
if (Y > 40) {
clear();
Y = 0;
}
}
else {
vram[Y][chpos] = c;
chpos++;
}
vram_attr[Y][chpos] = cursor;
vram_attr[Y][chpos-1] = 0;
vram_attr[Y][chpos+1] = 0;
}
static void charreset(void)
{
chpos = 0;
}
static void chartab(value_t v)
{
while(chpos < v)
charout(' ', NULL);
}
static void charoutstr(uint8_t *p)
{
int len =*p++;
while(len--)
charout(*p++, NULL);
}
void putstrz(char* p)
{
while(*p)
charout(*p++, NULL);
}
static void intout(value_t v)
{
const char *p = _itoa(v);
while(*p)
charout(*p++, NULL);
}
static void print_statement(void)
{
uint8_t nonl;
uint8_t t;
uint8_t nv = 0;
do {
t = current_token;
nonl = 0;
DEBUG_PRINTF("Print loop\n");
if (nv == 0) {
if(t == TOKENIZER_STRING) {
/* Handle string const specially - length rules */
tokenizer_string_func(charout, NULL);
tokenizer_next();
nv = 1;
continue;
} else if(TOKENIZER_STRINGEXP(t)) {
charoutstr(stringexpr());
nv = 1;
continue;
} else if(TOKENIZER_NUMEXP(t)) {
intout(intexpr());
nv = 1;
continue;
} else if(t == TOKENIZER_TAB) {
nv = 1;
accept_tok(TOKENIZER_TAB);
chartab(bracketed_intexpr());
continue;
} else if(t == TOKENIZER_AT) {
int x,y;
nv = 1;
accept_tok(TOKENIZER_AT);
y = intexpr();
accept_tok(TOKENIZER_COMMA);
x = intexpr();
if (move_cursor(x,y))
chpos = x;
continue;
}
}
nv = 0;
if(t == TOKENIZER_COMMA) {
charout('\t', NULL);
nonl = 1;
tokenizer_next();
} else if(t == TOKENIZER_SEMICOLON) {
nonl = 1;
tokenizer_next();
} else if (!statement_end()) {
syntax_error();
break;
}
} while(!statement_end());
if (!nonl)
charout('\n', 0);
DEBUG_PRINTF("End of print\n");
}
/*---------------------------------------------------------------------------*/
static void if_statement(void)
{
struct typevalue r;
expr(&r);
DEBUG_PRINTF("if_statement: relation %d\n", r.d.i);
/* FIXME allow THEN number */
accept_tok(TOKENIZER_THEN);
if(r.d.i) {
statementgroup();
} else {
tokenizer_newline();
}
}
/*---------------------------------------------------------------------------*/
static void let_statement(void)
{
var_t var;
struct typevalue v;
struct typevalue s[MAX_SUBSCRIPT];
int n = 0;
var = tokenizer_variable_num();
accept_either(TOKENIZER_INTVAR, TOKENIZER_STRINGVAR);
if (current_token == TOKENIZER_LEFTPAREN)
n = parse_subscripts(s);
accept_tok(TOKENIZER_EQ);
expr(&v);
DEBUG_PRINTF("let_statement: assign %d to %d\n", var, v.d.i);
ubasic_set_variable(var, &v, n, s);
}
/*---------------------------------------------------------------------------*/
static void return_statement(void)
{
if(gosub_stack_ptr > 0) {
gosub_stack_ptr--;
tokenizer_goto(gosub_stack[gosub_stack_ptr]);
} else {
DEBUG_PRINTF("return_statement: non-matching return\n");
}
}
/*---------------------------------------------------------------------------*/
static void next_statement(void)
{
int var;
struct for_state *fs;
struct typevalue t;
/* FIXME: support 'NEXT' on its own, also loop down the stack so if you
GOTO out of a layer of NEXT the right thing occurs */
var = tokenizer_variable_num();