-
Notifications
You must be signed in to change notification settings - Fork 6
/
pc.c
3361 lines (3043 loc) · 88.6 KB
/
pc.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <setjmp.h>
#include <conio.h>
#include <math.h>
#include <time.h>
#include "def.h"
#include "lib.h"
int main(int argc, char *argv[]){
if(argc > 1) load_program(argv[1]);
else load_program("p.pc");
prog = pbuf; // resets pointer to the beginning of the program
initial_setup();
// pre scans the source
pre_scan();
// finds the main function and executes it
execute_main();
/*
register int i,j;
for(i=0; i<struct_table_index; i++){
puts(struct_table[i].struct_name);
for(j=0; *struct_table[i].fields[j].field_name; j++){
printf("\t%s - type: %d", struct_table[i].fields[j].field_name, struct_table[i].fields[j].type);
}
putchar('\n');
}
*/
// releases used memory
free_mem();
return 0;
}
void initial_setup(void){
// sets up the stack variables
global_var_tos = 0;
local_var_tos = 0;
user_func_call_index = 1;
local_var_tos_history[0] = 0;
}
void free_mem(void){
register int i;
free_string_table();
free(pbuf);
for(i = 0; i < local_var_tos; i++)
if(_is_matrix(&local_variables[i])) free(local_variables[i].data.value.p);
}
void load_program(char *filename){
FILE *fp;
int i;
if((fp = fopen(filename, "rb")) == NULL){
printf("program source file not found");
exit(0);
}
if((pbuf = malloc(PROG_SIZE)) == NULL){
printf("failed to allocate memory for the program source");
exit(0);
}
prog = pbuf;
i = 0;
do{
*prog = getc(fp);
prog++;
i++;
} while(!feof(fp) && i < PROG_SIZE);
fclose(fp);
if(*(prog - 2) == 0x1A) *(prog - 2) = '\0';
else *(prog - 1) = '\0';
}
void pre_scan(void){
char *tp;
do{
tp = prog;
get_token();
if(token_type == END) return;
if(tok == DIRECTIVE){
get_token();
if(tok != INCLUDE) show_error(UNKNOWN_DIRECTIVE);
get_token();
if(tok != LESS_THAN) show_error(DIRECTIVE_SYNTAX);
get_token();
include_lib(token);
get_token();
if(tok != GREATER_THAN) show_error(DIRECTIVE_SYNTAX);
continue;
}
if(tok == STRUCT){
declare_struct();
continue;
}
if(tok == CONST) get_token();
if(tok != VOID && tok != CHAR && tok != INT && tok != FLOAT && tok != DOUBLE) show_error(NOT_VAR_OR_FUNC_OUTSIDE);
get_token();
while(tok == ASTERISK) get_token();
if(token_type != IDENTIFIER) show_error(IDENTIFIER_EXPECTED);
get_token();
if(tok == OPENING_PAREN){ //it must be a function declaration
prog = tp;
declare_func();
find_end_of_BLOCK();
}
else { //it must be variable declarations
prog = tp;
declare_global();
}
} while(token_type != END);
}
void dbg(char *s){
puts(s);
system("pause");
}
void call_lib_func(_FPTR fp){
func_ret.type = DT_INT;
func_ret.value.i = 0;
func_ret.ind_level = 0;
fp();
}
_FPTR find_lib_func(char *func_name){
register int i;
for(i = 0; *active_func_table[i].func_name; i++)
if(!strcmp(active_func_table[i].func_name, func_name)) return active_func_table[i].fp;
return NULL;
}
_DATA get_constant(char *str){
_DATA d;
register int i;
for(i = 0; i < active_const_table_tos; i++)
if(!strcmp(active_const_table[i].str, str)){
return active_const_table[i].data;
}
d.type = 0;
return d;
}
void execute_main(void){
register int i;
for(i = 0; *user_func_table[i].func_name; i++)
if(!strcmp(user_func_table[i].func_name, "main")){
current_func_index = i;
prog = user_func_table[i].code_location;
interp_block(); // starts interpreting the main function block;
return;
}
show_error(NO_MAIN_FOUND);
}
void call_func(int func_index){
char *t;
int temp_local_tos;
if(user_func_call_index == MAX_USER_FUNC_CALLS) show_error(USER_FUNC_CALLS_LIMIT_REACHED);
current_func_index = func_index;
temp_local_tos = local_var_tos;
get_func_parameters(func_index); // pushes the parameter variables into the local variables stack
if(tok != CLOSING_PAREN) show_error(CLOSING_PAREN_EXPECTED);
local_var_tos_history[user_func_call_index] = temp_local_tos;
user_func_call_index++;
// saves the current program address
t = prog;
prog = user_func_table[func_index].code_location; // sets the program pointer to the beginning of the function code, just before the "{" token
// resets the function returning value to 0
func_ret.value.c = 0;
func_ret.value.i = 0;
func_ret.value.f = 0.0;
func_ret.value.d = 0.0;
// starts executing the function code
interp_block();
// converts the expression value into the type defined by the function, if the function is not of the void type
convert_data(&func_ret, user_func_table[func_index].return_type);
// recovers the previous program address
prog = t;
// frees any arrays created by this function
register int i;
for(i = local_var_tos_history[user_func_call_index - 1]; i < local_var_tos; i++)
if(_is_matrix(&local_variables[i])) free(local_variables[i].data.value.p);
//recovers the previous local variable top of stack
user_func_call_index--;
local_var_tos = local_var_tos_history[user_func_call_index];
// resets the block jump flag
jump_flag = JF_NULL;
}
void get_func_parameters(int func_index){
register int i;
_VAR var;
// if this function has no parameters
if(!*user_func_table[func_index].parameters[0].param_name){
get_token();
return;
}
for(i = 0; *user_func_table[func_index].parameters[i].param_name; i++){
eval(&var.data);
convert_data(&var.data, user_func_table[func_index].parameters[i].type);
var.data.ind_level = user_func_table[func_index].parameters[i].ind_level;
strcpy(var.var_name, user_func_table[func_index].parameters[i].param_name);
var.constant = user_func_table[func_index].parameters[i].constant;
var.dims[0] = 0;
local_push(&var);
if(tok != COMMA && tok != CLOSING_PAREN) show_error(SYNTAX);
}
}
void convert_data(_DATA *data_to_convert, _BASIC_DATA into_type){
//converts the return value into the type defined by the function returning value
switch(into_type){
case DT_CHAR:
switch(data_to_convert -> type){
case DT_CHAR:
break;
case DT_INT:
data_to_convert -> value.c = (char) data_to_convert -> value.i;
break;
case DT_FLOAT:
data_to_convert -> value.c = (char) data_to_convert -> value.f;
break;
case DT_DOUBLE:
data_to_convert -> value.c = (char) data_to_convert -> value.d;
}
data_to_convert -> type = DT_CHAR;
break;
case DT_INT:
switch(data_to_convert -> type){
case DT_CHAR:
data_to_convert -> value.i = (int) data_to_convert -> value.c;
break;
case DT_INT:
break;
case DT_FLOAT:
data_to_convert -> value.i = (int) data_to_convert -> value.f;
break;
case DT_DOUBLE:
data_to_convert -> value.i = (int) data_to_convert -> value.d;
}
data_to_convert -> type = DT_INT;
break;
case DT_FLOAT:
switch(data_to_convert -> type){
case DT_CHAR:
data_to_convert -> value.f = (float) data_to_convert -> value.c;
break;
case DT_INT:
data_to_convert -> value.f = (float) data_to_convert -> value.i;
break;
case DT_FLOAT:
break;
case DT_DOUBLE:
data_to_convert -> value.f = (float) data_to_convert -> value.d;
}
data_to_convert -> type = DT_FLOAT;
break;
case DT_DOUBLE:
switch(data_to_convert -> type){
case DT_CHAR:
data_to_convert -> value.d = (double) data_to_convert -> value.c;
break;
case DT_INT:
data_to_convert -> value.d = (double) data_to_convert -> value.i;
break;
case DT_FLOAT:
data_to_convert -> value.d = (double) data_to_convert -> value.f;
break;
case DT_DOUBLE:
break;
}
data_to_convert -> type = DT_DOUBLE;
}
}
void interp_block(){
_DATA expr;
int brace = 0;
do{
// bypasses this block, if a break, continue or return statement has been found
if(jump_flag){
while(brace){
if(*prog == '{') brace++;
else if(*prog == '}') brace--;
prog++;
}
break; // a break statement has been found somewhere
}
get_token();
switch(tok){
case RETURN:
// evaluates the expression and puts it into the function return value variable
get_token();
if(tok == SEMICOLON){
expr.type = DT_CHAR;
expr.value.c = 0;
}
else{
putback();
eval(&expr);
}
if(tok != SEMICOLON) show_error(SEMICOLON_EXPECTED);
func_ret = expr;
jump_flag = JF_RETURN;
break;
case IF:
exec_if();
break;
case FOR:
exec_for();
break;
case WHILE:
exec_while();
break;
case DO:
exec_do();
break;
case BREAK:
get_token();
if(tok != SEMICOLON) show_error(SEMICOLON_EXPECTED);
jump_flag = JF_BREAK;
break;
case CONTINUE:
get_token();
if(tok != SEMICOLON) show_error(SEMICOLON_EXPECTED);
jump_flag = JF_CONTINUE;
break;
case CONST:
case VOID:
case CHAR:
case INT:
case FLOAT:
case DOUBLE:
case STRUCT:
putback();
declare_local();
break;
case OPENING_BRACE:
brace++;
break;
case CLOSING_BRACE:
brace--;
break;
case SEMICOLON:
break;
default:
if(token_type == END) show_error(CLOSING_BRACE_EXPECTED);
putback();
eval(&expr);
if(tok != SEMICOLON) show_error(SEMICOLON_EXPECTED);
}
} while(brace); // exits when it finds the last closing brace
}
void exec_switch(void){
}
void exec_while(void){
_DATA cond;
char *cond_loc;
cond_loc = prog; // holds the start of the condition expression
eval(&cond);
putback(); // puts the last token back, which may be a "{" token or another command
convert_data(&cond, DT_INT);
while(cond.value.i){
interp_block();
if(jump_flag == JF_BREAK || jump_flag == JF_RETURN) break; // if a continue, break or return command has been found, then this loop stops
else if(jump_flag == JF_CONTINUE) jump_flag = JF_NULL;
prog = cond_loc;
eval(&cond);
putback(); // puts the last token back
convert_data(&cond, DT_INT);
}
if(jump_flag == JF_NULL) find_end_of_block();
else if(jump_flag == JF_BREAK) jump_flag = JF_NULL; // resets the jump flag
}
void exec_do(void){
_DATA cond;
char *block_loc;
jump_flag = JF_NULL;
block_loc = prog;
do{
interp_block();
get_token();
if(tok != WHILE) show_error(WHILE_KEYWORD_EXPECTED);
if(jump_flag == JF_BREAK || jump_flag == JF_RETURN){
find_end_of_block(); // gets past the conditional expression
// resets the loop jumping flag to null, in case this loop is inside another loop
// but if the jump flag is a return flag, then it's not reset, otherwise the block which called this loop
// will not be skipped, and the function that called that block will not stop executing
if(jump_flag == JF_BREAK) jump_flag = JF_NULL;
break;
}
else if(jump_flag == JF_CONTINUE){
prog = block_loc;
jump_flag = JF_NULL;
continue;
}
eval(&cond);
convert_data(&cond, DT_CHAR);
if(cond.value.c) prog = block_loc;
} while(cond.value.c);
}
void exec_for(void){
_DATA expr, cond;
char *cond_loc, *incr_loc, *block_begin;
get_token();
if(tok != OPENING_PAREN) show_error(OPENING_PAREN_EXPECTED);
get_token();
if(tok != SEMICOLON){
putback();
eval(&expr);
}
if(tok != SEMICOLON) show_error(SEMICOLON_EXPECTED);
cond_loc = prog; // holds the condition code location
// checks for an empty condition, which means always true
get_token();
if(tok != SEMICOLON){
putback();
eval(&cond);
convert_data(&cond, DT_INT);
if(tok != SEMICOLON) show_error(SEMICOLON_EXPECTED);
}
else{
cond.type = DT_INT;
cond.value.i = 1;
}
incr_loc = prog; // holds the incremement code location
// gets past the increment expression, since it is not needed in the first loop
int paren = 1;
do{
if(*prog == '(') paren++;
else if(*prog == ')') paren--;
prog++;
} while(paren && *prog);
if(!*prog) show_error(CLOSING_PAREN_EXPECTED);
block_begin = prog; // holds the beginning of the for block
for( ; cond.value.i; ){
interp_block();
if(jump_flag == JF_CONTINUE) jump_flag = JF_NULL; // resets the jump flag, if a continue statement has been found, so that the loop continues to run normally
else if(jump_flag == JF_BREAK || jump_flag == JF_RETURN) break; // if either a break or a return command has been found, this stops the loop
prog = incr_loc;
// checks for an empty increment expression
get_token();
if(tok != CLOSING_PAREN){
putback();
eval(&expr);
}
prog = cond_loc;
// checks for an empty conditional expression, which means always true.
// Since if the condition is empty, it has been checked before entering the loop,
// the truth value of 1 is still valid, so there's no need to reset the condition to true
get_token();
if(tok != SEMICOLON){
putback();
eval(&cond);
convert_data(&cond, DT_INT);
}
prog = block_begin;
}
// if no jump flag has been found, then finds the end of the block, since after every loop, the program pointer is reset to the beginning of the block
if(jump_flag == JF_NULL) find_end_of_block();
else if(jump_flag == JF_BREAK) jump_flag = JF_NULL; // resets the flag, in case a break command was found, but not if a return command was found
}
void exec_if(void){
_DATA cond;
eval(&cond);
convert_data(&cond, DT_CHAR); // converts the conditional expression into a char
if(cond.value.c){
putback(); // puts the "{" or any other token back
interp_block();
// finds the end of the if structure
get_token();
while(tok == ELSE){
find_end_of_block(); // if there is an else token following the if statement, it must be skipped
get_token();
}
putback();
}
else{
putback(); // puts the "{" or ";" or other tokens back
find_end_of_block();
get_token();
if(tok == ELSE) interp_block();
else putback();
}
}
void find_end_of_block(void){
int paren = 0;
get_token();
switch(tok){
case IF:
// skips the conditional expression between parenthesis
get_token();
if(tok != OPENING_PAREN) show_error(OPENING_PAREN_EXPECTED);
paren = 1; // found the first parenthesis
do{
if(*prog == '(') paren++;
else if(*prog == ')') paren--;
prog++;
} while(paren && *prog);
if(!*prog) show_error(CLOSING_PAREN_EXPECTED);
find_end_of_block();
get_token();
if(tok == ELSE) find_end_of_block();
else
putback();
break;
case OPENING_BRACE: // if it's a block, then the block is skipped
putback();
find_end_of_BLOCK();
break;
case FOR:
get_token();
if(tok != OPENING_PAREN) show_error(OPENING_PAREN_EXPECTED);
paren = 1;
do{
if(*prog == '(') paren++;
else if(*prog == ')') paren--;
prog++;
} while(paren && *prog);
if(!*prog) show_error(CLOSING_PAREN_EXPECTED);
get_token();
if(tok != SEMICOLON){
putback();
find_end_of_block();
}
break;
default: // if it's not a keyword, then it must be an expression
putback(); // puts the last token back, which might be a ";" token
while(*prog++ != ';' && *prog);
if(!*prog) show_error(SEMICOLON_EXPECTED);
}
}
void find_end_of_BLOCK(void){
int brace = 0;
do{
if(*prog == '{') brace++;
else if(*prog == '}') brace--;
prog++;
} while(brace && *prog);
if(brace && !*prog) show_error(CLOSING_BRACE_EXPECTED);
}
void eval(_DATA *v){
eval_attrib(v);
}
// this function is necessary because any variables may be a matrix, or a struct. and there could be structs and matrices inside the original struct, so this function needs to go deep
// inside the structs and matrices in order to find the final field value, and then return it back to be read or assigned a new value.
_DATA_TYPE_OFFSET get_var_offset(char *var_name){
}
void eval_attrib(_DATA *v){
_DATA address;
char var_name[ID_LEN];
char *temp_prog;
temp_prog = prog;
get_token();
if(token_type == IDENTIFIER){
strcpy(var_name, token);
get_token();
if(tok == ATTRIBUTION){
if(is_matrix(var_name)) show_error(INVALID_MATRIX_ATTRIBUTION);
eval_attrib(v);
assign_var(var_name, v);
return;
}
else if(tok == OPENING_BRACKET){ // tests a matrix attribution
if(!is_matrix(var_name)) show_error(MATRIX_EXPECTED);
do{
while(*prog != ']' && *prog) prog++;
if(!*prog) show_error(CLOSING_BRACKET_EXPECTED);
prog++; // gets past the "]" token
get_token();
} while(tok == OPENING_BRACKET);
if(tok == ATTRIBUTION){ // is a matrix attrib.
_VAR *matrix;
_DATA index, expr;
int i; char data_size;
void *matrix_p;
prog = temp_prog;
get_token(); // gets past the variable name
matrix = get_var_pointer(var_name);
matrix_p = matrix -> data.value.p; // sets matrix_p to the beginning of the matrix memory block
v -> type = matrix -> data.type;
v -> ind_level = matrix -> data.ind_level;
data_size = get_data_size(&matrix -> data); // gets the matrix data size
// gets the correct matrix offset position for the assignment
get_token(); // gets past the first bracket
for(i = 0; matrix -> dims[i] && tok == OPENING_BRACKET; i++){
eval(&index);
if(tok != CLOSING_BRACKET) show_error(CLOSING_BRACKET_EXPECTED);
convert_data(&index, DT_INT);
matrix_p = matrix_p + index.value.i * get_matrix_offset(i, matrix) * data_size;
get_token();
}
if(i != matrix_dim_count(matrix)) show_error(INVALID_MATRIX_ATTRIBUTION);
// prog now should be past the equal sign
eval(&expr);
if(is_pointer(&(matrix -> data))){
if(!is_pointer(&expr)) show_error(POINTER_EXPECTED);
*(void **)(matrix_p) = expr.value.p;
v -> value.p = expr.value.p;
}
else{
switch(matrix -> data.type){
case DT_CHAR:
convert_data(&expr, DT_CHAR);
*(char *) matrix_p = expr.value.c;
break;
case DT_INT:
convert_data(&expr, DT_INT);
*(int *) matrix_p = expr.value.i;
break;
case DT_FLOAT:
convert_data(&expr, DT_FLOAT);
*(float *) matrix_p = expr.value.f;
break;
case DT_DOUBLE:
convert_data(&expr, DT_DOUBLE);
*(double *) matrix_p = expr.value.d;
}
}
return;
}
}
}
else if(tok == ASTERISK){ // tests if this is a pointer assignment
while(tok != SEMICOLON && token_type != END){
get_token();
if(tok == ATTRIBUTION){ // is an attribution statement
prog = temp_prog; // goes back to the beginning of the expression
get_token(); // gets past the first asterisk
eval_atom(&address);
if(!is_pointer(&address)) show_error(POINTER_EXPECTED);
// after evaluating the address expression, the token will be a "="
eval(v); // evaluates the value to be attributed to the address
convert_data(v, address.type);
if(address.ind_level == 1)
switch(address.type){
case DT_CHAR:
convert_data(v, DT_CHAR);
*(char *) address.value.p = v -> value.c;
break;
case DT_INT:
convert_data(v, DT_INT);
*(int *) address.value.p = v -> value.i;
break;
case DT_FLOAT:
convert_data(v, DT_FLOAT);
*(float *) address.value.p = v -> value.f;
break;
case DT_DOUBLE:
convert_data(v, DT_DOUBLE);
*(double *) address.value.p = v -> value.d;
}
else{
if(!is_pointer(v)) show_error(POINTER_EXPECTED);
*(void **) address.value.p = v -> value.p;
}
return;
}
}
}
prog = temp_prog;
eval_logical(v);
}
void eval_logical(_DATA *v){
_DATA partial;
char temp_tok;
eval_relational(v);
while(tok == LOGICAL_AND || tok == LOGICAL_OR){
temp_tok = tok;
eval_relational(&partial);
operate(v, &partial, temp_tok); // this operates on v, and sets its value to the result
}
}
void eval_relational(_DATA *v){
_DATA partial;
char temp_tok;
eval_terms(v);
while(tok == EQUAL || tok == NOT_EQUAL || tok == LESS_THAN || tok == GREATER_THAN || tok == LESS_THAN_OR_EQUAL || tok == GREATER_THAN_OR_EQUAL){
temp_tok = tok;
eval_terms(&partial);
operate(v, &partial, temp_tok); // this operates on v, and sets its value to the result
}
}
void eval_terms(_DATA *v){
_DATA partial;
char temp_tok;
eval_factors(v);
while(tok == PLUS || tok == MINUS){
temp_tok = tok;
eval_factors(&partial);
operate(v, &partial, temp_tok); // this operates on v, and sets its value to the result
}
}
void eval_factors(_DATA *v){
_DATA partial;
char temp_tok;
eval_atom(v);
while(tok == ASTERISK || tok == DIVISION || tok == MOD){
temp_tok = tok;
eval_atom(&partial);
operate(v, &partial, temp_tok); // this operates on v, and sets its value to the result
}
}
void eval_matrix(_DATA *v){
eval_atom(v);
}
void eval_atom(_DATA *v){
char temp_name[ID_LEN]; // holds the name of the variable or fuction found
int func_index;
get_token();
if(tok == MINUS){
eval_atom(v);
unary_minus(v);
putback();
}
else if(tok == LOGICAL_NOT){
eval_atom(v);
unary_logical_not(v);
putback();
}
else if(tok == BITWISE_NOT){
eval_atom(v);
unary_bitwise_not(v);
putback();
}
else if(tok == POINTER_REF){
get_token();
if(token_type != IDENTIFIER) show_error(IDENTIFIER_EXPECTED);
v -> type = get_var_type(token);
v -> ind_level = get_ind_level(token) + 1;
v -> value.p = get_var_address(token);
}
else if(tok == ASTERISK){ // starts pointer dereferencing
eval_atom(v);
if(!is_pointer(v)) show_error(POINTER_EXPECTED);
if(v -> ind_level == 1)
switch(v -> type){
case DT_CHAR:
v -> value.c = *(char *) v -> value.p;
break;
case DT_INT:
v -> value.i = *(int *) v -> value.p;
break;
case DT_FLOAT:
v -> value.f = *(float *) v -> value.p;
break;
case DT_DOUBLE:
v -> value.d = *(double *) v -> value.p;
}
else v -> value.p = *(void **) v -> value.p;
v -> ind_level--;
putback();
}
else if(tok == SIZEOF){
_ATOM temp_tok;
get_token();
if(tok != OPENING_PAREN) show_error(OPENING_PAREN_EXPECTED);
get_token();
temp_tok = tok;
if(tok == VOID || tok == CHAR || tok == INT || tok == FLOAT || tok == DOUBLE){
get_token();
if(tok == ASTERISK){
while(tok == ASTERISK) get_token();
v -> value.i = sizeof(void*);
}
else{
if(tok != CLOSING_PAREN) show_error(CLOSING_PAREN_EXPECTED);
switch(temp_tok){
case CHAR:
v -> value.i = sizeof(char);
break;
case INT:
v -> value.i = sizeof(int);
break;
case FLOAT:
v -> value.i = sizeof(float);
break;
case DOUBLE:
v -> value.i = sizeof(double);
}
}
}
else{
putback();
eval(v);
unary_sizeof(v);
}
v -> type = DT_INT;
v -> ind_level = 0;
}
else if(tok == OPENING_PAREN){
_ATOM temptok;
int ind_level;
ind_level = 0;
get_token();
if(tok == VOID || tok == CHAR || tok == INT || tok == FLOAT || tok == DOUBLE){
temptok = tok;
get_token();
while(tok == ASTERISK){
ind_level++;
get_token();
}
if(tok != CLOSING_PAREN) show_error(CLOSING_PAREN_EXPECTED);
eval_atom(v);
cast(v, temptok, ind_level);
putback();
}
else{
putback();
eval(v);
if(tok != CLOSING_PAREN) show_error(CLOSING_PAREN_EXPECTED);
}
}
else if(token_type == CHAR_CONST){
v -> ind_level = 0;
v -> type = DT_CHAR;
v -> value.c = *string_constant;
}
else if(token_type == STRING_CONST){
v -> ind_level = 1;
v -> type = DT_CHAR;
v -> value.p = add_string();
}
else if(token_type == INTEGER_CONST){
v -> ind_level = 0;
v -> type = DT_INT;
v -> value.i = atoi(token);
}
else if(token_type == FLOAT_CONST){
v -> type = DT_FLOAT;
v -> ind_level = 0;
v -> value.f = atof(token);
}
else if(token_type == DOUBLE_CONST){
v -> type = DT_DOUBLE;
v -> ind_level = 0;
v -> value.d = atof(token);
}
else if(token_type == IDENTIFIER){
strcpy(temp_name, token);
get_token();
if(tok == OPENING_BRACKET){ // matrix operations
_VAR *matrix; // pointer to the matrix variable
char i; char data_size; // matrix data size
_DATA index;
int dims;
if(!is_matrix(temp_name)){ // if the variable is not a matrix, then it must be a pointer. gives the variable value and returns, so that eval_matrix can work on it.
*v = get_var_value(temp_name);
return;
}
// otherwise, it is a matrix
matrix = get_var_pointer(temp_name); // gets a pointer to the variable holding the matrix address
data_size = get_data_size(&matrix -> data);
v -> value.p = matrix -> data.value.p; // sets v to the beginning of the memory block
v -> type = matrix -> data.type;
v -> ind_level = 1;
dims = matrix_dim_count(matrix); // gets the number of dimensions for this matrix
for(i = 0; i < dims; i++){
eval(&index); // evaluates the index expression
convert_data(&index, DT_INT);
if(index.value.i < 0 || index.value.i >= matrix -> dims[i]) show_error(MATRIX_INDEX_OUTSIDE_BOUNDS);
if(tok != CLOSING_BRACKET) show_error(CLOSING_BRACKET_EXPECTED);
// if not evaluating the final dimension, it keeps returning pointers to the current position within the matrix
if(i < dims - 1) v -> value.p = v -> value.p + ( index.value.i * get_matrix_offset(i, matrix) * data_size );
get_token();
if(tok != OPENING_BRACKET) break;
}
// if it has reached the last dimension, it gets the final value at that address, which is one of the basic data types
if(i == dims - 1){
v -> ind_level = 0;
switch(matrix -> data.type){
case DT_CHAR:
v -> value.c = *( (char *)(v -> value.p) + index.value.i );
break;
case DT_INT:
v -> value.i = *( (int *)(v -> value.p) + index.value.i );
break;
case DT_FLOAT:
v -> value.f = *( (float *)(v -> value.p) + index.value.i );
break;
case DT_DOUBLE:
v -> value.d = *( (double *)(v -> value.p) + index.value.i );