-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmainstate.c
3412 lines (3289 loc) · 113 KB
/
mainstate.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
/*
** This file is part of the Matrix Brandy Basic VI Interpreter.
** Copyright (C) 2000-2014 David Daniels
** Copyright (C) 2018-2024 Michael McConnell and contributors
**
** Brandy 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 2, or (at your option)
** any later version.
**
** Brandy 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 Brandy; see the file COPYING. If not, write to
** the Free Software Foundation, 59 Temple Place - Suite 330,
** Boston, MA 02111-1307, USA.
**
**
** This file contains the bulk of the Basic interpreter itself
**
** 20-Mar-2014 JGH: DEF correctly executed as a REM - skips to next line
**
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include "common.h"
#include "target.h"
#include "basicdefs.h"
#include "tokens.h"
#include "variables.h"
#include "stack.h"
#include "heap.h"
#include "strings.h"
#include "errors.h"
#include "statement.h"
#include "evaluate.h"
#include "convert.h"
#include "miscprocs.h"
#include "editor.h"
#include "mos.h"
#include "screen.h"
#include "lvalue.h"
#include "fileio.h"
#include "mainstate.h"
#include "keyboard.h"
#include "mos_sys.h"
#define MAXWHENS 500 /* maximum number of WHENs allowed per CASE statement */
/* Replacement for memmove where we dedupe pairs of double quotes */
static int memcpydedupe(char *dest, const unsigned char *src, size_t len, char dedupe) {
int sptr = 0, dptr=0, shorten=0;
DEBUGFUNCMSGIN;
while (sptr < len) {
*(dest+dptr) = *(src+sptr);
if (*(src+sptr)==dedupe && *(src+sptr+1)==dedupe) {
sptr++;
shorten++;
}
sptr++;
dptr++;
}
DEBUGFUNCMSGOUT;
return(shorten);
}
/*
** 'exec_assembler' is invoked when a '[' is found. This version of
** the interpreter does not include an assembler
*/
void exec_assembler(void) {
DEBUGFUNCMSGIN;
DEBUGFUNCMSGOUT;
error(ERR_UNSUPPORTED);
}
/*
** 'exec_asmend' is called with a ']' is found. This version of
** the interpreter does not include an assembler
*/
void exec_asmend(void) {
DEBUGFUNCMSGIN;
DEBUGFUNCMSGOUT;
error(ERR_UNSUPPORTED);
}
/*
** 'exec_oscmd' deals with '*' commands. The text of the '*' is
** retrieved from the source part of the line and passed to the
** operating system as a command. The effect of a command that
** overwrites the Basic program or the interpreter is undefined.
*/
void exec_oscmd(void) {
char *p;
DEBUGFUNCMSGIN;
p = CAST(GET_SRCADDR(basicvars.current), char *); /* Get the address of the command text */
mos_oscli(p, FALSE, NULL); /* Run command but do not capture output */
basicvars.current+=1+SIZESIZE; /* Skip the '*' and the offset after it */
DEBUGFUNCMSGOUT;
}
/*
** 'exec_call' deals with the Basic 'CALL' statement. This is not
** supported at the moment
*/
void exec_call(void) {
int32 address, parmcount, parameters[1];
DEBUGFUNCMSGIN;
basicvars.current++;
parmcount = 0;
address = eval_integer();
check_ateol();
mos_call(address, parmcount, parameters);
DEBUGFUNCMSGOUT;
}
/*
** 'exec_case' deals with a 'CASE' statement.
** The way 'CASE' statements are handled is to build a table of pointers to
** expressions and statement sequences the first time the statement is seen.
** This eliminates the need to search for the 'WHEN' clauses each time the
** statement is executed (at the expense of some extra memory).
*/
void exec_case(void) {
stackitem casetype, whentype;
int32 n, intcase = 0;
uint8 uint8case = 0;
int64 int64case = 0;
static float64 floatcase = 0;
basicstring casestring = {0, NULL}, whenstring;
casetable *cp;
boolean found;
byte *here;
DEBUGFUNCMSGIN;
here = basicvars.current;
cp = GET_ADDRESS(basicvars.current, casetable *); /* Fetch address of 'CASE' table */
basicvars.current+=1+LOFFSIZE; /* Skip 'CASE' token and pointer */
expression();
casetype = GET_TOPITEM;
switch (casetype) { /* Check the type of the 'CASE' expression */
case STACK_INT: intcase = pop_int(); break;
case STACK_UINT8: uint8case = pop_uint8(); break;
case STACK_INT64: int64case = pop_int64(); break;
case STACK_FLOAT: floatcase = pop_float(); break;
case STACK_STRING: case STACK_STRTEMP: casestring = pop_string(); break;
default:
error(ERR_VARNUMSTR);
return;
}
/*
** Now go through the case table and try to find a 'WHEN' case that
** matches
*/
found = FALSE;
for (n=0; n<cp->whencount; n++) {
basicvars.current = cp->whentable[n].whenexpr; /* Point at the WHEN expression */
if (basicvars.traces.lines) trace_line(GET_LINENO(find_linestart(basicvars.current)));
while (TRUE) {
expression();
whentype = GET_TOPITEM;
if (casetype == STACK_INT) { /* Go by type of 'case' expression */
switch(whentype) { /* Then by type of 'WHEN' expression */
case STACK_INT: case STACK_UINT8: case STACK_INT64:
found = pop_anyint() == intcase; break;
case STACK_FLOAT: found = pop_float() == TOFLOAT(intcase); break;
default:
DEBUGFUNCMSGOUT;
error(ERR_TYPENUM);
return;
}
}
else if (casetype == STACK_UINT8) { /* Go by type of 'case' expression */
switch(whentype) { /* Then by type of 'WHEN' expression */
case STACK_INT: case STACK_UINT8: case STACK_INT64:
found = pop_anyint() == uint8case; break;
case STACK_FLOAT: found = pop_float() == TOFLOAT(uint8case); break;
default:
DEBUGFUNCMSGOUT;
error(ERR_TYPENUM);
return;
}
}
else if (casetype == STACK_INT64) { /* Go by type of 'case' expression */
switch(whentype) { /* Then by type of 'WHEN' expression */
case STACK_INT: case STACK_UINT8: case STACK_INT64:
found = pop_anyint() == int64case; break;
case STACK_FLOAT: found = pop_float() == TOFLOAT(int64case); break;
default:
DEBUGFUNCMSGOUT;
error(ERR_TYPENUM);
return;
}
}
else if (casetype == STACK_FLOAT) { /* 'case' expression is a floating point value */
found = pop_anynumfp() == floatcase;
}
else { /* This leaves just strings */
if (whentype != STACK_STRING && whentype != STACK_STRTEMP) {
DEBUGFUNCMSGOUT;
error(ERR_TYPESTR);
return;
}
whenstring = pop_string();
if (whenstring.stringlen != casestring.stringlen)
found = FALSE;
else if (whenstring.stringlen == 0)
found = TRUE;
else {
found = memcmp(whenstring.stringaddr, casestring.stringaddr, whenstring.stringlen) == 0;
}
if (whentype == STACK_STRTEMP) free_string(whenstring);
}
if (found || *basicvars.current == ':' || *basicvars.current == asc_NUL) break; /* Found a match or end of WHEN expression list so escape from loop */
if (*basicvars.current == ',') /* No match - Another value follows for this CASE */
basicvars.current++;
else {
DEBUGFUNCMSGOUT;
error(ERR_SYNTAX);
return;
}
}
if (found) break; /* Match found - Escape from outer loop */
}
if (casetype == STACK_STRTEMP) free_string(casestring);
if (found) { /* Case value matched */
if (basicvars.traces.branches) trace_branch(here, cp->whentable[n].whenaddr);
basicvars.current = cp->whentable[n].whenaddr;
}
else { /* Case value not matched - Use 'OTHERWISE' entry */
if (basicvars.traces.branches) trace_branch(here, cp->defaultaddr);
basicvars.current = cp->defaultaddr;
}
DEBUGFUNCMSGOUT;
}
/*
** 'exec_xcase' is called the first time a case statement is seen to go
** through the statement and build a case table for it. Each entry of
** this consists of pair of addresses, one for an expression and one for
** the code after the 'WHEN'. On entry, 'current' points at the address
** of the 'XCASE' token
*/
void exec_xcase(void) {
byte *tp, *lp, *defaultaddr;
int32 whencount, depth, n;
casetable *cp;
whenvalue whentable[MAXWHENS];
DEBUGFUNCMSGIN;
lp = basicvars.current;
do { /* Find the end of the current line */
tp = lp;
lp = skip_token(lp);
} while (*lp != asc_NUL);
if (*tp != BASTOKEN_OF) { /* Last item on line must be 'OF' */
DEBUGFUNCMSGOUT;
error(ERR_OFMISS);
return;
}
lp++; /* Point at the start of the line after the 'CASE' */
whencount = 0;
defaultaddr = NIL;
depth = 1;
while (depth>0) {
if (AT_PROGEND(lp)) { /* No ENDCASE found for this CASE */
DEBUGFUNCMSGOUT;
error(ERR_ENDCASE);
return;
}
tp = FIND_EXEC(lp); /* Find the first executable token */
switch (*tp) {
case BASTOKEN_XWHEN: case BASTOKEN_WHEN: /* Have found a 'WHEN' */
tp+=(1+OFFSIZE); /* Skip token and the offset after it */
if (depth == 1) { /* Only want WHENs from CASE at this level */
if (whencount == MAXWHENS) {
DEBUGFUNCMSGOUT;
error(ERR_WHENCOUNT);
return;
}
whentable[whencount].whenexpr = tp;
while (*tp != asc_NUL && *tp != ':') tp = skip_token(tp); /* Find code after ':' */
if (*tp == ':') tp++;
if (*tp == asc_NUL) { /* At end of line - Skip to next line */
tp++;
tp = FIND_EXEC(tp);
}
whentable[whencount].whenaddr = tp;
whencount++;
}
break;
case BASTOKEN_XOTHERWISE: case BASTOKEN_OTHERWISE: /* Have found an 'OTHERWISE' */
if (depth == 1) {
tp+=(1+OFFSIZE); /* Skip token and the offset after it */
if (*tp == ':') tp++;
if (*tp == asc_NUL) { /* 'OTHERWISE' is at end of line */
tp++; /* Move to start of next line */
if (AT_PROGEND(tp)) {
DEBUGFUNCMSGOUT;
error(ERR_ENDCASE);
return;
}
tp = FIND_EXEC(tp);
}
defaultaddr = tp;
}
break;
case BASTOKEN_ENDCASE: /* Have reached the end of the statement */
depth--;
if (depth == 0 && defaultaddr == NIL) defaultaddr = tp+1;
break;
}
/* See if a nested CASE statement starts on this line */
if (depth>0) {
tp = FIND_EXEC(lp);
while (*tp != asc_NUL && *tp != BASTOKEN_XCASE) tp = skip_token(tp);
if (*tp == BASTOKEN_XCASE) depth++;
lp+=GET_LINELEN(lp);
}
}
/* Create 'CASE' table */
cp = allocmem(sizeof(casetable)+whencount*sizeof(whenvalue), 1); /* Hacksville, Tennessee */
cp->whencount = whencount;
cp->defaultaddr = defaultaddr;
for (n=0; n<whencount; n++) cp->whentable[n] = whentable[n];
*basicvars.current = BASTOKEN_CASE;
set_address(basicvars.current, cp);
exec_case(); /* Now go and process the CASE statement */
DEBUGFUNCMSGOUT;
}
/*
** 'exec_chain' deals with the Basic 'CHAIN' statement.
*/
void exec_chain(void) {
basicstring namedesc;
stackitem stringtype;
char *filename;
DEBUGFUNCMSGIN;
basicvars.current++;
expression();
stringtype = GET_TOPITEM;
if (stringtype != STACK_STRING && stringtype != STACK_STRTEMP) {
DEBUGFUNCMSGOUT;
error(ERR_TYPESTR);
return;
}
namedesc = pop_string();
filename = tocstring(namedesc.stringaddr, namedesc.stringlen);
if (stringtype == STACK_STRTEMP) free_string(namedesc);
check_ateol();
read_basic(filename);
DEBUGFUNCMSGOUT;
run_program(NIL);
}
/*
** 'exec_clear' is called to clear all the variables defined in the
** program and remove all references to them from the tokenised program.
** It also clears the Basic stack so it is not a good idea to use it
** in a procedure or function. It probably will not crash the interpreter
** but there is no guarantee of this.
*/
void exec_clear(void) {
DEBUGFUNCMSGIN;
basicvars.current++; /* Move past token */
if ((*basicvars.current == 0xFF) && (*(basicvars.current+1) == BASTOKEN_HIMEM)) {
basicvars.current+=2;
exec_clear_himem();
} else {
check_ateol();
clear_offheaparrays();
clear_varptrs();
clear_varlists();
clear_strings();
clear_heap();
clear_stack();
init_expressions();
}
DEBUGFUNCMSGOUT;
}
/*
** 'exec_data' hendles the 'DATA' statement when one of these is
** encountered in normal program execution. The DATA token will
** always be the last thing on the line. It moves the current
** pointer to the NUL at the end of the line
*/
void exec_data(void) {
DEBUGFUNCMSGIN;
basicvars.current = skip_token(basicvars.current);
DEBUGFUNCMSGOUT;
}
/*
** 'exec_rem' hendles the 'REM' statement when one of these is
** encountered in normal program execution. The REM token will
** always be the last thing on the line. It moves the current
** pointer to the NUL at the end of the line
*/
void exec_rem(void) {
DEBUGFUNCMSGIN;
basicvars.current = skip_token(basicvars.current);
DEBUGFUNCMSGOUT;
}
/*
** 'exec_def' processes 'DEF'-type statements. A DEF is executed
** identically to a REM - execution skips to the next line.
** (which is why we check for 0 instead of using the ateol table)
*/
void exec_def(void) {
DEBUGFUNCMSGIN;
while (*basicvars.current != '\0') {
basicvars.current = skip_token(basicvars.current);
}
DEBUGFUNCMSGOUT;
}
/*
** define_byte_array - Called to handle a DIM of the form:
** DIM <name> <size>
*/
static void define_byte_array(variable *vp, boolean offheap) {
boolean isindref, islocal;
int64 offset = 0, highindex;
byte *ep = NULL;
/*
** Allocate a byte *array* (index range 0..highindex) of the requested
** size. The address stored is in fact the byte offset of the array
** from the start of the Basic workspace
*/
DEBUGFUNCMSGIN;
if (vp->varflags == VAR_UINT8) {
DEBUGFUNCMSGOUT;
error(ERR_UNSUITABLEVAR);
return;
}
if (vp->varflags != VAR_INTWORD && vp->varflags != VAR_INTLONG && vp->varflags != VAR_FLOAT) {
DEBUGFUNCMSGOUT;
error(ERR_VARNUM);
return;
}
isindref = *basicvars.current == '!';
if (isindref) {
/* Deal with indirection operator in DIM <variable>!<offset> <size> */
basicvars.current++;
if (vp->varflags == VAR_INTWORD)
offset = vp->varentry.varinteger + eval_intfactor();
else if (vp->varflags == VAR_INTLONG)
offset = vp->varentry.var64int + eval_intfactor();
else {
offset = TOINT64(vp->varentry.varfloat) + eval_intfactor();
}
} else if (offheap) {
if (vp->varflags == VAR_INTWORD)
offset = vp->varentry.varinteger;
else if (vp->varflags == VAR_INTLONG)
offset = vp->varentry.var64int;
else {
offset = TOINT64(vp->varentry.varfloat);
}
}
islocal = *basicvars.current == BASTOKEN_LOCAL;
if (islocal) { /* Allocating block on stack */
basicvars.current++;
highindex = eval_int64();
if ((basicvars.procstack == NIL) && (highindex != -1)) {
/* LOCAL found outside a PROC or FN. Allow -1 as that shows stack pointer. */
DEBUGFUNCMSGOUT;
error(ERR_LOCAL);
return;
}
if (highindex < -1) {
DEBUGFUNCMSGOUT;
error(ERR_NEGBYTEDIM, vp->varname); /* Dimension is out of range */
return;
}
ep = alloc_stackmem(highindex + 1); /* Allocate memory on the stack */
if (ep == NIL) { /* Not enough memory left */
DEBUGFUNCMSGOUT;
error(ERR_BADBYTEDIM, vp->varname);
return;
}
}
else { /* Allocating block from heap or malloc*/
highindex = eval_int64();
if (highindex < -1) { /* Dimension is out of range */
DEBUGFUNCMSGOUT;
error(ERR_NEGBYTEDIM, vp->varname);
return;
}
if (offheap) {
ep = (byte *)(size_t)offset;
if (highindex == -1) {
free(ep);
ep = 0;
} else {
byte *newep = realloc(ep, highindex+1);
if (!newep) { /* Not enough memory left */
DEBUGFUNCMSGOUT;
error(ERR_BADBYTEDIM);
return;
}
ep = newep;
#ifdef MATRIX64BIT
if ((vp->varflags == VAR_INTWORD) && ((int64)ep > 0xFFFFFFFFll)) {
free(ep); /* Can't store the address in the variable type given so free it before complaining */
DEBUGFUNCMSGOUT;
error(ERR_ADDRESS);
return;
}
#endif
}
} else {
if (highindex == -1) { /* Treat size of -1 as special case, although it does not have to be */
ep = basicvars.vartop;
#ifdef MATRIX64BIT
if ((vp->varflags == VAR_INTWORD) && ((int64)ep > 0xFFFFFFFFll)) {
DEBUGFUNCMSGOUT;
error(ERR_ADDRESS);
return;
}
#endif
} else {
#ifdef MATRIX64BIT
if ((vp->varflags == VAR_INTWORD) && ((int64)(basicvars.stacklimit.bytesp+highindex+1) > 0xFFFFFFFFll)) {
DEBUGFUNCMSGOUT;
error(ERR_ADDRESS);
return;
}
#endif
ep = allocmem(highindex+1, 0);
if (ep == NIL) { /* Not enough memory left */
DEBUGFUNCMSGOUT;
error(ERR_BADBYTEDIM, vp->varname);
return;
}
}
}
}
if (isindref)
store_integer(offset, (size_t)ep);
else if (vp->varflags == VAR_INTWORD)
vp->varentry.varinteger = (size_t)ep;
else if (vp->varflags == VAR_INTLONG)
vp->varentry.var64int = (size_t)ep;
else {
vp->varentry.varfloat = TOFLOAT((size_t)ep);
}
DEBUGFUNCMSGOUT;
}
/*
** 'exec_dim' is called to handle DIM statements
*/
void exec_dim(void) {
byte *base, *ep;
variable *vp;
boolean offheap = 0; /* TRUE if allocating a block of memory off the heap */
boolean blockdef; /* TRUE if we are allocating a block of memory and not creating an array */
DEBUGFUNCMSGIN;
do {
boolean islocal = FALSE; /* TRUE if we are defining a local array on the stack */
basicvars.current++; /* Skip 'DIM' token or ',' */
/* Is the next token HIMEM? If so we're doing an off-heap memory block */
if ((*basicvars.current == 0xFF) && (*(basicvars.current+1) == BASTOKEN_HIMEM)) {
offheap = TRUE;
basicvars.current+=2;
}
/* Must always have a variable name next */
if (*basicvars.current != BASTOKEN_STATICVAR && *basicvars.current != BASTOKEN_XVAR) {
DEBUGFUNCMSGOUT;
error(ERR_NAMEMISS);
return;
}
if (*basicvars.current == BASTOKEN_STATICVAR) { /* Found a static variable */
vp = &basicvars.staticvars[*(basicvars.current+1)];
basicvars.current+=2;
blockdef = TRUE; /* Can only be defining a block of memory */
} else { /* Found dynamic variable or array name */
base = GET_SRCADDR(basicvars.current); /* Point 'base' at start of array name */
ep = skip_name(base); /* Point ep at byte after name */
basicvars.current+=1+LOFFSIZE; /* Skip the pointer to the name */
blockdef = *(ep-1) != '(' && *(ep-1) != '[';
vp = find_variable(base, ep-base);
if (blockdef) { /* Defining a block of memory (byte array) */
if (vp == NIL) { /* Variable does not exist */
if (*basicvars.current == '!') { /* Variable name followed by indirection operator */
DEBUGFUNCMSGOUT;
error(ERR_VARMISS, tocstring(CAST(base, char *), ep-base));
return;
} else { /* Reference to variable only - Create the variable */
vp = create_variable(base, ep-base, NIL);
}
}
}
else if (vp == NIL) /* Defining an array - Array does not exist yet */
vp = create_variable(base, ep-base, NIL);
else { /* Array name exists */
if (vp->varentry.vararray != NIL) { /* Array aleady defined */
DEBUGFUNCMSGOUT;
error(ERR_DUPLDIM, vp->varname);
return;
}
islocal = TRUE; /* Name exists but definition does not. Assume a local array */
}
}
if (blockdef) /* Defining a block of memory */
define_byte_array(vp, offheap);
else { /* Defining a normal array */
define_array(vp, islocal, offheap);
}
} while (*basicvars.current == ',');
check_ateol();
DEBUGFUNCMSGOUT;
}
/*
** 'start_blockif' returns 'TRUE' if the line starting at 'tp' marks the
** start of a block 'IF' statement
*/
static boolean start_blockif(byte *tp) {
DEBUGFUNCMSGIN;
while (*tp != asc_NUL) {
if (*tp == BASTOKEN_THEN && *(tp+1) == asc_NUL) {
DEBUGFUNCMSGOUT;
return TRUE;
}
tp = skip_token(tp);
}
DEBUGFUNCMSGOUT;
return FALSE;
}
/*
** 'exec_elsewhen' deals with an ELSE keyword for both block and single
** line IF statements as well as the 'WHEN' and 'OTHERWISE' keywords in
** a 'CASE' statement. In all cases, the keyword marks the end of one of
** more statements and a branch to another part of the code (the code
** after the ENDIF or ENDCASE) is needed.
** On entry, basicvars.current points at the token for the keyword. The
** offset is in the two bytes that follow. 'GET_DEST' differs from most
** of the other macros used to manipulate offsets and pointers in that
** the argument has to point at the offset and not at the byte before it.
** The reason for this is that there can be more than one offset in
** a row, for example, after an 'IF' token
*/
void exec_elsewhen(void) {
byte *p;
DEBUGFUNCMSGIN;
p = basicvars.current+1; /* Point at offset */
p = GET_DEST(p);
if (basicvars.traces.enabled) {
if (basicvars.traces.lines) trace_line(GET_LINENO(find_linestart(p)));
if (basicvars.traces.branches) trace_branch(basicvars.current, p);
}
basicvars.current = p;
DEBUGFUNCMSGOUT;
}
/*
** 'exec_xelse' deals with the first reference to an ELSE in a single line
** IF statement. It fills in the offset to reach the next line
*/
void exec_xelse(void) {
byte *p;
DEBUGFUNCMSGIN;
*basicvars.current = BASTOKEN_ELSE;
p = basicvars.current+1+OFFSIZE; /* Start at the token after the offset */
do
p = skip_token(p);
while (*p != asc_NUL);
p++; /* Point at start of next line */
set_dest(basicvars.current+1, FIND_EXEC(p));
exec_elsewhen();
DEBUGFUNCMSGOUT;
}
/*
** 'exec_xlhelse' deals with the first reference to an 'ELSE' that is part
** of a block 'IF' statement. It will be encountered after executing the
** statements following the 'THEN' part of the 'IF'. It locates the 'ENDIF'
** and fills in the offset to the next statement after the 'ENDIF' after
** the 'ELSE' token. Note that it is possible for there to be a statement
** on the same line as the 'ELSE' so the search starts there. The 'ENDIF'
** has to be the first token on a line.
*/
void exec_xlhelse(void) {
int32 depth;
byte *lp, *lp2;
DEBUGFUNCMSGIN;
lp = find_linestart(basicvars.current); /* Find the start of the line with the 'ELSE' */
lp2 = basicvars.current; /* Ensure the code won't find an ENDIF first thing */
depth = 1;
do { /* Look for the matching ENDIF */
if (*lp2 == BASTOKEN_ENDIF) depth--;
if (start_blockif(lp2)) depth++; /* Scan line to see if another block 'IF' starts in it */
if (depth == 0) break;
lp+=GET_LINELEN(lp);
if (AT_PROGEND(lp)) { /* No ENDIF found */
DEBUGFUNCMSGOUT;
error(ERR_ENDIF);
return;
}
lp2 = FIND_EXEC(lp);
} while (TRUE);
lp2++; /* Skip the ENDIF token */
if (*lp2 == asc_NUL) { /* There is nothing else on the line after the ENDIF */
lp2++; /* Move to start of next line */
if (basicvars.traces.lines) trace_line(GET_LINENO(lp2));
lp2 = FIND_EXEC(lp2); /* Find the first executable token */
}
*basicvars.current = BASTOKEN_LHELSE;
set_dest(basicvars.current+1, lp2); /* Set destination of branch */
exec_elsewhen();
DEBUGFUNCMSGOUT;
}
/*
** 'exec_end' executes an 'END' statement. The normal use of 'END'
** is to end a program, but under RISC OS the form 'END=<value>' can
** be used to extend the memory available to the program above the
** top of the Basic stack to give a user-controlled heap. In this
** version of the interpreter, this statement form is recognised
** but has no effect
**
** If being used to finish a program, all it does is return to the
** command loop after closing any open files (if that option is
** in effect)
*/
void exec_end(void) {
DEBUGFUNCMSGIN;
basicvars.current++; /* Skip END token */
if (*basicvars.current == '=') { /* Have got 'END=' version */
int32 newend = 0;
basicvars.current++;
expression();
check_ateol();
newend = pop_anynum32();
mos_setend(newend);
}
else { /* Normal 'END' statement */
check_ateol();
end_run();
}
DEBUGFUNCMSGOUT;
}
/*
** 'exec_endifcase' is called when an 'ENDCASE' or an 'ENDIF' statement
** is found. In the normal case of events these are skipped automatically
** by the 'WHEN' and 'ELSE' code but it is not an error if they are
** encountered during normal program execution. They act as 'no op's in
** this case
*/
void exec_endifcase(void) {
DEBUGFUNCMSGIN;
basicvars.current++; /* Skip token */
if (!ateol[*basicvars.current]) {
DEBUGFUNCMSGOUT;
error(ERR_SYNTAX);
return;
}
if (*basicvars.current == ':') basicvars.current++; /* Skip ':' */
if (*basicvars.current == asc_NUL) { /* Token is at end of line */
basicvars.current++; /* Move to start of next line */
if (basicvars.traces.lines) trace_line(GET_LINENO(basicvars.current));
basicvars.current = FIND_EXEC(basicvars.current);
}
DEBUGFUNCMSGOUT;
}
/*
** 'exec_endproc' deals with returns from procedures.
** The stack layout at this point should be:
** <stuff to chuck away> <local vars> <return block> <arguments>
** Everything here has to be discarded.
*/
void exec_endproc(void) {
fnprocinfo returnblock;
stackitem item;
DEBUGFUNCMSGIN;
basicvars.errorislocal = 0;
if (basicvars.procstack == NIL) { /* ENDPROC found outside a PROC */
DEBUGFUNCMSGOUT;
error(ERR_ENDPROC);
return;
}
item = stack_unwindlocal();
if (item == STACK_ERROR) basicvars.error_handler = pop_error();
if (GET_TOPITEM != STACK_PROC) empty_stack(STACK_PROC); /* Throw away unwanted entries on Basic stack */
returnblock = pop_proc(); /* Fetch return address and so forth */
if (returnblock.parmcount != 0) restore_parameters(returnblock.parmcount); /* Procedure had parameters - Restore old values */
if (basicvars.traces.enabled) {
if (basicvars.traces.procs) trace_proc(returnblock.fnprocname, FALSE);
if (basicvars.traces.branches) trace_branch(basicvars.current, returnblock.retaddr);
}
basicvars.current = returnblock.retaddr;
DEBUGFUNCMSGOUT;
}
/*
** 'exec_fnreturn' deals with function returns.
** One unfortunate point is that if the result is a string and the string
** came from a variable, that is, the type is STACK_STRING, it is necessary
** to make a copy of the string in case the string came from a local
** variable. The reason for this is that the local variable will be
** destroyed when the function returns and the string returned to the
** heap
*/
void exec_fnreturn(void) {
stackitem resultype, item;
int32 intresult = 0;
int64 int64result = 0;
uint8 uint8result = 0;
static float64 fpresult;
basicstring stresult = {0, NULL};
char *sp;
fnprocinfo returnblock;
DEBUGFUNCMSGIN;
basicvars.errorislocal = 0;
if (basicvars.procstack == NIL) { /* '=<expr>' found outside a FN */
DEBUGFUNCMSGOUT;
error(ERR_FNRETURN);
return;
}
basicvars.current++;
expression();
resultype = GET_TOPITEM;
if (resultype == STACK_INT) /* Pop result from stack and ensure type is legal */
intresult = pop_int();
else if (resultype == STACK_UINT8) /* Pop result from stack and ensure type is legal */
uint8result = pop_uint8();
else if (resultype == STACK_INT64)
int64result = pop_int64();
else if (resultype == STACK_FLOAT)
fpresult = pop_float();
else if (resultype == STACK_STRING) { /* Have to make a copy of the string for safety */
stresult = pop_string();
sp = alloc_string(stresult.stringlen);
if (stresult.stringlen != 0) memmove(sp, stresult.stringaddr, stresult.stringlen);
stresult.stringaddr = sp;
resultype = STACK_STRTEMP;
}
else if (resultype == STACK_STRTEMP)
stresult = pop_string();
else {
DEBUGFUNCMSGOUT;
error(ERR_VARNUMSTR);
return;
}
item = stack_unwindlocal();
if (item == STACK_ERROR) basicvars.error_handler = pop_error();
empty_stack(STACK_FN); /* Throw away unwanted entries on Basic stack */
returnblock = pop_fn(); /* Fetch return address and so forth */
if (returnblock.parmcount != 0) restore_parameters(returnblock.parmcount); /* Procedure had arguments - restore old values */
if (resultype == STACK_INT) { /* Lastly, put the result back on the stack */
push_int(intresult);
}
else if (resultype == STACK_UINT8) { /* Lastly, put the result back on the stack */
push_uint8(uint8result);
}
else if (resultype == STACK_INT64) {
push_int64(int64result);
}
else if (resultype == STACK_FLOAT) {
push_float(fpresult);
}
else if (resultype == STACK_STRING) {
push_string(stresult);
}
else if (resultype == STACK_STRTEMP) {
push_strtemp(stresult.stringlen, stresult.stringaddr);
}
if (basicvars.traces.enabled) {
if (basicvars.traces.procs) trace_proc(returnblock.fnprocname, FALSE);
if (basicvars.traces.branches) trace_branch(basicvars.current, returnblock.retaddr);
}
basicvars.current = returnblock.retaddr;
DEBUGFUNCMSGOUT;
}
/*
** 'exec_endwhile' deals with the ENDWHILE statement. It is in fact
** more important than the 'WHILE' in that whether to continue with
** the loop or to simply move on to the next statement is decided here.
**
** One point to note is that Basic V/VI allows loops to be nested
** incorrectly in that if one loop is not terminated within another,
** the inner one is automatically terminated by the outer one, for
** example:
** WHILE X%<10
** REPEAT ...
** ENDWHILE
** Here, the 'REPEAT' loop is automatically terminated when then
** 'ENDWHILE' is reached. The code mimics this behaviour (in
** 'get_while').
*/
void exec_endwhile(void) {
byte *tp;
stack_while *wp;
int64 result = 0;
DEBUGFUNCMSGIN;
tp = basicvars.current+1;
if (!ateol[*tp]) {
DEBUGFUNCMSGOUT;
error(ERR_SYNTAX);
return;
}
if (GET_TOPITEM == STACK_WHILE) /* WHILE control block is top of stack */
wp = basicvars.stacktop.whilesp;
else { /* Discard contents of stack as far as WHILE block */
wp = get_while();
}
if (wp == NIL) { /* Not in a WHILE loop */
DEBUGFUNCMSGOUT;
error(ERR_NOTWHILE);
return;
}
basicvars.current = wp->whilexpr;
expression();
result = pop_anynum64();
if (result != BASFALSE) { /* Condition still true - Continue with loop */
if (basicvars.traces.branches) trace_branch(tp, wp->whileaddr);
basicvars.current = wp->whileaddr;
}
else { /* Escape from loop - Remove WHILE control block from stack */
pop_while();
if (*tp == ':') tp++; /* Continue at next token after ENDWHILE */
if (*tp == asc_NUL) {
tp++; /* Move to the start of the next line */
if (basicvars.traces.lines) trace_line(GET_LINENO(tp));
tp = FIND_EXEC(tp); /* Skip to start of the executable tokens */
}
basicvars.current = tp;
}
DEBUGFUNCMSGOUT;
}
/*
** 'exec_error' deals with the 'ERROR' statement, which halts a
** program with a user-defined error.
** Note that the 'ERROR EXT' statement is not supported yet
*/
void exec_error(void) {
int32 errnumber;
stackitem stringtype;
basicstring descriptor;
char *errtext;
DEBUGFUNCMSGIN;
basicvars.current++; /* Skip the ERROR token*/
errnumber = eval_integer();
if (*basicvars.current != ',') { /* Comma missing */
DEBUGFUNCMSGOUT;
error(ERR_COMISS);
return;
}
basicvars.current++;
expression();
check_ateol();
stringtype = GET_TOPITEM;
if (stringtype != STACK_STRING && stringtype != STACK_STRTEMP) {
DEBUGFUNCMSGOUT;
error(ERR_TYPESTR);
return;
}
descriptor = pop_string();
errtext = tocstring(descriptor.stringaddr, descriptor.stringlen);
if (stringtype == STACK_STRTEMP) free_string(descriptor);
show_error(errnumber, errtext); /* Report the error */
DEBUGFUNCMSGOUT;
}
/*
** 'exec_exit' implements EXIT FOR, EXIT REPEAT and EXIT WHILE. These are
** based on extensions from Richard Russell's BB4W, BBCSDL, BBCTTY and
** BBCZ80 v5.
** One difference: EXIT FOR does not accept a control variable.
*/
void exec_exit(void) {
stack_for *fp;