-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathiostate.c
2206 lines (2106 loc) · 61.5 KB
/
iostate.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.
**
** Fix for format problem in print_screen() was supplied by
** Mark de Wilde
**
** This file contains the Basic I/O statements
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "common.h"
#include "target.h"
#include "basicdefs.h"
#include "tokens.h"
#include "stack.h"
#include "strings.h"
#include "errors.h"
#include "miscprocs.h"
#include "evaluate.h"
#include "convert.h"
#include "mos.h"
#include "fileio.h"
#include "screen.h"
#include "lvalue.h"
#include "statement.h"
#include "iostate.h"
/* #define DEBUG */
/*
** 'fn_spc' emulates the 'SPC' function. It leaves 'current' pointing
** at the character after the function's operand
*/
static void fn_spc(void) {
int32 count;
DEBUGFUNCMSGIN;
count = eval_intfactor();
if (count > 0) {
count = count & BYTEMASK; /* Basic V/VI only uses the low-order byte of the value */
basicvars.printcount+=count;
#if !defined(USE_SDL) && !defined(TARGET_RISCOS)
echo_off();
#endif
while (count > 0) {
emulate_vdu(' ');
count--;
}
#if !defined(USE_SDL) && !defined(TARGET_RISCOS)
echo_on();
#endif
}
DEBUGFUNCMSGOUT;
}
/*
** 'fn_tab' deals with the Basic 'TAB' function
** The function returns a pointer to the character after the ')'
** at the end of the function call
*/
static void fn_tab(void) {
int32 x, y;
DEBUGFUNCMSGIN;
x = eval_integer();
if (*basicvars.current == ')') { /* 'TAB(x)' form of function */
if (x > 0) { /* Nothing happens is 'tab' count is less than 0 */
x = x & BYTEMASK;
if (x < basicvars.printcount) { /* Tab position is to left of current cursor position */
emulate_newline();
basicvars.printcount = 0;
}
x = x-basicvars.printcount; /* figure out how many blanks to print */
basicvars.printcount+=x;
#if !defined(USE_SDL) && !defined(TARGET_RISCOS)
echo_off();
#endif
while (x > 0) { /* Print enough blanks to reach tab position */
emulate_vdu(' ');
x--;
}
#if !defined(USE_SDL) && !defined(TARGET_RISCOS)
echo_on();
#endif
}
}
else if (*basicvars.current == ',') { /* 'TAB(x,y)' form of function */
basicvars.current++;
y = eval_integer();
if (*basicvars.current != ')') {
error(ERR_RPMISS);
return;
}
emulate_tab(x, y);
}
else { /* Error - ',' or ')' needed */
DEBUGFUNCMSGOUT;
error(ERR_CORPNEXT);
return;
}
basicvars.current++; /* Skip the ')' */
DEBUGFUNCMSGOUT;
}
/*
** 'input_number' reads a number from the string starting at 'p'
** and stores it at the location given by 'destination'. It
** returns a pointer to the start of the next field or NIL if an
** error occured (to allow the calling function to try again)
*/
static char *input_number(lvalue destination, char *p) {
boolean isint;
int32 intvalue;
int64 int64value;
static float64 fpvalue;
DEBUGFUNCMSGIN;
p = tonumber(p, &isint, &intvalue, &int64value, &fpvalue);
if (p == NIL) return NIL; /* 'tonumber' hit an error - return to caller */
while (*p != asc_NUL && *p != ',') p++; /* Find the end of the field */
if (*p == ',') p++; /* Move to start of next field */
switch (destination.typeinfo) {
case VAR_INTWORD: /* Normal integer variable */
*destination.address.intaddr = isint ? intvalue : TOINT(fpvalue);
break;
case VAR_UINT8: /* unsigned 8-bit integer variable */
*destination.address.uint8addr = isint ? intvalue : TOINT(fpvalue);
break;
case VAR_INTLONG: /* 64-bit integer variable */
*destination.address.int64addr = isint ? int64value : TOINT64(fpvalue);
break;
case VAR_FLOAT: /* Normal floating point variable */
*destination.address.floataddr = isint ? TOFLOAT(intvalue) : fpvalue;
break;
case VAR_INTBYTEPTR: /* Indirect reference to byte-sized integer */
basicvars.memory[destination.address.offset] = isint ? intvalue : TOINT(fpvalue);
break;
case VAR_INTWORDPTR: /* Indirect reference to word-sized integer */
store_integer(destination.address.offset, isint ? intvalue : TOINT(fpvalue));
break;
case VAR_FLOATPTR: /* Indirect reference to floating point value */
store_float(destination.address.offset, isint ? TOFLOAT(intvalue) : fpvalue);
break;
}
DEBUGFUNCMSGOUT;
return p;
}
/*
** 'input_string' reads a character string from the line starting
** at 'p' and storing it at the location given by 'dest'. It returns
** a pointer to the start of the next field or NIL if an error is
** detected. IF 'inputall' is TRUE then all of the line from 'p'
** to the terminating null, including preceding and trailing blanks,
** is used (this is used for 'INPUT LINE')
** Note that the string buffer only has to be large enough to hold
** the number of characters that can be fitted on the command line.
*/
static char *input_string(lvalue destination, char *p, boolean inputall) {
char *cp, tempstring[INPUTLEN+1];
int32 index;
DEBUGFUNCMSGIN;
index = 0;
if (inputall) { /* Want everything up to the end of line */
while (*p != asc_NUL) {
if (index == MAXSTRING) {
error(ERR_STRINGLEN);
return NULL;
}
tempstring[index] = *p;
index++;
p++;
}
}
else { /* Only want text as far as next delimiter */
p = skip_blanks(p);
if (*p == '\"') { /* Want string up to next double quote */
boolean more;
p++;
more = *p != asc_NUL;
while (more) {
if (*p == '\"') { /* Found a '"'. See if it is followed by another one */
p++;
more = *p == '\"'; /* Continue if '""' found else stop */
}
if (more) {
if (index == MAXSTRING) {
DEBUGFUNCMSGOUT;
error(ERR_STRINGLEN);
return NULL;
}
tempstring[index] = *p;
index++;
p++;
if (*p == asc_NUL) {
DEBUGFUNCMSGOUT;
error(WARN_QUOTEMISS);
}
}
}
}
else { /* Normal string */
while (*p != asc_NUL && *p != ',') {
if (index == MAXSTRING) {
DEBUGFUNCMSGOUT;
error(ERR_STRINGLEN);
return NULL;
}
tempstring[index] = *p;
index++;
p++;
}
}
while (*p != asc_NUL && *p != ',') p++;
if (*p == ',') p++;
}
if (destination.typeinfo == VAR_STRINGDOL) { /* Normal string variable */
free_string(*destination.address.straddr);
cp = alloc_string(index);
memmove(cp, tempstring, index);
destination.address.straddr->stringlen = index;
destination.address.straddr->stringaddr = cp;
}
else { /* '$<addr>' variety of string */
tempstring[index] = asc_CR;
memmove(&basicvars.memory[destination.address.offset], tempstring, index+1);
}
DEBUGFUNCMSGOUT;
return p;
}
/*
** 'read_input' is the function called to deal with both 'INPUT' and
** 'INPUT LINE' statements. 'inputline' is set to 'TRUE' if dealing
** with 'INPUT LINE' (where the value for each variable to be read
** is taken from a new line)
*/
static void read_input(boolean inputline) {
char *cp, line[INPUTLEN];
lvalue destination;
boolean bad;
int n, length;
DEBUGFUNCMSGIN;
do { /* Loop around prompts and items to read */
boolean prompted = FALSE;
byte token;
while (*basicvars.current == ',' || *basicvars.current == ';') basicvars.current++;
token = *basicvars.current;
line [0] = asc_NUL;
/* Deal with prompt */
while (token == BASTOKEN_STRINGCON || token == BASTOKEN_QSTRINGCON || token == '\'' || token == TYPE_PRINTFN) {
prompted = TRUE;
switch(token) {
case BASTOKEN_STRINGCON: /* Got a prompt string */
length =GET_SIZE(basicvars.current+1+OFFSIZE);
if (length>0) emulate_vdustr(TOSTRING(GET_SRCADDR(basicvars.current)), length);
basicvars.current = skip_token(basicvars.current);
break;
case BASTOKEN_QSTRINGCON: /* Prompt string with '""' in it */
cp = TOSTRING(GET_SRCADDR(basicvars.current));
length = GET_SIZE(basicvars.current+1+OFFSIZE);
for (n=0; n<length; n++) {
emulate_vdu(*cp);
if (*cp == '"') cp++; /* Print only '"' when string contains '""' */
cp++;
}
basicvars.current = skip_token(basicvars.current);
break;
case '\'': /* Got a "'" - Skip to new line */
emulate_newline();
basicvars.current++;
break;
case TYPE_PRINTFN: /* 'SPC()' and 'TAB()' */
switch (*(basicvars.current+1)) {
case BASTOKEN_SPC:
basicvars.current+=2; /* Skip two byte function token */
fn_spc();
break;
case BASTOKEN_TAB:
basicvars.current+=2; /* Skip two byte function token */
fn_tab();
break;
default:
bad_token();
}
}
while (*basicvars.current == ',' || *basicvars.current == ';') { /* An arbitrary number of these can appear here */
prompted = FALSE;
basicvars.current++;
}
token = *basicvars.current;
}
cp = &line[0]; /* Point at start of input buffer */
/* This points at a NUL here */
/* Now go through all the variables listed and attempt to assign values to them */
while (!ateol[*basicvars.current]
&& *basicvars.current != BASTOKEN_STRINGCON && *basicvars.current != BASTOKEN_QSTRINGCON
&& *basicvars.current != '\'' && *basicvars.current != TYPE_PRINTFN) {
get_lvalue(&destination);
if (*cp == asc_NUL) { /* There be nowt left to read on the line */
if (!prompted) emulate_vdu('?');
prompted = FALSE;
if (!read_line(line, INPUTLEN)) {
DEBUGFUNCMSGOUT;
error(ERR_ESCAPE);
return;
}
cp = &line[0];
}
switch (destination.typeinfo) {
case VAR_INTWORD: case VAR_UINT8: case VAR_INTLONG: case VAR_FLOAT: /* Numeric items */
case VAR_INTBYTEPTR: case VAR_INTWORDPTR: case VAR_FLOATPTR:
do {
cp = input_number(destination, cp); /* Try to read a number */
bad = cp == NIL;
if (bad) { /* Hit an error - Try again */
emulate_vdu('?');
if (!read_line(line, INPUTLEN)) {
DEBUGFUNCMSGOUT;
error(ERR_ESCAPE);
}
cp = &line[0];
}
} while (bad);
break;
case VAR_STRINGDOL: case VAR_DOLSTRPTR:
do {
cp = input_string(destination, cp, inputline);
bad = cp == NIL;
if (bad) { /* Hit an error - Try again */
emulate_vdu('?');
if (!read_line(line, INPUTLEN)) {
DEBUGFUNCMSGOUT;
error(ERR_ESCAPE);
return;
}
cp = &line[0];
}
} while (bad);
break;
default:
DEBUGFUNCMSGOUT;
error(ERR_VARNUMSTR); /* Numeric or string variable required */
return;
}
while (*basicvars.current == ',' || *basicvars.current == ';') basicvars.current++;
if (inputline) { /* Signal that another line is required for 'INPUT LINE' */
line[0] = asc_NUL;
cp = &line[0];
}
}
} while (!ateol[*basicvars.current]);
basicvars.printcount = 0; /* Line will have been ended by a newline */
DEBUGFUNCMSGOUT;
}
/*
** 'exec_beats' handles the Basic statement 'BEATS'
*/
void exec_beats(void) {
int32 beats;
DEBUGFUNCMSGIN;
basicvars.current++;
beats = eval_integer();
check_ateol();
mos_wrbeat(beats);
DEBUGFUNCMSGOUT;
}
/*
** 'exec_bput' deals with the 'BPUT' statement
** This is an extended version of the statement that allows a
** number of values to be output at a time
*/
void exec_bput(void) {
int32 handle;
stackitem stringtype;
basicstring descriptor;
DEBUGFUNCMSGIN;
basicvars.current++; /* Skip BPUT token */
if (*basicvars.current != '#') {
DEBUGFUNCMSGOUT;
error(ERR_HASHMISS);
return;
}
basicvars.current++;
handle = eval_intfactor(); /* Get the file handle */
if (*basicvars.current != ',') {
DEBUGFUNCMSGOUT;
error(ERR_COMISS);
return;
}
basicvars.current++;
do {
expression(); /* Now fetch the value to be written */
switch (GET_TOPITEM) {
case STACK_INT: case STACK_UINT8: case STACK_INT64: case STACK_FLOAT:
fileio_bput(handle, pop_anynum32());
break;
case STACK_STRING: case STACK_STRTEMP:
stringtype = GET_TOPITEM;
descriptor = pop_string();
fileio_bputstr(handle, descriptor.stringaddr, descriptor.stringlen);
/* If string is last item on line, output a newline as well */
if (ateol[*basicvars.current]) fileio_bput(handle, '\n');
if (stringtype == STACK_STRTEMP) free_string(descriptor);
break;
default: /* Item is neither a number nor a string */
DEBUGFUNCMSGOUT;
error(ERR_VARNUMSTR);
return;
}
if (*basicvars.current == ',') /* Anything more to come? */
basicvars.current++; /* Yes */
else if (*basicvars.current == ';') {
basicvars.current++;
if (ateol[*basicvars.current]) break; /* Nothing after ';' - End of statement */
}
else if (ateol[*basicvars.current]) /* Anything else - Check for end of statement */
break;
else {
DEBUGFUNCMSGOUT;
error(ERR_SYNTAX);
return;
}
} while (TRUE);
DEBUGFUNCMSGOUT;
}
/*
** 'exec_circle' deals with the Basic statement 'CIRCLE'
*/
void exec_circle(void) {
int32 x, y, radius;
boolean filled;
DEBUGFUNCMSGIN;
basicvars.current++; /* Skip CIRCLE token */
filled = *basicvars.current == BASTOKEN_FILL;
if (filled) basicvars.current++;
x = eval_integer(); /* Get x coordinate of centre */
if (*basicvars.current != ',') {
DEBUGFUNCMSGOUT;
error(ERR_COMISS);
return;
}
basicvars.current++;
y = eval_integer(); /* Get y coordinate of centre */
if (*basicvars.current != ',') {
DEBUGFUNCMSGOUT;
error(ERR_COMISS);
return;
}
basicvars.current++;
radius = eval_integer(); /* Get radius of circle */
check_ateol();
emulate_circle(x, y, radius, filled);
DEBUGFUNCMSGOUT;
}
/*
** 'exec_clg' handles the Basic statement 'CLG'
*/
void exec_clg(void) {
DEBUGFUNCMSGIN;
basicvars.current++;
check_ateol();
emulate_vdu(VDU_CLEARGRAPH);
DEBUGFUNCMSGOUT;
}
/*
** 'exec_close' handles the 'CLOSE' statement.
*/
void exec_close(void) {
int32 handle;
DEBUGFUNCMSGIN;
basicvars.current++; /* Skip CLOSE token */
if (*basicvars.current != '#') {
DEBUGFUNCMSGOUT;
error(ERR_HASHMISS);
return;
}
basicvars.current++;
expression(); /* Get the file handle */
check_ateol();
handle = pop_anynum32();
fileio_close(handle);
DEBUGFUNCMSGOUT;
}
/*
** 'exec_cls' clears the screen. How it does this is an operating
** system-dependent.
*/
void exec_cls(void) {
DEBUGFUNCMSGIN;
basicvars.current++;
check_ateol();
emulate_vdu(VDU_CLEARTEXT);
basicvars.printcount = 0;
DEBUGFUNCMSGOUT;
}
/*
** exec_colofon - Handle new style 'COLOUR OF' statement
*/
static void exec_colofon(void) {
int32 red = 0, green = 0, blue = 0, backred = 0;
int32 backgreen = 0, backblue = 0, form = 0;
/*
** form says what the statement contains:
** Bit 0: Foreground 0 = colour number, 1 = RGB value
** Bit 1: Background 0 = colour number, 1 = RGB value
** Bit 2: 1 = Change foreground
** Bit 3: 1 = Change background
*/
DEBUGFUNCMSGIN;
if (*basicvars.current == BASTOKEN_OF) {
basicvars.current++; /* Skip OF */
form += 4;
red = eval_integer();
if (*basicvars.current == ',') {
form += 1;
basicvars.current++;
green = eval_integer();
if (*basicvars.current != ',') {
DEBUGFUNCMSGOUT;
error(ERR_COMISS);
return;
}
basicvars.current++;
blue = eval_integer();
}
}
if (*basicvars.current == BASTOKEN_ON) { /* COLOUR OF ... ON */
basicvars.current++;
form += 8;
backred = eval_integer();
if (*basicvars.current == ',') {
form += 2;
basicvars.current++;
backgreen = eval_integer();
if (*basicvars.current != ',') {
DEBUGFUNCMSGOUT;
error(ERR_COMISS);
return;
}
basicvars.current++;
backblue = eval_integer();
}
}
check_ateol();
if ((form & 4) != 0) { /* Set foreground colour */
if ((form & 1) != 0) /*Set foreground RGB value */
emulate_setcolour(FALSE, red, green, blue);
else {
emulate_setcolnum(FALSE, red);
}
}
if ((form & 8) != 0) { /* Set background colour */
if ((form & 2) != 0) /*Set background RGB value */
emulate_setcolour(TRUE, backred, backgreen, backblue);
else {
emulate_setcolnum(TRUE, backred);
}
}
DEBUGFUNCMSGOUT;
}
/*
** exec_colnum - Handle old style COLOUR statement
*/
static void exec_colnum(void) {
int32 colour, tint, parm2;
DEBUGFUNCMSGIN;
colour = eval_integer();
switch (*basicvars.current) {
case BASTOKEN_TINT: /* Got 'COLOUR ... TINT' */
basicvars.current++;
tint = eval_integer();
check_ateol();
emulate_colourtint(colour, tint);
break;
case ',': /* Got 'COLOUR <n>,...' */
basicvars.current++;
parm2 = eval_integer(); /* Assume 'COLOUR <colour>,<physical colour>' */
if (*basicvars.current != ',') {
check_ateol();
emulate_mapcolour(colour, parm2);
} else { /* Have got at least three parameters */
int32 parm3;
basicvars.current++;
parm3 = eval_integer(); /* Assume 'COLOUR <red>,<green>,<blue>' */
if (*basicvars.current != ',') {
check_ateol();
emulate_setcolour(FALSE, colour, parm2, parm3);
} else {
int32 parm4;
basicvars.current++;
parm4 = eval_integer(); /* Assume 'COLOUR <colour>,<red>,<green>,<blue> */
check_ateol();
emulate_defcolour(colour, parm2, parm3, parm4);
}
}
break;
default:
check_ateol();
emulate_vdu(VDU_TEXTCOL);
emulate_vdu(colour);
}
DEBUGFUNCMSGOUT;
}
/*
** 'exec_colour' deals with the Basic statement 'COLOUR'
*/
void exec_colour(void) {
DEBUGFUNCMSGIN;
basicvars.current++;
if (*basicvars.current == BASTOKEN_OF || *basicvars.current == BASTOKEN_ON)
exec_colofon();
else {
exec_colnum();
}
DEBUGFUNCMSGOUT;
}
/*
** 'exec_draw' handles the Basic 'DRAW [BY]' statement
*/
void exec_draw(void) {
int32 x, y;
int32 plotcode = DRAW_SOLIDLINE+DRAW_ABSOLUTE;
DEBUGFUNCMSGIN;
basicvars.current++;
if (*basicvars.current == BASTOKEN_BY) {
basicvars.current++;
plotcode = DRAW_SOLIDLINE+DRAW_RELATIVE;
}
x = eval_integer(); /* Get x coordinate of end point */
if (*basicvars.current != ',') {
DEBUGFUNCMSGOUT;
error(ERR_COMISS);
return;
}
basicvars.current++;
y = eval_integer(); /* Get y coordinate of end point */
check_ateol();
emulate_plot(plotcode, x, y);
DEBUGFUNCMSGOUT;
}
/*
** 'exec_ellipse' is called to deal with the Basic 'ELLIPSE' statement
*/
void exec_ellipse(void) {
int32 x, y, majorlen, minorlen;
static float64 angle;
boolean isfilled;
DEBUGFUNCMSGIN;
basicvars.current++; /* Skip ELLIPSE token */
isfilled = *basicvars.current == BASTOKEN_FILL;
if (isfilled) basicvars.current++;
x = eval_integer(); /* Get x coordinate of centre */
if (*basicvars.current != ',') {
DEBUGFUNCMSGOUT;
error(ERR_COMISS);
return;
}
basicvars.current++;
y = eval_integer(); /* Get y coordinate of centre */
if (*basicvars.current != ',') {
DEBUGFUNCMSGOUT;
error(ERR_COMISS);
return;
}
basicvars.current++;
majorlen = eval_integer(); /* Get length of semi-major axis */
if (*basicvars.current != ',') {
DEBUGFUNCMSGOUT;
error(ERR_COMISS);
return;
}
basicvars.current++;
minorlen = eval_integer(); /* Get length of semi-minor axis */
if (*basicvars.current == ',') { /* Get angle at which ellipse is inclined */
basicvars.current++;
expression();
angle = pop_anynumfp();
}
else {
angle = 0;
}
check_ateol();
emulate_ellipse(x, y, majorlen, minorlen, angle, isfilled);
DEBUGFUNCMSGOUT;
}
/*
** 'exec_envelope' deals with the Basic 'ENVELOPE' statement. Under
** Basic V/VI, this statement calls the appropriate OS_Word call, however
** this call has no effect in RISC OS and appears to be supported
** only for backwards compatibilty with the BBC Micro.
*/
void exec_envelope(void) {
int32 n;
DEBUGFUNCMSGIN;
basicvars.current++;
for (n=1; n<14; n++) { /* Do the first 13 parameters */
(void) eval_integer();
if (*basicvars.current != ',') {
DEBUGFUNCMSGOUT;
error(ERR_COMISS);
return;
}
basicvars.current++;
}
(void) eval_integer();
check_ateol();
DEBUGFUNCMSGOUT;
}
/*
** 'exec_fill' handles the Basic 'FILL' statement
*/
void exec_fill(void) {
int32 x, y;
DEBUGFUNCMSGIN;
basicvars.current++; /* Skip the FILL token */
x = eval_integer(); /* Get x coordinate of start of fill */
if (*basicvars.current != ',') {
DEBUGFUNCMSGOUT;
error(ERR_COMISS);
return;
}
basicvars.current++;
y = eval_integer(); /* Get y coordinate of start of fill */
check_ateol();
emulate_plot(FLOOD_BACKGROUND+DRAW_ABSOLUTE, x, y);
DEBUGFUNCMSGOUT;
}
/*
** 'exec_fillby' handles the Basic 'FILL BY' statement
*/
void exec_fillby(void) {
int32 x, y;
DEBUGFUNCMSGIN;
basicvars.current++;
x = eval_integer(); /* Get relative x coordinate of start of fill */
if (*basicvars.current != ',') {
DEBUGFUNCMSGOUT;
error(ERR_COMISS);
return;
}
basicvars.current++;
y = eval_integer(); /* Get relative y coordinate of start of fill */
check_ateol();
emulate_plot(FLOOD_BACKGROUND+DRAW_RELATIVE, x, y);
DEBUGFUNCMSGOUT;
}
/*
** exec_gcolofon - Handle the GCOL OF ... ON statement:
** GCOL OF <action>, <colour> ON <action>, <colour>
** GCOL OF <action>, <red>, <green>, <blue> ON <action>, <red>, <green>, <blue>
** where <action> and either the OF or the ON clauses are optional
*/
static void exec_gcolofon(void) {
int32 red = 0, green = 0, blue = 0, backact = 0, backred = 0;
int32 action = 0, backgreen = 0, backblue = 0, form = 0;
/*
** form says what the statement contains:
** Bit 0: Foreground 0 = colour number, 1 = RGB value
** Bit 1: Background 0 = colour number, 1 = RGB value
** Bit 2: 1 = Change foreground
** Bit 3: 1 = Change background
*/
DEBUGFUNCMSGIN;
if (*basicvars.current == BASTOKEN_OF) {
form += 4;
basicvars.current++;
red = eval_integer();
if (*basicvars.current == ',') { /* Assume OF <red>, <green> */
basicvars.current++;
green = eval_integer();
if (*basicvars.current == ',') { /* Assume OF <red>, <green>, <blue> */
basicvars.current++;
form += 1; /* Set RGB flag */
blue = eval_integer();
if (*basicvars.current == ',') { /* Got OF <action>, <red>, <green>, <blue> */
basicvars.current++;
action = red;
red = green;
green = blue;
blue = eval_integer();
}
}
else { /* Only got two parameters - Assume OF <action>, <colour> */
action = red;
red = green;
}
}
}
/* Now look for background colour details */
if (*basicvars.current == BASTOKEN_ON) {
form += 8;
basicvars.current++;
backred = eval_integer();
if (*basicvars.current == ',') { /* Assume OF <red>, <green> */
basicvars.current++;
backgreen = eval_integer();
if (*basicvars.current == ',') { /* Assume OF <red>, <green>, <blue> */
basicvars.current++;
form += 2; /* Set RGB flag */
backblue = eval_integer();
if (*basicvars.current == ',') { /* Got OF <action>, <red>, <green>, <blue> */
basicvars.current++;
backact = backred;
backred = backgreen;
backgreen = backblue;
backblue = eval_integer();
}
}
else { /* Only got two parameters - Assume OF <action>, <colour> */
backact = backred;
backred = backgreen;
}
}
}
check_ateol();
if ((form & 4) != 0) { /* Set graphics foreground colour */
if ((form & 1) != 0) /* Set foreground RGB colour */
emulate_gcolrgb(action, FALSE, red, green, blue);
else {
emulate_gcolnum(action, FALSE, red);
}
}
if ((form & 8) != 0) { /* Set graphics background colour */
if ((form & 2) != 0) /* Set foreground RGB colour */
emulate_gcolrgb(backact, TRUE, backred, backgreen, backblue);
else {
emulate_gcolnum(backact, TRUE, backred);
}
}
DEBUGFUNCMSGOUT;
}
/*
** exec_gcolnum - Called to handle an old-style GCOL statement:
** GCOL <action>, <number> TINT <value>
** GCOL <action>, <red>, <green>, <blue>
** where <action> and TINT are optional
*/
static void exec_gcolnum(void) {
int32 colour, action, tint, gotrgb, green, blue;
DEBUGFUNCMSGIN;
action = 0;
tint = 0;
gotrgb = FALSE;
colour = eval_integer();
if (*basicvars.current == ',') {
basicvars.current++;
action = colour;
colour = eval_integer();
}
if (*basicvars.current == BASTOKEN_TINT) {
basicvars.current++;
tint = eval_integer();
}
else if (*basicvars.current == ',') { /* > 2 parameters - Got GCOL <red>, <green>, <blue> */
gotrgb = TRUE;
basicvars.current++;
green = eval_integer();
if (*basicvars.current == ',') { /* GCOL <action>, <red>, <green>, <blue> */
basicvars.current++;
blue = eval_integer();
}
else { /* Only three values supplied. Form is GCOL <red>, <green>, <blue> */
blue = green;
green = colour;
colour = action;
action = 0;
}
}
check_ateol();
if (gotrgb)
emulate_gcolrgb(action, FALSE, colour, green, blue);
else {
emulate_gcol(action, colour, tint);
}
DEBUGFUNCMSGOUT;
}
/*
** 'exec_gcol' deals with all forms of the Basic 'GCOL' statement
*/
void exec_gcol(void) {
DEBUGFUNCMSGIN;
basicvars.current++;
if (*basicvars.current == BASTOKEN_OF || *basicvars.current == BASTOKEN_ON)
exec_gcolofon();
else {
exec_gcolnum();
}
DEBUGFUNCMSGOUT;
}
/*
** 'input_file' is called to deal with an 'INPUT#' statement which is
** used to read binary values from a file. On entry, 'basicvars.current'
** points at the '#' of 'INPUT#'.
**
** This function needs to be revised as the type checking is carried
** out in the 'fileio' module. It should be done here.
*/
static void input_file(void) {
int32 handle, length;
int64 intvalue;
float64 floatvalue;
char *cp;
boolean isint;
lvalue destination;
DEBUGFUNCMSGIN;
basicvars.current++; /* Skip '#' token */
handle = eval_intfactor(); /* Find handle of file */
if (ateol[*basicvars.current]) { /* Nothing to do */
DEBUGFUNCMSGOUT;
return;
}
if (*basicvars.current != ',') {
DEBUGFUNCMSGOUT;
error(ERR_SYNTAX);
return;
}
do { /* Now read the values from the file */
basicvars.current++; /* Skip the ',' token */
get_lvalue(&destination);
switch (destination.typeinfo & PARMTYPEMASK) {
case VAR_INTWORD:
fileio_getnumber(handle, &isint, &intvalue, &floatvalue);
*destination.address.intaddr = isint ? intvalue : TOINT(floatvalue);
break;
case VAR_UINT8:
fileio_getnumber(handle, &isint, &intvalue, &floatvalue);
*destination.address.uint8addr = isint ? intvalue : TOINT(floatvalue);
break;
case VAR_INTLONG:
fileio_getnumber(handle, &isint, &intvalue, &floatvalue);
*destination.address.int64addr = isint ? intvalue : TOINT64(floatvalue);
break;
case VAR_FLOAT:
fileio_getnumber(handle, &isint, &intvalue, &floatvalue);
*destination.address.floataddr = isint ? TOFLOAT(intvalue) : floatvalue;
break;
case VAR_STRINGDOL:
free_string(*destination.address.straddr);
length = fileio_getstring(handle, basicvars.stringwork);
cp = alloc_string(length);
if (length>0) memmove(cp, basicvars.stringwork, length);
destination.address.straddr->stringlen = length;
destination.address.straddr->stringaddr = cp;
break;
case VAR_INTBYTEPTR:
fileio_getnumber(handle, &isint, &intvalue, &floatvalue);
basicvars.memory[destination.address.offset] = isint ? intvalue : TOINT(floatvalue);
break;