-
Notifications
You must be signed in to change notification settings - Fork 1
/
evaluate.c
1032 lines (857 loc) · 23.8 KB
/
evaluate.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
/* ccalc -- evaluate C-style expressions from the command line.
Copyright (C) 2015-2017 Gregory Kikola.
This file is part of ccalc.
ccalc 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 3 of the License, or
(at your option) any later version.
ccalc 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 ccalc. If not, see <http://www.gnu.org/licenses/>.
*/
/* Written by Gregory Kikola <gkikola@gmail.com>. */
#include <math.h>
#include <stdbool.h>
#include <string.h>
#include "error.h"
#include "evaluate.h"
#define MAX_IDENTIFIER_LENGTH 32
#define MAX_ARGUMENTS 8
typedef enum {
TOKEN_UNKNOWN,
TOKEN_END,
TOKEN_IDENTIFIER,
TOKEN_LITERAL,
TOKEN_LEFT_PAREN,
TOKEN_RIGHT_PAREN,
TOKEN_QMARK,
TOKEN_COLON,
TOKEN_OP_COMMA,
TOKEN_OP_PLUS,
TOKEN_OP_MINUS,
TOKEN_OP_TIMES,
TOKEN_OP_DIVIDE,
TOKEN_OP_IDIVIDE,
TOKEN_OP_MOD,
TOKEN_OP_POW,
TOKEN_OP_EQUAL,
TOKEN_OP_NOT_EQUAL,
TOKEN_OP_LESS_THAN,
TOKEN_OP_LESS_THAN_EQ,
TOKEN_OP_GREATER_THAN,
TOKEN_OP_GREATER_THAN_EQ,
TOKEN_OP_AND,
TOKEN_OP_OR,
TOKEN_OP_NOT,
TOKEN_OP_BIT_AND,
TOKEN_OP_BIT_OR,
TOKEN_OP_BIT_XOR,
TOKEN_OP_BIT_NOT,
TOKEN_OP_BIT_SHIFT_LEFT,
TOKEN_OP_BIT_SHIFT_RIGHT,
} token_type;
typedef struct Parser {
char *expr;
int pos;
options *program_opts;
token_type cur_token;
char str_value[MAX_IDENTIFIER_LENGTH + 1];
value numeric_value;
} parser;
bool is_whitespace(char c);
void get_token(parser *parse);
token_type peek_token(parser *parse);
void get_identifier(parser *parse);
void get_literal(parser *parse);
void parse_expression(parser *parse, value *result);
void parse_conditional_expression(parser *parse, value *result);
void parse_logical_or_expression(parser *parse, value *result);
void parse_logical_and_expression(parser *parse, value *result);
void parse_inclusive_or_expression(parser *parse, value *result);
void parse_exclusive_or_expression(parser *parse, value *result);
void parse_and_expression(parser *parse, value *result);
void parse_equality_expression(parser *parse, value *result);
void parse_relational_expression(parser *parse, value *result);
void parse_shift_expression(parser *parse, value *result);
void parse_additive_expression(parser *parse, value *result);
void parse_multiplicative_expression(parser *parse, value *result);
void parse_unary_expression(parser *parse, value *result);
void parse_exponential_expression(parser *parse, value *result);
void parse_primary(parser *parse, value *result);
void evaluate(char *expr, value *result, options *opts) {
parser parse;
parse.expr = expr;
parse.pos = 0;
parse.program_opts = opts;
parse_expression(&parse, result);
//next token should be TOKEN_END
get_token(&parse);
switch (parse.cur_token) {
case TOKEN_END:
break;
case TOKEN_RIGHT_PAREN:
raise_error(ERROR_EXPR, "unmatched parenthesis ')'");
default:
raise_error(ERROR_EXPR, "unexpected token '%s'", parse.str_value);
}
}
bool is_whitespace(char c) {
return c == ' ' || c == '\t' || c == '\r' || c == '\n';
}
void get_token(parser *parse) {
parse->numeric_value.type = INT;
parse->numeric_value.data.ivalue = 0;
parse->str_value[0] = 0;
//skip whitespace
while (is_whitespace(parse->expr[parse->pos]))
parse->pos++;
parse->cur_token = peek_token(parse);
switch (parse->cur_token) {
case TOKEN_END:
break;
case TOKEN_IDENTIFIER:
get_identifier(parse);
break;
case TOKEN_LITERAL:
get_literal(parse);
break;
case TOKEN_LEFT_PAREN:
case TOKEN_RIGHT_PAREN:
case TOKEN_QMARK:
case TOKEN_COLON:
case TOKEN_OP_COMMA:
case TOKEN_OP_PLUS:
case TOKEN_OP_MINUS:
case TOKEN_OP_TIMES:
case TOKEN_OP_DIVIDE:
case TOKEN_OP_MOD:
case TOKEN_OP_LESS_THAN:
case TOKEN_OP_GREATER_THAN:
case TOKEN_OP_NOT:
case TOKEN_OP_BIT_AND:
case TOKEN_OP_BIT_OR:
case TOKEN_OP_BIT_XOR:
case TOKEN_OP_BIT_NOT:
parse->str_value[0] = parse->expr[parse->pos];
parse->str_value[1] = '\0';
parse->pos++;
break;
case TOKEN_OP_IDIVIDE:
case TOKEN_OP_EQUAL:
case TOKEN_OP_NOT_EQUAL:
case TOKEN_OP_LESS_THAN_EQ:
case TOKEN_OP_GREATER_THAN_EQ:
case TOKEN_OP_AND:
case TOKEN_OP_OR:
case TOKEN_OP_BIT_SHIFT_LEFT:
case TOKEN_OP_BIT_SHIFT_RIGHT:
parse->str_value[0] = parse->expr[parse->pos];
parse->str_value[1] = parse->expr[parse->pos + 1];
parse->str_value[2] = '\0';
parse->pos += 2;
break;
case TOKEN_OP_POW:
if (!parse->program_opts->caret_exp || parse->expr[parse->pos] == '*') {
strncpy(parse->str_value, "**", 3);
parse->pos += 2;
} else if (parse->expr[parse->pos] == '^') {
strncpy(parse->str_value, "^", 3);
parse->pos++;
}
}
//skip whitespace
while (is_whitespace(parse->expr[parse->pos]))
parse->pos++;
}
token_type peek_token(parser *parse) {
//skip whitespace
while (is_whitespace(parse->expr[parse->pos]))
parse->pos++;
char next_ch = parse->expr[parse->pos];
if ((next_ch >= 'A' && next_ch <= 'Z')
|| (next_ch >= 'a' && next_ch <= 'z')
|| next_ch == '_') {
return TOKEN_IDENTIFIER;
} else if ((next_ch >= '0' && next_ch <= '9') || next_ch == '.') {
return TOKEN_LITERAL;
} else {
switch (next_ch) {
case 0:
return TOKEN_END;
case '(':
return TOKEN_LEFT_PAREN;
case ')':
return TOKEN_RIGHT_PAREN;
case '?':
return TOKEN_QMARK;
case ':':
return TOKEN_COLON;
case ',':
return TOKEN_OP_COMMA;
case '+':
return TOKEN_OP_PLUS;
case '-':
return TOKEN_OP_MINUS;
case '*':
//could be * or **
if (parse->expr[parse->pos + 1] == '*')
return TOKEN_OP_POW;
else
return TOKEN_OP_TIMES;
case '/':
//could be / or //
if (parse->expr[parse->pos + 1] == '/')
return TOKEN_OP_IDIVIDE;
else
return TOKEN_OP_DIVIDE;
case '%':
return TOKEN_OP_MOD;
case '=':
//must be ==, assignment not supported
if (parse->expr[parse->pos + 1] == '=') {
return TOKEN_OP_EQUAL;
}
else {
raise_error(ERROR_EXPR, "assignment operator '=' is not supported");
return TOKEN_UNKNOWN;
}
case '!':
//could be ! or !=
if (parse->expr[parse->pos + 1] == '=')
return TOKEN_OP_NOT_EQUAL;
else
return TOKEN_OP_NOT;
case '<':
//could be <, <=, or <<
if (parse->expr[parse->pos + 1] == '=')
return TOKEN_OP_LESS_THAN_EQ;
else if (parse->expr[parse->pos + 1] == '<')
return TOKEN_OP_BIT_SHIFT_LEFT;
else
return TOKEN_OP_LESS_THAN;
case '>':
//could be >, >=, or >>
if (parse->expr[parse->pos + 1] == '=')
return TOKEN_OP_GREATER_THAN_EQ;
else if (parse->expr[parse->pos + 1] == '>')
return TOKEN_OP_BIT_SHIFT_RIGHT;
else
return TOKEN_OP_GREATER_THAN;
case '&':
//could be & or &&
if (parse->expr[parse->pos + 1] == '&')
return TOKEN_OP_AND;
else
return TOKEN_OP_BIT_AND;
case '|':
//could be | or ||
if (parse->expr[parse->pos + 1] == '|')
return TOKEN_OP_OR;
else
return TOKEN_OP_BIT_OR;
case '^':
if (parse->program_opts->caret_exp)
return TOKEN_OP_POW;
else
return TOKEN_OP_BIT_XOR;
case '~':
return TOKEN_OP_BIT_NOT;
default:
raise_error(ERROR_EXPR, "unexpected character '%c'", next_ch);
return TOKEN_UNKNOWN;
}
}
}
void get_literal(parser *parse) {
int base = 10;
bool is_float = false;
int start_pos = parse->pos;
//check for a decimal point or exponent
int pos = parse->pos;
while (true) {
char cur_ch = parse->expr[pos];
if ((cur_ch < '0' || cur_ch > '9') && (cur_ch < 'A' || cur_ch > 'F')
&& (cur_ch < 'a' || cur_ch > 'f') && cur_ch != '.')
break;
else if (cur_ch == '.' || cur_ch == 'E' || cur_ch == 'e')
is_float = true;
pos++;
}
//look for binary, hex, or octal prefix
if (parse->expr[parse->pos] == '0') {
switch (parse->expr[parse->pos + 1]) {
case 'x':
case 'X':
base = 16;
parse->pos += 2;
is_float = false;
break;
case 'b':
case 'B':
base = 2;
parse->pos += 2;
is_float = false;
break;
default:
if (!is_float) {
base = 8;
parse->pos += 1;
}
break;
}
}
value result;
result.type = INT;
result.data.ivalue = 0;
int float_exponent = 0;
double float_frac_factor = 0.1;
bool reading_exponent = false;
bool negative_exponent = false;
bool done = false;
while(!done) {
char cur_ch = parse->expr[parse->pos];
if (result.type == INT) {
switch(cur_ch) {
default:
done = true;
break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
if (base == 2 && cur_ch > '1')
raise_error(ERROR_EXPR, "unexpected digit '%c' in binary constant",
cur_ch);
else if (base == 8 && cur_ch > '7')
raise_error(ERROR_EXPR, "unexpected digit '%c' in octal constant",
cur_ch);
result.data.ivalue = base * result.data.ivalue + (cur_ch - '0');
break;
case 'A':
case 'B':
case 'C':
case 'D':
case 'F':
if (base == 10)
raise_error(ERROR_EXPR, "unexpected digit '%c' in constant", cur_ch);
else if (base == 2)
raise_error(ERROR_EXPR, "unexpected digit '%c' in binary constant",
cur_ch);
else if (base == 8)
raise_error(ERROR_EXPR, "unexpected digit '%c' in octal constant",
cur_ch);
result.data.ivalue = base * result.data.ivalue + (10 + cur_ch - 'A');
break;
case 'a':
case 'b':
case 'c':
case 'd':
case 'f':
if (base == 10)
raise_error(ERROR_EXPR, "unexpected digit '%c' in constant", cur_ch);
else if (base == 2)
raise_error(ERROR_EXPR, "unexpected digit '%c' in binary constant",
cur_ch);
else if (base == 8)
raise_error(ERROR_EXPR, "unexpected digit '%c' in octal constant",
cur_ch);
result.data.ivalue = base * result.data.ivalue + (10 + cur_ch - 'a');
break;
case 'E':
case 'e':
if (base == 2)
raise_error(ERROR_EXPR, "unexpected digit '%c' in binary constant",
cur_ch);
else if (base == 8)
raise_error(ERROR_EXPR, "unexpected digit '%c' in octal constant",
cur_ch);
else if (base != 10) //this 'e' is a digit
result.data.ivalue = base * result.data.ivalue
+ (10 + cur_ch - ((cur_ch == 'E') ? 'A' : 'a'));
else { //we've encountered an exponent
result.type = FLOAT;
result.data.fvalue = (double) result.data.ivalue;
reading_exponent = true;
if (parse->expr[parse->pos + 1] == '+') {
parse->pos++;
} else if (parse->expr[parse->pos + 1] == '-') {
negative_exponent = true;
parse->pos++;
}
//make sure we have digits in the exponent
if (parse->expr[parse->pos + 1] < '0'
|| parse->expr[parse->pos + 1] > '9')
raise_error(ERROR_EXPR, "exponent has no digits");
}
break;
case '.':
if (base == 16)
raise_error(ERROR_EXPR, "hexadecimal constant must be an integer");
else if (base == 2)
raise_error(ERROR_EXPR, "binary constant must be an integer");
result.type = FLOAT;
result.data.fvalue = (double) result.data.ivalue;
break;
}
} else if (result.type == FLOAT && reading_exponent) {
switch (cur_ch) {
default:
done = true;
break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
float_exponent = base * float_exponent + (cur_ch - '0');
}
} else { //float, no exponent yet
switch (cur_ch) {
default:
done = true;
break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
result.data.fvalue += float_frac_factor * (cur_ch - '0');
float_frac_factor /= base;
break;
case 'E': case 'e': //float has an exponent modifier
reading_exponent = true;
if (parse->expr[parse->pos + 1] == '+') {
parse->pos++;
} else if (parse->expr[parse->pos + 1] == '-') {
negative_exponent = true;
parse->pos++;
}
//make sure we have digits in the exponent
if (parse->expr[parse->pos + 1] < '0'
|| parse->expr[parse->pos + 1] > '9')
raise_error(ERROR_EXPR, "exponent has no digits");
break;
}
}
if (!done)
parse->pos++;
}
if (result.type == FLOAT && float_exponent) {
if (negative_exponent)
float_exponent = -float_exponent;
result.data.fvalue *= pow(10, float_exponent);
}
parse->cur_token = TOKEN_LITERAL;
parse->numeric_value.type = result.type;
parse->numeric_value.data = result.data;
//save string-form for use in error messages
int length = parse->pos - start_pos;
if (length >= MAX_IDENTIFIER_LENGTH)
length = MAX_IDENTIFIER_LENGTH - 1;
strncpy(parse->str_value, parse->expr + start_pos, length);
parse->str_value[length] = '\0';
}
void get_identifier(parser *parse) {
int length = 0;
while (length <= MAX_IDENTIFIER_LENGTH) {
char cur_ch = parse->expr[parse->pos];
if ((cur_ch >= 'A' && cur_ch <= 'Z')
|| (cur_ch >= 'a' && cur_ch <= 'z')
|| (cur_ch >= '0' && cur_ch <= '9')
|| cur_ch == '_') {
parse->str_value[length] = cur_ch;
length++;
parse->pos++;
} else {
break;
}
}
parse->cur_token = TOKEN_IDENTIFIER;
parse->str_value[length] = '\0';
}
void parse_expression(parser *parse, value *result) {
parse_conditional_expression(parse, result);
bool done = false;
while (!done) {
switch (peek_token(parse)) {
default:
done = true;
break;
case TOKEN_OP_COMMA:
get_token(parse);
//evaluate right side, discard everything to the left of the comma
parse_conditional_expression(parse, result);
break;
}
}
}
void parse_conditional_expression(parser *parse, value *result) {
value left;
parse_logical_or_expression(parse, result);
left = *result;
if (peek_token(parse) == TOKEN_QMARK) {
//this is a conditional expression
value on_true, on_false;
get_token(parse);
//read first value
parse_expression(parse, &on_true);
//read colon
get_token(parse);
switch (parse->cur_token) {
case TOKEN_COLON:
break;
case TOKEN_END:
raise_error(ERROR_EXPR, "unexpected end of input");
default:
raise_error(ERROR_EXPR, "unexpected token '%s'", parse->str_value);
}
//read second value
parse_conditional_expression(parse, &on_false);
conditional(&left, &on_true, &on_false, result);
}
}
void parse_logical_or_expression(parser *parse, value *result) {
value left, right;
parse_logical_and_expression(parse, result);
left = *result;
bool done = false;
while (!done) {
switch (peek_token(parse)) {
default:
done = true;
break;
case TOKEN_OP_OR:
get_token(parse);
parse_logical_and_expression(parse, &right);
or(&left, &right, result);
left = *result;
break;
}
}
}
void parse_logical_and_expression(parser *parse, value *result) {
value left, right;
parse_inclusive_or_expression(parse, result);
left = *result;
bool done = false;
while (!done) {
switch (peek_token(parse)) {
default:
done = true;
break;
case TOKEN_OP_AND:
get_token(parse);
parse_inclusive_or_expression(parse, &right);
and(&left, &right, result);
left = *result;
break;
}
}
}
void parse_inclusive_or_expression(parser *parse, value *result) {
value left, right;
parse_exclusive_or_expression(parse, result);
left = *result;
bool done = false;
while (!done) {
switch (peek_token(parse)) {
default:
done = true;
break;
case TOKEN_OP_BIT_OR:
get_token(parse);
parse_exclusive_or_expression(parse, &right);
bit_or(&left, &right, result);
left = *result;
break;
}
}
}
void parse_exclusive_or_expression(parser *parse, value *result) {
value left, right;
parse_and_expression(parse, result);
left = *result;
bool done = false;
while (!done) {
switch (peek_token(parse)) {
default:
done = true;
break;
case TOKEN_OP_BIT_XOR:
get_token(parse);
parse_and_expression(parse, &right);
bit_xor(&left, &right, result);
left = *result;
break;
}
}
}
void parse_and_expression(parser *parse, value *result) {
value left, right;
parse_equality_expression(parse, result);
left = *result;
bool done = false;
while (!done) {
switch (peek_token(parse)) {
default:
done = true;
break;
case TOKEN_OP_BIT_AND:
get_token(parse);
parse_equality_expression(parse, &right);
bit_and(&left, &right, result);
left = *result;
break;
}
}
}
void parse_equality_expression(parser *parse, value *result) {
value left, right;
parse_relational_expression(parse, result);
left = *result;
bool done = false;
while (!done) {
switch (peek_token(parse)) {
default:
done = true;
break;
case TOKEN_OP_EQUAL:
get_token(parse);
parse_relational_expression(parse, &right);
equal(&left, &right, result);
left = *result;
break;
case TOKEN_OP_NOT_EQUAL:
get_token(parse);
parse_relational_expression(parse, &right);
not_equal(&left, &right, result);
left = *result;
break;
}
}
}
void parse_relational_expression(parser *parse, value *result) {
value left, right;
parse_shift_expression(parse, result);
left = *result;
bool done = false;
while (!done) {
switch (peek_token(parse)) {
default:
done = true;
break;
case TOKEN_OP_LESS_THAN:
get_token(parse);
parse_shift_expression(parse, &right);
less_than(&left, &right, result);
left = *result;
break;
case TOKEN_OP_LESS_THAN_EQ:
get_token(parse);
parse_shift_expression(parse, &right);
less_than_eq(&left, &right, result);
left = *result;
break;
case TOKEN_OP_GREATER_THAN:
get_token(parse);
parse_shift_expression(parse, &right);
greater_than(&left, &right, result);
left = *result;
break;
case TOKEN_OP_GREATER_THAN_EQ:
get_token(parse);
parse_shift_expression(parse, &right);
greater_than_eq(&left, &right, result);
left = *result;
break;
}
}
}
void parse_shift_expression(parser *parse, value *result) {
value left, right;
parse_additive_expression(parse, result);
left = *result;
bool done = false;
while (!done) {
switch (peek_token(parse)) {
default:
done = true;
break;
case TOKEN_OP_BIT_SHIFT_LEFT:
get_token(parse);
parse_additive_expression(parse, &right);
bit_shift_left(&left, &right, result);
left = *result;
break;
case TOKEN_OP_BIT_SHIFT_RIGHT:
get_token(parse);
parse_additive_expression(parse, &right);
bit_shift_right(&left, &right, result);
left = *result;
break;
}
}
}
void parse_additive_expression(parser *parse, value *result) {
value left, right;
parse_multiplicative_expression(parse, result);
left = *result;
bool done = false;
while (!done) {
switch (peek_token(parse)) {
default:
done = true;
break;
case TOKEN_OP_PLUS:
get_token(parse);
parse_multiplicative_expression(parse, &right);
add(&left, &right, result);
left = *result;
break;
case TOKEN_OP_MINUS:
get_token(parse);
parse_multiplicative_expression(parse, &right);
subtract(&left, &right, result);
left = *result;
break;
}
}
}
void parse_multiplicative_expression(parser *parse, value *result) {
value left, right;
parse_unary_expression(parse, result);
left = *result;
bool done = false;
while (!done) {
switch (peek_token(parse)) {
default:
done = true;
break;
case TOKEN_OP_TIMES:
get_token(parse);
parse_unary_expression(parse, &right);
multiply(&left, &right, result);
left = *result;
break;
case TOKEN_OP_DIVIDE:
get_token(parse);
parse_unary_expression(parse, &right);
divide(&left, &right, result);
left = *result;
break;
case TOKEN_OP_IDIVIDE:
get_token(parse);
parse_unary_expression(parse, &right);
int_divide(&left, &right, result);
left = *result;
break;
case TOKEN_OP_MOD:
get_token(parse);
parse_unary_expression(parse, &right);
modulo(&left, &right, result);
left = *result;
break;
}
}
}
void parse_unary_expression(parser *parse, value *result) {
value operand;
switch (peek_token(parse)) {
default:
parse_exponential_expression(parse, result);
break;
case TOKEN_OP_PLUS:
get_token(parse);
parse_unary_expression(parse, result);
break;
case TOKEN_OP_MINUS:
get_token(parse);
parse_unary_expression(parse, &operand);
negate(&operand, result);
break;
case TOKEN_OP_BIT_NOT:
get_token(parse);
parse_unary_expression(parse, &operand);
bit_not(&operand, result);
break;
case TOKEN_OP_NOT:
get_token(parse);
parse_unary_expression(parse, &operand);
not(&operand, result);
break;
}
}
void parse_exponential_expression(parser *parse, value *result) {
value left, right;
parse_primary(parse, result);
left = *result;
bool done = false;
while (!done) {
switch (peek_token(parse)) {
default:
done = true;
break;
case TOKEN_OP_POW:
get_token(parse);
parse_unary_expression(parse, &right);
power(&left, &right, result);
left = *result;
break;
}
}
}
void parse_primary(parser *parse, value *result) {
get_token(parse);
switch (parse->cur_token) {
default:
raise_error(ERROR_EXPR, "unexpected token '%s'", parse->str_value);
break;
case TOKEN_END:
raise_error(ERROR_EXPR, "unexpected end of input");
break;
case TOKEN_IDENTIFIER:
//is this a function or a constant?
if (peek_token(parse) == TOKEN_LEFT_PAREN) {
int argc = 0;
value argv[MAX_ARGUMENTS];
char id[MAX_IDENTIFIER_LENGTH + 1];
strncpy(id, parse->str_value, MAX_IDENTIFIER_LENGTH + 1);
//eat parenthesis
get_token(parse);
//is there an argument list?
if (peek_token(parse) == TOKEN_RIGHT_PAREN) {
argc = 0;
} else {
bool done = false;
while (!done) {
if (argc >= MAX_ARGUMENTS)
raise_error(ERROR_EXPR, "too many arguments to function '%s'", id);
parse_conditional_expression(parse, &argv[argc]);
argc++;
switch (peek_token(parse)) {
default:
done = true;
break;
case TOKEN_OP_COMMA:
get_token(parse);
break;
}