-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
161 lines (131 loc) · 4.27 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
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
#include <iostream>
#include <sstream>
#include <fstream>
#include "lexer.h"
#include "parser.h"
#include "executor.h"
#include "serialization.h"
int main(int argc, char** argv) {
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " <filename>\n";
exit(1);
}
if (std::string(argv[1]) == "-h" || std::string(argv[1]) == "-help") {
std::cout << "Usage: " << argv[0] << " <filename> <?args>\n\n"
<< "-help (-h) Displays help for program.\n"
<< "-dump (-d) Dumps debug logs into file.\n"
<< "-run (-r) Executes the program after compiling.\n"
<< "-nostrict (-s) Turns off strict mode during runtime.\n"
<< "-runfile (-rf) Runs program from a precompiled file.\n"
<< "-outfile (-o) Sets output file for compiled code.\n"
;
exit(0);
}
bool dump = false;
bool run = false;
bool strict = true;
bool runfile = false;
std::string compFileName = "out.nsn.o";
if (argc >= 3) {
for (int i = 2; i < argc; i++) {
std::string arg(argv[i]);
if (arg == "-dump" || arg == "-d") {
dump = true;
} else if (arg == "-run" || arg == "-r") {
if (runfile) {
std::cerr << "Invaild arguments. -r and -rf cannot both be true.\n";
exit(1);
}
run = true;
} else if (arg == "-nostrict" || arg == "-s") {
strict = false;
} else if (arg == "-runfile" || arg == "-rf") {
if (run) {
std::cerr << "Invaild arguments. -r and -rf cannot both be true.\n";
exit(1);
}
runfile = true;
} else if (arg == "-outfile" || arg == "-o") {
if (run || runfile) {
std::cerr << "Invalid arguments. -o and -r/-rf cannot both be true.\n";
exit(1);
}
if (i + 1 == argc) {
std::cerr << "Invalid arguments. -o expects a filename.\n";
exit(1);
}
compFileName = std::string(argv[i + 1]);
i += 1;
}
else {
std::cerr << "Unrecognized argument \"" << arg << "\"\n";
exit(1);
}
}
}
std::ifstream file(argv[1]);
std::stringstream* buffer = new std::stringstream;
*buffer << file.rdbuf();
if (!file) {
std::cerr << "Error loading file \"" << argv[1] << "\".\n";
exit(1);
}
if (!runfile) {
file.close();
auto lexresult = lex(buffer);
auto parseresult = parse(lexresult);
if (run) {
int rcode = exec(parseresult, strict);
std::cout << "\nExited with code " << rcode << std::endl;
} else {
//compile to source
serialize(parseresult, compFileName);
}
if (dump) {
std::stringstream finaldout;
finaldout << "======= LEXER RESULT =======\n";
for (auto y : lexresult) {
finaldout << "[ ";
for (std::string x : y) {
finaldout << x << ", ";
}
finaldout << "]\n";
}
finaldout << "\n ======= PARSER RESULT =======\n";
for (auto toPush : parseresult) {
finaldout << "------------------------\n"
<< "Type: " << toPush.type << "\n"
<< "size: " << toPush.size << "\n"
<< "uid: " << toPush.uid << "\n"
<< "jmp_index: " << toPush.jmp_index << "\n"
<< "args: [";
for (std::string piss : toPush.args) {
finaldout << piss << ", ";
}
finaldout << "]\narg_types: [";
for (int a : toPush.arg_types) {
finaldout << a << ", ";
}
finaldout << "]\n";
}
finaldout << "------------------------\n";
std::ofstream dumpFile;
dumpFile.open("FINAL_DUMP.txt");
if (!dumpFile) {
std::cerr << "ERROR GENERATING DUMP FILE\n";
exit(1);
}
dumpFile << finaldout.str();
dumpFile.close();
std::cout << "==> Dump File Created Successfully!\n";
if (run) saveDebugLog();
}
} else {
// exec serialized file
auto parsed = deserialize(buffer);
exec(parsed, strict);
if (dump) saveDebugLog();
}
delete buffer;
return 0;
}