-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
71 lines (70 loc) · 1.66 KB
/
main.cpp
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
#include <string>
#include <fstream>
#include "lexer.h"
#include "parser.h"
#include "error.h"
#include "sym_table.h"
#include "asm_generator.h"
int main(int argc, const char *argv[]){
if (argc == 1){
cout << "fatal error: no input files\ncompilation terminated\n";
}
ofstream fout;
try {
if (argc == 3){
lexer L(argv[2]);
if (strcmp(argv[1], "-p") == 0){
parser P(&L);
fout.open("parser.out");
P.parse(fout);
P.print_sym_table(fout);
fout.close();
} else if (strcmp(argv[1], "-l") == 0){
fout.open("lexer.out");
while (L.token_can_exist()){
fout << L.next();
}
fout.close();
} else if (strcmp(argv[1], "-g") == 0){
parser P(&L);
fout.open("asmgen.asm");
P.parse(fout);
asm_code *code = new asm_code(P);
code->print(fout);
fout.close();
}
}
if (argc == 4){
lexer L(argv[3]);
if (strcmp(argv[1], "-p") == 0)
if (strcmp(argv[2], "-decl") == 0){
parser P(&L);
P.point_of_entry = true;
fout.open("declar.out");
P.parse(fout);
P.print_sym_table(fout);
fout.close();
} else if (strcmp(argv[2], "-expr") == 0){
parser P(&L);
P.point_of_entry = true;
fout.open("expression.out");
P.tcast = false; /* Disable type checking */
P.parse_expr(nullptr)->print(fout, 0);
fout.close();
} if (strcmp(argv[2], "-tcast") == 0){
parser P(&L);
P.point_of_entry = false;
fout.open("typecast.out");
P.tcast = true; /* Disable type checking */
P.parse(fout);
P.print_sym_table(fout);
fout.close();
}
}
} catch (error e){
fout << e.msg;
} catch (...){
fout << "Unexpected exception. ";
}
return 0;
}