forked from nerun/bwbasic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbwb_exp.c
3519 lines (3235 loc) · 87.5 KB
/
bwb_exp.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
/****************************************************************
bwb_exp.c Expression Parser
for Bywater BASIC Interpreter
Copyright (c) 1993, Ted A. Campbell
Bywater Software
email: [email protected]
Copyright and Permissions Information:
All U.S. and international rights are claimed by the author,
Ted A. Campbell.
This software is released under the terms of the GNU General
Public License (GPL), which is distributed with this software
in the file "COPYING". The GPL specifies the terms under
which users may copy and use the software in this distribution.
A separate license is available for commercial distribution,
for information on which you should contact the author.
***************************************************************/
/*---------------------------------------------------------------*/
/* NOTE: Modifications marked "JBV" were made by Jon B. Volkoff, */
/* 11/1995 ([email protected]). */
/* */
/* Those additionally marked with "DD" were at the suggestion of */
/* Dale DePriest ([email protected]). */
/* */
/* Version 3.00 by Howard Wulf, AF5NE */
/* */
/* Version 3.10 by Howard Wulf, AF5NE */
/* */
/* Version 3.20 by Howard Wulf, AF5NE */
/* */
/*---------------------------------------------------------------*/
#include "bwbasic.h"
/*
--------------------------------------------------------------------------------------------
EXPRESSION PARSER
Inspired by https://groups.google.com/forum/m/#!topic/comp.compilers/RCyhEbLfs40
...
// Permission is given to use this source provided an acknowledgement is given.
// I'd also like to know if you've found it useful.
//
// The following Research Report describes the idea, and shows how the
// parsing method may be understood as an encoding of the usual family-of-
// parsing-procedures technique as used e.g. in Pascal compilers.
// @techreport{QMW-DCS-383-1986a,
// author ="Clarke, Keith",
// title ="The Top-Down Parsing of Expressions",
// institution ="Department of Computer Science, Queen Mary College, University of London, England",
// year ="1986",
// month ="June",
// number ="QMW-DCS-1986-383",
// scope ="theory",
// abstractURL ="http://www.dcs.qmw.ac.uk/publications/report_abstracts/1986/383",
// keywords ="Recursive-descent parsing, expression parsing, operator precedence parsing."
// }
// A formal proof of the algorithm was made, as part of his PhD thesis work,
// by A.M. Abbas of QMC, London, in the framework of Constructive Set Theory.
// copyright Keith Clarke, Dept of Computer Science, QMW, University of London,
// England. email [email protected]
...
--------------------------------------------------------------------------------------------
*/
/*
For all functions named "line_*", "LineType * line" is the first parameter.
For all functions named "buff_*", "char * buffer, int * position" are the first two parameters.
FALSE must be zero.
TRUE must be non-zero.
*/
/* OperatorType.Arity */
#define UNARY 1
#define BINARY 2
/* OperatorType.IsAlpha */
#define IS_ALPHA 'T'
#define NO_ALPHA 'F'
#define COPY_VARIANT( X, Y ) if( X != NULL ) { bwb_memcpy( X, Y, sizeof( VariantType ) ); bwb_memset( Y, 0, sizeof( VariantType ) ); }
typedef ResultType (OperatorFunctionType) (VariantType * X, VariantType * Y);
struct OperatorStruct
{
const unsigned char ThisPrec;
const unsigned char NextPrec; /* if BINARY and LEFT assoc, then ThisPrec+1, else ThisPrec */
const unsigned char Arity; /* UNARY or BINARY */
const char IsAlpha; /* IS_ALPHA or NO_ALPHA, determines how operator is matched */
const char *Name;
OperatorFunctionType *Eval;
const char *Syntax;
const char *Description;
OptionVersionType OptionVersionBitmask; /* OPTION VERSION bitmask */
};
typedef struct OperatorStruct OperatorType;
static int both_are_long (VariantType * X, VariantType * Y);
static int both_integer_type (VariantType * X, VariantType * Y);
static int both_number_type (VariantType * X, VariantType * Y);
static int both_string_type (VariantType * X, VariantType * Y);
static ResultType buff_read_expr (char *buffer, int *position,
VariantType * X, unsigned char LastPrec);
static ResultType buff_read_function (char *buffer, int *position,
VariantType * X);
static ResultType buff_read_internal_constant (char *buffer, int *position,
VariantType * X);
static OperatorType *buff_read_operator (char *buffer, int *position,
unsigned char LastPrec,
unsigned char Arity);
static ResultType buff_read_primary (char *buffer, int *position,
VariantType * X);
static ResultType buff_read_string_constant (char *buffer, int *position,
VariantType * X);
static ResultType buff_read_variable (char *buffer, int *position,
VariantType * X);
static int bwb_isodigit (int C);
static int is_integer_type (VariantType * X);
static int is_long_value (VariantType * X);
static int is_number_type (VariantType * X);
static int is_string_type (VariantType * X);
static char Largest_TypeCode (char TypeCode, VariantType * X);
static char math_type (VariantType * X, VariantType * Y);
static char max_number_type (char X, char Y);
static char min_value_type (VariantType * X);
static ResultType OP_ADD (VariantType * X, VariantType * Y);
static ResultType OP_AMP (VariantType * X, VariantType * Y);
static ResultType OP_AND (VariantType * X, VariantType * Y);
static ResultType OP_DIV (VariantType * X, VariantType * Y);
static ResultType OP_EQ (VariantType * X, VariantType * Y);
static ResultType OP_EQV (VariantType * X, VariantType * Y);
static ResultType OP_EXP (VariantType * X, VariantType * Y);
static ResultType OP_GE (VariantType * X, VariantType * Y);
static ResultType OP_GT (VariantType * X, VariantType * Y);
static ResultType OP_IDIV (VariantType * X, VariantType * Y);
static ResultType OP_IMP (VariantType * X, VariantType * Y);
static ResultType OP_LE (VariantType * X, VariantType * Y);
static ResultType OP_LIKE (VariantType * X, VariantType * Y);
static ResultType OP_LT (VariantType * X, VariantType * Y);
static ResultType OP_MAX (VariantType * X, VariantType * Y);
static ResultType OP_MIN (VariantType * X, VariantType * Y);
static ResultType OP_MOD (VariantType * X, VariantType * Y);
static ResultType OP_MUL (VariantType * X, VariantType * Y);
static ResultType OP_NE (VariantType * X, VariantType * Y);
static ResultType OP_NEG (VariantType * X, VariantType * Y);
static ResultType OP_NOT (VariantType * X, VariantType * Y);
static ResultType OP_OR (VariantType * X, VariantType * Y);
static ResultType OP_POS (VariantType * X, VariantType * Y);
static ResultType OP_SUB (VariantType * X, VariantType * Y);
static ResultType OP_XOR (VariantType * X, VariantType * Y);
static void SortAllOperatorsForManual (void);
static ResultType test_eq (VariantType * X, VariantType * Y, int TrueValue,
int FalseValue);
static ResultType test_gt (VariantType * X, VariantType * Y, int TrueValue,
int FalseValue);
static ResultType test_lt (VariantType * X, VariantType * Y, int TrueValue,
int FalseValue);
/* table of operators */
/*
In BASIC, 2 ^ 3 ^ 2 = ( 2 ^ 3 ) ^ 2 = 64, and -2 ^ 2 = - (2 ^ 2) = -4.
*/
static OperatorType OperatorTable[ /* NUM_OPERATORS */ ] =
{
/* LOGICAL */
{0x01, 0x02, BINARY, IS_ALPHA, "IMP", OP_IMP, "X IMP Y", "Bitwise IMP",
B15 | B93 | D64 | G65 | G67 | G74 | S70 | I70 | I73 | C77 | D71 | M80 | T80
| H14},
{0x02, 0x03, BINARY, IS_ALPHA, "EQV", OP_EQV, "X EQV Y", "Bitwise EQV",
B15 | B93 | D64 | G65 | G67 | G74 | S70 | I70 | I73 | C77 | D71 | M80 | T80
| H14},
{0x03, 0x04, BINARY, IS_ALPHA, "XOR", OP_XOR, "X XOR Y",
"Bitwise Exclusive OR",
B15 | B93 | HB1 | HB2 | D64 | G65 | G67 | G74 | S70 | I70 | I73 | C77 | D71
| M80 | T79 | R86 | T80 | H14},
{0x03, 0x04, BINARY, IS_ALPHA, "XRA", OP_XOR, "X XRA Y",
"Bitwise Exclusive OR",
HB2},
{0x04, 0x05, BINARY, IS_ALPHA, "OR", OP_OR, "X OR Y", "Bitwise OR",
B15 | B93 | HB1 | HB2 | D64 | G65 | G67 | G74 | S70 | I70 | I73 | C77 | D71
| E86 | M80 | T79 | R86 | T80 | H80 | V09 | H14},
{0x05, 0x06, BINARY, IS_ALPHA, "AND", OP_AND, "X AND Y", "Bitwise AND",
B15 | B93 | HB1 | HB2 | D64 | G65 | G67 | G74 | S70 | I70 | I73 | C77 | D71
| E86 | M80 | T79 | R86 | T80 | H80 | V09 | H14},
{0x06, 0x06, UNARY, IS_ALPHA, "NOT", OP_NOT, "NOT X", "Bitwise NOT",
B15 | B93 | HB1 | HB2 | D64 | G65 | G67 | G74 | S70 | I70 | I73 | C77 | D71
| E86 | M80 | T79 | R86 | T80 | H80 | V09 | H14},
/* RELATIONAL */
{0x07, 0x08, BINARY, IS_ALPHA, "NE", OP_NE, "X NE Y", "Not Equal",
0},
{0x07, 0x08, BINARY, NO_ALPHA, "#", OP_NE, "X # Y", "Not Equal",
0},
{0x07, 0x08, BINARY, NO_ALPHA, "<>", OP_NE, "X <> Y", "Not Equal",
B15 | B93 | HB1 | HB2 | D64 | G65 | G67 | G74 | S70 | I70 | I73 | C77 | D71
| D70 | D73 | E78 | E86 | M80 | T79 | R86 | T80 | H80 | V09 | H14},
{0x07, 0x08, BINARY, NO_ALPHA, "><", OP_NE, "X >< Y", "Not Equal",
B15 | B93 | HB1 | HB2 | D64 | G65 | G67 | G74 | S70 | I70 | I73 | C77 | D71
| D70 | D73 | E78 | E86 | M80 | T79 | R86 | T80 | H80 | V09 | H14},
{0x07, 0x08, BINARY, IS_ALPHA, "GE", OP_GE, "X GE Y",
"Greater than or Equal",
0},
{0x07, 0x08, BINARY, NO_ALPHA, ">=", OP_GE, "X >= Y",
"Greater than or Equal",
B15 | B93 | HB1 | HB2 | D64 | G65 | G67 | G74 | S70 | I70 | I73 | C77 | D71
| D70 | D73 | E78 | E86 | M80 | T79 | R86 | T80 | H80 | V09 | H14},
{0x07, 0x08, BINARY, NO_ALPHA, "=>", OP_GE, "X => Y",
"Greater than or Equal",
B15 | B93 | HB1 | HB2 | D64 | G65 | G67 | G74 | S70 | I70 | I73 | C77 | D71
| D70 | D73 | E78 | E86 | M80 | T79 | R86 | T80 | H80 | V09 | H14},
{0x07, 0x08, BINARY, IS_ALPHA, "LE", OP_LE, "X LE Y", "Less than or Equal",
0},
{0x07, 0x08, BINARY, NO_ALPHA, "<=", OP_LE, "X <= Y", "Less than or Equal",
B15 | B93 | HB1 | HB2 | D64 | G65 | G67 | G74 | S70 | I70 | I73 | C77 | D71
| D70 | D73 | E78 | E86 | M80 | T79 | R86 | T80 | H80 | V09 | H14},
{0x07, 0x08, BINARY, NO_ALPHA, "=<", OP_LE, "X =< Y", "Less than or Equal",
B15 | B93 | HB1 | HB2 | D64 | G65 | G67 | G74 | S70 | I70 | I73 | C77 | D71
| D70 | D73 | E78 | E86 | M80 | T79 | R86 | T80 | H80 | V09 | H14},
{0x07, 0x08, BINARY, IS_ALPHA, "EQ", OP_EQ, "X EQ Y", "Equal",
0},
{0x07, 0x08, BINARY, NO_ALPHA, "=", OP_EQ, "X = Y", "Equal",
B15 | B93 | HB1 | HB2 | D64 | G65 | G67 | G74 | S70 | I70 | I73 | C77 | D71
| D70 | D73 | E78 | E86 | M80 | T79 | R86 | T80 | H80 | V09 | H14},
{0x07, 0x08, BINARY, IS_ALPHA, "LT", OP_LT, "X LT Y", "Less than",
0},
{0x07, 0x08, BINARY, NO_ALPHA, "<", OP_LT, "X < Y", "Less than",
B15 | B93 | HB1 | HB2 | D64 | G65 | G67 | G74 | S70 | I70 | I73 | C77 | D71
| D70 | D73 | E78 | E86 | M80 | T79 | R86 | T80 | H80 | V09 | H14},
{0x07, 0x08, BINARY, IS_ALPHA, "GT", OP_GT, "X GT Y", "Greater than",
0},
{0x07, 0x08, BINARY, NO_ALPHA, ">", OP_GT, "X > Y", "Greater than",
B15 | B93 | HB1 | HB2 | D64 | G65 | G67 | G74 | S70 | I70 | I73 | C77 | D71
| D70 | D73 | E78 | E86 | M80 | T79 | R86 | T80 | H80 | V09 | H14},
{0x07, 0x08, BINARY, IS_ALPHA, "LIKE", OP_LIKE, "A$ LIKE B$",
"Compare A$ to the pattern in B$",
B15},
{0x07, 0x08, BINARY, IS_ALPHA, "MAX", OP_MAX, "X MAX Y", "Maximum",
0},
{0x07, 0x08, BINARY, IS_ALPHA, "MIN", OP_MIN, "X MIN Y", "Minimum",
0},
/* CONCATENATION */
{0x08, 0x09, BINARY, NO_ALPHA, "&", OP_AMP, "X & Y", "Concatenation",
B15 | B93 | HB2},
/* ARITHMETIC */
{0x09, 0x0A, BINARY, NO_ALPHA, "+", OP_ADD, "X + Y", "Addition",
B15 | B93 | HB1 | HB2 | D64 | G65 | G67 | G74 | S70 | I70 | I73 | C77 | D71
| D70 | D73 | E78 | E86 | M80 | T79 | R86 | T80 | H80 | V09 | H14},
{0x09, 0x0A, BINARY, NO_ALPHA, "-", OP_SUB, "X - Y", "Subtraction",
B15 | B93 | HB1 | HB2 | D64 | G65 | G67 | G74 | S70 | I70 | I73 | C77 | D71
| D70 | D73 | E78 | E86 | M80 | T79 | R86 | T80 | H80 | V09 | H14},
{0x0A, 0x0B, BINARY, IS_ALPHA, "MOD", OP_MOD, "X MOD Y", "Integer Modulus",
B15 | B93 | HB1 | HB2 | D71 | M80 | R86 | T80 | H14},
{0x0B, 0x0C, BINARY, NO_ALPHA, "\\", OP_IDIV, "X \\ Y", "Integer Division",
B15 | B93 | HB1 | HB2 | D64 | G65 | G67 | G74 | S70 | I70 | I73 | C77 | D71
| E78 | E86 | M80 | T80 | H14},
{0x0C, 0x0D, BINARY, NO_ALPHA, "*", OP_MUL, "X * Y", "Multiplication",
B15 | B93 | HB1 | HB2 | D64 | G65 | G67 | G74 | S70 | I70 | I73 | C77 | D71
| D70 | D73 | E78 | E86 | M80 | T79 | R86 | T80 | H80 | V09 | H14},
{0x0C, 0x0D, BINARY, NO_ALPHA, "/", OP_DIV, "X / Y", "Division",
B15 | B93 | HB1 | HB2 | D64 | G65 | G67 | G74 | S70 | I70 | I73 | C77 | D71
| D70 | D73 | E78 | E86 | M80 | T79 | R86 | T80 | H80 | V09 | H14},
{0x0D, 0x0D, UNARY, NO_ALPHA, "#", OP_POS, "# X", "Posation",
B15 | B93 | HB1 | HB2 | D64 | G65 | G67 | G74 | C77 | D71 | E86 | M80 | T79
| R86 | T80 | H80 | H14},
{0x0D, 0x0D, UNARY, NO_ALPHA, "+", OP_POS, "+ X", "Posation",
B15 | B93 | HB1 | HB2 | D64 | G65 | G67 | G74 | S70 | I70 | I73 | C77 | D71
| D70 | D73 | E78 | E86 | M80 | T79 | R86 | T80 | H80 | V09 | H14},
{0x0D, 0x0D, UNARY, NO_ALPHA, "-", OP_NEG, "- X", "Negation",
B15 | B93 | HB1 | HB2 | D64 | G65 | G67 | G74 | S70 | I70 | I73 | C77 | D71
| D70 | D73 | E78 | E86 | M80 | T79 | R86 | T80 | H80 | V09 | H14},
{0x0E, 0x0F, BINARY, NO_ALPHA, "^", OP_EXP, "X ^ Y", "Exponential",
B15 | B93 | HB1 | HB2 | D64 | G65 | G67 | G74 | S70 | I70 | I73 | C77 | D71
| D70 | D73 | E78 | E86 | M80 | T79 | R86 | H80 | V09 | H14},
{0x0E, 0x0F, BINARY, NO_ALPHA, "[", OP_EXP, "X [ Y", "Exponential",
B15 | HB1 | HB2 | T80},
{0x0E, 0x0F, BINARY, NO_ALPHA, "**", OP_EXP, "X ** Y", "Exponential",
B15 | B93 | HB1 | HB2 | D64 | G65 | G67 | G74 | S70 | I70 | I73 | C77 | D71
| D70 | D73 | E78},
};
static const size_t NUM_OPERATORS =
sizeof (OperatorTable) / sizeof (OperatorType);
/*
--------------------------------------------------------------------------------------------
Helpers
--------------------------------------------------------------------------------------------
*/
extern void
SortAllOperators (void) /* SortAllOperators() should be called by bwb_init() */
{
/* sort the operators by decreasing length, so "**" matches before "*" and so on. */
int i;
for (i = 0; i < NUM_OPERATORS - 1; i++)
{
int j;
int k;
int m;
k = i;
m = bwb_strlen (OperatorTable[i].Name);
for (j = i + 1; j < NUM_OPERATORS; j++)
{
int n;
n = bwb_strlen (OperatorTable[j].Name);
if (n > m)
{
m = n;
k = j;
}
}
if (k > i)
{
/* swap */
OperatorType t;
OperatorType *T;
OperatorType *I;
OperatorType *K;
T = &t;
I = &OperatorTable[i];
K = &OperatorTable[k];
bwb_memcpy (T, I, sizeof (t));
bwb_memcpy (I, K, sizeof (t));
bwb_memcpy (K, T, sizeof (t));
}
}
}
static void
SortAllOperatorsForManual (void) /* SortAllOperators() should be called aftwards */
{
/* sort the operators by by precedence (high-to-low) then name (alphabetically). */
int i;
for (i = 0; i < NUM_OPERATORS - 1; i++)
{
int j;
int k;
int m;
k = i;
m = OperatorTable[i].ThisPrec;
for (j = i + 1; j < NUM_OPERATORS; j++)
{
int n;
n = OperatorTable[j].ThisPrec;
if (n > m)
{
m = n;
k = j;
}
else
if (n == m
&& bwb_stricmp (OperatorTable[j].Name, OperatorTable[k].Name) < 0)
{
m = n;
k = j;
}
}
if (k > i)
{
/* swap */
OperatorType t;
OperatorType *T;
OperatorType *I;
OperatorType *K;
T = &t;
I = &OperatorTable[i];
K = &OperatorTable[k];
bwb_memcpy (T, I, sizeof (t));
bwb_memcpy (I, K, sizeof (t));
bwb_memcpy (K, T, sizeof (t));
}
}
}
static char
min_value_type (VariantType * X)
{
/* returns the minimal TypeCode, based upon a NUMBER's value */
assert (X != NULL);
if (isnan (X->Number))
{
/*** FATAL - INTERNAL ERROR - SHOULD NEVER HAPPEN ***/
WARN_INTERNAL_ERROR;
return NulChar;
}
if (X->Number == bwb_rint (X->Number))
{
/* INTEGER */
if (MINBYT <= X->Number && X->Number <= MAXBYT)
{
return ByteTypeCode;
}
if (MININT <= X->Number && X->Number <= MAXINT)
{
return IntegerTypeCode;
}
if (MINLNG <= X->Number && X->Number <= MAXLNG)
{
return LongTypeCode;
}
if (MINCUR <= X->Number && X->Number <= MAXCUR)
{
return CurrencyTypeCode;
}
}
/* FLOAT */
if (MINSNG <= X->Number && X->Number <= MAXSNG)
{
return SingleTypeCode;
}
if (MINDBL <= X->Number && X->Number <= MAXDBL)
{
return DoubleTypeCode;
}
/* OVERFLOW */
if (X->Number < 0)
{
X->Number = MINDBL;
}
else
{
X->Number = MAXDBL;
}
if (WARN_OVERFLOW)
{
/* ERROR */
}
/* CONTINUE */
return DoubleTypeCode;
}
static char
max_number_type (char X, char Y)
{
/* returns the maximal TypeCode, given two NUMBER TypeCode's */
if (X == DoubleTypeCode || Y == DoubleTypeCode)
{
return DoubleTypeCode;
}
if (X == SingleTypeCode || Y == SingleTypeCode)
{
return SingleTypeCode;
}
if (X == CurrencyTypeCode || Y == CurrencyTypeCode)
{
return CurrencyTypeCode;
}
if (X == LongTypeCode || Y == LongTypeCode)
{
return LongTypeCode;
}
if (X == IntegerTypeCode || Y == IntegerTypeCode)
{
return IntegerTypeCode;
}
if (X == ByteTypeCode || Y == ByteTypeCode)
{
return ByteTypeCode;
}
/*** FATAL - INTERNAL ERROR - SHOULD NEVER HAPPEN ***/
WARN_INTERNAL_ERROR;
return NulChar;
}
static char
math_type (VariantType * X, VariantType * Y)
{
/*
**
** Returns the TypeCode resulting from a math operation, such as addition.
** The return TypeCode should be the maximal of:
** a. The original X's TypeCode.
** b. The original Y's TypeCode.
** c. The result's minimal TypeCode.
**
*/
assert (X != NULL);
assert (Y != NULL);
return
max_number_type (max_number_type (X->VariantTypeCode, Y->VariantTypeCode),
min_value_type (X));
}
static char
Largest_TypeCode (char TypeCode, VariantType * X)
{
assert (X != NULL);
if (is_integer_type (X))
{
X->Number = bwb_rint (X->Number);
}
return max_number_type (TypeCode, min_value_type (X));
}
static int
is_string_type (VariantType * X)
{
/* if value is a STRING, then TRUE, else FALSE */
assert (X != NULL);
switch (X->VariantTypeCode)
{
case ByteTypeCode:
case IntegerTypeCode:
case LongTypeCode:
case CurrencyTypeCode:
case SingleTypeCode:
case DoubleTypeCode:
if (X->Buffer != NULL)
{
/*** FATAL - INTERNAL ERROR - SHOULD NEVER HAPPEN ***/
WARN_INTERNAL_ERROR;
return FALSE;
}
return FALSE;
case StringTypeCode:
if (X->Buffer == NULL)
{
/*** FATAL - INTERNAL ERROR - SHOULD NEVER HAPPEN ***/
WARN_INTERNAL_ERROR;
return FALSE;
}
return TRUE;
}
/*** FATAL - INTERNAL ERROR - SHOULD NEVER HAPPEN ***/
WARN_INTERNAL_ERROR;
return FALSE;
}
static int
is_number_type (VariantType * X)
{
/* if value is a NUMBER, then TRUE, else FALSE */
assert (X != NULL);
switch (X->VariantTypeCode)
{
case ByteTypeCode:
case IntegerTypeCode:
case LongTypeCode:
case CurrencyTypeCode:
case SingleTypeCode:
case DoubleTypeCode:
if (X->Buffer != NULL)
{
/*** FATAL - INTERNAL ERROR - SHOULD NEVER HAPPEN ***/
WARN_INTERNAL_ERROR;
return FALSE;
}
return TRUE;
case StringTypeCode:
if (X->Buffer == NULL)
{
/*** FATAL - INTERNAL ERROR - SHOULD NEVER HAPPEN ***/
WARN_INTERNAL_ERROR;
return FALSE;
}
return FALSE;
}
/*** FATAL - INTERNAL ERROR - SHOULD NEVER HAPPEN ***/
WARN_INTERNAL_ERROR;
return FALSE; /* never reached */
}
static int
is_integer_type (VariantType * X)
{
/* if value is an INTEGER, then TRUE, else FALSE */
assert (X != NULL);
switch (X->VariantTypeCode)
{
case ByteTypeCode:
return TRUE;
case IntegerTypeCode:
return TRUE;
case LongTypeCode:
return TRUE;
case CurrencyTypeCode:
return TRUE;
case SingleTypeCode:
return FALSE;
case DoubleTypeCode:
return FALSE;
case StringTypeCode:
return FALSE;
}
/*** FATAL - INTERNAL ERROR - SHOULD NEVER HAPPEN ***/
WARN_INTERNAL_ERROR;
return FALSE;
}
static int
both_string_type (VariantType * X, VariantType * Y)
{
/* if both values are a STRING, then TRUE, else FALSE */
assert (X != NULL);
assert (Y != NULL);
if (is_string_type (X) && is_string_type (Y))
{
return TRUE;
}
return FALSE;
}
static int
both_number_type (VariantType * X, VariantType * Y)
{
/* if both values are a NUMBER, then TRUE, else FALSE */
assert (X != NULL);
assert (Y != NULL);
if (is_number_type (X) && is_number_type (Y))
{
return TRUE;
}
return FALSE;
}
static int
both_integer_type (VariantType * X, VariantType * Y)
{
/* if both values are an INTEGER, then TRUE, else FALSE */
assert (X != NULL);
assert (Y != NULL);
if (is_integer_type (X) && is_integer_type (Y))
{
return TRUE;
}
return FALSE;
}
static int
is_long_value (VariantType * X)
{
/* if the NUMBER's value can be a LONG, then TRUE, else FALSE */
assert (X != NULL);
if (isnan (X->Number))
{
/*** FATAL - INTERNAL ERROR - SHOULD NEVER HAPPEN ***/
WARN_INTERNAL_ERROR;
return FALSE;
}
if (X->Number == bwb_rint (X->Number))
{
if (MINCUR <= X->Number && X->Number <= MAXCUR)
{
return TRUE;
}
}
return FALSE;
}
static int
both_are_long (VariantType * X, VariantType * Y)
{
/* if both values can be a LONG, then TRUE, else FALSE */
assert (X != NULL);
assert (Y != NULL);
if (is_long_value (X) && is_long_value (Y))
{
return TRUE;
}
return FALSE;
}
static int
bwb_isodigit (int C)
{
switch (C)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
return TRUE;
}
return FALSE;
}
/*
--------------------------------------------------------------------------------------------
Operators
--------------------------------------------------------------------------------------------
*/
static ResultType
OP_ADD (VariantType * X, VariantType * Y)
{
assert (X != NULL);
assert (Y != NULL);
if (both_number_type (X, Y))
{
/* X = (X + Y) */
X->Number += Y->Number;
if (both_integer_type (X, Y))
{
X->Number = bwb_rint (X->Number);
}
X->VariantTypeCode = math_type (X, Y);
return RESULT_SUCCESS;
}
if (both_string_type (X, Y))
{
/* X$ = (X$ + Y$) */
return OP_AMP (X, Y);
}
WARN_TYPE_MISMATCH;
return RESULT_ERROR;
}
static ResultType
OP_AMP (VariantType * X, VariantType * Y)
{
/* X$ = (X & Y ) */
/* X$ = (X & Y$) */
/* X$ = (X$ & Y ) */
/* X$ = (X$ & Y$) */
size_t CharsRemaining;
VariantType t;
VariantType *T;
assert (X != NULL);
assert (Y != NULL);
T = &t;
if (X->VariantTypeCode != StringTypeCode)
{
/* coerce X to X$ */
if ((X->Buffer = (char *) calloc (NUMLEN, sizeof (char))) == NULL) /* free() called by OP_ADD() */
{
WARN_OUT_OF_MEMORY;
return RESULT_ERROR;
}
FormatBasicNumber (X->Number, X->Buffer);
X->Length = bwb_strlen (X->Buffer);
X->VariantTypeCode = StringTypeCode;
}
if (Y->VariantTypeCode != StringTypeCode)
{
/* coerce Y to Y$ */
if ((Y->Buffer = (char *) calloc (NUMLEN, sizeof (char))) == NULL) /* free() called by OP_ADD() */
{
WARN_OUT_OF_MEMORY;
return RESULT_ERROR;
}
FormatBasicNumber (Y->Number, Y->Buffer);
Y->Length = bwb_strlen (Y->Buffer);
Y->VariantTypeCode = StringTypeCode;
}
if (X->Length > MAXLEN)
{
WARN_STRING_TOO_LONG;
X->Length = MAXLEN;
}
if (Y->Length > MAXLEN)
{
WARN_STRING_TOO_LONG;
Y->Length = MAXLEN;
}
T->VariantTypeCode = StringTypeCode;
T->Length = X->Length + Y->Length;
if (T->Length > MAXLEN)
{
WARN_STRING_TOO_LONG;
T->Length = MAXLEN;
}
/* we always allocate a buffer, even for non-empty strings */
if ((T->Buffer =
(char *) calloc (T->Length + 1 /* NulChar */ , sizeof (char))) == NULL)
{
WARN_OUT_OF_MEMORY;
return RESULT_ERROR;
}
CharsRemaining = T->Length;
if (X->Length > CharsRemaining)
{
X->Length = CharsRemaining;
}
if (X->Length > 0)
{
bwb_memcpy (T->Buffer, X->Buffer, X->Length);
CharsRemaining -= X->Length;
}
if (Y->Length > CharsRemaining)
{
Y->Length = CharsRemaining;
}
if (Y->Length > 0)
{
bwb_memcpy (&T->Buffer[X->Length], Y->Buffer, Y->Length);
CharsRemaining -= Y->Length;
}
if (CharsRemaining != 0)
{
WARN_INTERNAL_ERROR;
return RESULT_ERROR;
}
T->Buffer[T->Length] = NulChar;
RELEASE_VARIANT (X);
RELEASE_VARIANT (Y);
COPY_VARIANT (X, T);
return RESULT_SUCCESS;
}
static ResultType
OP_SUB (VariantType * X, VariantType * Y)
{
/* X = (X - Y) */
assert (X != NULL);
assert (Y != NULL);
if (both_number_type (X, Y))
{
X->Number -= Y->Number;
if (both_integer_type (X, Y))
{
X->Number = bwb_rint (X->Number);
}
X->VariantTypeCode = math_type (X, Y);
return RESULT_SUCCESS;
}
WARN_TYPE_MISMATCH;
return RESULT_ERROR;
}
static ResultType
OP_MUL (VariantType * X, VariantType * Y)
{
/* X = (X * Y) */
assert (X != NULL);
assert (Y != NULL);
if (both_number_type (X, Y))
{
X->Number *= Y->Number;
if (both_integer_type (X, Y))
{
X->Number = bwb_rint (X->Number);
}
X->VariantTypeCode = math_type (X, Y);
return RESULT_SUCCESS;
}
WARN_TYPE_MISMATCH;
return RESULT_ERROR;
}
static ResultType
OP_IDIV (VariantType * X, VariantType * Y)
{
assert (X != NULL);
assert (Y != NULL);
assert(My != NULL);
assert(My->CurrentVersion != NULL);
if (both_number_type (X, Y))
{
/* X = (X \ Y) */
X->Number = bwb_rint (X->Number);
Y->Number = bwb_rint (Y->Number);
if (Y->Number == 0)
{
/* - Evaluation of an expression results in division
* by zero (nonfatal, the recommended recovery
* procedure is to supply machine infinity with the
* sign of the numerator and continue)
*/
if (X->Number < 0)
{
/* NEGATIVE */
X->Number = MINDBL; /* NEGATIVE INFINITY */
}
else
{
/* POSITIVE */
X->Number = MAXDBL; /* POSITIVE INFINITY */
}
if (WARN_DIVISION_BY_ZERO)
{
return RESULT_ERROR;
}
/* CONTINUE */
}
else
{
DoubleType N;
N = bwb_rint (X->Number / Y->Number);
if (My->CurrentVersion->OptionVersionValue & (R86))
{
/* for RBASIC's RESIDUE function */
My->RESIDUE = bwb_rint (X->Number - N * Y->Number);
}
X->Number = N;
}
X->VariantTypeCode = math_type (X, Y);
return RESULT_SUCCESS;
}
WARN_TYPE_MISMATCH;
return RESULT_ERROR;
}
static ResultType
OP_DIV (VariantType * X, VariantType * Y)
{
assert (X != NULL);
assert (Y != NULL);
if (both_number_type (X, Y))
{
/* X = (X / Y) */
if (both_integer_type (X, Y))
{
return OP_IDIV (X, Y);
}
if (Y->Number == 0)
{
/* - Evaluation of an expression results in division
* by zero (nonfatal, the recommended recovery
* procedure is to supply machine infinity with the
* sign of the numerator and continue)
*/
if (X->Number < 0)
{
/* NEGATIVE */
X->Number = MINDBL; /* NEGATIVE INFINITY */
}
else
{
/* POSITIVE */
X->Number = MAXDBL; /* POSITIVE INFINITY */
}
if (WARN_DIVISION_BY_ZERO)
{
return RESULT_ERROR;
}
/* CONTINUE */
}
else
{
X->Number /= Y->Number;
}
X->VariantTypeCode = math_type (X, Y);
return RESULT_SUCCESS;
}
WARN_TYPE_MISMATCH;
return RESULT_ERROR;
}
static ResultType
OP_MOD (VariantType * X, VariantType * Y)
{
assert (X != NULL);
assert (Y != NULL);
if (both_number_type (X, Y))
{
/* X = (X MOD Y) */
X->Number = bwb_rint (X->Number);
Y->Number = bwb_rint (Y->Number);
if (Y->Number == 0)
{
/* - Evaluation of an expression results in division
* by zero (nonfatal, the recommended recovery