-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
173 lines (148 loc) · 4.9 KB
/
main.cpp
File metadata and controls
173 lines (148 loc) · 4.9 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
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
#include <iostream>
#include <fstream>
#include <string>
#include "ast.h"
extern FILE* yyin;
extern int yyparse();
extern Program* program;
void usage() {
std::cerr << "Usage: linguo [options] file.linguo" << std::endl;
std::cerr << "Options:" << std::endl;
std::cerr << " -emit-ir Output LLVM IR to file.ll" << std::endl;
std::cerr << " -emit-bc Output LLVM bitcode to file.bc" << std::endl;
std::cerr << " -optimize Run optimization passes" << std::endl;
std::cerr << " -run Run the program immediately" << std::endl;
std::cerr << " -verbose Print additional information" << std::endl;
std::cerr << " -tokens Display parsed tokens" << std::endl;
std::cerr << " -help Display this help message" << std::endl;
}
// Function to output the AST structure
void printAST(Program* program) {
std::cout << "Parsed tokens:" << std::endl;
std::cout << "==============" << std::endl;
// Print out the program structure
if (program) {
program->printTokens();
} else {
std::cout << "No tokens parsed or empty program." << std::endl;
}
std::cout << "==============" << std::endl;
}
int main(int argc, char** argv) {
// Parse command line arguments
bool emitIR = false;
bool emitBC = false;
bool optimize = false;
bool runProgram = false;
bool verbose = false;
bool showTokens = false;
std::string inputFile;
for (int i = 1; i < argc; i++) {
std::string arg = argv[i];
if (arg == "-emit-ir") {
emitIR = true;
} else if (arg == "-emit-bc") {
emitBC = true;
} else if (arg == "-optimize") {
optimize = true;
} else if (arg == "-run") {
runProgram = true;
} else if (arg == "-verbose") {
verbose = true;
} else if (arg == "-tokens") {
showTokens = true;
} else if (arg == "-help" || arg == "--help" || arg == "-h") {
usage();
return 0;
} else if (arg[0] == '-') {
std::cerr << "Unknown option: " << arg << std::endl;
usage();
return 1;
} else {
inputFile = arg;
}
}
// Check for input file
if (inputFile.empty()) {
std::cerr << "No input file specified." << std::endl;
usage();
return 1;
}
// Open the input file
FILE* file = fopen(inputFile.c_str(), "r");
if (!file) {
std::cerr << "Could not open input file: " << inputFile << std::endl;
return 1;
}
// Set the input file for flex
yyin = file;
// Parse the input file
if (verbose) {
std::cout << "Parsing input file: " << inputFile << std::endl;
}
int parseResult = yyparse();
// Close the input file
fclose(file);
if (parseResult != 0) {
std::cerr << "Parsing failed." << std::endl;
return 1;
}
if (verbose) {
std::cout << "Parsing successful." << std::endl;
}
// Display tokens if requested
if (showTokens) {
printAST(program);
}
// Generate LLVM IR
if (verbose) {
std::cout << "Generating LLVM IR..." << std::endl;
}
CodeGenContext context;
try {
context.generateCode(program);
// Always print IR in verbose mode
if (verbose) {
std::cout << "\nGenerated LLVM IR:\n" << std::endl;
context.printIR();
}
// Run optimization passes if requested
if (optimize) {
if (verbose) {
std::cout << "Running optimization passes..." << std::endl;
}
context.optimize();
if (verbose) {
std::cout << "Optimized IR:" << std::endl;
context.printIR();
}
}
// Output LLVM IR if requested
if (emitIR) {
std::string irFile = inputFile.substr(0, inputFile.find_last_of('.')) + ".ll";
if (verbose) {
std::cout << "Writing LLVM IR to: " << irFile << std::endl;
}
context.writeIR(irFile);
}
// Run the program if requested
if (runProgram) {
if (verbose) {
std::cout << "\n=== Running program ===\n" << std::endl;
}
context.runJIT();
if (verbose) {
std::cout << "\n=== Program execution completed ===\n" << std::endl;
}
}
} catch (const std::exception& ex) {
std::cerr << "Error during code generation or execution: " << ex.what() << std::endl;
return 1;
} catch (...) {
std::cerr << "Unknown error during code generation or execution" << std::endl;
return 1;
}
// Clean up
delete program;
return 0;
}