-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompiler.cpp
More file actions
327 lines (306 loc) · 8.96 KB
/
compiler.cpp
File metadata and controls
327 lines (306 loc) · 8.96 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import utilities;
import <iostream>;
import <fstream>;
import <string>;
import <vector>;
import <unordered_map>;
import <stack>;
import <unordered_set>;
import <sstream>;
import <queue>;
using namespace std;
/*
Powerhouse
A language designed to be faster than C++, but with less lines of code.
It aims to have three compilers: one with only what you need for competitive programming (with error handling), one without error handling, and one with error handling and all other features
The syntax is a mix of Cpp, Rust and it's own syntax.
It uses Rust data types, uses ownership, and no error handling unless you run it with the error handler compiler.
However, it also has C++ features like struct and memory management from C++
NOTES:
- comments with "bad?" are possibly bad implementations
*/
// AI usage
class Variable {
public:
enum Type { INT, FLOAT, STRING, UNDEFINED } type;
int intValue;
float floatValue;
string stringValue;
Variable() : type(UNDEFINED) {}
Variable(int value) : type(INT), intValue(value) {}
Variable(float value) : type(FLOAT), floatValue(value) {}
Variable(string value) : type(STRING), stringValue(value) {}
string toString() const {
switch (type) {
case INT: return to_string(intValue);
case FLOAT: return to_string(floatValue);
case STRING: return stringValue;
default: return "undefined";
}
}
};
class Method {
// Method implementation
};
class ClassInfo {
public:
std::unordered_map<std::string, Variable> privateMembers;
std::unordered_map<std::string, Variable> publicMembers;
std::unordered_map<std::string, Method> privateMethods;
std::unordered_map<std::string, Method> publicMethods;
};
// Storage
std::unordered_map<std::string, ClassInfo> classDefinitions;
std::unordered_map<std::string, Variable> variables;
// It increases each time you go deeper into a method
int currentScopeDepth = 0;
// State enums
// Generally used in reading lines
enum class EXPECT { none, variable, method, waitForEndOfLine, comment, logInputOrReturnString, logInputOrReturnInt, waitTillNoSpace };
EXPECT expect = EXPECT::none;
// Used to process variables
enum class VAREXPECT { vartype, varassignment, varvalue };
VAREXPECT varexpect = VAREXPECT::vartype;
bool isInClass = false;
// the class we are in currently
string currentClass = "";
// Variables
string currWord = "";
string varname = "";
string vartype = "";
string methodname = "";
bool firstChar = true;
const unordered_set<string> builtInMethods = { "" };
const unordered_set<string> builtInSpecialMethods = { "log", "logn", "return", "input" };
// vartype -> varassignment -> varvalue
// x: int = 5 -> go past the ":", get the type and then go past the assignment and get the value
static void checkForVariable(const char& ch) {
//cout << ch << '\n';
if (varexpect == VAREXPECT::vartype) {
// If we expect assignment, go past the = sign and find the expected value
if (ch != ' ') {
currWord += ch;
}
else {
vartype = currWord;
currWord = "";
varexpect = VAREXPECT::varassignment;
}
}
// This is to skip the space and equal sign to get to the value
else if (varexpect == VAREXPECT::varassignment) {
if (ch == ' ' || ch == '=') {
return;
}
else {
currWord += ch;
varexpect = VAREXPECT::varvalue;
}
}
// Add the characters to currWord. In the '\n' check in int main(), it will actually assign the variable in memory
else if (varexpect == VAREXPECT::varvalue) {
currWord += ch;
}
}
static void parseClass(const std::string& className, const std::string& code) {
bool inStaticMethod = false;
}
// AI Usage
static string getVariable(const string& variableName) {
if (currentScopeDepth == 0) {
if (variables.find(variableName) != variables.end()) {
return variables[variableName].toString();
}
}
else if (currentScopeDepth == 1) {
// TODO
}
else if (isInClass) {
// TODO
}
return "undefined";
}
// (AI) Function to ensure the vector is resized properly before accessing an element
template <typename T> void ensureCapacity(std::vector<T>& vec, size_t index)
{
if (index >= vec.size())
{
vec.resize(index + 1);
}
}
//static void print(string str) {
// cout << str;
//}
//
//static void println(string str) {
// cout << str << '\n';
//}
int main()
{
try {
ifstream file("test.txt");
if (!file.is_open()) {
cerr << "Failed to open file." << endl;
cout << "Waiting on exit...";
int t; cin >> t;
return 1;
}
string code((istreambuf_iterator<char>(file)), istreambuf_iterator<char>());
// artifically add \n to ensure that the last line will be executed
code += "\n";
file.close();
for (char ch : code) {
//cout << ch << '\n';
// To check if the first character is a special symbol such as comment
if (firstChar) {
if (ch == '/') {
expect = EXPECT::comment;
continue;
}
// We have a global declaration
else if (ch == '#') {
// TODO: implement global logic
}
firstChar = false;
}
// ## CHECK FOR NEW LINES. THIS SHOULD BE AT THE TOP. ##
if (ch == '\n') {
if (expect == EXPECT::logInputOrReturnString) {
// TODO: Just testing...
cout << "TESTING\n";
// This must be at the end because otherwise it outputs true always
handleConcatenationWithDifferentTypes(currWord, getVariable);
continue;
if (methodname == "log") {
cout << handleStrConcatenation(currWord, getVariable);
}
else if (methodname == "logn") {
cout << handleStrConcatenation(currWord, getVariable) << '\n';
}
} else if (expect == EXPECT::logInputOrReturnInt) {
// TODO: Just testing...
cout << "TESTING\n";
// This must be at the end because otherwise it outputs true always
handleConcatenationWithDifferentTypes(currWord, getVariable);
continue;
//cout << currWord;
if (methodname == "log") {
cout << handleExpression(currWord);
}
else if (methodname == "logn") {
cout << handleExpression(currWord) << '\n';
}
}
// This must be at the end because otherwise it outputs true always
else if (expect == EXPECT::variable) {
// If we had a variable declaration, we can finish up here
if (varexpect == VAREXPECT::varvalue) {
//cout << varname << ": " << vartype << " = " << currWord << '\n';
// AI Usage
if (vartype == "str") {
//variables[varname] = Variable(handleStrConcatenation(currWord));
variables[varname] = Variable(currWord);
}
else if (vartype == "int") {
variables[varname] = Variable(handleExpression(currWord));
}
else if (vartype == "float") {
variables[varname] = Variable(stof(currWord));
}
}
}
expect = EXPECT::none;
currWord = "";
// Reset first char since there is a \n
firstChar = true;
continue;
}
// ## CHECK IF DECLARATION ALREADY KNOWN ##
if (expect != EXPECT::none) {
if (expect == EXPECT::waitForEndOfLine) {
continue;
}
else if (expect == EXPECT::logInputOrReturnString) {
currWord += ch;
// We automatically assume the log is printing a string/variable unless we see parentheses, which means integer evaluation
if (ch == '(') {
expect = EXPECT::logInputOrReturnInt;
}
continue;
}
else if (expect == EXPECT::logInputOrReturnInt) {
currWord += ch;
}
else if (expect == EXPECT::waitTillNoSpace) {
if (ch == ' ') {
// Reset currword
currWord = "";
expect = EXPECT::variable;
}
continue;
}
else if (expect == EXPECT::variable) {
checkForVariable(ch);
}
else if (expect == EXPECT::method) {
// ## WHERE METHODS ARE RUN ##
if (methodname == "log") {
if (ch == ')') {
expect = EXPECT::waitForEndOfLine;
// Run the method
//print(currWord);
}
else {
currWord += ch;
}
}
}
continue;
}
else if (expect == EXPECT::comment) {
continue;
}
// It's definitely a variable declaration
if (ch == ':') {
// bad?
//currWord = currWord.substr(0, currWord.length() - 1);
varname = currWord;
expect = EXPECT::waitTillNoSpace;
// We need to find out the type of the variable and the value
varexpect = VAREXPECT::vartype;
currWord = "";
}
// It's definitely a log, input, return, for, while or class
if (ch == ' ') {
if (builtInSpecialMethods.find(currWord) != builtInSpecialMethods.end()) {
expect = EXPECT::logInputOrReturnString;
methodname = currWord;
currWord = "";
}
else {
// check if it's a for loop, while, class, etc
}
}
// It's a method call
else if (ch == '(') {
// check if it's a built in method call
if (builtInMethods.find(currWord) != builtInMethods.end()) {
methodname = currWord;
currWord = "";
expect = EXPECT::method;
}
// TODO: or check if it's a user created method
}
// Otherwise just add the characters to currWord
else {
currWord += ch;
}
}
}
catch (const std::exception& e) {
cout << e.what();
}
cout << "Waiting on exit...";
int t; cin >> t;
return 0;
}