-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhw4b.c
2189 lines (1879 loc) · 74.9 KB
/
hw4b.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 <unistd.h> /* Symbolic Constants */
#include <sys/types.h> /* Primitive System Data Types */
#include <errno.h> /* Errors */
#include <stdio.h> /* Input/Output */
#include <stdlib.h> /* General Utilities */
#include <pthread.h> /* POSIX Threads */
#include <string.h>
#include <time.h>
#include <math.h>
#include <ctype.h>
#include "khash.h"
#define FALSE 0
#define TRUE 1
#define MAX_LINES 100
#define MAX_LINE_LENGTH 100
#define MAX_WORDS_PER_LINE 10
#define MAX_WORD_LENGTH 30
#define MAX_PRINT_LENGTH 30
#define MAX_THREADS 1
#define UNINITIALIZED -23562
#define STOPPED 0
#define RUNNING 1
#define FINISHED 2
#define SLEEP 3
#define NUM_INSTR_CHANGE 5
#define CHECK_IF_VAR(token) \
if(check_if_var( token ) == 0) \
return 1;
#define CHECK_IF_GLOBAL_VAR(token) \
if (check_if_global_var( token ) == 1) \
return 1;
#define CHECK_IF_VARVAL(token) \
if (check_if_var( token ) == 0) \
if ( checkIfInteger( token ) == FALSE ) \
return 1;
// shorthand way to get the key from hashtable or defVal if not found
#define kh_get_val(kname, hash, key, defVal) ({k=kh_get(kname, hash, key);(k!=kh_end(hash)?kh_val(hash,k):defVal);})
// shorthand way to set value in hash with single line command. Returns value
// returns 0=replaced existing item, 1=bucket empty (new key), 2-adding element previously deleted
#define kh_set(kname, hash, key, val) ({int ret; k = kh_put(kname, hash,key,&ret); kh_value(hash,k) = val; ret;})
const int khStrInt = 33;
KHASH_MAP_INIT_STR(khStrInt, int); // setup khash to handle string key with int payload
khash_t(khStrInt) *h_glob; //for global Variables
const char s[] = " \t\r\n\v\f"; //delimeter lines by whitespace
const char p[] = "\"";
struct plist{
int id;
char progName[MAX_WORD_LENGTH];
int argc;
char argv[MAX_WORDS_PER_LINE][MAX_WORD_LENGTH];
char token[MAX_LINES][MAX_WORDS_PER_LINE][MAX_WORD_LENGTH];
int compiled;
int state;
int pc;
clock_t startSleepTime;
int secOfSleep;
khash_t(khStrInt) *h_loc;
struct plist *nxt;
struct plist *prv;
};
struct plist *head = NULL;
/**** LIST FUNCTIONS ********/
void list_init(){
head = (struct plist *)malloc(sizeof(struct plist));
head->nxt = head;
head->prv = head;
}
//is list empty
int isEmpty() {
return head == NULL;
}
void list_display() {
//start from the beginning
struct plist *ptr = head->nxt;
printf("\n");
while(ptr != head && ptr!= NULL) {
printf("[Id:%d | name: %s | state: %d | pc: %d]\n",ptr->id,ptr->progName, ptr->state, ptr->pc);
ptr = ptr->nxt;
}
printf("\n");
}
void list_insert(int id, int argc, char argv[MAX_WORDS_PER_LINE][MAX_WORD_LENGTH]) {
int i;
struct plist *curr;
curr = (struct plist *)malloc(sizeof(struct plist));
//values
curr->id = id;
curr->state = STOPPED;
curr->compiled = FALSE;
curr->pc = 1;
curr->argc = argc;
memset(curr->progName, '\0', sizeof(curr->progName));
strcpy(curr->progName, argv[1]);
for(i=0; i < MAX_LINES; i++)
memset(curr->token[i], '\0', sizeof(curr->token[i]));
for(i=0; i < MAX_WORDS_PER_LINE; i++)
memset(curr->argv[i], '\0', sizeof(curr->argv[i]));
for(i=0; argv[i][0] != '\0'; i++){
strcpy(curr->argv[i], argv[i]);
printf("argv[%d]: %s\n", i, argv[i]);
}
//insert at head
curr->nxt = head->nxt;
curr->prv = head;
curr->nxt->prv = curr;
curr->prv->nxt = curr;
}
int list_remove(int id){
struct plist *curr;
head->id = id;
for(curr=head->nxt; curr->id!=id; curr=curr->nxt){}
if (curr != head) {
curr->nxt->prv = curr->prv;
curr->prv->nxt = curr->nxt;
free(curr);
return(1);
}
return(0);
}
/**** GENERAL FUNCTIONS **************/
int checkIfInteger(char *str){
int valid = TRUE;
int i = 0;
if (str[0] == '-') {
i++;
}
for ( ; i < strlen(str); i++)
{
if (!isdigit(str[i]))
{
valid = FALSE;
break;
}
}
return(valid);
}
int countLines(char* file_arg){ //inline int checkSyntax
char buf[200];
int count=0;
FILE* f = fopen(file_arg, "r");
if(f == NULL) {
return(-1);
}
while(fgets(buf,sizeof(buf), f) != NULL){
count++;
}
fclose(f);
return(count);
}
int getTempTokenArray(char token[MAX_WORD_LENGTH], khash_t(khStrInt) *h){
int i,j, k, value;
char arg[MAX_WORD_LENGTH],
value_str[MAX_WORD_LENGTH];
khiter_t k_it;
memset(value_str, '\0', sizeof(value_str));
k=0;
for (i=0 ; i < strlen(token); i++)
{
if (token[i] == '['){
j=i+1;
while(token[j] != ']'){
arg[k] = token[j];
j++;
k++;
}
arg[k] = '\0';
break;
}
}
k_it = kh_get(khStrInt, h, arg);
value = kh_val(h, k_it);
if(value == UNINITIALIZED){
return(-1);
}
sprintf(value_str, "%d", value);
//eisagwgi sth leksh
k=0;
for (i=0 ; i < strlen(token); i++){
if (token[i] == '['){
for(j=0; j < strlen(value_str); ++j){
token[i+1+j] = value_str[j];
}
token[i+1+j] = ']';
token[i+1+j+1] = '\0';
}
}
return 0;
}
//return 0:(val or array with integer arg), 1:(array with string arg)
int check_if_array(const char *token, khash_t(khStrInt) *h) {
int i,j, k,
isArray = FALSE;
char arg[MAX_WORD_LENGTH];
k=0;
for (i=0 ; i < strlen(token); i++)
{
if (token[i] == '[')
{
j=i+1;
while(token[j] != ']'){
arg[k] = token[j];
j++;
k++;
}
arg[k] = '\0';
isArray = TRUE;
break;
}
}
if(isArray == FALSE)
return(0);
if(checkIfInteger(arg) == FALSE){
return(1);
}
return(0);
}
int check_if_global_var( char *token ) {
if(token[0] == '$') {
if ( isalpha(token[1]) ) {
/*
* insert here more checking
*/
}
else {
return (1);
}
}
else{
return (1);
}
return (0);
}
int check_if_var( char *token ) {
if(token[0] == '$'){
if ( isalpha(token[1]) ) {
/*
* insert here more checking
*/
}
else {
return (0);
}
}
else {
return (0);
}
return (1);
}
int free_var_mem(khash_t(khStrInt) *h){
khiter_t k;
const char *key;
for (k = 0; k < kh_end(h); ++k)
if (kh_exist(h, k)){
key = kh_key(h,k);
if(check_if_array(key ,h) == TRUE)
free((char*)kh_key(h, k));
}
return(0);
}
int check_if_should_wake(struct plist *curr){
clock_t now = clock();
if(( (double)(now - curr->startSleepTime) / CLOCKS_PER_SEC) >= curr->secOfSleep){
return(TRUE);
}
return(FALSE);
}
int checkSyntax(char token[MAX_WORDS_PER_LINE][MAX_WORD_LENGTH], khash_t(khStrInt) *h_loc){ //inline int checkSyntax || inline kai tis mikres sun?
int j, r;
khiter_t k;
char instruction[MAX_WORD_LENGTH];
//initialize
memset(instruction, '\0', sizeof(instruction));
strcpy(instruction, token[0]);
if(strcmp(instruction, "LOAD") == 0){
// 1st parameter: var
CHECK_IF_VAR( token[1] )
if( check_if_array(token[1], h_loc) == 0){
kh_put(khStrInt, h_loc, token[1], &r);
if(r == 1)
kh_set(khStrInt, h_loc, token[1], UNINITIALIZED); //kh_put in memory (no initialize)
}
// 2nd parameter: globalVar
CHECK_IF_GLOBAL_VAR( token[2] )
if(check_if_array(token[2], h_glob) == 0){
kh_put(khStrInt, h_glob, token[2], &r);
if(r == 1)
kh_set(khStrInt, h_glob, token[2], UNINITIALIZED); //kh_put in memory (no initialize)
}
//elegxos stin periptwsi: LOAD $r1 $r2 $r3
if(token[3][0] != '\0')
return(1);
}
else if(strcmp(instruction, "STORE") == 0){
// 1st parameter: globalVar
CHECK_IF_GLOBAL_VAR( token[1] )
if(check_if_array(token[1], h_glob) == 0){
kh_put(khStrInt, h_glob, token[1], &r);
if(r == 1)
kh_set(khStrInt, h_glob, token[1], UNINITIALIZED); //kh_put in memory (no initialize)
}
// 2nd parameter: var or int.
CHECK_IF_VARVAL(token[2])
if(checkIfInteger(token[2]) == FALSE){
if(check_if_array(token[2], h_loc) == 0){
kh_put(khStrInt, h_loc, token[2], &r);
if(r == 1)
kh_set(khStrInt, h_loc, token[2], UNINITIALIZED); //kh_put in memory (no initialize)
}
}
// elegxos stin periptwsi: STORE $r1 $r2 $r3
if(token[3][0] != '\0')
return(1);
}
else if(strcmp(instruction, "SET") == 0){
// 1st parameter: var
CHECK_IF_VAR( token[1] )
if(check_if_array(token[1], h_loc) == 0) {
kh_put(khStrInt, h_loc, token[1], &r);
if(r == 1)
kh_set(khStrInt, h_loc, token[1], UNINITIALIZED); //kh_put in memory (no initialize)
}
// 2nd parameter: var or int.
CHECK_IF_VARVAL(token[2])
if(checkIfInteger(token[2]) == FALSE){
if(check_if_array(token[2], h_loc) == 0){
kh_put(khStrInt, h_loc, token[2], &r);
if(r == 1)
kh_set(khStrInt, h_loc, token[2], UNINITIALIZED); //kh_put in memory (no initialize)
}
}
// elegxos stin periptwsi: SET $r1 $r2 $r3
if(token[3][0] != '\0')
return(1);
}
else if(strcmp(instruction, "ADD") == 0){
// 1st parameter: var
CHECK_IF_VAR( token[1] )
if( check_if_array(token[1], h_loc) == 0){
kh_put(khStrInt, h_loc, token[1], &r);
if(r == 1)
kh_set(khStrInt, h_loc, token[1], UNINITIALIZED); //kh_put in memory (no initialize)
}
// 2nd parameter: var or int.
CHECK_IF_VARVAL(token[2])
if(checkIfInteger(token[2]) == FALSE){
if(check_if_array(token[2], h_loc) == 0){
kh_put(khStrInt, h_loc, token[2], &r);
if(r == 1)
kh_set(khStrInt, h_loc, token[2], UNINITIALIZED);
}
}
// 3rd parameter: var or int.
CHECK_IF_VARVAL(token[3])
if(checkIfInteger(token[3]) == FALSE){
if(check_if_array(token[3], h_loc) == 0){
kh_put(khStrInt, h_loc, token[3], &r);
if(r == 1)
kh_set(khStrInt, h_loc, token[3], UNINITIALIZED);
}
}
// elegxos stin periptwsi: ADD $r1 $r2 $r3 $r4
if(token[4][0] != '\0')
return(1);
}
else if(strcmp(instruction, "SUB") == 0){
// 1st parameter: var
CHECK_IF_VAR( token[1] )
if( check_if_array(token[1], h_loc) == 0){
kh_put(khStrInt, h_loc, token[1], &r);
if(r == 1)
kh_set(khStrInt, h_loc, token[1], UNINITIALIZED); //kh_put in memory (no initialize)
}
// 2nd parameter: var or int.
CHECK_IF_VARVAL(token[2])
if(checkIfInteger(token[2]) == FALSE){
if(check_if_array(token[2], h_loc) == 0){
kh_put(khStrInt, h_loc, token[2], &r);
if(r == 1)
kh_set(khStrInt, h_loc, token[2], UNINITIALIZED);
}
}
// 3rd parameter: var or int.
CHECK_IF_VARVAL(token[3])
if(checkIfInteger(token[3]) == FALSE){
if(check_if_array(token[3], h_loc) == 0){
kh_put(khStrInt, h_loc, token[3], &r);
if(r == 1)
kh_set(khStrInt, h_loc, token[3], UNINITIALIZED);
}
}
// elegxos stin periptwsi: SUB $r1 $r2 $r3 $r4
if(token[4][0] != '\0')
return(1);
}
else if(strcmp(instruction, "MUL") == 0){
// 1st parameter: var
CHECK_IF_VAR( token[1] )
if( check_if_array(token[1], h_loc) == 0) {
kh_put(khStrInt, h_loc, token[1], &r);
if(r == 1)
kh_set(khStrInt, h_loc, token[1], UNINITIALIZED); //kh_put in memory (no initialize)
}
// 2nd parameter: var or int.
CHECK_IF_VARVAL(token[2])
if(checkIfInteger(token[2]) == FALSE){
if(check_if_array(token[2], h_loc) == 0){
kh_put(khStrInt, h_loc, token[2], &r);
if(r == 1)
kh_set(khStrInt, h_loc, token[2], UNINITIALIZED);
}
}
// 3rd parameter: var or int.
CHECK_IF_VARVAL(token[3])
if(checkIfInteger(token[3]) == FALSE){
if(check_if_array(token[3], h_loc) == 0){
kh_put(khStrInt, h_loc, token[3], &r);
if(r == 1)
kh_set(khStrInt, h_loc, token[3], UNINITIALIZED);
}
}
// elegxos stin periptwsi: MUL $r1 $r2 $r3 $r4
if(token[4][0] != '\0')
return(1);
}
else if(strcmp(instruction, "DIV") == 0){
// 1st parameter: var
CHECK_IF_VAR( token[1] )
if( check_if_array(token[1], h_loc) == 0) {
kh_put(khStrInt, h_loc, token[1], &r);
if(r == 1)
kh_set(khStrInt, h_loc, token[1], UNINITIALIZED); //kh_put in memory (no initialize)
}
// 2nd parameter: var or int.
CHECK_IF_VARVAL(token[2])
if(checkIfInteger(token[2]) == FALSE){
if(check_if_array(token[2], h_loc) == 0) {
kh_put(khStrInt, h_loc, token[2], &r);
if(r == 1)
kh_set(khStrInt, h_loc, token[2], UNINITIALIZED);
}
}
// 3rd parameter: var or int.
CHECK_IF_VARVAL(token[3])
if(checkIfInteger(token[3]) == FALSE){
if(check_if_array(token[3], h_loc) == 0) {
kh_put(khStrInt, h_loc, token[3], &r);
if(r == 1)
kh_set(khStrInt, h_loc, token[3], UNINITIALIZED);
}
}
// elegxos stin periptwsi: DIV $r1 $r2 $r3 $r4
if(token[4][0] != '\0')
return(1);
}
else if(strcmp(instruction, "MOD") == 0){
// 1st parameter: var
CHECK_IF_VAR( token[1] )
if( check_if_array(token[1], h_loc) == 0) {
kh_put(khStrInt, h_loc, token[1], &r);
if(r == 1)
kh_set(khStrInt, h_loc, token[1], UNINITIALIZED); //kh_put in memory (no initialize)
}
// 2nd parameter: var or int.
CHECK_IF_VARVAL(token[2])
if(checkIfInteger(token[2]) == FALSE){
if(check_if_array(token[2], h_loc) == 0) {
kh_put(khStrInt, h_loc, token[2], &r);
if(r == 1)
kh_set(khStrInt, h_loc, token[2], UNINITIALIZED);
}
}
// 3rd parameter: var or int.
CHECK_IF_VARVAL(token[3])
if(checkIfInteger(token[3]) == FALSE){
if(check_if_array(token[3], h_loc) == 0) {
kh_put(khStrInt, h_loc, token[3], &r);
if(r == 1)
kh_set(khStrInt, h_loc, token[3], UNINITIALIZED);
}
}
// elegxos stin periptwsi: MOD $r1 $r2 $r3 $r4
if(token[4][0] != '\0')
return(1);
}
else if(strcmp(instruction, "BRGT") == 0){
// 1st parameter: var or int.
CHECK_IF_VARVAL(token[1])
if(checkIfInteger(token[1]) == FALSE){
if(check_if_array(token[1], h_loc) == 0){
kh_put(khStrInt, h_loc, token[1], &r);
if(r == 1)
kh_set(khStrInt, h_loc, token[1], UNINITIALIZED); //kh_put in memory (no initialize)
}
}
// 2nd parameter: var or int.
CHECK_IF_VARVAL(token[2])
if(checkIfInteger(token[2]) == FALSE){
if(check_if_array(token[2], h_loc) == 0){
kh_put(khStrInt, h_loc, token[2], &r);
if(r == 1)
kh_set(khStrInt, h_loc, token[2], UNINITIALIZED);
}
}
// elegxos stin periptwsi: $r1 $r2 label $r3
if(token[4][0] != '\0')
return(1);
}
else if(strcmp(instruction, "BRGE") == 0){
// 1st parameter: var or int.
CHECK_IF_VARVAL(token[1])
if(checkIfInteger(token[1]) == FALSE){
if(check_if_array(token[1], h_loc) == 0){
kh_put(khStrInt, h_loc, token[1], &r);
if(r == 1)
kh_set(khStrInt, h_loc, token[1], UNINITIALIZED); //kh_put in memory (no initialize)
}
}
// 2nd parameter: var or int.
CHECK_IF_VARVAL(token[2])
if(checkIfInteger(token[2]) == FALSE){
if(check_if_array(token[2], h_loc) == 0){
kh_put(khStrInt, h_loc, token[2], &r);
if(r == 1)
kh_set(khStrInt, h_loc, token[2], UNINITIALIZED);
}
}
// elegxos stin periptwsi: $r1 $r2 label $r3
if(token[4][0] != '\0')
return(1);
}
else if(strcmp(instruction, "BRLT") == 0){
// 1st parameter: var or int.
CHECK_IF_VARVAL(token[1])
if(checkIfInteger(token[1]) == FALSE){
if(check_if_array(token[1], h_loc) == 0) {
kh_put(khStrInt, h_loc, token[1], &r);
if(r == 1)
kh_set(khStrInt, h_loc, token[1], UNINITIALIZED); //kh_put in memory (no initialize)
}
}
// 2nd parameter: var or int.
CHECK_IF_VARVAL(token[2])
if(checkIfInteger(token[2]) == FALSE){
if(check_if_array(token[2], h_loc) == 0) {
kh_put(khStrInt, h_loc, token[2], &r);
if(r == 1)
kh_set(khStrInt, h_loc, token[2], UNINITIALIZED);
}
}
// elegxos stin periptwsi: $r1 $r2 label $r3
if(token[4][0] != '\0')
return(1);
}
else if(strcmp(instruction, "BRLE") == 0){
// 1st parameter: var or int.
CHECK_IF_VARVAL(token[1])
if(checkIfInteger(token[1]) == FALSE){
if(check_if_array(token[1], h_loc) == 0) {
kh_put(khStrInt, h_loc, token[1], &r);
if(r == 1)
kh_set(khStrInt, h_loc, token[1], UNINITIALIZED); //kh_put in memory (no initialize)
}
}
// 2nd parameter: var or int.
CHECK_IF_VARVAL(token[2])
if(checkIfInteger(token[2]) == FALSE){
if(check_if_array(token[2], h_loc) == 0){
kh_put(khStrInt, h_loc, token[2], &r);
if(r == 1)
kh_set(khStrInt, h_loc, token[2], UNINITIALIZED);
}
}
// elegxos stin periptwsi: $r1 $r2 label $r3
if(token[4][0] != '\0')
return(1);
}
else if(strcmp(instruction, "BREQ") == 0){
// 1st parameter: var or int.
CHECK_IF_VARVAL(token[1])
if(checkIfInteger(token[1]) == FALSE){
if(check_if_array(token[1], h_loc) == 0) {
kh_put(khStrInt, h_loc, token[1], &r);
if(r == 1)
kh_set(khStrInt, h_loc, token[1], UNINITIALIZED); //kh_put in memory (no initialize)
}
}
// 2nd parameter: var or int.
CHECK_IF_VARVAL(token[2])
if(checkIfInteger(token[2]) == FALSE){
if(check_if_array(token[2], h_loc) == 0) {
kh_put(khStrInt, h_loc, token[2], &r);
if(r == 1)
kh_set(khStrInt, h_loc, token[2], UNINITIALIZED);
}
}
// elegxos stin periptwsi: $r1 $r2 label $r3
if(token[4][0] != '\0')
return(1);
}
else if(strcmp(instruction, "BRA") == 0){
// elegxos stin periptwsi: BRA label $r1
if(token[2][0] != '\0')
return(1);
}
else if(strcmp(instruction, "DOWN") == 0){
CHECK_IF_GLOBAL_VAR(token[1])
if(check_if_array(token[1], h_glob) == 0){
kh_put(khStrInt, h_glob, token[1], &r);
if(r == 1)
kh_set(khStrInt, h_glob, token[1], UNINITIALIZED); //kh_put in memory (no initialize)
}
//elegxos stin periptwsi: DOWN $r1 $r2
if(token[2][0] != '\0')
return(1);
}
else if(strcmp(instruction, "UP") == 0){
CHECK_IF_GLOBAL_VAR(token[1])
if(check_if_array(token[1], h_glob) == 0){
kh_put(khStrInt, h_glob, token[1], &r);
if(r == 1)
kh_set(khStrInt, h_glob, token[1], UNINITIALIZED); //kh_put in memory (no initialize)
}
// elegxos stin periptwsi: UP $r1 $r2
if(token[2][0] != '\0')
return(1);
}
else if(strcmp(instruction, "SLEEP") == 0){
// 1st parameter: var or int.
CHECK_IF_VARVAL(token[1])
if(check_if_array(token[1], h_loc) == 0) {
kh_put(khStrInt, h_loc, token[1], &r);
if(r == 1)
kh_set(khStrInt, h_loc, token[1], UNINITIALIZED); //put in memory (no initialize)
}
// elegxos stin periptwsi: SLEEP $r1 $r2
if(token[2][0] != '\0')
return(1);
}
else if(strcmp(instruction, "PRINT") == 0){
for(j=2; token[j][0] != '\0'; j++){
if(checkIfInteger(token[j]) == FALSE){
CHECK_IF_VARVAL(token[j])
if(check_if_array(token[j], h_loc) == 0){
kh_put(khStrInt, h_loc, token[j], &r);
if(r == 1)
kh_set(khStrInt, h_loc, token[j], UNINITIALIZED);
}
}
}
}
else if(strcmp(instruction, "RETURN") == 0){
if(token[1][0] != '\0')
return(1);
}
else if(strcmp(instruction, "#PROGRAM") == 0){
if(token[1][0] != '\0')
return(1);
}
else{
return(1);
}
return(0);
}
int compile(struct plist *curr){
int i, j, r,
pc,
totalLines,
labelCount,
t_id;
//int **args;
char line[MAX_LINE_LENGTH];
char temp_token[MAX_WORD_LENGTH];
char argv[MAX_WORDS_PER_LINE][MAX_WORD_LENGTH],
*temp_string; //prosorini metabliti gia iteration metaksi tokens
char temp_argv_str[MAX_WORD_LENGTH],
temp_num_str[MAX_WORD_LENGTH],
temp_pr[MAX_PRINT_LENGTH]; //prosorini metabliti gia print instruction
//initialize
for(j=0; j < MAX_WORDS_PER_LINE; j++){
memset(argv[j], '\0', sizeof(argv[j]));
}
int argc = curr->argc;
for(j=0; j <= argc; j++)
strcpy(argv[j], curr->argv[j]);
/*** hash table*******/
khiter_t k;
khash_t(khStrInt) *h_loc = kh_init(khStrInt); // create a hashtable
//thread id
t_id = atoi(argv[0]);
//metrame grammes arxeiou
totalLines = countLines(argv[1]);
if(totalLines == -1){
printf("(Thread id: %d) Error getting num of lines from file.\n", t_id);
free_var_mem(h_loc);
kh_destroy(khStrInt, h_loc);
return -1;
}
char token[totalLines][MAX_WORDS_PER_LINE][MAX_WORD_LENGTH],
labels[totalLines][MAX_WORD_LENGTH];
/******initialize memory *******/
memset(temp_pr, '\0', sizeof(temp_pr));
memset(temp_token, '\0', sizeof(temp_token));
memset(temp_argv_str, '\0', sizeof(temp_argv_str));
for(j=0; j < totalLines; j++){
memset(token[j], '\0', sizeof(token[j]));
memset(labels[j], '\0', sizeof(labels[j]));
}
FILE* fp = fopen(argv[1], "r");
if(fp == NULL) {
printf("(Thread id: %d) Error opening file", t_id);
free_var_mem(h_loc);
kh_destroy(khStrInt, h_loc);
return -1;
}
//put arguments in the right place
strcpy(argv[1], argv[0]);
for(i=1; i < argc; i++){
strcpy(argv[i-1], argv[i]);
}
//argument count
argc = argc - 1;
//insert arguments with values in memory
for(i=0;i < argc;i++){
if(checkIfInteger(argv[i]) == FALSE){
printf("(Thread id: %d) Invalid line argument. They can only be integers\n", t_id);
free_var_mem(h_loc);
kh_destroy(khStrInt, h_loc);
fclose(fp);
return -1;
}
//convert $argv[i] to string in order to put in memory
strcpy(temp_argv_str, "$argv[");
sprintf(temp_num_str, "%d", i);
strcat(temp_argv_str, temp_num_str);
strcat(temp_argv_str, "]");
//debugging
printf("temp_argv_str: %s\n", temp_argv_str);
k = kh_put(khStrInt, h_loc, temp_argv_str, &r);
if(r)
kh_key(h_loc, k) = strdup(temp_argv_str);
kh_set(khStrInt, h_loc, temp_argv_str, atoi(argv[i]));
printf("thread id: %d argc: %d argv[%d] = %s\n", t_id, argc, i, argv[i]);
}
kh_set(khStrInt, h_loc, "$argc", argc);
printf("\nTokens of user program code:\n");
/******************** COMPILE **********************************************************/
pc = 0; //program counter
labelCount = 0;
while (pc < totalLines){ //for every line
if (fgets(line, 200, fp) == NULL)
break; //end of file
/***** TOKENS ON THE SAME LINE********/
i = 0; //iteration between tokens
temp_string = strtok(line, s); // get the first token
while( temp_string != NULL ) { // walk through other tokens on the same line
strcpy(temp_token, temp_string);
//case "print"
if(temp_token[0] == '\"'){
strcpy(temp_pr, strtok(NULL, p));
for(j = 1; temp_token[j] != '\0'; ++j){ //delete-> "
temp_token[j-1] = temp_token[j];
}
temp_token[j-1] = ' ';
for(k = 0; temp_pr[k] != '\0'; ++k, ++j){ //olokliri print se 1 token
temp_token[j] = temp_pr[k];
}
temp_token[j] = '\0';
}
//assing
strcpy(token[pc][i] ,temp_token);
//next token on the line
i++;
temp_string = strtok(NULL, s);
}
/***** STORE LABEL OF THE LINE ******************/
if((token[pc][0][0] == 'L') && (strcmp(token[pc][0], "LOAD") != 0)){
strcpy(labels[labelCount], token[pc][0]);
//value of pc at the label
kh_put(khStrInt, h_loc, labels[labelCount], &r);
if(r == 0){
printf("(Thread id: %d) There can only unique labels. Error line (%d)\n", t_id, pc+1);
free_var_mem(h_loc);
kh_destroy(khStrInt, h_loc);
fclose(fp);
return -1;
}
kh_set(khStrInt, h_loc,labels[labelCount], pc);
labelCount++;
//delete label from instruction
for(j=1; token[pc][j][0] != '\0'; j++){
strcpy(token[pc][j-1], token[pc][j]);
}
memset(token[pc][j-1], '\0', sizeof(token[pc][j-1]));
}
/***** CHECK SYNTAX ERROR FOR THE LINE *********/
if(checkSyntax(token[pc], h_loc)){
printf("(Thread id: %d) There is a syntax error at line (%d). Exiting.\n", t_id, pc+1);
free_var_mem(h_loc);
kh_destroy(khStrInt, h_loc);
fclose(fp);
return -1;
}
//debugging print 3
for(j=0; token[pc][j][0] != '\0'; j++){
printf("%d: %s\n", j, token[pc][j]);
}
//next line
pc++;
printf("*****************New line****************\n");
}
if(strcmp(token[0][0], "#PROGRAM")){
printf("(Thread id: %d) Wrong syntax at line 0. \"#PROGRAM\" is needed. Exiting\n", t_id);
free_var_mem(h_loc);
kh_destroy(khStrInt, h_loc);
fclose(fp);
return -1;
}
/*************debug compile*****************************************/
int tval;
printf("size: %d\tbuckets: %d\n", kh_size(h_loc), kh_n_buckets(h_loc));
printf("\nIeterate all LOCAL keys\n");
for (k = kh_begin(h_loc); k != kh_end(h_loc); ++k) {
if (kh_exist(h_loc, k)) {
const char *key = kh_key(h_loc,k);
//kh_value(h_loc, k) = UNINITIALIZED;
tval = kh_value(h_loc, k);
printf("key=%s val=%d\n", key, tval);
}
}
printf("\nIeterate all GLOBAL keys\n");
for (k = kh_begin(h_glob); k != kh_end(h_glob); ++k) {
if (kh_exist(h_glob, k)) {
const char *key = kh_key(h_glob,k);
//kh_value(h_glob, k) = UNINITIALIZED;
tval = kh_value(h_glob, k);
printf("key=%s val=%d\n", key, tval);
}
}
curr->argc = argc;
for(j=0; j <= argc; j++)
strcpy(curr->argv[j], argv[j]);
curr->h_loc = h_loc;
for(i=0; token[i][0][0] != '\0'; i++){
for(j=0; token[i][j][0] != '\0'; j++){
strcpy(curr->token[i][j], token[i][j]);
}
}
return(0);
}
/********************** RUN **********************************************************/
int run(struct plist *curr){