-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
134 lines (122 loc) · 3.02 KB
/
main.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
/*
* main.c -- SPL compiler
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common.h"
#include "utils.h"
#include "sym.h"
#include "types.h"
#include "absyn.h"
#include "scanner.h"
#include "parser.h"
#include "table.h"
#include "semant.h"
#include "varalloc.h"
#include "codegen.h"
#define VERSION "1.1"
static void version(char *myself) {
printf("%s version %s (compiled %s)\n", myself, VERSION, __DATE__);
}
static void help(char *myself) {
/* show some help how to use the program */
printf("Usage: %s [options] <input file> <output file>\n", myself);
printf("Options:\n");
printf(" --tokens show stream of tokens\n");
printf(" --absyn show abstract syntax\n");
printf(" --tables show symbol tables\n");
printf(" --vars show variable allocation\n");
printf(" --version show compiler version\n");
printf(" --help show this help\n");
}
int main(int argc, char *argv[]) {
int i;
char *inFileName;
char *outFileName;
boolean optionTokens;
boolean optionAbsyn;
boolean optionTables;
boolean optionVars;
int token;
Table *globalTable;
FILE *outFile;
/* analyze command line */
inFileName = NULL;
outFileName = NULL;
optionTokens = FALSE;
optionAbsyn = FALSE;
optionTables = FALSE;
optionVars = FALSE;
for (i = 1; i < argc; i++) {
if (argv[i][0] == '-') {
/* option */
if (strcmp(argv[i], "--tokens") == 0) {
optionTokens = TRUE;
} else
if (strcmp(argv[i], "--absyn") == 0) {
optionAbsyn = TRUE;
} else
if (strcmp(argv[i], "--tables") == 0) {
optionTables = TRUE;
} else
if (strcmp(argv[i], "--vars") == 0) {
optionVars = TRUE;
} else
if (strcmp(argv[i], "--version") == 0) {
version(argv[0]);
exit(0);
} else
if (strcmp(argv[i], "--help") == 0) {
help(argv[0]);
exit(0);
} else {
error("unrecognized option '%s'; try '%s --help'",
argv[i], argv[0]);
}
} else {
/* file */
if (outFileName != NULL) {
error("more than two file names not allowed");
}
if (inFileName != NULL) {
outFileName = argv[i];
} else {
inFileName = argv[i];
}
}
}
if (inFileName == NULL) {
error("no input file");
}
if (outFileName == NULL) {
error("no output file");
}
yyin = fopen(inFileName, "r");
if (yyin == NULL) {
error("cannot open input file '%s'", inFileName);
}
if (optionTokens) {
do {
token = yylex();
showToken(token);
} while (token != 0);
fclose(yyin);
exit(0);
}
yyparse();
fclose(yyin);
if (optionAbsyn) {
showAbsyn(progTree);
exit(0);
}
globalTable = check(progTree, optionTables);
allocVars(progTree, globalTable, optionVars);
outFile = fopen(outFileName, "w");
if (outFile == NULL) {
error("cannot open output file '%s'", outFileName);
}
genCode(progTree, globalTable, outFile);
fclose(outFile);
return 0;
}