-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler.c
105 lines (76 loc) · 1.95 KB
/
compiler.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
#include <stdio.h>
#include <string.h>
#include "vm.h"
#include "lex.h"
#include "parser.h"
int main(int argc, char ** argv) {
if(argc < 2) {
puts("No input file or switches given.");
puts("Switches are \"-l\" \"-a\" and \"-v\"");
puts("Usage: ./compile [-l] [-a] [-v] prog (in any order)");
}
char * progname;
char * switches[] = { "-l", "-a", "-v" };
int switchez[3];
switchez[0] = switchez[1] = switchez[2] = 0;
int i;
for(int j=1; j < argc; j++) {
if((i = findIndex(argv[j], switches, 0, 3)) >= 0) {
switchez[i] = 1;
}
else
progname = argv[j];
}
if(progname == NULL) {
puts("No input file");
return 0;
}
int lo = lexscan(progname, switchez[0]);
if(lo)
return printerr(lo, "compiler");
if(!lo) {
FILE * lexe = fopen("lexemeList", "r");
char ** lex;
int lexl = 0;
char word[15];
while(fscanf(lexe, "%s", word) != EOF) lexl++;
lex = (char **) malloc(lexl * sizeof(char *));
rewind(lexe);
int h = 0;
while(fscanf(lexe, "%s", word) != EOF) {
lex[h] = malloc((sizeof(char) * strlen(word)) + 1);
strcpy(lex[h], word);
h++;
}
rewind(lexe);
// printfile(lexe);
puts("\n\n");
if(switchez[0]) {
puts("Lexeme List:");
for(int i = 0; i < lexl; i++)
printf("%s ", lex[i]);
puts("\n");
}
putchar('\n');
// for(int i = 0; i < lexl; i++)
// printf("%-3d", i);
fclose(lexe);
// printf("token count: %d\n", lexl);
if(lexl == 0)
return printerr(9, "compiler");
int p = programp(lex, lexl);
if(p)
printf("Program is syntactically correct!\n\n");
if(p && switchez[1])
for(int i = 0; i < ci; i++)
printf("%2d: %s %d %d\n", i, opname(code[i].op), code[i].l, code[i].m);
if(p) {
puts("\n\n");
vm(code, switchez[2]);
}
for(int i = 0; i < lexl; i++)
free(lex[i]);
free(lex);
}
remove("lexemeList");
}