-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
84 lines (71 loc) · 2.07 KB
/
main.c
File metadata and controls
84 lines (71 loc) · 2.07 KB
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
#include "Expression.h"
#include "optimizer.h"
#include "codegen.h"
#include "symtab.h"
#include "Parser.h"
#include "Lexer.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int yyparse(NodeList** funcdecls, int* errorCount, SymTable* symtable,
yyscan_t scanner);
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: %s <source file>\n", argv[0]);
exit(0);
}
// Open file to read
FILE* f = fopen(argv[1], "r");
if (!f) {
printf("Cannot find file: %s\n", argv[1]);
exit(0);
}
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET);
char* source_content = malloc(fsize + 1);
fread(source_content, fsize, 1, f);
fclose(f);
source_content[fsize] = 0;
//printf("Input source file\n");
//printf("%s\n", source_content);
NodeList* funcdecls;
YY_BUFFER_STATE state;
yyscan_t scanner;
if (yylex_init(&scanner)) return -1;
SymTable* symtable = init_hash_table();
if (!symtable) {
printf("Error creating symbolic table\n");
return -1;
}
// Scan the source file
state = yy_scan_string(source_content, scanner);
// Parse the source file
int errorCount = 0;
int stat = yyparse(&funcdecls, &errorCount, symtable, scanner);
if (errorCount > 0) {
printf("%d error(s) encounter while parsing.\n", errorCount);
return -1;
}
// Delete buffer;
yy_delete_buffer(state, scanner);
// Destroy lexer
yylex_destroy(scanner);
// Delete symbol table now
DeleteSymTable(symtable);
/************************************************************
* THIS IS WHERE WE DO STUFF
* Essentially, the variable funcdecls contain the AST of function declaration.
* And the function declaration is what we have to compile.
*************************************************************/
printf("Printing the AST BEFORE OPTIMIZATION\n");
PrintProgram(funcdecls);
Optimizer(funcdecls);
printf("Printing the AST AFTER OPTIMIZATION\n");
PrintProgram(funcdecls);
Codegen(funcdecls);
// Finish.
free(source_content);
FreeProgram(funcdecls);
return 0;
}