-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexpr.c
868 lines (812 loc) · 19.5 KB
/
expr.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
/*
* Course IFJ @ FIT VUT Brno, 2015
* IFJ15 Interpreter Project
*
* Authors:
* Lukas Osadsky - xosads00
* Pavol Plaskon - xplask00
* Pavel Pospisil - xpospi88
* Matej Postolka - xposto02
*
* Unless otherwise stated, all code is licensed under a
* GNU General Public License v2.0
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "stack.h"
#include "expr.h"
#include "lex.h"
#include "enums.h"
#include "error.h"
#include "ilist.h"
#include "adt.h"
#include "ial.h"
#include "interpret.h"
#include "shared.h"
#include "time.h"
#define RULE_COUNTER 13
const char prece_table[RULE_COUNTER][RULE_COUNTER] = {
/* st\in + - * / ( ) id < > <= >= == != */
/* + */ { HI, HI, LO, LO, LO, HI, LO, HI, HI, HI, HI, HI, HI },
/* - */ { HI, HI, LO, LO, LO, HI, LO, HI, HI, HI, HI, HI, HI },
/* * */ { HI, HI, HI, HI, LO, HI, LO, HI, HI, HI, HI, HI, HI },
/* / */ { HI, HI, HI, HI, LO, HI, LO, HI, HI, HI, HI, HI, HI },
/* ( */ { LO, LO, LO, LO, LO, EQ, LO, LO, LO, LO, LO, LO, LO },
/* ) */ { HI, HI, HI, HI, ER, HI, ER, HI, HI, HI, HI, HI, HI },
/* id */ { HI, HI, HI, HI, ER, HI, ER, HI, HI, HI, HI, HI, HI },
/* < */ { LO, LO, LO, LO, LO, HI, LO, HI, HI, HI, HI, HI, HI },
/* > */ { LO, LO, LO, LO, LO, HI, LO, HI, HI, HI, HI, HI, HI },
/* <= */ { LO, LO, LO, LO, LO, HI, LO, HI, HI, HI, HI, HI, HI },
/* >= */ { LO, LO, LO, LO, LO, HI, LO, HI, HI, HI, HI, HI, HI },
/* == */ { LO, LO, LO, LO, LO, HI, LO, LO, LO, LO, LO, HI, HI },
/* != */ { LO, LO, LO, LO, LO, HI, LO, LO, LO, LO, LO, HI, HI }
};
const char operation_table[4][4] = {
/* dst\src i d s a */
/* i */ {EQ, EQ, ER, ER},
/* d */ {EQ, EQ, ER, ER},
/* s */ {ER, ER, EQ, ER},
/* a */ {EQ, EQ, ER, EQ}
};
TStack *in_out_stack;
TStack *postfix_output_stack;
TStack *t_vars_stack;
TToken *token;
/*
OMEZENÍ
Maximálně 1 000 000 000 proměnných výrazu
INTERNÍ INFORMACE
Aktuální volné error_pos_in_code 21+
*/
/* BEGIN DEBUG FUNCTIONS */
#ifdef DEBUG_MODE
void print_ins_type(const int *type)
{
switch (*type) {
case INS_ASSIGN:
printf("INS_ASSIGN");
break;
case INS_ADD:
printf("INS_ADD");
break;
case INS_SUB:
printf("INS_SUB");
break;
case INS_MUL:
printf("INS_MUL");
break;
case INS_DIV:
printf("INS_DIV");
break;
case INS_EQ:
printf("INS_EQ");
break;
case INS_NEQ:
printf("INS_NEQ");
break;
case INS_GREATER:
printf("INS_GREATER");
break;
case INS_GREATEQ:
printf("INS_GREATEQ");
break;
case INS_LESSER:
printf("INS_LESSER");
break;
case INS_LESSEQ:
printf("INS_LESSEQ");
break;
case INS_JMP:
printf("INS_JMP");
break;
case INS_CJMP:
printf("INS_CJMP");
break;
case INS_LAB:
printf("INS_LAB");
break;
case INS_PUSH_PARAM:
printf("INS_PUSH_PARAM");
break;
case INS_CALL:
printf("INS_CALL");
break;
case INS_RET:
printf("INS_RET");
break;
case INS_PUSH_TAB:
printf("INS_PUSH_TAB");
break;
case INS_POP_TAB:
printf("INS_POP_TAB");
break;
case INS_LENGTH:
printf("INS_LENGTH");
break;
case INS_SUBSTR:
printf("INS_SUBSTR");
break;
case INS_CONCAT:
printf("INS_CONCAT");
break;
case INS_FIND:
printf("INS_FIND");
break;
case INS_SORT:
printf("INS_SORT");
break;
case INS_CIN:
printf("INS_CIN");
break;
case INS_COUT:
printf("INS_COUT");
break;
}
}
void print_variable(const TVariable *var)
{
if (var != NULL) {
if (var->name == NULL) {
printf("%d[NULL] ", var->var_type);
} else {
printf("%d[%s] ", var->var_type, var->name);
}
} else {
printf("NULL ");
}
}
void print_token(const TToken *tok)
{
printf("%d[", tok->type);
switch (tok->type) {
case TOKEN_MUL:
printf("*");
break;
case TOKEN_DIV:
printf("/");
break;
case TOKEN_ADD:
printf("+");
break;
case TOKEN_SUB:
printf("-");
break;
case TOKEN_EQUAL:
printf("==");
break;
case TOKEN_NOT_EQUAL:
printf("!=");
break;
case TOKEN_GREATER:
printf(">");
break;
case TOKEN_GREATER_EQUAL:
printf(">=");
break;
case TOKEN_LESS:
printf("<");
break;
case TOKEN_LESS_EQUAL:
printf("<=");
break;
case TOKEN_SEMICOLON:
printf(";");
break;
default:
printf("%s", tok->data);
}
printf("]");
}
void stack_print(/*const */TStack *st)
{
TToken *tok;
TStack *cache_stack;
cache_stack = stack_init();
while (!stack_empty(st)) {
tok = stack_top(st);
print_token(tok);
printf(" ");
stack_push(cache_stack, tok);
stack_pop(st);
}
while (!stack_empty(cache_stack)) {
tok = stack_top(cache_stack);
stack_push(st, tok);
stack_pop(cache_stack);
}
stack_free(cache_stack);
}
void after_infix()
{
printf("expr: Unget token ");
print_token(token);
printf("\n");
printf("expr: Postfix ");
stack_print(postfix_output_stack);
printf("\n");
}
void before_start()
{
printf("expr: --START--\n");
printf("expr: hint TYPE[VALUE]\n");
token = get_token();
printf("expr: First token ");
print_token(token);
printf("\n");
unget_token(token);
}
void print_ins(const int *ins_type, TVariable *addr1, TVariable *addr2, TVariable *addr3)
{
printf("expr: ");
print_ins_type(ins_type);
printf(" ");
if (*ins_type != INS_CALL) {
print_variable(addr1);
} else {
htab_item *item;
item = (htab_item*)addr1;
printf(" %s", item->data.function->name);
}
print_variable(addr2);
print_variable(addr3);
printf("\n");
}
#endif
/* END DEBUG FUNCTIONS */
/* Special error function with debug printing */
void my_exit_error(const int error_type, const int error_pos_in_code)
{
#ifdef DEBUG_MODE
printf("expr: EXIT_ERROR! %d (error position %d)\n", error_type, error_pos_in_code);
#endif
(void)error_pos_in_code;
exit_error(error_type);
}
/* Compare token type and selected type */
int token_is(const TToken *tok, const int token_type)
{
return tok->type == token_type;
}
/* Returns next variable used in expression calculations */
TVariable* next_t_var(const int *t_x_type, int *t_x_var_counter)
{
TVariable *var;
if (*t_x_var_counter >= t_vars_stack->used) {
var = malloc(sizeof(TVariable));
if (var == NULL) {
my_exit_error(E_ALLOC, 10);
}
var->initialized = 1;
var->constant = 1;
var->var_type = *t_x_type;
stack_push(t_vars_stack, var);
(*t_x_var_counter)++;
return var;
} else {
return t_vars_stack->data[*t_x_var_counter];
}
}
/* Create instruction from input parametres */
TList_item *create_ins(const int ins_type, TVariable *addr1, TVariable *addr2, TVariable *addr3)
{
TList_item *ins;
if (ins_type == INS_ASSIGN) {
if (operation_table[addr1->var_type][addr2->var_type] == ER) {
my_exit_error(E_SEMANTIC_TYPES, 11);
}
if (addr1->var_type == TYPE_AUTO) {
addr1->var_type = addr2->var_type;
}
}
ins = malloc(sizeof(TList_item));
if (ins == NULL) {
my_exit_error(E_ALLOC, 9);
}
ins->ins_type = ins_type;
ins->addr1 = addr1;
ins->addr2 = addr2;
ins->addr3 = addr3;
#ifdef DEBUG_MODE
print_ins(&ins_type, addr1, addr2, addr3);
#endif
return ins;
}
/* Compare variable type and selected type */
int t_compare(const TVariable *var, const int type)
{
return (var->var_type == type);
}
/* Return data type after OPERAND operator OPERAND */
int type_after_operation(const int *operator_type, const TVariable *var_1, const TVariable *var_2)
{
/* String XOR String */
if (t_compare(var_1, TYPE_STRING) ^ t_compare(var_2, TYPE_STRING)) {
my_exit_error(E_SEMANTIC_TYPES, 1);
}
switch (*operator_type) {
case TOKEN_MUL:
case TOKEN_DIV:
case TOKEN_ADD:
case TOKEN_SUB:
/* String +/-* String */
if (t_compare(var_1, TYPE_STRING) || t_compare(var_2, TYPE_STRING)) {
my_exit_error(E_SEMANTIC_TYPES, 2);
}
if (t_compare(var_1, TYPE_DOUBLE) || t_compare(var_2, TYPE_DOUBLE)) {
return TYPE_DOUBLE;
}
}
return TYPE_INT;
}
/* Return instruction type from token type */
int operator_2_ins_type(const TToken *tok)
{
switch (tok->type) {
case TOKEN_MUL:
return INS_MUL;
case TOKEN_DIV:
return INS_DIV;
case TOKEN_ADD:
return INS_ADD;
case TOKEN_SUB:
return INS_SUB;
case TOKEN_EQUAL:
return INS_EQ;
case TOKEN_NOT_EQUAL:
return INS_NEQ;
case TOKEN_GREATER:
return INS_GREATER;
case TOKEN_GREATER_EQUAL:
return INS_GREATEQ;
case TOKEN_LESS:
return INS_LESSER;
case TOKEN_LESS_EQUAL:
return INS_LESSEQ;
case TOKEN_LENGTH:
return INS_LENGTH;
case TOKEN_SUBSTR:
return INS_SUBSTR;
case TOKEN_CONCAT:
return INS_CONCAT;
case TOKEN_FIND:
return INS_FIND;
case TOKEN_SORT:
return INS_SORT;
}
return 0;
}
int token_is_operand(const TToken *tok)
{
switch (tok->type) {
case TOKEN_IDENTIFIER:
case TOKEN_STRING_VALUE:
case TOKEN_INT_VALUE:
case TOKEN_DOUBLE_VALUE:
return 1;
}
return 0;
}
int token_is_operator(const TToken *tok)
{
switch (tok->type) {
case TOKEN_MUL:
case TOKEN_DIV:
case TOKEN_ADD:
case TOKEN_SUB:
case TOKEN_EQUAL:
case TOKEN_NOT_EQUAL:
case TOKEN_GREATER:
case TOKEN_GREATER_EQUAL:
case TOKEN_LESS:
case TOKEN_LESS_EQUAL:
return 1;
}
return 0;
}
/* Counts operands and operators - error founder*/
void postfix_count_test()
{
int counter;
TToken *tok;
counter = -1;
for (int i = 0; i < postfix_output_stack->used; i++) {
tok = postfix_output_stack->data[i];
if (token_is_operand(tok)) {
counter++;
} else {
counter--;
}
}
if (counter) {
my_exit_error(E_SYNTAX, 3);
}
}
/* Return variable from actual function stack or new variable created from constant value */
TVariable *find_var(/*const */TToken *tok, const int clean_data_if_found)
{
switch (tok->type) {
case TOKEN_IDENTIFIER:
for(int i = G.g_frameStack->used-1; i >= 0; i--) {
TFunction *f = G.g_frameStack->data[i];
htab_item *found = htab_lookup(f->local_tab, tok->data);
if(found) {
if (clean_data_if_found == 1) {
free(tok->data);
}
return found->data.variable;
}
}
my_exit_error(E_SEMANTIC_DEF, 4);
case TOKEN_STRING_VALUE:
case TOKEN_INT_VALUE:
case TOKEN_DOUBLE_VALUE:
return token_to_const(tok);
}
return 0;
}
/* Transforms postfix to instructions */
void generate_code(TVariable *ret_var, Tins_list *act_ins_list)
{
TToken *tok;
TList_item *actual_ins;
TVariable *var_1;
TVariable *var_2;
TVariable *var_to_push;
TVariable *new_t_var;
int t_var_type;
int t_var_counter;
t_var_counter = 0;
postfix_count_test();
for (int i = 0; i < postfix_output_stack->used; i++) {
tok = postfix_output_stack->data[i];
if (token_is_operand(tok)) {
var_to_push = find_var(tok, 1);
//free(tok->data);
free(tok);
stack_push(in_out_stack, var_to_push);
} else {
var_1 = stack_top(in_out_stack);
stack_pop(in_out_stack);
var_2 = stack_top(in_out_stack);
stack_pop(in_out_stack);
t_var_type = type_after_operation(&tok->type, var_1, var_2);
new_t_var = next_t_var(&t_var_type, &t_var_counter);
actual_ins = create_ins(operator_2_ins_type(tok), new_t_var, var_2, var_1);
list_insert(act_ins_list, actual_ins);
stack_push(in_out_stack, new_t_var);
free(tok->data); // uvolneni tokenu s operatorem
free(tok); // uvolneni tokenu s operatorem
}
}
actual_ins = create_ins(INS_ASSIGN, ret_var, stack_top(in_out_stack), NULL);
stack_pop(in_out_stack);
list_insert(act_ins_list, actual_ins);
stack_clear(postfix_output_stack); //zasobnik obsahuje prvky NULL
}
/* Returns next function arguments */
TVariable *get_next_para(const int operand_type)
{
TVariable *new_var;
token = get_token();
if (!token_is_operand(token)) {
if (token_is(token, TOKEN_RROUND_BRACKET)) {
my_exit_error(E_SEMANTIC_TYPES, 12);
} else {
my_exit_error(E_SYNTAX, 8);
}
}
new_var = find_var(token, 1);
if (operation_table[operand_type][new_var->var_type] == ER) {
my_exit_error(E_SEMANTIC_TYPES, 5);
}
return new_var;
}
/* For example skips commas between function arguments */
void skip_token(int token_type)
{
token = get_token();
if ((token_type != TOKEN_RROUND_BRACKET) && token_is(token, TOKEN_RROUND_BRACKET)) {
my_exit_error(E_SEMANTIC_TYPES, 20);
}
if (token_is(token, TOKEN_COMMA) && (token_type != TOKEN_COMMA)) {
my_exit_error(E_SEMANTIC_TYPES, 19);
}
if ((token_type == TOKEN_RROUND_BRACKET) && token_is_operand(token)) {
my_exit_error(E_SYNTAX, 6);
}
if (token->type != token_type) {
my_exit_error(E_SYNTAX, 7);
}
}
void generate_external_function(TVariable *ret_var, Tins_list *act_ins_list)
{
TList_item *actual_ins;
htab_item *h_item;
TFunction *f_stored;
int f_sto_param_count;
TVariable *f_stored_var;
TVariable *f_readed_var;
token = get_token();
h_item = htab_lookup(G.g_globalTab, token->data);
f_stored = h_item->data.function;
skip_token(TOKEN_LROUND_BRACKET);
f_sto_param_count = f_stored->params_stack->used;
for(int i = 0; i < f_sto_param_count; i++) {
f_stored_var = f_stored->params_stack->data[i];
f_readed_var = get_next_para(f_stored_var->var_type);
if (i < f_sto_param_count-1) {
skip_token(TOKEN_COMMA);
}
actual_ins = create_ins(INS_PUSH_PARAM, f_readed_var, NULL, NULL);
list_insert(act_ins_list, actual_ins);
}
actual_ins = create_ins(INS_CALL, (TVariable*)h_item, NULL, NULL);
list_insert(act_ins_list, actual_ins);
actual_ins = create_ins(INS_ASSIGN, ret_var, f_stored->return_var, NULL);
list_insert(act_ins_list, actual_ins);
skip_token(TOKEN_RROUND_BRACKET);
}
void generate_internal_function(TVariable *ret_var, Tins_list *act_ins_list)
{
TList_item *actual_ins;
int ins_type;
TVariable *var_1;
TVariable *var_2;
TVariable *var_3;
token = get_token();
ins_type = operator_2_ins_type(token);
skip_token(TOKEN_LROUND_BRACKET);
switch (ins_type) {
case INS_LENGTH:
case INS_SORT:
var_1 = get_next_para(TYPE_STRING);
actual_ins = create_ins(ins_type, ret_var, var_1, NULL);
list_insert(act_ins_list, actual_ins);
break;
case INS_CONCAT:
case INS_FIND:
var_1 = get_next_para(TYPE_STRING);
skip_token(TOKEN_COMMA);
var_2 = get_next_para(TYPE_STRING);
actual_ins = create_ins(ins_type, ret_var, var_1, var_2);
list_insert(act_ins_list, actual_ins);
break;
case INS_SUBSTR:
var_1 = get_next_para(TYPE_STRING);
skip_token(TOKEN_COMMA);
var_2 = get_next_para(TYPE_INT);
skip_token(TOKEN_COMMA);
var_3 = get_next_para(TYPE_INT);
actual_ins = create_ins(INS_PUSH_PARAM, var_1, NULL, NULL);
list_insert(act_ins_list, actual_ins);
actual_ins = create_ins(INS_PUSH_PARAM, var_2, NULL, NULL);
list_insert(act_ins_list, actual_ins);
actual_ins = create_ins(INS_PUSH_PARAM, var_3, NULL, NULL);
list_insert(act_ins_list, actual_ins);
actual_ins = create_ins(ins_type, ret_var, NULL, NULL); /* SUBSTR with ASSIGN params */
list_insert(act_ins_list, actual_ins);
}
skip_token(TOKEN_RROUND_BRACKET);
}
int its_function()
{
int answer;
htab_item *item;
answer = not_function;
token = get_token();
switch (token->type) {
case TOKEN_IDENTIFIER:
item = htab_lookup(G.g_globalTab, token->data);
if (item == NULL) {
find_var(token, 0);
} else {
answer = external_function;
}
break;
case TOKEN_LENGTH:
case TOKEN_SUBSTR:
case TOKEN_CONCAT:
case TOKEN_FIND:
case TOKEN_SORT:
answer = internal_function;
}
unget_token(token);
return answer;
}
int token_to_op_type(const TToken *tok)
{
switch (tok->type) {
case TOKEN_ADD:
return oper_add;
case TOKEN_SUB:
return oper_sub;
case TOKEN_MUL:
return oper_mul;
case TOKEN_DIV:
return oper_div;
case TOKEN_LROUND_BRACKET:
return oper_lr_bracket;
case TOKEN_LESS:
return oper_less;
case TOKEN_GREATER:
return oper_greater;
case TOKEN_LESS_EQUAL:
return oper_less_e;
case TOKEN_GREATER_EQUAL:
return oper_greater_e;
case TOKEN_EQUAL:
return oper_equal;
case TOKEN_NOT_EQUAL:
return oper_not_equal;
}
return 0;
}
int stack_lower_prio(const TToken *tok_st)
{
int x;
int y;
x = token_to_op_type(token);
y = token_to_op_type(tok_st);
if (prece_table[y][x] == LO) {
return 1;
}
return 0;
}
void transfer_rest_of_in_out_stack()
{
while (!stack_empty(in_out_stack)) {
stack_push(postfix_output_stack, stack_top(in_out_stack));
stack_pop(in_out_stack);
}
}
void check_expr_integrity(const TToken *tok, int *last_type)
{
switch (*last_type) {
case TOKEN_EOF:
if (!token_is_operand(tok) && !token_is(tok, TOKEN_LROUND_BRACKET)) {
my_exit_error(E_SYNTAX, 14);
}
break;
case TOKEN_INT_VALUE:
case TOKEN_DOUBLE_VALUE:
case TOKEN_STRING_VALUE:
case TOKEN_IDENTIFIER:
if (!token_is_operator(tok) && !token_is(tok, TOKEN_RROUND_BRACKET)) {
my_exit_error(E_SYNTAX, 15);
}
break;
case TOKEN_LROUND_BRACKET:
if (!token_is_operand(tok) && !token_is(tok, TOKEN_LROUND_BRACKET)) {
my_exit_error(E_SYNTAX, 16);
}
break;
case TOKEN_RROUND_BRACKET:
if (!token_is_operator(tok) && !token_is(tok, TOKEN_RROUND_BRACKET)) {
my_exit_error(E_SYNTAX, 17);
}
break;
case TOKEN_MUL:
case TOKEN_DIV:
case TOKEN_ADD:
case TOKEN_SUB:
case TOKEN_EQUAL:
case TOKEN_NOT_EQUAL:
case TOKEN_GREATER:
case TOKEN_GREATER_EQUAL:
case TOKEN_LESS:
case TOKEN_LESS_EQUAL:
if (!(token_is_operand(tok)) && !token_is(tok, TOKEN_LROUND_BRACKET)) {
my_exit_error(E_SYNTAX, 18);
}
}
*last_type = tok->type;
}
TToken *save_tok()
{
TToken *tok_push;
tok_push = malloc(sizeof(TToken));
tok_push->type = token->type;
tok_push->data = token->data;
return tok_push;
}
void infix_2_postfix()
{
TToken *token_stack;
int bracket_counter;
int last_token_type = TOKEN_EOF;
bracket_counter = 1;
token = get_token();
while ((token->type != TOKEN_SEMICOLON) && (bracket_counter)) {
check_expr_integrity(token, &last_token_type);
switch (token->type) {
case TOKEN_INT_VALUE:
case TOKEN_DOUBLE_VALUE:
case TOKEN_STRING_VALUE:
case TOKEN_IDENTIFIER:
stack_push(postfix_output_stack, save_tok());
break;
case TOKEN_LROUND_BRACKET:
bracket_counter++;
stack_push(in_out_stack, save_tok());
break;
case TOKEN_RROUND_BRACKET:
bracket_counter--;
if (!bracket_counter) {
unget_token(token);
break;
}
token_stack = stack_top(in_out_stack);
stack_pop(in_out_stack);
while (!token_is(token_stack, TOKEN_LROUND_BRACKET))
{
stack_push(postfix_output_stack, token_stack);
token_stack = stack_top(in_out_stack);
stack_pop(in_out_stack);
}
break;
case TOKEN_MUL:
case TOKEN_DIV:
case TOKEN_ADD:
case TOKEN_SUB:
case TOKEN_EQUAL:
case TOKEN_NOT_EQUAL:
case TOKEN_GREATER:
case TOKEN_GREATER_EQUAL:
case TOKEN_LESS:
case TOKEN_LESS_EQUAL:
token_stack = stack_top(in_out_stack);
if (stack_empty(in_out_stack) || stack_lower_prio(token_stack)) {
stack_push(in_out_stack, save_tok());
} else {
while (!stack_empty(in_out_stack) && !stack_lower_prio(token_stack)) {
stack_push(postfix_output_stack, token_stack);
stack_pop(in_out_stack);
token_stack = stack_top(in_out_stack);
}
stack_push(in_out_stack, save_tok());
}
break;
}
token = get_token();
}
transfer_rest_of_in_out_stack();
#ifdef DEBUG_MODE
after_infix();
#endif
unget_token(token);
}
void expr_init()
{
in_out_stack = stack_init();
postfix_output_stack = stack_init();
t_vars_stack = stack_init();
}
void expr_destroy()
{
stack_free(in_out_stack);
stack_free(postfix_output_stack);
stack_free(t_vars_stack);
}
void expression(TVariable *ret_var, Tins_list *act_ins_list)
{
#ifdef DEBUG_MODE
before_start();
#endif
switch (its_function()) {
case not_function:
infix_2_postfix();
generate_code(ret_var, act_ins_list);
break;
case external_function:
generate_external_function(ret_var, act_ins_list);
break;
case internal_function:
generate_internal_function(ret_var, act_ins_list);
}
#ifdef DEBUG_MODE
printf("expr: --END--\n");
#endif
}