-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
133 lines (116 loc) · 2.97 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
#include <stdlib.h>
#include <errno.h>
#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 intermediate code */
#define NO_CODE FALSE
#include "util.h"
#if NO_PARSE
#include "scan.h"
#else
#include "parse.h"
#if !NO_ANALYZE
#include "analyze.h"
#if !NO_CODE
#include "intercode.h"
#endif
#endif
#endif
#include "assemblycode.h"
#include "binarycode.h"
/* allocate global variables */
int progloc = 0;
int lineno = 0;
FILE * source;
FILE * listing;
FILE * codeinter;
FILE * acode;
FILE * bcode;
/* allocate and set tracing flags */
int EchoSource = FALSE;
int TraceScan = TRUE;
int TraceParse = TRUE;
int TraceAnalyze = TRUE;
int TraceCode = TRUE;
int Error = FALSE;
int main( int argc, char * argv[] ){
TreeNode * syntaxTree;
QuadList Quads;
AssemblyCode codehead;
char pgm[120]; /* source code file name */
if (argc != 3)
{
fprintf(stderr,"Usage: %s <filename> <id>\n",argv[0]);
exit(1);
}
char *p;
errno = 0;
progloc = (int) strtol(argv[2], &p, 10);
if (*p != '\0' || errno != 0)
{
fprintf(stderr, "Invalid ID. Aborting.\n");
exit(1);
}
strcpy(pgm,argv[1]);
if (strchr (pgm, '.') == NULL)
strcat(pgm,".cm");
source = fopen(pgm,"r");
if (source==NULL)
{
fprintf(stderr,"Arquivo %s nao encontrado\n",pgm);
exit(1);
}
listing = stdout; /* send listing to screen */
fprintf(listing,"\nCompilacao C-: %s\n",pgm);
codeinter = fopen("codintermediario.txt","w+");
acode = fopen("codassembly.txt","w+");
bcode = fopen("bincode.txt","w+");
#if NO_PARSE
while (getToken()!=ENDFILE);
#else
syntaxTree = parse();
if (TraceParse)
{
fprintf(listing,"\nArvore Sintatica:\n");
printTree(syntaxTree);
}
#if !NO_ANALYZE
if (! Error)
{
if (TraceAnalyze) fprintf(listing,"\nConstruindo Tabela de Simbolos...\n");
buildSymtab(syntaxTree);
if (TraceAnalyze) fprintf(listing,"\nChecando Tipos...\n");
typeCheck(syntaxTree);
if (TraceAnalyze) fprintf(listing,"\nChecagem de Tipo Concluida\n");
}
#if !NO_CODE
if(!Error)
{
if (codeinter == NULL)
{ printf("Nao foi possivel abrir \"codintermediario.txt\"\n");
exit(1);
}
fprintf(codeinter, "\nCodigo Intermediario do %s:\n\n", pgm);
if (TraceCode) fprintf(listing,"\nConstruindo Codigo Intermediario...\n");
Quads = GenInterCode(syntaxTree);
if (TraceCode) fprintf(listing,"\nCodigo Intermediario Concluido\n");
fclose(codeinter);
fprintf(acode, "\nCodigo Assembly do %s:\n\n", pgm);
if (TraceCode) fprintf(listing,"\nConstruindo Codigo Assembly...\n");
codehead = GenAssembly(Quads);
if (TraceCode) fprintf(listing,"\nCodigo Assembly Concluido\n");
if (TraceCode) fprintf(listing,"\nConstruindo Codigo Binario...\n");
GenBinary(codehead,getSize());
if (TraceCode) fprintf(listing,"\nCodigo Binario Concluido\n");
fclose(acode);
fclose(bcode);
}
#endif
#endif
#endif
fclose(source);
return 0;
}