-
Notifications
You must be signed in to change notification settings - Fork 1
/
interpret.cpp
1804 lines (1442 loc) · 33.8 KB
/
interpret.cpp
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
// awBASIC interpreter (c) 2002 Anthony J. Wood
// www.awsoftware.org
//
// This file is part of awbasic.
// awbasic 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 of the License, or
// (at your option) any later version.
#include <stdio.h>
#include <ctype.h> // isdigit
#include <malloc.h>
#include <memory.h> // memset
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include "basic.h"
void SkipWhiteCode(struct BasicCode *code)
{
while (isspace(*code->pc)) // skip white space (any control code or space)
code->pc++;
}
bool IsEosToken(unsigned char token)
{
return (token == 0 || token == ':' || token==TOK_ELSE); // else can terminate a THEN statement
}
void SkipToEOS(struct BasicCode *code)
{
while (!IsEosToken(*code->pc)) // skip to EOS
code->pc++;
}
unsigned char NextTokenOrChar(struct BasicCode *code)
{
while (isspace(*code->pc)) // skip white space
code->pc++;
return *code->pc;
}
int GotoLine(unsigned int linenum, struct BasicCode *code)
{
unsigned int next_addr = 0;
unsigned int this_ln, i;
while (TRUE)
{
i = next_addr;
next_addr = code->image[i] + (code->image[i+1]<<8);
if (next_addr == 0)
break;
this_ln = code->image[i+2] + (code->image[i+3]<<8);
if (this_ln == linenum)
{
code->pc = &code->image[i];
return ERR_NORMAL_EOL;
}
}
return ERR_MISSING_LN;
}
int NextAtoI(struct BasicCode *code)
{
int value = 0;
while (uisdigit(*code->pc))
{
value = value*10;
value = value + *code->pc-'0';
code->pc++;
}
return value;
}
int DoPrint(struct BasicCode *code)
{
unsigned int len;
int err;
unsigned char token;
struct TypedValue v, v2;
unsigned char b[256];
short sp;
bool suppress_newline = FALSE;
struct IoConsole *io;
/* determine which console to print to */
if (*(code->pc-1)==TOK_LPRINT)
{
if (code->print_con==NULL)
{
if (WinLprintMessage()) // Print to file? Msg
code->print_con=IoFileWriteOpen(NULL);
if (code->print_con==NULL)
code->print_con=IoNullOpen();
if (code->print_con==NULL)
return ERR_INTERNAL;
}
io = code->print_con;
}
else
{
io = code->std_con;
}
/*
* PRINT @ expression?
*/
token = NextTokenOrChar(code);
if (token=='@')
{
code->pc++; // skip @
err = EvalExpression(code->pc, &len, code, &v);
if (err!=ERR_OKAY)
return err;
code->pc = code->pc + len;
if (v.type_flags&VT_STRING)
{
free(v.value.string);
return ERR_TM;
}
else if (IsNumber(&v))
{
int i = IntVal(&v);
if (i < 0 || i >= io->io_width*io->io_height)
return ERR_FC;
io_set_cursor(io, i);
}
else
return ERR_SYNTAX;
if (IsEosToken(token=NextTokenOrChar(code))) /* L2 Basic handels "PRINT@256" by moving cursor then SN Error*/
return ERR_SYNTAX;
if (token!=',')
return ERR_SYNTAX;
code->pc++; /* skip comma */
}
/*
* PRINT USING has two forms:
* PRINT USING <STRING>;<NUMBER> e.g PRINT USING "##.#";3.4
* PRINT USING <STRING>;<STRING>;... e.g PRINT USING "!";"ABC";"DEF"
* currently only support the first one
*/
token = NextTokenOrChar(code);
if (NextTokenOrChar(code)==TOK_USING)
{
code->pc++;
err = EvalExpression(code->pc, &len, code, &v);
if (err!=ERR_OKAY)
return err;
code->pc = code->pc + len;
if (NextTokenOrChar(code)==';')
code->pc++;
err = EvalExpression(code->pc, &len, code, &v2);
if (err!=ERR_OKAY)
{
if (v.type_flags&VT_STRING)
free(v.value.string);
return err;
}
code->pc = code->pc + len;
if (v.type_flags&VT_STRING && IsNumber(&v2))
io_printusingnum(io, v.value.string, DoubleVal(&v2));
else
{
io_putstr(io, "? UNSUPORTED USE OF PRINT USING");
SkipToEOS(code);
}
if (v.type_flags&VT_STRING)
free(v.value.string);
if (v2.type_flags&VT_STRING)
free(v2.value.string);
token = NextTokenOrChar(code);
if (token!=';')
io_putc(io, '\n');
else
{
code->pc++;
token = NextTokenOrChar(code);
}
if (IsEosToken(token))
return ERR_OKAY;
else
return ERR_SYNTAX;
}
/*
* <expression | TAB> [; | ,] [<expression | TAB>] ...
* E.g: PRINT
* PRINT A;
* PRINT "x=";x
* PRINT "x="x
* PRINT @100,"fred"
* PRINT ATAB(5)B
* PRINT ;;;;;;A
* PRINT ,,A
*/
while (TRUE)
{
token = NextTokenOrChar(code);
if (IsEosToken(token))
{
if (!suppress_newline)
io_putc(io, '\n');
return ERR_OKAY;
}
suppress_newline = FALSE;
/*
* a comma advances to next 16 char print zone
*/
if (token==',')
{
code->pc++;
sp = 16-(io_get_cursor(io)%16);
for (int i=0; i < sp; i++)
io_putc(io, ' ');
suppress_newline = TRUE;
}
/*
* TAB(expression) moves the cursor on the current line to position <expression>
*/
else if (token==TOK_TAB)
{
code->pc++; /* Skip TAB( */
err = EvalExpression(code->pc, &len, code, &v);
if (err!=ERR_OKAY)
return err;
code->pc = code->pc + len;
if (v.type_flags&VT_STRING)
{
free(v.value.string);
return ERR_TM;
}
else if (IsNumber(&v))
{
short i = IntVal(&v);
if (i < 0 || i > 255)
return ERR_FC;
if (io->io_width>0)
sp = i-(io_get_cursor(io)%io->io_width);
else
sp = i-io_get_cursor(io);
for (i=0; i < sp; i++)
io_putc(io, ' ');
}
else
return ERR_SYNTAX;
if (NextTokenOrChar(code)!=')')
return ERR_SYNTAX;
code->pc++; // skip )
}
else if (token==';')
{
code->pc++;
suppress_newline = TRUE;
}
/*
* eval and print expression
*/
else
{
err = EvalExpression(code->pc, &len, code, &v);
if (err!=ERR_OKAY)
return err;
code->pc = code->pc + len;
if (v.type_flags&VT_STRING)
{
if (strlen(v.value.string) > sizeof(b)-1)
sprintf((char*)b, "<INTERNAL ERR: STRING TOO LONG>");
else
sprintf((char*)b, "%s", v.value.string);
free(v.value.string);
}
else if (IsNumber(&v))
{
if (v.type_flags==VT_DOUBLE)
sprintf((char*)b, "% .14g ", v.value.doubleval);
else
sprintf((char*)b, "% g ",FloatVal(&v));
}
else
return ERR_SYNTAX;
io_putstr(io, (char*)b);
}
}
}
int DoIf(struct BasicCode *code)
{
unsigned int len;
struct TypedValue r;
int err;
unsigned char token;
err = EvalExpression(code->pc, &len, code, &r);
if (err!=ERR_OKAY)
return err;
code->pc = code->pc + len;
if (r.type_flags&VT_STRING && r.value.string)
free (r.value.string);
if (!IsNumber(&r))
return ERR_TM;
if (IntVal(&r)!=0) // expression evaluated TRUE
{
token = NextTokenOrChar(code);
if (token == TOK_THEN)
{
code->pc++; // skip optional THEN
token = NextTokenOrChar(code);
}
if (uisdigit(token))
return GotoLine(NextAtoI(code), code);
else
return ERR_NORMAL_CONT; // resume execution on code at PC
}
// search for else (allow nested if-then-else)
short nest = 0;
while (*code->pc!=0x00) // Find end of line
{
if (*code->pc==TOK_IF)
nest++;
else if (*code->pc==TOK_ELSE)
{
if (nest == 0)
{
code->pc++; // skip ELSE token
token = NextTokenOrChar(code);
if (uisdigit(token)) // ELSE line_number
return GotoLine(NextAtoI(code), code);
else
return ERR_NORMAL_CONT; // resume execution on code at PC
}
nest--;
}
code->pc++;
}
return ERR_OKAY;
}
int DoRem(struct BasicCode *code)
{
while (*code->pc)
code->pc++; // Skip to end-of-line marker (NULL)
return ERR_OKAY;
}
int DoLet(struct BasicCode *code, VariableValue** varptr, unsigned char* typeptr)
{
int err;
unsigned int len;
unsigned char type;
struct TypedValue vval;
VariableValue* vlocptr;
unsigned char token;
/*
* Decode variable name
*/
err = ParseVariable(code, code->pc, &vlocptr, &type, &len);
if (err!=ERR_OKAY)
return err;
code->pc+= len;
if (varptr)
*varptr = vlocptr;
if (typeptr)
*typeptr = type;
/*
* Make sure equal sign is next: LET A1 = EXPRESSION
*/
token = NextTokenOrChar(code);
if (token!=TOK_EQ)
return ERR_SYNTAX;
code->pc++; // skip over equal sign
/*
* evaluate expression & Set variable
*/
err = EvalExpression(code->pc, &len, code, &vval);
if (err!=ERR_OKAY)
return err;
code->pc+=len;
if (IsNumberT(type) && IsNumber(&vval))
ConvertType(&vval, type);
else if (type!=vval.type_flags) // one number, one string
return ERR_TM;
SetVariable(vlocptr, vval.value, type);
return ERR_OKAY;
}
int DoGoto(struct BasicCode *code)
{
unsigned char token;
token = NextTokenOrChar(code);
if (uisdigit(token))
return GotoLine(NextAtoI(code), code);
else
return ERR_SYNTAX;
}
int GosubLine(unsigned int linenum, struct BasicCode *code)
{
struct StackFrame frame;
// SkipWhiteCode(code); Is the right command here, but it detects syntax errors that Real L2 TRS Basic doesn't
SkipToEOS(code);
frame.type = TOK_GOSUB;
frame.gosub_frame.return_pc=code->pc;
frame.gosub_frame.current_line_start_pc=code->current_line_start_pc;
frame.gosub_frame.current_statement_start_pc=code->current_statement_start_pc;
if (!PushStack(&code->stack, &frame))
return ERR_OUTOFMEM;
return GotoLine(linenum, code);
}
int DoGosub(struct BasicCode *code)
{
unsigned char token;
int dest;
token = NextTokenOrChar(code);
if (!uisdigit(token))
return ERR_SYNTAX;
dest = NextAtoI(code);
return GosubLine(dest, code);
}
/*
* GOSUB RETURN
* Pop the return value of the stack. Unwind any FOR stack entries
* until a gosub return is found.
*/
int DoReturn(struct BasicCode *code)
{
StackFrame *frame;
do
{
if (!PopStack(&code->stack, &frame))
return ERR_RG;
} while (frame->type!=TOK_GOSUB);
code->pc=frame->gosub_frame.return_pc;
code->current_line_start_pc=frame->gosub_frame.current_line_start_pc;
code->current_statement_start_pc=frame->gosub_frame.current_statement_start_pc;
return ERR_OKAY;
}
int DoFor(struct BasicCode *code)
{
struct StackFrame frame;
unsigned char token;
unsigned int len;
int err;
struct TypedValue r;
/*
* Start to build the FOR stack frame
* First, set the variable index & assign the variable name
*/
err=DoLet(code, &frame.for_frame.variable, &frame.for_frame.type_flags);
if (err!=ERR_OKAY)
return err;
/*
* TO should be next, check for it then set the 'to' frame value
*/
token = NextTokenOrChar(code);
if (token!=TOK_TO)
return ERR_SYNTAX;
code->pc++; // skip TO
err = EvalExpression(code->pc, &len, code, &r);
if (err!=ERR_OKAY)
return err;
code->pc = code->pc + len;
if (r.type_flags&VT_STRING && r.value.string)
free (r.value.string);
if (!IsNumber(&r))
return ERR_TM;
ConvertType(&r, frame.for_frame.type_flags);
CopyVariableValue(&frame.for_frame.to_val, &r.value);
/*
* Check for optional STEP. Set step value
*/
r.type_flags=VT_INT; // default step is 1
r.value.intval=1;
token = NextTokenOrChar(code);
if (token==TOK_STEP)
{
code->pc++; // skip STEP
err = EvalExpression(code->pc, &len, code, &r);
if (err!=ERR_OKAY)
return err;
code->pc = code->pc + len;
if (r.type_flags&VT_STRING && r.value.string)
free (r.value.string);
if (!IsNumber(&r))
return ERR_TM;
}
ConvertType(&r, frame.for_frame.type_flags);
CopyVariableValue(&frame.for_frame.step_val, &r.value);
SkipWhiteCode(code);
frame.for_frame.start_pc = code->pc;
/*
* FOR frame is built, push it on the stack.
* If this FOR is already on the stack (in use), then reset stack to that point.
* This handles cases like:
* 10 For a=1 to 20
* 20 For b=1 to 20
* 30 IF x=1 then goto 10
* 40 next b:next a
*/
unsigned int frame_index;
frame.type=TOK_FOR;
frame.for_frame.current_line_start_pc=code->current_line_start_pc;
frame.for_frame.current_statement_start_pc=code->current_statement_start_pc;
if (SearchForStack(&code->stack, frame.for_frame.variable, &frame_index))
UnwindStack(&code->stack, frame_index);
PushStack(&code->stack, &frame);
return ERR_OKAY;
}
/*
* Handle: "NEXT" or "NEXT X" or "NEXT X,Y,Z"
* if a variable is given, and the last FOR frame pushed on the stack doesn't match,
* then the stack is unwound while looking for a FOR that matches.
* EG:
* FOR X=1 to 5
* FOR Y=1 to 10
* NEXT X
* OR
* FOR X=1 to 5
* FOR Y=1 to 10
* IF Y < 3 THEN NEXT Y
* NEXT X
*
* both are legal.
*/
int DoNext(struct BasicCode *code)
{
unsigned char token;
VariableValue* value;
unsigned char type;
bool is_var;
bool isdone;
StackFrame* frame;
int err;
unsigned int i;
token = NextTokenOrChar(code);
if (isalpha(token)) // NEXT with no variable.
{
is_var = TRUE;
}
else
{
is_var = FALSE;
if (!IsEosToken(token))
return ERR_SYNTAX;
}
while (TRUE)
{
if (is_var)
{
err=ParseVariable(code, code->pc, &value, &type, &i);
if (err!=ERR_OKAY)
return err;
code->pc+= i;
}
while (TRUE) {
if (!PeekStack(&code->stack, &frame))
return ERR_NF;
if (frame->type!=TOK_FOR)
return ERR_NF;
if (!is_var)
break;
if (frame->for_frame.variable==value)
break;
if (!PopStack(&code->stack, 0)) // Next with wrong variable type uwinds stack, looking for correct kind
return ERR_NF;
}
is_var = TRUE;
if (frame->for_frame.type_flags&VT_INT)
{
frame->for_frame.variable->intval+= frame->for_frame.step_val.intval;
if (frame->for_frame.step_val.intval > 0)
isdone = frame->for_frame.variable->intval > frame->for_frame.to_val.intval;
else
isdone = frame->for_frame.variable->intval < frame->for_frame.to_val.intval;
}
else if (frame->for_frame.type_flags&VT_FLOAT)
{
frame->for_frame.variable->floatval+= frame->for_frame.step_val.floatval;
if (frame->for_frame.step_val.floatval > 0)
isdone = frame->for_frame.variable->floatval > frame->for_frame.to_val.floatval;
else
isdone = frame->for_frame.variable->floatval < frame->for_frame.to_val.floatval;
}
else if (frame->for_frame.type_flags&VT_DOUBLE)
{
frame->for_frame.variable->doubleval+= frame->for_frame.step_val.doubleval;
if (frame->for_frame.step_val.doubleval > 0)
isdone = frame->for_frame.variable->doubleval > frame->for_frame.to_val.doubleval;
else
isdone = frame->for_frame.variable->doubleval < frame->for_frame.to_val.doubleval;
}
else return ERR_INTERNAL; // should never happen
if (isdone)
PopStack(&code->stack, &frame);
else
{
code->pc=frame->for_frame.start_pc;
code->current_line_start_pc=frame->for_frame.current_line_start_pc;
code->current_statement_start_pc=frame->for_frame.current_statement_start_pc;
return ERR_OKAY;
}
token = NextTokenOrChar(code);
if (token==',')
{
code->pc++;
token = NextTokenOrChar(code);
}
else
{
if (IsEosToken(token))
return ERR_OKAY;
else
return ERR_SYNTAX;
}
}
}
int DoDim(struct BasicCode *code)
{
unsigned char token;
unsigned char type;
unsigned short label;
unsigned int i;
unsigned int numdims;
unsigned int* dimsizes;
unsigned int* ap;
unsigned int allocsize;
int err;
while (TRUE)
{
/*
* get variable label that will be an array
*/
err=SimpleParseVariable(code->deftab, code->pc, &type, &label, &i);
if (err!=ERR_OKAY)
return err;
code->pc+=i;
/*
* make sure a ( is next
*/
token = NextTokenOrChar(code);
if (token!='(')
return ERR_SYNTAX;
code->pc++;
/*
* determine the number of array dimensions (allow multi-dimensional arrays)
* and value of each index
*/
err = CalcIndexValues(code, code->pc, &numdims, &dimsizes, &i);
if (err!=ERR_OKAY)
return err;
code->pc+=i;
/*
* create array stucture
* int 0: number of dimensions (n)
* int 1..n: size for each dimension
* n X VariableValue's
*/
allocsize = 0;
for (i=0; i <numdims; i++)
allocsize = allocsize*(dimsizes[i]+1) + (dimsizes[i]+1);
allocsize*=sizeof(union VariableValue);
allocsize+=(1+numdims)*sizeof(int);
ap=(unsigned int*)malloc(allocsize);
if (ap==0)
return ERR_OUTOFMEM;
memset(ap, 0, allocsize);
ap[0]=numdims;
for (i=0; i <numdims; i++)
ap[i+1]=dimsizes[i]+1; // range is zero -> dim value, inclusive (1+dim value)
free(dimsizes);
/*
* create the array variable entry
*/
err = CreateArray(code->variables, label, type, ap);
if (err!=ERR_OKAY)
return err;
/*
* A , indicates another array to be dimensioned
*/
token = NextTokenOrChar(code);
if (token!=',')
break;
code->pc++; // skip comma
}
return ERR_OKAY;
}
/*
* Used by input and READ to parse a user entered string
* If the string is surrounded by quotes, it may contain any characters.
* If it has no quotes, it is terminated by a return (null) or a comma
*/
int ParseInputString(const unsigned char* expr, struct TypedValue* v, unsigned int* len)
{
const unsigned char* expr_start = expr;
unsigned short k;
if (*expr == '\"')
return ParseStringLiteral(expr, v, len);
k=0;
while (expr[k] && expr[k]!=',')
k++;
v->value.string = (char*)malloc(k+1);
if (v->value.string == 0)
return ERR_OS;
strncpy(v->value.string, (char*)expr, k);
v->value.string[k] = 0;
if (len)
*len = k;
v->type_flags = VT_STRING;
return ERR_OKAY;
}
int DoInput(struct BasicCode *code)
{
unsigned char token;
char buffer[160];
int err, bi;
unsigned int len;
unsigned char type;
struct TypedValue vval;
VariableValue* vlocptr;
/*
* INPUT "optional quote";<variable> [, <variable>] ...
*/
token = NextTokenOrChar(code);
if (token=='\"')
{
while (TRUE)
{
token = *(++code->pc);
if (token == '\"' || token==NULL)
break;
io_putc(code->std_con, token);
}
if (token!='\"')
return ERR_SYNTAX;
++code->pc; // skip end quote
token = NextTokenOrChar(code);
if (token!=';')
return ERR_SYNTAX;
code->pc++; // skip semicolon
}
io_putstr(code->std_con, "? ");
if (io_gets(code->std_con, buffer)==0) // the only error currently supported in gets is break triggered during gets
return ERR_BREAK;
bi =0;
while (TRUE)
{
/*
* Decode variable name
*/
err = ParseVariable(code, code->pc, &vlocptr, &type, &len);
if (err!=ERR_OKAY)
return err;
code->pc+= len;
/*
* Parse user input and then Set variable value
*/
do
{
if (IsNumberT(type))
{
if (buffer[bi]==0) // allow user to press return with no number entered for zero
{
vval.type_flags=VT_INT;
vval.value.intval=0;
err=ERR_OKAY;
len =0;
}
else
err = ParseNumberFull((unsigned char*)&buffer[bi], &vval, &len, type==VT_DOUBLE);
}
else
err = ParseInputString((unsigned char*)&buffer[bi], &vval, &len);
if (err!=ERR_OKAY)
{
io_putstr(code->std_con, "?REDO\n? ");
if (io_gets(code->std_con, buffer)==0) // the only error currently supported in gets is break triggered during gets
return ERR_BREAK;
bi = 0;
}
} while (err!=ERR_OKAY);
bi+=len;
if (IsNumberT(type) && IsNumber(&vval))
ConvertType(&vval, type);
else if (type!=vval.type_flags) // one number, one string
return ERR_INTERNAL; // shouldn't happen
if (type==VT_STRING) // TRS80 L2 Compatibility Hack: If user press return on string input, keep string var the same
if (strlen(vval.value.string)==0)
{
free(vval.value.string);
vval.value.string=NULL;
}
if (!(type==VT_STRING && vval.value.string==NULL))
SetVariable(vlocptr, vval.value, type);
/*
* More variables to INPUT?
*/
token = NextTokenOrChar(code);
if (token!=',')
break;
code->pc++; // skip over equal sign
if (buffer[bi]==0)
{
io_putstr(code->std_con, " ?? ");
if (io_gets(code->std_con, buffer)==0) // the only error currently supported in gets is break triggered during gets
return ERR_BREAK;
bi=0;
}
else
bi++;
}
if (buffer[bi]!=0)
io_putstr(code->std_con, "?EXTRA IGNORED\n");
SkipToEOS(code); // For copatibility with L2 BASIC: Allows garbage to terminate the input statement.
// EG, INPUT PP; Is tolerated.
return ERR_OKAY;
}
int DoClear(struct BasicCode *code)
{
BasicClear(code);
SkipToEOS(code);
return ERR_OKAY;
}
int DoCls(struct BasicCode *code)
{
io_cls(code->std_con);
return ERR_OKAY;
}
int DoDef(struct BasicCode* code, unsigned char type)
{
unsigned char a,b;