-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
75 lines (59 loc) · 1.57 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
#include "definitions.h"
#define extern_
#include "data.h"
#undef extern_
#include "declarations.h"
#include <errno.h>
// Compiler set-up and top-level execution
// Initialize global variables
static void init() {
Line = 1;
Putback = '\n';
Globals = 0;
Locals = NSYMBOLS - 1;
O_dumpAST = 0;
}
// Print instructions if program arguments are incorrect
static void usage(char *prog) {
fprintf(stderr, "Usage: %s [-T] infile\n", prog);
exit(1);
}
// Open/scan the file and its tokens
int main(int argc, char *argv[]) {
int i;
init(); // Initialize globals
// Scan for command-line options
for (i = 1; i < argc; i++) {
if (*argv[i] != '-') break;
for (int j = 1; argv[i][j]; j++) {
switch (argv[i][j]) {
case 'T':
O_dumpAST = 1;
break;
default:
usage(argv[0]);
}
}
}
// Ensure we have an input file argument
if (i >= argc)
usage(argv[0]);
if ((Infile = fopen(argv[i], "r")) == NULL) {
fprintf(stderr, "Unable to open %s: %s\n", argv[i], strerror(errno));
exit(1);
}
if ((Outfile = fopen("out.s", "w")) == NULL) {
fprintf(stderr, "Unable to create `out.s`: %s\n", strerror(errno));
exit(1);
}
// For now, ensure that `void printint()` is defined
addglobal("printint", P_INT, S_FUNCTION, C_GLOBAL, 0, 0);
addglobal("printchar", P_VOID, S_FUNCTION, C_GLOBAL, 0, 0);
scan(&Token); // Get the first token from the input
genpreamble();
global_declarations(); // Parse the global declarations
genpostamble();
fclose(Infile);
fclose(Outfile);
return 0;
}