-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
380 lines (313 loc) · 8.73 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// main.c
// GRAMCNF.BIN - c-like interpreter.
// Ported from Gramado 32bit.
// 2022 - Fred Nora
#include "gramcnf.h"
// see: parser.h
struct program_d program;
const char *VersionString = "1.0";
//default name.
char program_name[] = "[Default program name]";
char *compiler_name;
//static int running = 1;
int running = 1;
//Para o caso de não precisarmos produzir
//nenhum arquivo de output.
int no_output=0;
/* While POSIX defines isblank(), it's not ANSI C. */
//#define IS_BLANK(c) ((c) == ' ' || (c) == '\t')
// #important
// Specification for gramc.
//char *standard_spec = "%{%{CC} -c %{I} -o %{O}}";
// =====================================================
static void doUsage(char **argv);
static void doVersion(char **argv);
static int gramcnf_initialize(void);
static void debugShowStat(void);
// =====================================================
static void doUsage(char **argv)
{
printf ("\n");
printf("#todo: %s doUsage\n",argv[0]);
}
static void doVersion(char **argv)
{
printf ("\n");
printf ("====================\n");
printf ("%s version %s \n", argv[0], VersionString );
}
/*
int is_letter(char c);
int is_letter(char c)
{
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
*/
// gramcnf_initialize:
// Initialize global variables.
static int gramcnf_initialize(void)
{
int Status = 0;
register int i=0;
//printf ("gramcnf_initialize:\n");
// Clear buffers
infile_size = 0;
outfile_size = 0;
// Clear infile and outfile buffers.
for ( i=0; i<INFILE_MAX_SIZE; i++ ){
infile[i] = '\0';
};
sprintf (infile, "; ======================== \n");
strcat (infile, "; Initializing infile ... \n\n");
for ( i=0; i<OUTFILE_MAX_SIZE; i++ ){
outfile[i] = '\0';
};
sprintf (outfile, "; ========================\n");
strcat (outfile, ";Initializing outfile ... \n\n");
// Clear text, data, bss buffers.
sprintf (TEXT, "; ======================== \n");
strcat (TEXT, "; Initializing TEXT buffer \n");
strcat (TEXT, "segment .text \n");
sprintf (DATA, "; ======================== \n");
strcat (DATA, "; Initializing DATA buffer \n");
strcat (DATA, "segment .data \n");
sprintf (BSS, "; ======================== \n");
strcat (BSS, "; Initializing BSS buffer \n");
strcat (BSS, "segment .bss \n");
// Table.
// Contador para não estourar a lista.
keyword_count = 0;
identifier_count = 0;
keyword_count = 0;
constant_count = 0;
string_count = 0;
separator_count = 0;
special_count = 0;
// ...
// Usado pelo lexar pra saber
// qual lugar na lista colocar o lexeme.
current_keyword = 0;
current_identifier = 0;
current_keyword = 0;
current_constant = 0;
current_string = 0;
current_separator = 0;
current_special = 0;
// The 'program' structure.
program.name = program_name;
program.function_count;
program.function_list = NULL;
//...
// Initializing the metadata structure.
// See: globals.h and globals.c
for (i=0; i<32; i++)
{
metadata[i].id = 0;
metadata[i].initialized = FALSE;
metadata[i].tag_size = 0;
metadata[i].name_size = 0;
metadata[i].content_size = 0;
};
return (int) Status;
}
// Mostra as estatísticas para o desenvolvedor.
static void debugShowStat(void)
{
register int i=0;
printf("\n");
printf("debugShowStat\n");
// -------------------------
// Lexer
printf("==========================================\n");
printf("== Lexer info ==\n");
printf("number of lines: {%d}\n",lexer_number_of_lines);
printf("first line: {%d}\n",lexer_firstline);
printf("last line: {%d}\n",lexer_lastline);
printf("current line: {%d}\n",lexer_currentline); // The number of lines.
printf("token count: {%d}\n",lexer_token_count);
// ...
// -------------------------
// Parser
printf("==========================================\n");
printf("== Parser info ==\n");
printf("infile_size: {%d bytes}\n",infile_size);
printf("outfile_size: {%d bytes}\n",outfile_size);
// ...
printf("\n");
printf("==========================================\n");
printf("Printing metadata structure. :)\n");
// Metadata
for (i=0; i<32; i++)
{
if (metadata[i].initialized == TRUE)
{
printf("\n");
printf("id{%d}: tag{%s} name{%s} content{%s}\n",
metadata[i].id,
metadata[i].meta_tag,
metadata[i].name,
metadata[i].content );
//printf("name{%s}\n", metadata[i].name);
//printf("content{%s}\n", metadata[i].content);
}
};
printf("\n");
printf("==========================================\n");
}
int main(int argc, char *argv[])
{
// Input
FILE *fp;
// Output file for compiler.
FILE *____O;
register int i=0;
char *filename;
// Output string.
char *o;
// Switches
int flagA = 0;
int flagB = 0;
int flagC = 0;
int flagD = 0;
int flagString1 = 0;
int flagString2 = 0;
int flagString3 = 0;
int flagString4 = 0;
int flagX = 0;
int flagY = 0;
int flagZ = 0;
int flagR = 0;
int flagS = 0;
int flagT = 0;
int fShowStats = FALSE; //#bugbug
int fDumpOutput = FALSE; // Dump output file
// Carregamos o arquivo num buffer em ring0.
// getc() precisa ler os dados em stdin
// #bugbug:
// Se o buffer for maior que isso, read() falha.
char __buf[1024];
int nreads=0;
// Initializing
//debug_print ("gramcnf: Initializing ...\n");
//printf ("\n");
//printf ("main: Initializing ..\n");
// Inicializa variáveis globais.
gramcnf_initialize();
//printf ("*breakpoint");
//while (1){}
//
// ## Args ##
//
// #todo
// O nome do programa é o primeiro comando da linha.
// compiler_name = argv[0];
// #debug
// Mostrando os argumentos.
//printf ("argc=%d \n", argc );
//for ( i=0; i < argc; i++ ){
// printf("arg %d = %s \n", i, argv[i] );
//};
// flags.
// Comparando os argumentos para acionar as flags.
for (i=0; i<argc; i++)
{
// #todo
// Create symmetric symbols for these flags.
if ( strncmp( argv[i], "-a", 2) == 0 ){
}
if ( strncmp( argv[i], "-b", 2) == 0 ){
}
if ( strncmp( argv[i], "-s", 2) == 0 ){
asm_flag = 1;
}
if ( strncmp( argv[i], "-v", 2) == 0 ){
doVersion(argv);
return EXIT_SUCCESS;
}
// Show stats
if ( strncmp( argv[i], "--stats", 7) == 0 ){
fShowStats = TRUE;
}
// Dump (Show) output file.
// see: dump_output_file() in parser.c
if ( strncmp( argv[i], "--dumpo", 7) == 0 ){
fDumpOutput = TRUE;
}
// help
if ( strncmp( argv[i], "--help", 6) == 0 ){
doUsage(argv);
return EXIT_SUCCESS;
}
// version
if ( strncmp( argv[i], "--version", 9) == 0 ){
doVersion(argv);
return EXIT_SUCCESS;
}
//...
};
// # Arquivo de entrada #
// #bugbug
// lembrando que não podemos mais usar os elementos
// da estrutura em user mode.
// Então o buffer é gerenciado pelo kernel.
// podemos copiar o conteúdo do arquivo para um buffer aqui no programa
// através de fread, mas fread está disponível apenas na libc03.
// Open
//printf ("\n");
//printf("Calling fopen() :)\n");
//while(1){}
// Input file:
fp = fopen((char *) argv[2], "rb");
if (fp == NULL)
{
printf("gramcnf: Couldn't open the input file\n");
doUsage(argv);
goto fail;
}
// Input file.
// para que getc leia desse arquivo que carregamos.
stdin = fp;
finput = fp;
// #todo:
// Maybe we can get the file size here,
// we don't wanna handle long files for now.
// #debug
// Esse while está aqui para visualizarmos o arquivo carregado.
//int c;
//while(1)
//{
//c=getc(stdin);
//if(c == EOF)
//break;
//printf("%c",c);
//}
//fflush(stdout);
//while(1){}
//=====================================
// Compiler
// Routine:
// + Initialize the lexer.
// + Parse the tokens.
// + Return a pointer to the output file.
// IN: dump output file?
// #bugbug
// Do not write things in stdout, because its gonna show it
// into the screen.
// We gotta use another output file to simply save it into the disk.
____O = (FILE *) compiler(fDumpOutput);
//if ( (void*) ____O == NULL )
//{
//
//}
if (fShowStats){
debugShowStat();
}
printf("Done :)\n");
return EXIT_SUCCESS;
fail:
printf("Failed :(\n");
return EXIT_FAILURE;
}
//
// End
//