-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple.c
More file actions
1255 lines (1136 loc) · 46.6 KB
/
simple.c
File metadata and controls
1255 lines (1136 loc) · 46.6 KB
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
//Simple compiler
//12/3/16
//Eric Glover
//TM T - Swift
//STATUS : ex 12.26 & ex 12.27 done, it seems to be working overvall
//if..goto may be done, seems to work fine
//secondPass() seems functional
//simpletron seems functional
//currently working on ex 12.28 optimization of code, seems operational
//working on ex 12.29
//TO-DO :
//consider changing the filename of the sml instructions to [whatever the input file name was sans .simple.txt].sml.txt
//ex 12.29, modifications : syntax and error checking
//simple language, a complex explanation
/*
command example statement description
rem 50 rem this is a remark text following rem is ignored by compiler
input 30 input x prompt user to enter an int, store it in x
let 80 let u = 4 * (j-56)) assign u the value of RH expression (arbtrarily complex)
print 10 print w desplay value of w
goto 70 goto 45 transfer program control to line 45
if...goto 35 if i == z goto 80 compare i & z for equality and transfer control
to line 80 if true, if false continue to next line
end 99 end terminate program execution
7 commands total
only lowercase letters can be used in the file
no descriptive variable names, variables are a single letter
only int variables
no variable declaration, merely mentioning a variable cause it to be
declared and initialized to 0
no string manipulation (strings are a syntax error)
compiler assumes the program is entered correctly
every simple statement has a line number (they must be in ascending order)
and a simple instruction
all commands can be used repeatedly except for end which must occur only once
equality operators allowed in goto & if goto statements (<, >, <=, >=, ==, !=)
example program
1 10 rem determine and print the sum of two intergers
2 15 rem
3 20 rem input the two ints
4 30 input a
5 40 input b
6 45 rem
7 50 rem add ints and store result in c
8 60 let c = a + b
9 65 rem
10 70 rem print result
11 80 print c
12 90 rem terminate program
13 99 end
example a) input 3 ints, determine their average and print
1 1 rem input 3 ints, determine their average and print
2 2 input a
3 3 input b
4 4 input c
5 5 let z = (a+b+c) / 3
6 6 print z
7 7 end
example b)
1 1 rem use a sentinel-controlled loop to input 10 ints and compute and print their sum
2 2 input a
3 3 if a == -9999 goto 6
4 4 let z = z + a
5 5 goto 2
6 6 print z
7 7 end
example c)
1 1 rem use counter-controlled loop to input seven ints (some pos, some neg)
2 2 rem and compute and print their average
3 3 input a
4 4 let z = z + a
5 5 let c = c + 1
6 6 if c < 7 goto input a
7 7 let c = c / 7
8 8 print c
9 9 end
example d)
1 1 rem input a series of ints, determine and print largest
2 2 rem first input indicates how many numbers will be processed
3 3 input s
4 4 input a
5 5 let c = c + 1
6 6 if a <= z goto 8
7 7 let z = a
8 8 if c < s goto 4
9 9 print z
10 10 end
//the above functions like an if statement inside a while loop oddly enough
example e)
1 1 rem input 10 ints and print the smallest
2 2 rem s is the total number of ints to try
3 3 let s = 10
4 4 input a
5 5 rem z = current smallest #
6 6 let z = a
7 7 rem c = counter var (init to zero)
8 8 let c = c + 1
9 9 rem thus begins the while loop with the condition at if c < s
10 10 input a
11 11 rem the if statement
12 12 if a >= z goto 14
13 13 let z = a
14 14 if c < s goto 10
15 15 print z
16 16 end
example f)
1 1 rem calculate and print the sum of the even intergers from 2 - 30
2 2 rem so set total = t, int to add = a, stop value = s = 30
3 3 let a = 2
4 4 let s = 30
5 5 let t = t + a
6 6 if a == s goto 9
7 7 let a = a + 2
8 8 goto 5
9 9 print t
10 10 end
1 1 rem calculate and print the sum of the even intergers from 2 - 30
2 2 rem so set total = t, int to add = a, stop value = s = 30
3 3 let s = 30
4 4 let a = a + 2
5 5 let t = t + a
6 6 if a < s goto 4
7 7 print t
8 8 end
example g)
1 1 rem calculate and print the product of odd ints from 1 to 9
2 2 rem total = t; int to * = a = 1; stop value = 9
3 3 let s = 9
4 4 let a = 3
5 5 let t = 1
6 6 let t = t * a
7 7 let a = a + 2
8 8 if a <= s goto 6
9 9 print t
10 10 end
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "simpletron.h"
#include "ex12.h"
//use strtok to read lines ??
//if the token is a line #, var, or constant then put it in the symbol table
//symbol table is an array of tableEntrys of size 100 for the moment
//location data type : for line #'s it's element in simpletron's memory where that line begins
//for vars and constants it's the address where those things are stored
//vars and consts are allocated at the end of simpletron's memory, going backward to front
//ie 99, 98, 97 etc
//note to track memory usage during compilation and print an error if exceeded
//how to do this is in page 7
//instruction counter : keeps track of the location for the next SML instruction to be placed into simpletron's memory
//data counter : keep track of where the location for the next var / constant to be placed into simpletron's memory
//if instruction counter > data counter the program is full b/c instructions go front -> back and vars / constant storage is back -> front
struct tableEntry {
int symbol;
char type; // 'C', 'L' or 'V' : constant or line number or variable
int location; // 00 to 99 (to 999 I beleive actually)
};
//do the nickname thing
typedef struct tableEntry TableEntry;
//TableEntry table[TABLESIZE];
TableEntry *tablePtr = NULL; //used to pass ex12_main the reference to the symbol table
FILE *output = NULL;
#define TABLESIZE 100
void firstPass(FILE *filePtr, TableEntry *table, FILE *output);
void secondPass(FILE *filePtr, TableEntry *table, FILE *output, char *outName);
void setConstants(void);
void optimize(char *outFile, TableEntry *table);
//table functions
void printTable(TableEntry table[]);
//int searchTable(TableEntry table[]); //return index of entry or -1
int searchTable(TableEntry table[], char *token, char type);
TableEntry* getTable(void);
FILE* getOutput(void);
int instructionCounter, dataCounter;
int flags[MEMSIZE];
// ./a.out simple.input.txt output.txt <option>
//option "o" = optimize
int main(int argc, char *argv[]){
int z = 0;
char *simpleCommands[3] = {argv[0], argv[2], "simpletron.dump.txt"};
printf("simpleCommands[0] = %s\n", simpleCommands[0]);
printf("simpleCommands[1] = %s\n", simpleCommands[1]);
printf("simpleCommands[2] = %s\n", simpleCommands[2]);
//create the symbol table
TableEntry table[TABLESIZE];
for( z = 0; z < TABLESIZE; z++){
table[z].symbol = -1; //indicates undeclared/unused
table[z].location = -1; //indicates undeclared/unused
table[z].type = 'u'; //indicates undeclared/unused
}
tablePtr = table;
//alternate style of doing things .... StackNodePtr newPtr = malloc(sizeof(StackNode));
/* PRIMER ON USING searchTable
printf("searchTable(table, '123', 'C') returns %d\n", searchTable(table, "123", 'C')); //-1
table[0].symbol = 1;
table[0].location = 11;
table[0].type = 'C';
table[1].symbol = atoi("11");
table[1].location = 10;
table[1].type = 'L';
table[99].symbol = 'x';
table[99].location = 9;
table[99].type = 'V';
table[98].symbol = atoi("1");
table[98].location = 8;
table[98].type = 'C';
printf("searchTable(table, '1', 'L') returns %d\n", searchTable(table, "1", 'L')); //0
printf("searchTable(table, '11', 'L') returns %d\n", searchTable(table, "11", 'L')); //1
printf("searchTable(table, 'x', 'V') returns %d\n", searchTable(table, "x", 'V')); //99
printf("searchTable(table, '1', 'C') returns %d\n", searchTable(table, "1", 'C')); //98
printf("first empty index = %d\n", searchTable(table,"first", 'u') );
printf("last empty index = %d\n", searchTable(table,"last", 'u') );
printTable(table);
puts("Thus ends the searchTable Test");
*/
//int instructionCounter, dataCounter;
instructionCounter = 0;
//memsize of simpletron is currently 1000 i believe
dataCounter = MEMSIZE - 1;
//setup flags array
//int flags[MEMSIZE] = {-1}; // this is declared in global namespace already
for(size_t q = 0; q < MEMSIZE; q++){
flags[q] = -1;
}
//read txt file formatted in simple and use it as the program
//must input ./a.out <input.txt file name here> <output.txt file name here>
if(argc < 3){
printf("You provided %d arguments, please provide 3 or 4\n", argc);
return 0;
}
FILE *filePtr;
FILE *outPtr;
if((filePtr = fopen(argv[1], "r")) == NULL){
printf("Couldn't open %s\n", argv[1]);
return 0;
}else if( (outPtr = fopen(argv[2], "w")) == NULL){
printf("Counldn't write to %s\n", argv[2]);
return 0;
}
output = outPtr;
firstPass(filePtr, table, outPtr);
/*fclose(outPtr);
if( (outPtr = fopen(argv[2], "rb+")) == NULL){
printf("Couldn't write to %s\n", argv[2]);
return 0;
}*/
fclose(outPtr);
if(argc > 3){ //if given an option
if (! strcmp(argv[3], "o") ){
puts("running optimize");
optimize(argv[2], table);
}
}
if( (outPtr = fopen(argv[2], "r+")) == NULL){
printf("Counldn't write to %s\n", argv[2]);
return 0;
}
secondPass(filePtr, table, outPtr, argv[2]);
fclose(filePtr);
fclose(outPtr);
simpletron_main(3, simpleCommands);
//simpletron then reads compilation output file
//simpletron()
//simpletron() writes output to screen and to an output.txt file
//try and run simpletron from here
//simpletron_main(argc, argv);
//ex12_main(infix);
return 0;
}
/*SIMPLE FUNCTIONS */
//still need to do the flags for the secondPass in goto / if ...goto
void firstPass(FILE *filePtr, TableEntry *table, FILE *output){
//current iteration breaks files into 500 char lines
char d[] = " \n"; //if you don't delimit escape sequences they'll appear in your token string
char *token = NULL;
char line[500];
int i, z, cNum, operand, operationCode, instruction, lineNum = 0; //sml instruction = operationCode[2 digits] + operand[3 digits]
int needFlag = 0;
char *command[] = {"rem", "input", "let", "print", "goto", "if", "end"};
char *eqOp[] = {"<", ">", "<=", ">=", "==", "!="}; //allowed operators (<, >, <=, >=, ==, !=)
int command_length = 7; //# of commands allowed in the language
//let
char var[10] = {0}; //list of vars to be assigned
int varMem[10] = {-1}; //list of corresponding locations of vars , keep size same as var
int tempM = 0;
//char infix[100];
char *infix = malloc(sizeof(char) * 500);
char *postfix;
char *temp;
//if goto vars
//char op1[10];
//char op2[10];
int op1, op2, branchLine = -1;
//char branchLine[10];
i = 0;
//read the file line by line , i guess that's an fgets thing ......
while ( fgets(line, sizeof(line) , filePtr) != NULL){
instruction = cNum = operand = operationCode = z = i = lineNum = 0;
needFlag = 0;
printf("line is %s\n", line);
for (z = 0; z < 10; z++ ){ //just in case reset the arrays used in let
var[z] = 0;
varMem[z] = -1; //was 0
}
//at the begining of every line reset infix to /0
//printf("infix is now %s\n", infix);
memset(infix, '\0', sizeof(infix));
//printf("infix is now %s\n", infix);
//begin a do while loop for this line
do {
if(i == 0){
token = strtok( line, d);
}
printf("%s is token\n", token);
//look for symbol in library, if not found insert into table
if(i == 0 && checkNum(*token)){
//LINE # //this should find a line # or it's a syntax error
if ( -1 == (searchTable(table, token, 'L')) ){ //if not in table then add it
z = searchTable(table, "first", 'u'); //find first empty index for a line #
table[z].symbol = atoi(token);
table[z].location = instructionCounter;
table[z].type = 'L'; // 'L' for Line #
printf("table[%d].symbol = %d\n", z, table[z].symbol);
printf("table[%d].location = %d\n", z, table[z].location);
z = 0; //reset z
}
}else if (i == 1){
//COMMAND //this should find a command or it's a syntax error
//match the token with a command, find it's index in the command string array: index = cNum
while(cNum < command_length && (strcmp(token, command[cNum])) ){ //fails when a command is found at index cNum
cNum++;
}
/*this works too
while(cNum < 6 ? strcmp(token, command[cNum]) : 0 ){
cNum++;
}
*/
if(cNum > command_length - 1){ //is command_length -1 right?
puts("Error command not found");
}/*else{
//puts("cNum > 5 = false?");
//printf("cNum = %d\n", cNum);
//printf("strcmp(token, command[cNum]) = %d\n", strcmp(token, command[cNum]) );
}*/
switch (cNum) {
case 0 :
//REM, ignore everything after rem in this line
//don't move instructionCounter, all sequential rem lines refer to the same line of sml code (the next actual instruction)
while( (token = strtok( NULL, d) != NULL)){
//cycle through the rest of the tokens until the end of the line
}
break;
case 1 :
// INPUT , check the next token for a var, see if that's in the symbol table, if not put it in, convert to sml code , #define READ 10
//10 input x 00 +1099 read x into location 99
//how to talk to simpletron from here ?...don't just write this in a file for simpletron to use
//if not found
puts("input is running");
operationCode = READ;
//find var, should be next token ---consider error checking this
token = strtok(NULL, d);
//change this later to allow for multiple variable assignment
z = searchTable(table, token, 'V');
if ( z == -1){ //symbol not found
operand = dataCounter--;
z = searchTable(table, "last", 'u'); //find last empty index
table[z].symbol = *token; //token is char
table[z].type = 'V'; // 'V' for variable
table[z].location = operand;
}else { //symbol was found
operand = table[z].location;
}
//write this sml instruction to file and increment the instructionCounter
//convert operationCode + operand into one #
instruction = MEMSIZE * operationCode;
instruction += operand;
fprintf(output, "%d\n", instruction);
instructionCounter++;
break;
case 2 :
// LET
//find all symbols , allow multiple variable assignment? fuck it why not
//consider passing through the line[] more than once
//line is index, workaround
//printf("strlen(line) = %lu\n", strlen(line));
/*
for(size_t i = 0; i < 500 ; i++){
if ( line[i] == '='){
infix = line + i;
break;
}
}*/
//copy line into infix token by token ?
//infix = strchr(line, '='); //find remainder of line from = onwards and assign to infix
//memcpy(infix, strchr(line, '='), sizeof(strchr(line, '='))+1);
//printf("line is : %s\n section after '=' is : %s\n", line, infix);
//simple.c:410:58: warning: result of comparison against a string literal is unspecified (use strncmp instead) [-Wstring-compare]
//for( size_t i = 0; (token = strtok(NULL, d)) != "="; i++){
//for( size_t i = 0; (token = strtok(NULL, d)) != "="; i++){
for( size_t i = 0; strcmp((token = strtok(NULL, d)), "="); i++){ //search through line for vars, stop when token = "=" //non-zero is true
var[i] = *token; //questionable string char problems
//unoptimized search here
z = searchTable(table, token, 'V');
if (z == - 1){
z = searchTable(table, "last", 'u');
table[z].symbol = *token;
table[z].location = dataCounter--;
table[z].type = 'V';
varMem[i] = table[z].location;
}else {
varMem[i] = table[z].location;
}
}
for ( size_t i = 0; i < 10; i++){
printf("var[i] is %c, varMem[i] is %d\n", var[i], varMem[i]);
}
//printf("before the while loop infix is %s\n", infix);
//make sure everything on the right hand side is in the symbol table && put the expression into infix
while ( (token = strtok(NULL, d)) != NULL ){
strcat(infix, token);
strcat(infix, " ");
//is token var or constant ?
//search for symbol in table , if not found insert it
//fuck me I have to deal with multidigit numbers again ......... ohhh, I can atoi it?
//printf("INSIDE WHILE LOOP: token is %s\n", token);
//printf("INSIDE WHILE LOOP: infix is %s\n", infix);
//what about operators???
if( atoi(token) ){ //if it's a number, possibly multidigit
z = searchTable(table, token, 'C');
if (z == -1 ){
z = searchTable(table, "last", 'u');
table[z].symbol = (atoi(token));
table[z].type = 'C';
table[z].location = dataCounter--;
printf("didn't find %s now is at table[%d]\n", token, z);
}else {
//do nothing if found
printf("found %s at table[%d]\n", token, z);
}
}else{ //if it's a var
z = searchTable(table, token, 'V');
if (z == - 1 && (*token >= 97 && *token <= 122 )){
z = searchTable(table, "last", 'u');
table[z].symbol = *token;
table[z].location = dataCounter--;
table[z].type = 'V';
printf("didn't find %s now is at table[%d]\n", token, z);
}else {
//do nothing if found, or if it's a non-number / non-letter
printf("found %s at table[%d]\n", token, z);
}
}
}
printTable(table);
printf("line is : %s\n section after '=' is : %s\n", line, infix);
/*for( size_t i = 0; i < 10; i++){
printf("This is var[%zu] : %c\n", i, var[i]);
}*/
//convert to postfix
//convertToPostfix(infix, postfix);
printf("infix is now %s\nThe converted postfix of this is : %s\n", infix, postfix);
//evaluate postfix kinda
//evaluatePostfixExpression(postfix)
//create appropriate code
//load ex12_main(infix);
//store in vars
tempM = ex12_main(infix); //returns the temp mem address
printf("the temporary address is %d\n", tempM);
z = 0;
while( varMem[z] != -1 ){
printf("This is varMem[%d] : %d\n", z, varMem[z]);
//load
operationCode = 20; //LOAD = 20
instruction = MEMSIZE * operationCode;
instruction += tempM;
fprintf(output, "%d\n", instruction);
instructionCounter++;
//store
operationCode = 21; //STORE = 21
instruction = MEMSIZE * operationCode;
instruction += varMem[z];
fprintf(output, "%d\n", instruction);
instructionCounter++;
z++;
}
break;
case 3 :
//PRINT
//Note : merely mentioning a variable name in a program causes the variable to be declared and initialized to zero automatically.
//if symbol is found or if it's not found the sml instruction is the same (address may be different obvi)
operationCode = WRITE;
//find var, should be next token ---consider error checking this
token = strtok(NULL, d);
z = searchTable(table, token, 'V');
if ( z == -1){ //symbol not found
operand = dataCounter--;
z = searchTable(table, "last", 'u'); //find last empty index
table[z].symbol = *token; //token is char
table[z].type = 'V'; // 'V' for variable
table[z].location = operand;
}else { //symbol was found
operand = table[z].location;
}
//write this sml instruction to file and increment the instructionCounter
//convert operationCode + operand into one #
instruction = MEMSIZE * operationCode;
instruction += operand;
fprintf(output, "%d\n", instruction);
instructionCounter++;
break;
case 4 :
//GOTO
operationCode = BRANCH; //BRANCH == 40
token = strtok(NULL, d);
printf("token of the goto line is : %s\n", token);
z = searchTable(table, token, 'L');
if (z == -1){ //not found in table
operand = 0;
flags[instructionCounter] = atoi(token);
break; //skip to next token
}else { //found in table
operand = table[z].location;
}
//write this sml instruction to file and increment the instructionCounter
//convert operationCode + operand into one #
instruction = MEMSIZE * operationCode;
instruction += operand;
fprintf(output, "%d\n", instruction);
instructionCounter++;
break;
case 5 :
//IF ... GOTO
//syntax rules? if expression goto line #
//what can an expression consist of ?, currently it will consist of
//<one operand> <eqaulity operator> <one operand>
//<one operand> : int or single letter var
//expression EQUALITY OPERATOR expression , is presumably acceptable
//char *eqOp[] = {"<", ">", "<=", ">=", "==", "!="}; //allowed operators (<, >, <=, >=, ==, !=)
//if goto vars
//char *op1;
//char *op2;
//char *branchLine;
token = strtok(NULL, d); //find first operand
//searchTable for op1
printf("%s is token will be op1\n", token);
printf("searchTable(table, token, 'V')] = %d\n", searchTable(table, token, 'V'));
printf("table[searchTable(table, token, 'V')].location = %d\n", table[searchTable(table, token, 'V')].location);
if ( atoi( token ) ){
op1 = searchTable(table, token, 'C');
if(op1 == -1 ){
op1 = searchTable(table, "last", 'u');
table[op1].location = dataCounter;
table[op1].symbol = atoi(token);
table[op1].type = 'C';
op1 = dataCounter; //op1 needs to be the memory address and not the index in the symbol table
dataCounter--;
}else{
op1 = table[op1].location;
}
}else{
op1 = searchTable(table, token, 'V');
printf("this is op1 : %d\n", op1);
if(op1 == -1 ){
op1 = searchTable(table, "last", 'u');
printf("this is op1 : %d\n", op1);
table[op1].location = dataCounter;
table[op1].symbol = *token;
table[op1].type = 'V';
op1 = dataCounter; //op1 needs to be the memory address and not the index in the symbol table
printf("this is op1 : %d\n", op1);
printf("dataCounter = %d\n", dataCounter);
dataCounter--;
}else{
op1 = table[op1].location;
}
}
printf("this is op1 : %d\n", op1);
token = strtok(NULL, d); //find eqaulity operator
//printf("%s is token is eq operator\n", token);
for(z = 0; z < 6; z++){
if (! strcmp(eqOp[z], token) ){ //found the correct operator
printf("token is %s, eqOp[z] is %s, they're equal. z is %d\n", token, eqOp[z], z);
break;
}
}
token = strtok(NULL, d); //find op2
printf("%s is token will be op2\n", token);
//searchTable for op2
if ( atoi( token ) ){
op2 = searchTable(table, token, 'C');
if(op2 == -1 ){
op2 = searchTable(table, "last", 'u');
table[op2].location = dataCounter;
table[op2].symbol = atoi(token);
table[op2].type = 'C';
op2 = dataCounter; //op1 needs to be the memory address and not the index in the symbol table
dataCounter--;
}else{
op2 = table[op2].location;
}
}else{
op2 = searchTable(table, token, 'V');
printf("this is op2 : %d\n", op2);
if(op2 == -1 ){
op2 = searchTable(table, "last", 'u');
printf("this is op2 : %d\n", op2);
table[op2].location = dataCounter;
table[op2].symbol = *token;
table[op2].type = 'V';
op2 = dataCounter; //op1 needs to be the memory address and not the index in the symbol table
printf("this is op2 : %d\n", op2);
printf("dataCounter = %d\n", dataCounter);
dataCounter--;
}else{
op2 = table[op2].location;
}
}
printf("this is op2 : %d\n", op2);
token = strtok(NULL, d); //find goto //check later
token = strtok(NULL, d); //find line
//strcpy(branchLine, token);
if( (branchLine = searchTable(table, token, 'L')) == -1 ){
//flag it later at the correct instructionCounter
//flags[instructionCounter] = atoi(token);
branchLine = atoi(token);
needFlag = 1;//can't be fucking up my instructions with wrong brancline codes
}else{
//if found give the memory address to branchLine
branchLine = table[branchLine].location;
}
//search branchLine
printf("this is branchLine : %d, needFlag = %d\n", branchLine, needFlag);
switch (z) {
case 0:
//<
//load op1
operationCode = 20; //LOAD = 20
instruction = MEMSIZE * operationCode;
instruction += op1;
fprintf(output, "%d\n", instruction);
instructionCounter++;
//subtract op2
operationCode = 31; //SUBTRACT = 31
instruction = MEMSIZE * operationCode;
instruction += op2;
fprintf(output, "%d\n", instruction);
instructionCounter++;
//branchneg to the line after goto
operationCode = 41; //BRANCHNEG = 41
instruction = MEMSIZE * operationCode;
//needFlag ? flags[instructionCounter] = branchLine : instruction += branchLine;
if(needFlag){
flags[instructionCounter] = branchLine;
}else{
instruction += branchLine;
}
//instruction += branchLine;
fprintf(output, "%d\n", instruction);
instructionCounter++;
break;
case 1:
//>
//load op2
operationCode = 20; //LOAD = 20
instruction = MEMSIZE * operationCode;
instruction += op2;
fprintf(output, "%d\n", instruction);
instructionCounter++;
//subtract op1
operationCode = 31; //SUBTRACT = 31
instruction = MEMSIZE * operationCode;
instruction += op1;
fprintf(output, "%d\n", instruction);
instructionCounter++;
//branchneg to the line after goto
operationCode = 41; //BRANCHNEG = 41
instruction = MEMSIZE * operationCode;
if(needFlag){
flags[instructionCounter] = branchLine;
}else{
instruction += branchLine;
}
fprintf(output, "%d\n", instruction);
instructionCounter++;
break;
case 2:
// <=
//load op1
operationCode = 20; //LOAD = 20
instruction = MEMSIZE * operationCode;
instruction += op1;
fprintf(output, "%d\n", instruction);
instructionCounter++;
instruction = 0;
//subtract op2
operationCode = 31; //SUBTRACT = 31
instruction = MEMSIZE * operationCode;
instruction += op2;
fprintf(output, "%d\n", instruction);
instructionCounter++;
//branchneg to the line after goto
operationCode = 41; //BRANCHNEG = 41
instruction = MEMSIZE * operationCode;
if(needFlag){
flags[instructionCounter] = branchLine;
}else{
instruction += branchLine;
}
fprintf(output, "%d\n", instruction);
instructionCounter++;
//branch zero to the line after goto
operationCode = 42; //BRANCHZERO = 42
instruction = MEMSIZE * operationCode;
if(needFlag){
flags[instructionCounter] = branchLine;
}else{
instruction += branchLine;
}
fprintf(output, "%d\n", instruction);
instructionCounter++;
break;
case 3:
//>=
//load op2
operationCode = 20; //LOAD = 20
instruction = MEMSIZE * operationCode;
instruction += op2;
fprintf(output, "%d\n", instruction);
instructionCounter++;
//subtract op1
operationCode = 31; //SUBTRACT = 31
instruction = MEMSIZE * operationCode;
instruction += op1;
fprintf(output, "%d\n", instruction);
instructionCounter++;
//branchneg to the line after goto
operationCode = 41; //BRANCHNEG = 41
instruction = MEMSIZE * operationCode;
if(needFlag){
flags[instructionCounter] = branchLine;
}else{
instruction += branchLine;
}
fprintf(output, "%d\n", instruction);
instructionCounter++;
//branch zero to the line after goto
operationCode = 42; //BRANCHZERO = 42;
instruction = MEMSIZE * operationCode;
if(needFlag){
flags[instructionCounter] = branchLine;
}else{
instruction += branchLine;
}
fprintf(output, "%d\n", instruction);
instructionCounter++;
break;
case 4:
//==
//load op1
operationCode = 20; //LOAD = 20
instruction = MEMSIZE * operationCode;
instruction += op1;
printf("instruction is : %d\n", instruction);
fprintf(output, "%d\n", instruction);
instructionCounter++;
//subtract op2
operationCode = 31; //SUBTRACT = 31
instruction = MEMSIZE * operationCode;
instruction += op2;
fprintf(output, "%d\n", instruction);
instructionCounter++;
//branch zero to the line after goto
operationCode = 42; //BRANCHZERO = 42;
instruction = MEMSIZE * operationCode;
if(needFlag){
flags[instructionCounter] = branchLine;
}else{
instruction += branchLine;
puts("noflag needed bby");
}
fprintf(output, "%d\n", instruction);
instructionCounter++;
break;
case 5:
//!= //more difficult, just do the code for == to branch away from if the condition is true,
//load op1
//subtract op2
//branchZero to the line after branch , flag this
//load op1
operationCode = 20; //LOAD = 20
instruction = MEMSIZE * operationCode;
instruction += op1;
fprintf(output, "%d\n", instruction);
instructionCounter++;
//subtract op2
operationCode = 31; //SUBTRACT = 31
instruction = MEMSIZE * operationCode;
instruction += op2;
fprintf(output, "%d\n", instruction);
instructionCounter++;
//branch zero to the line after the next branch statement
operationCode = 42; //BRANCHZERO = 42;
instruction = MEMSIZE * operationCode;
instruction += instructionCounter + 2; //does this work ?? //currently don't know where this'll end up at
//no flag neded now?? //flags[instructionCounter] = atoi(branchLine); //so flag it
fprintf(output, "%d\n", instruction);
instructionCounter++;
//branch to the line after goto
operationCode = 40; //BRANCH = 40
instruction = MEMSIZE * operationCode;
if(needFlag){
flags[instructionCounter] = branchLine;
}else{
instruction += branchLine;
}
fprintf(output, "%d\n", instruction);
instructionCounter++;
break;
default :
puts("Error is Equality operator switch");
}
z = 0;
break;
case 6 :
//END //End == 43
operationCode = 43;
//write this sml instruction to file and increment the instructionCounter
//convert operationCode + operand into one #
instruction = MEMSIZE * operationCode;
//no operand assignment necessary, always = 000
instruction += operand;
fprintf(output, "%d\n", instruction);
instructionCounter++;
break;
default :
puts("Error in cNum switch");
}
}else if (i > 2){
//EXPRESSION //this should find an expression or it's a syntax error
//remember to handle if..goto differently
}
//firstPass();
i++;
} while((token = strtok( NULL, d)) != NULL);
/*end of the line do while loop */
printf("run %d of while\n", i);
}
}
void printTable(TableEntry table[]){
int z = 0;
for( z = 0; z < TABLESIZE; z++){
printf("Table Entry %d: symbol = %d;\t location = %d;\t type = %c\n", z, table[z].symbol, table[z].location, table[z].type );
}
}
//return index of entry or -1
//optimized to only look in the area where the token should be so if entries get into the wrong areas it wont see them
// when searching for the first empty index call searchTable(table, "first", 'u')
// when searching for the last empty index call searchTable(table, "last", 'u')
int searchTable(TableEntry table[], char *token, char type){
int z = 0;
switch (type) {
case 'L':
for ( z = 0; z < TABLESIZE; z++ ){
if (table[z].symbol == atoi(token) && table[z].type == 'L'){
return z;
}else if (table[z].type == 'u'){ //unassigned you've searched too far
return -1;
}else{
//keep looking
}
}
return -1; //couldn't find it
case 'V':
for ( z = TABLESIZE - 1; z >= 0; z-- ){
if (table[z].symbol == *token && table[z].type == 'V'){
return z;
}else if (table[z].type == 'u'){ //unassigned you've searched too far
return -1;
}else{
//keep looking
}
}
return -1; //couldn't find it
case 'C':
for ( z = TABLESIZE - 1; z >= 0; z-- ){
if (table[z].symbol == atoi(token) && table[z].type == 'C'){
return z;
}else if (table[z].type == 'u'){ //unassigned you've searched too far
return -1;
}else{
//keep looking
}
}
return -1; //couldn't find it
case 'u': //searching for the first empty spot