-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfunctions.c
2331 lines (2194 loc) · 63.3 KB
/
functions.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 all of the built-in Basic functions
*/
/*
** Changed by Crispian Daniels on August 12th 2002.
** Changed 'fn_rnd' to use a pseudo-random generator equivalent
** to the BASIC II implementation.
**
** 05-Apr-2014 JGH: Seperated BEAT from BEATS, they do different things.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <ctype.h>
#include "common.h"
#include "target.h"
#include "basicdefs.h"
#include "tokens.h"
#include "variables.h"
#include "strings.h"
#include "convert.h"
#include "stack.h"
#include "errors.h"
#include "evaluate.h"
#include "keyboard.h"
#include "screen.h"
#include "mos.h"
#include "miscprocs.h"
#include "fileio.h"
#include "functions.h"
#include "mos_sys.h"
/* #define DEBUG */
/*
* This file contains all of the built-in Basic functions. Most of
* them are dispatched via exec_function() as they have two byte
* tokens but some of them, particularly tokens that can be used
* as either functions or statements such as 'MODE', are called
* directly from the factor code in evaluate.c. The ones
* invoked via exec_function() are marked as 'static'. If they
* are not static they are called from evaluate.c. The value of
* basicvars.current depends on where the function was called
* from. If from exec_function() then it points at the byte
* after the function's token. (This is a two byte value where
* the second byte is a function number and basicvars.current
* points at the byte after the function number.) If called from
* factor() then it points at the function token still. This
* will always be a one byte token.
*/
#define RADCONV 57.29577951308232286 /* Used when converting degrees -> radians and vice versa */
#define TIMEFORMAT "%a,%d %b %Y.%H:%M:%S" /* Date format used by 'TIME$' */
/* RISC OS BASIC V uses &B0A, BASIC VI uses &110A. RTR BASICs use &90A */
#define STRFORMAT 0x110A /* Default format used by function STR$ */
static int32 lastrandom; /* 32-bit pseudo-random number generator value */
static int32 randomoverflow; /* 1-bit overflow from pseudo-random number generator */
static float64 floatvalue; /* Temporary for holding floating point values */
/*
** 'bad_token' is called to report a bad token value. This could mean
** two things: either the program has been corrupted or there is a bug
** in the interpreter. At this stage, the latter is more often the
** case.
*/
static void bad_token(void) {
DEBUGFUNCMSGIN;
#ifdef DEBUG
fprintf(stderr, "Bad token value %x at %p\n", *basicvars.current, basicvars.current);
#endif
DEBUGFUNCMSGOUT;
error(ERR_BROKEN, __LINE__, "functions");
}
static uint64 resize32(size_t value) {
DEBUGFUNCMSGIN;
if (sizeof(size_t) == 4) { /* 32-bit */
value &= 0xFFFFFFFFll;
}
DEBUGFUNCMSGOUT;
return value;
}
/*
** 'fn_himem' pushes the value of HIMEM on to the Basic stack
*/
static void fn_himem(void) {
DEBUGFUNCMSGIN;
if (matrixflags.pseudovarsunsigned) {
push_int64(resize32((size_t)basicvars.himem));
} else {
push_int64((size_t)basicvars.himem);
}
DEBUGFUNCMSGOUT;
}
/*
** 'fn_ext' pushes the size of the open file referenced by the handle
** given by its argument on to the Basic stack
*/
static void fn_ext(void) {
DEBUGFUNCMSGIN;
if (*basicvars.current != '#') {
DEBUGFUNCMSGOUT;
error(ERR_HASHMISS);
return;
}
basicvars.current++;
push_int64(fileio_getext(eval_intfactor()));
DEBUGFUNCMSGOUT;
}
/*
** 'fn_filepath' pushes a copy of the current program and library
** load path on to the Basic stack
*/
static void fn_filepath(void) {
int32 length;
char *cp;
DEBUGFUNCMSGIN;
if (basicvars.loadpath == NIL)
length = 0;
else {
length = strlen(basicvars.loadpath);
}
cp = alloc_string(length);
if (length>0) memcpy(cp, basicvars.loadpath, length);
push_strtemp(length, cp);
DEBUGFUNCMSGOUT;
}
/*
** 'fn_left' handles the 'LEFT$(' function
*/
static void fn_left(void) {
stackitem stringtype;
basicstring descriptor;
int32 length;
char *cp;
DEBUGFUNCMSGIN;
expression(); /* Fetch the string */
stringtype = GET_TOPITEM;
if (stringtype != STACK_STRING && stringtype != STACK_STRTEMP) {
DEBUGFUNCMSGOUT;
error(ERR_TYPESTR);
return;
}
if (*basicvars.current == ',') { /* Function call is of the form LEFT(<string>,<value>) */
basicvars.current++;
length = eval_integer();
if (*basicvars.current != ')') { /* ')' missing */
DEBUGFUNCMSGOUT;
error(ERR_RPMISS);
return;
}
basicvars.current++;
if (length<0)
return; /* Do nothing if required length is negative, that is, return whole string */
else if (length == 0) { /* Don't want anything from the string */
descriptor = pop_string();
if (stringtype == STACK_STRTEMP) free_string(descriptor);
cp = alloc_string(0); /* Allocate a null string */
push_strtemp(0, cp);
}
else {
descriptor = pop_string();
if (length>=descriptor.stringlen) /* Substring length exceeds that of original string */
push_string(descriptor); /* So put the old string back on the stack */
else {
cp = alloc_string(length);
memcpy(cp, descriptor.stringaddr, length);
push_strtemp(length, cp);
if (stringtype == STACK_STRTEMP) free_string(descriptor);
}
}
}
else { /* Return original string with the last character sawn off */
if (*basicvars.current != ')') { /* ')' missing */
DEBUGFUNCMSGOUT;
error(ERR_RPMISS);
return;
}
basicvars.current++; /* Skip past the ')' */
descriptor = pop_string();
length = descriptor.stringlen-1;
if (length<=0) {
if (stringtype == STACK_STRTEMP) free_string(descriptor);
cp = alloc_string(0); /* Allocate a null string */
push_strtemp(0, cp);
}
else { /* Create a new string of the required length */
cp = alloc_string(length);
memmove(cp, descriptor.stringaddr, length);
push_strtemp(length, cp);
if (stringtype == STACK_STRTEMP) free_string(descriptor);
}
}
DEBUGFUNCMSGOUT;
}
/*
** 'fn_lomem' pushes the address of the start of the Basic heap on
** to the Basic stack
*/
static void fn_lomem(void) {
DEBUGFUNCMSGIN;
if (matrixflags.pseudovarsunsigned) {
push_int64(resize32((size_t)basicvars.lomem));
} else {
push_int64((size_t)basicvars.lomem);
}
DEBUGFUNCMSGOUT;
}
/*
** 'fn_mid' handles the 'MID$(' function, which returns the middle
** part of a string
*/
static void fn_mid(void) {
stackitem stringtype;
basicstring descriptor;
int32 start, length;
char *cp;
DEBUGFUNCMSGIN;
expression(); /* Fetch the string */
stringtype = GET_TOPITEM;
if (stringtype != STACK_STRING && stringtype != STACK_STRTEMP) {
DEBUGFUNCMSGOUT;
error(ERR_TYPESTR);
return;
}
if (*basicvars.current != ',') {
DEBUGFUNCMSGOUT;
error(ERR_COMISS);
return;
}
basicvars.current++;
start = eval_integer();
if (*basicvars.current == ',') { /* Call of the form 'MID$(<string>,<expr>,<expr>) */
basicvars.current++;
length = eval_integer();
if (length<0) length = MAXSTRING; /* -ve length = use remainder of string */
}
else { /* Length not given - Use remainder of string */
length = MAXSTRING;
}
if (*basicvars.current != ')') { /* ')' missing */
DEBUGFUNCMSGOUT;
error(ERR_RPMISS);
return;
}
basicvars.current++;
descriptor = pop_string();
if (length == 0 || start<0 || start>descriptor.stringlen) { /* Don't want anything from the string */
if (stringtype == STACK_STRTEMP) free_string(descriptor);
cp = alloc_string(0); /* Allocate a null string */
push_strtemp(0, cp);
}
else { /* Want only some of the original string */
if (start>0) start-=1; /* Turn start position into an offset from zero */
if (start == 0 && length>=descriptor.stringlen) /* Substring is entire string */
push_string(descriptor); /* So put the old string back on the stack */
else {
if (start+length>descriptor.stringlen) length = descriptor.stringlen-start;
cp = alloc_string(length);
memcpy(cp, descriptor.stringaddr+start, length);
push_strtemp(length, cp);
if (stringtype == STACK_STRTEMP) free_string(descriptor);
}
}
DEBUGFUNCMSGOUT;
}
/*
** 'fn_page' pushes the address of the start of the basic program on to the
** Basic stack
*/
static void fn_page(void) {
DEBUGFUNCMSGIN;
if (matrixflags.pseudovarsunsigned) {
push_int64(resize32((size_t)basicvars.page));
} else {
push_int64((size_t)basicvars.page);
}
DEBUGFUNCMSGOUT;
}
/*
** 'fn_ptr' returns the current offset within the file of the file
** pointer for the file associated with file handle 'handle'
*/
static void fn_ptr(void) {
DEBUGFUNCMSGIN;
if (*basicvars.current == '#') {
basicvars.current++;
push_int64(fileio_getptr(eval_intfactor()));
} else if (*basicvars.current == '(') {
stackitem topitem;
basicarray *descriptor;
basicstring strdesc;
basicvars.current++;
expression();
topitem = GET_TOPITEM;
switch(topitem) {
case STACK_INTARRAY: case STACK_UINT8ARRAY: case STACK_INT64ARRAY: case STACK_FLOATARRAY: case STACK_STRARRAY:
descriptor=pop_array();
push_int64((int64)(size_t)descriptor);
break;
case STACK_STRING:
strdesc=pop_string();
push_int64((int64)(size_t)strdesc.stringaddr);
break;
default:
DEBUGFUNCMSGOUT;
error(ERR_UNSUITABLEVAR);
return;
}
basicvars.current++;
} else {
DEBUGFUNCMSGOUT;
error(ERR_HASHMISS);
}
DEBUGFUNCMSGOUT;
}
/*
** 'fn_right' evaluates the function 'RIGHT$('.
*/
static void fn_right(void) {
stackitem stringtype;
basicstring descriptor;
char *cp;
DEBUGFUNCMSGIN;
expression(); /* Fetch the string */
stringtype = GET_TOPITEM;
if (stringtype != STACK_STRING && stringtype != STACK_STRTEMP) {
DEBUGFUNCMSGOUT;
error(ERR_TYPESTR);
return;
}
if (*basicvars.current == ',') { /* Function call is of the form RIGHT$(<string>,<value>) */
int32 length;
basicvars.current++;
length = eval_integer();
if (*basicvars.current != ')') { /* ')' missing */
DEBUGFUNCMSGOUT;
error(ERR_RPMISS);
return;
}
basicvars.current++;
if (length<=0) { /* Do not want anything from string */
descriptor = pop_string();
if (stringtype == STACK_STRTEMP) free_string(descriptor);
cp = alloc_string(0); /* Allocate a null string */
push_strtemp(0, cp);
}
else {
descriptor = pop_string();
if (length>=descriptor.stringlen) /* Substring length exceeds that of original string */
push_string(descriptor); /* So put the old string back on the stack */
else {
cp = alloc_string(length);
memcpy(cp, descriptor.stringaddr+descriptor.stringlen-length, length);
push_strtemp(length, cp);
if (stringtype == STACK_STRTEMP) free_string(descriptor);
}
}
}
else { /* Return only the last character */
if (*basicvars.current != ')') { /* ')' missing */
DEBUGFUNCMSGOUT;
error(ERR_RPMISS);
return;
}
basicvars.current++; /* Skip past the ')' */
descriptor = pop_string();
if (descriptor.stringlen == 0)
push_string(descriptor); /* String length is zero - Just put null string back on stack */
else { /* Create a new single character string */
cp = alloc_string(1);
*cp = *(descriptor.stringaddr+descriptor.stringlen-1);
push_strtemp(1, cp);
if (stringtype == STACK_STRTEMP) free_string(descriptor);
}
}
DEBUGFUNCMSGOUT;
}
/*
** 'timedol' returns the date and time as string in the standard
** RISC OS format. There is no need to emulate this as standard C
** functions can be used to return the value
*/
static void fn_timedol(void) {
size_t length;
time_t thetime;
char *cp;
DEBUGFUNCMSGIN;
thetime = time(NIL);
length = strftime(basicvars.stringwork, MAXSTRING, TIMEFORMAT, localtime(&thetime));
cp = alloc_string(length);
memcpy(cp, basicvars.stringwork, length);
push_strtemp(length, cp);
DEBUGFUNCMSGOUT;
}
/*
** 'fn_time' returns the value of the centisecond timer. How accurate
** this is depends on the underlying OS
*/
static void fn_time(void) {
DEBUGFUNCMSGIN;
if (*basicvars.current == '$') {
basicvars.current++;
fn_timedol();
} else {
push_int(mos_rdtime());
}
DEBUGFUNCMSGOUT;
}
/*
** 'fn_abs' returns the absolute value of the function's argument. The
** values are updated in place on the Basic stack
*/
static void fn_abs(void) {
stackitem numtype;
DEBUGFUNCMSGIN;
(*factor_table[*basicvars.current])();
numtype = GET_TOPITEM;
if (numtype == STACK_UINT8)
return; /* No-op on unsigned 8-bit int */
else if (numtype == STACK_INT)
ABS_INT;
else if (numtype == STACK_INT64)
ABS_INT64;
else if (numtype == STACK_FLOAT)
ABS_FLOAT;
else {
DEBUGFUNCMSGOUT;
error(ERR_TYPENUM);
}
DEBUGFUNCMSGOUT;
}
/*
** 'fn_acs' evalutes the arc cosine of its argument
*/
static void fn_acs(void) {
DEBUGFUNCMSGIN;
(*factor_table[*basicvars.current])();
push_float(acos(pop_anynumfp()));
DEBUGFUNCMSGOUT;
}
/*
** 'fn_adval' deals with the 'ADVAL' function. This is a BBC Micro-specific
** function that returns the current value of that machine's built-in A/D
** convertor. As per RISC OS, using the function for this purpose generates
** an error. 'ADVAL' can also be used to return the space left or the number
** of entries currently used in the various buffers within RISC OS for the
** serial port, parallel port and so on. A dummy value is returned for
** these
*/
static void fn_adval(void) {
DEBUGFUNCMSGIN;
push_int(mos_adval(eval_intfactor()));
DEBUGFUNCMSGOUT;
}
/*
** fn_argc pushes the number of command line arguments on to the
** Basic stack
*/
static void fn_argc(void) {
DEBUGFUNCMSGIN;
push_int(basicvars.argcount);
DEBUGFUNCMSGOUT;
}
/*
** fn_argvdol' pushes a copy of a command line parameter on to
** the Basic stack
*/
static void fn_argvdol(void) {
int32 number, length;
cmdarg *ap;
char *cp;
DEBUGFUNCMSGIN;
number = eval_intfactor(); /* Fetch number of argument to push on to stack */
if (number<0 || number>basicvars.argcount) {
DEBUGFUNCMSGOUT;
error(ERR_RANGE);
return;
}
ap = basicvars.arglist;
while (number > 0) {
number--;
ap = ap->nextarg;
}
length = strlen(ap->argvalue);
cp = alloc_string(length);
if (length>0) memcpy(cp, ap->argvalue, length);
push_strtemp(length, cp);
DEBUGFUNCMSGOUT;
}
/*
** 'fn_asc' returns the character code for the first character of the string
** given as its argument or -1 if the string is the null string
*/
static void fn_asc(void) {
basicstring descriptor;
stackitem topitem;
DEBUGFUNCMSGIN;
(*factor_table[*basicvars.current])();
topitem = GET_TOPITEM;
if (topitem == STACK_STRING || topitem == STACK_STRTEMP) {
descriptor = pop_string();
if (descriptor.stringlen == 0) /* Null string returns -1 with ASC */
push_int(-1);
else {
push_int(*descriptor.stringaddr & BYTEMASK);
if (topitem == STACK_STRTEMP) free_string(descriptor);
}
}
else { /* String wanted */
DEBUGFUNCMSGOUT;
error(ERR_TYPESTR);
}
DEBUGFUNCMSGOUT;
}
/*
** 'fn_asn' evalutes the arc sine of its argument
*/
static void fn_asn(void) {
DEBUGFUNCMSGIN;
(*factor_table[*basicvars.current])();
push_float(asin(pop_anynumfp()));
DEBUGFUNCMSGOUT;
}
/*
** 'fn_atn' evalutes the arc tangent of its argument
*/
static void fn_atn(void) {
DEBUGFUNCMSGIN;
if (*basicvars.current == '(') {
float64 parmx;
basicvars.current++;
expression();
parmx=pop_anynumfp();
if(*basicvars.current != ',') {
push_float(atan(parmx));
} else {
float64 parmy;
basicvars.current++;
expression();
parmy=pop_anynumfp();
push_float(atan2(parmx, parmy));
}
if (*basicvars.current != ')') {
DEBUGFUNCMSGOUT;
error(ERR_SYNTAX);
return;
}
basicvars.current++;
} else {
(*factor_table[*basicvars.current])();
push_float(atan(pop_anynumfp()));
}
DEBUGFUNCMSGOUT;
}
/*
** 'fn_beat' is one of the functions associated with the RISC OS sound system.
** 'BEAT' returns the current microbeat number.
*/
static void fn_beat(void) {
DEBUGFUNCMSGIN;
push_int(mos_rdbeat());
DEBUGFUNCMSGOUT;
}
/*
** 'fn_beats' is one of the functions associated with the RISC OS sound system.
** 'BEATS' returns the number of microbeats in a bar.
*/
void fn_beats(void) {
DEBUGFUNCMSGIN;
basicvars.current++;
push_int(mos_rdbeats());
DEBUGFUNCMSGOUT;
}
/*
** 'bget' returns the next byte from the file identified by the
** handle specified as its argument
*/
static void fn_bget(void) {
DEBUGFUNCMSGIN;
if (*basicvars.current != '#') {
DEBUGFUNCMSGOUT;
error(ERR_HASHMISS);
return;
}
basicvars.current++;
push_int(fileio_bget(eval_intfactor()));
DEBUGFUNCMSGOUT;
}
/*
** 'fn_chr' converts the value given as its argument to a single
** character string
*/
static void fn_chr(void) {
char *cp, value;
DEBUGFUNCMSGIN;
(*factor_table[*basicvars.current])();
value = pop_anynum32();
cp = alloc_string(1);
*cp=value;
push_strtemp(1, cp);
DEBUGFUNCMSGOUT;
}
/*
** fn_colour - Return the colour number of the colour
** which most closely matches the colour with red, green
** and blue components passed to it. The colour is matched
** against the colours available in the current screen
** mode
*/
void fn_colour(void) {
int32 red, green, blue;
DEBUGFUNCMSGIN;
basicvars.current++;
if (*basicvars.current != '(') { /* COLOUR must be followed by a '(' */
DEBUGFUNCMSGOUT;
error(ERR_SYNTAX);
return;
}
basicvars.current++;
red = eval_integer();
if (*basicvars.current != ',') {
DEBUGFUNCMSGOUT;
error(ERR_SYNTAX);
return;
}
basicvars.current++;
green = eval_integer();
if (*basicvars.current != ',') {
DEBUGFUNCMSGOUT;
error(ERR_SYNTAX);
return;
}
basicvars.current++;
blue = eval_integer();
if (*basicvars.current != ')') {
DEBUGFUNCMSGOUT;
error(ERR_RPMISS);
return;
}
basicvars.current++;
push_int(emulate_colourfn(red, green, blue));
DEBUGFUNCMSGOUT;
}
/*
** 'fn_cos' evaluates the cosine of its argument
*/
static void fn_cos(void) {
DEBUGFUNCMSGIN;
(*factor_table[*basicvars.current])();
push_float(cos(pop_anynumfp()));
DEBUGFUNCMSGOUT;
}
/*
** 'fn_count' returns the number of characters printed on the current
** line by 'PRINT'
*/
static void fn_count(void) {
DEBUGFUNCMSGIN;
push_int(basicvars.printcount);
DEBUGFUNCMSGOUT;
}
/*
** 'get_arrayname' parses an array name and returns a pointer to its
** symbol table entry. On entry, 'basicvars.current' points at the
** array's variable token. It is left pointing at the byte after the
** pointer to the array's symbol table entry
*/
static variable *get_arrayname(void) {
variable *vp = NULL;
DEBUGFUNCMSGIN;
if (*basicvars.current == BASTOKEN_ARRAYVAR) /* Known reference */
vp = GET_ADDRESS(basicvars.current, variable *);
else if (*basicvars.current == BASTOKEN_XVAR) { /* Reference not seen before */
byte *base, *ep;
base = GET_SRCADDR(basicvars.current); /* Find address of array's name */
ep = skip_name(base);
vp = find_variable(base, ep-base);
if (vp == NIL) {
error(ERR_ARRAYMISS, tocstring(CAST(base, char *), ep-base));
return NULL;
}
if ((vp->varflags & VAR_ARRAY) == 0) { /* Not an array */
DEBUGFUNCMSGOUT;
error(ERR_VARARRAY);
return NULL;
}
if (*(basicvars.current+LOFFSIZE+1) != ')') { /* Array name must be suppled as 'array()' */
DEBUGFUNCMSGOUT;
error(ERR_RPMISS);
return NULL;
}
*basicvars.current = BASTOKEN_ARRAYVAR;
set_address(basicvars.current, vp);
}
else { /* Not an array name */
DEBUGFUNCMSGOUT;
error(ERR_VARARRAY); /* Name not found */
return NULL;
}
if (vp->varentry.vararray == NIL) { /* Array has not been dimensioned */
DEBUGFUNCMSGOUT;
error(ERR_NODIMS, vp->varname);
return NULL;
}
basicvars.current+=LOFFSIZE+2; /* Skip pointer to array and ')' */
DEBUGFUNCMSGOUT;
return vp;
}
/*
** 'fn_dim' handles the 'DIM' function. This returns either the number
** of dimensions the specified array has or the upper bound of the
** dimension given by the second parameter
*/
void fn_dim(void) {
variable *vp;
int32 dimension;
DEBUGFUNCMSGIN;
basicvars.current++;
if (*basicvars.current != '(') { /* DIM must be followed by a '(' */
DEBUGFUNCMSGOUT;
error(ERR_SYNTAX);
return;
}
basicvars.current++;
vp = get_arrayname();
switch (*basicvars.current) {
case ',': /* Got 'array(),<x>) - Return upper bound of dimension <x> */
basicvars.current++;
dimension = eval_integer(); /* Get dimension number */
if (*basicvars.current != ')') {
DEBUGFUNCMSGOUT;
error(ERR_RPMISS);
return;
}
basicvars.current++; /* Skip the trailing ')' */
if (dimension<1 || dimension>vp->varentry.vararray->dimcount) {
DEBUGFUNCMSGOUT;
error(ERR_DIMRANGE);
return;
}
push_int(vp->varentry.vararray->dimsize[dimension-1]-1);
break;
case ')': /* Got 'array())' - Return the number of dimensions */
push_int(vp->varentry.vararray->dimcount);
basicvars.current++;
break;
default:
DEBUGFUNCMSGOUT;
error(ERR_SYNTAX);
}
DEBUGFUNCMSGOUT;
}
/*
** The function 'DEG' converts an angrle expressed in radians to degrees
*/
static void fn_deg(void) {
DEBUGFUNCMSGIN;
(*factor_table[*basicvars.current])();
push_float(pop_anynumfp()*RADCONV);
DEBUGFUNCMSGOUT;
}
/*
** 'fn_end' deals with the 'END' function, which pushes the address of
** the top of the Basic program and variables on to the Basic stack
*/
void fn_end(void) {
DEBUGFUNCMSGIN;
basicvars.current++;
if (matrixflags.pseudovarsunsigned) {
push_int64(resize32((size_t)basicvars.vartop));
} else {
push_int64((size_t)basicvars.vartop);
}
DEBUGFUNCMSGOUT;
}
/*
** 'fn_eof' deals with the 'EOF' function, which returns 'TRUE' if the
** 'at end of file' flag is set for the file specified
*/
static void fn_eof(void) {
int32 handle;
DEBUGFUNCMSGIN;
if (*basicvars.current != '#') {
DEBUGFUNCMSGOUT;
error(ERR_HASHMISS);
return;
}
basicvars.current++;
handle = eval_intfactor();
push_int(fileio_eof(handle) ? BASTRUE : BASFALSE);
DEBUGFUNCMSGOUT;
}
/*
** 'fn_erl' pushes the line number of the line at which the last error
** occured
*/
static void fn_erl(void) {
DEBUGFUNCMSGIN;
push_int(basicvars.error_line);
DEBUGFUNCMSGOUT;
}
/*
** 'fn_err' pushes the error number of the last error on to the Basic
** stack
*/
static void fn_err(void) {
DEBUGFUNCMSGIN;
push_int(basicvars.error_number);
DEBUGFUNCMSGOUT;
}
/*
** 'fn_eval' deals with the function 'eval'
** The argument of the function is tokenized and stored in
** 'evalexpr'. The current value of 'basicvars.current' is
** saved locally, but this is not the proper place if an
** error occurs in the expression being evaluated as the
** current will not be pointing into the Basic program. I
** think the value should be saved on the Basic stack.
*/
static void fn_eval(void) {
stackitem stringtype;
basicstring descriptor;
byte evalexpr[MAXSTATELEN];
DEBUGFUNCMSGIN;
(*factor_table[*basicvars.current])();
stringtype = GET_TOPITEM;
if (stringtype != STACK_STRING && stringtype != STACK_STRTEMP) {
DEBUGFUNCMSGOUT;
error(ERR_TYPESTR);
return;
}
descriptor = pop_string();
memmove(basicvars.stringwork, descriptor.stringaddr, descriptor.stringlen);
basicvars.stringwork[descriptor.stringlen] = asc_NUL; /* Now have a null-terminated version of string */
if (stringtype == STACK_STRTEMP) free_string(descriptor);
tokenize(basicvars.stringwork, evalexpr, NOLINE, FALSE); /* 'tokenise' leaves its results in 'thisline' */
// tokenize(basicvars.stringwork, evalexpr, NOLINE); /* 'tokenise' leaves its results in 'thisline' */
save_current(); /* Save pointer to current position in expression */
basicvars.current = FIND_EXEC(evalexpr);
expression();
if (basicvars.runflags.flag_cosmetic && (*basicvars.current != asc_NUL)) {
DEBUGFUNCMSGOUT;
error(ERR_SYNTAX);
return;
}
restore_current();
DEBUGFUNCMSGOUT;
}
/*
** 'fn_exp' evaluates the exponentinal function of its argument
*/
static void fn_exp(void) {
DEBUGFUNCMSGIN;
(*factor_table[*basicvars.current])();
push_float(exp(pop_anynumfp()));
DEBUGFUNCMSGOUT;
}
/*
** 'fn_false' pushes the value which represents 'FALSE' on to the Basic stack
*/
void fn_false(void) {
DEBUGFUNCMSGIN;
basicvars.current++;
push_int(BASFALSE);
DEBUGFUNCMSGOUT;
}
/*
** 'fn_get' implements the 'get' function which reads a character from the
** keyboard and saves it on the Basic stack as a number
*/
static void fn_get(void) {
DEBUGFUNCMSGIN;
if (*basicvars.current == '(') { /* Have encountered the 'GET(x,y)' version */
int32 x, y;
basicvars.current++;
x = eval_integer();
if (*basicvars.current != ',') {
DEBUGFUNCMSGOUT;
error(ERR_COMISS);
return;
}
basicvars.current++;
y = eval_integer();
if (*basicvars.current != ')') {
DEBUGFUNCMSGOUT;
error(ERR_RPMISS);
return;
}
basicvars.current++;
push_int(get_character_at_pos(x, y));
} else {
int ch;
do {
ch=kbd_get() & 0xFF;
} while (ch==0);
push_int(ch);
}
DEBUGFUNCMSGOUT;
}
/*
** 'fn_getdol' implements the 'get$' function which either reads a character
** from the keyboard or a string from a file
*/
static void fn_getdol(void) {
char *cp;
int ch;
int32 handle, count;
DEBUGFUNCMSGIN;
if (*basicvars.current == '(') { /* Have encountered the 'GET$(x,y)' version */
int32 x, y;
basicvars.current++;
x = eval_integer();
if (*basicvars.current != ',') {
DEBUGFUNCMSGOUT;
error(ERR_COMISS);
return;
}
basicvars.current++;