forked from bhargavkulk/CSF363-baseline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cc
107 lines (87 loc) · 2.41 KB
/
main.cc
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
#include <iostream>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <fstream>
#include "parser.hh"
#include "ast.hh"
#include "llvmcodegen.hh"
extern FILE *yyin;
extern int yylex();
extern char *yytext;
NodeStmts *final_values;
#define ARG_OPTION_L 0
#define ARG_OPTION_P 1
#define ARG_OPTION_S 2
#define ARG_OPTION_O 3
#define ARG_FAIL -1
int parse_arguments(int argc, char *argv[]) {
if (argc == 3 || argc == 4) {
if (strlen(argv[2]) == 2 && argv[2][0] == '-') {
if (argc == 3) {
switch (argv[2][1]) {
case 'l':
return ARG_OPTION_L;
case 'p':
return ARG_OPTION_P;
case 's':
return ARG_OPTION_S;
}
} else if (argv[2][1] == 'o') {
return ARG_OPTION_O;
}
}
}
std::cerr << "Usage:\nEach of the following options halts the compilation process at the corresponding stage and prints the intermediate output:\n\n";
std::cerr << "\t`./bin/base <file_name> -l`, to tokenize the input and print the token stream to stdout\n";
std::cerr << "\t`./bin/base <file_name> -p`, to parse the input and print the abstract syntax tree (AST) to stdout\n";
std::cerr << "\t`./bin/base <file_name> -s`, to compile the file to LLVM assembly and print it to stdout\n";
std::cerr << "\t`./bin/base <file_name> -o <output>`, to compile the file to LLVM bitcode and write to <output>\n";
return ARG_FAIL;
}
int main(int argc, char *argv[]) {
int arg_option = parse_arguments(argc, argv);
if (arg_option == ARG_FAIL) {
exit(1);
}
std::string file_name(argv[1]);
FILE *source = fopen(argv[1], "r");
if(!source) {
std::cerr << "File does not exists.\n";
exit(1);
}
yyin = source;
if (arg_option == ARG_OPTION_L) {
extern std::string token_to_string(int token, const char *lexeme);
while (true) {
int token = yylex();
if (token == 0) {
break;
}
std::cout << token_to_string(token, yytext) << "\n";
}
fclose(yyin);
return 0;
}
final_values = nullptr;
yyparse();
fclose(yyin);
if(final_values) {
if (arg_option == ARG_OPTION_P) {
std::cout << final_values->to_string() << std::endl;
return 0;
}
llvm::LLVMContext context;
LLVMCompiler compiler(&context, "base");
compiler.compile(final_values);
if (arg_option == ARG_OPTION_S) {
compiler.dump();
} else {
compiler.write(std::string(argv[3]));
}
} else {
std::cerr << "empty program";
}
return 0;
}