-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
104 lines (95 loc) · 2.38 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
/****************************************************/
/* File: main.c */
/* Main program for C- compiler */
/* Max Forasteiro */
/****************************************************/
#include "globals.h"
/* set NO_PARSE to TRUE to get a scanner-only compiler */
#define NO_PARSE FALSE
/* set NO_ANALYZE to TRUE to get a parser-only compiler */
#define NO_ANALYZE FALSE
/* set NO_CODE to TRUE to get a compiler that does not
* generate code
*/
#define NO_CODE TRUE
#include "util.h"
#if NO_PARSE
#include "scan.h"
#else
#include "parse.h"
#if !NO_ANALYZE
#include "analyze.h"
#if !NO_CODE
#include "cgen.h"
#endif
#endif
#endif
/* allocate global variables */
int lineno = 0;
FILE *source;
FILE *listing;
FILE *code;
/* allocate and set tracing flags */
int EchoSource = FALSE;
int TraceScan = FALSE;
int TraceParse = FALSE;
int TraceAnalyze = TRUE;
int TraceCode = FALSE;
int Error = FALSE;
int main(int argc, char *argv[]) {
TreeNode *syntaxTree;
char pgm[120]; /* source code file name */
if (argc != 2) {
fprintf(stderr, "usage: %s <filename>\n", argv[0]);
exit(1);
}
strcpy(pgm, argv[1]) ;
if (strchr(pgm, '.') == NULL)
strcat(pgm, ".cminus");
source = fopen(pgm, "r");
if (source == NULL) {
fprintf(stderr, "File %s not found\n", pgm);
exit(1);
}
listing = stdout; /* send listing to screen */
fprintf(listing, "\nC- COMPILATION: %s\n", pgm);
#if NO_PARSE
while (getToken() != ENDFILE);
#else
syntaxTree = parse();
if (TraceParse) {
fprintf(listing, "\nSyntax tree:\n");
printTree(syntaxTree);
}
#if !NO_ANALYZE
if (!Error) {
if (TraceAnalyze)
fprintf(listing, "\nBuilding Symbol Table...\n");
buildSymtab(syntaxTree);
if (TraceAnalyze)
fprintf(listing, "\nChecking Types...\n");
typeCheck(syntaxTree);
if (TraceAnalyze)
fprintf(listing, "\nType Checking Finished\n");
}
#if !NO_CODE
if (!Error) {
char * codefile;
int fnlen = strcspn(pgm, ".");
codefile = (char *) calloc(fnlen + 4, sizeof(char));
strncpy(codefile, pgm, fnlen);
strcat(codefile, ".tm");
code = fopen(codefile, "w");
if (code == NULL) {
printf("Unable to open %s\n", codefile);
exit(1);
}
codeGen(syntaxTree, codefile);
fclose(code);
}
#endif
#endif
#endif
fclose(source);
return 0;
}