-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
198 lines (187 loc) · 5.11 KB
/
test.cpp
File metadata and controls
198 lines (187 loc) · 5.11 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
#include <iostream>
#include <vector>
#include <array>
#include <string>
using namespace std;
enum HANDLER { none, quotes, variableormethod, numberorfloat, operation, additionorconcatenation };
HANDLER handling = HANDLER::none;
const char digits[10] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
//// bytes in digits / bytes in one element of the array = total num of elements in array (idk why don't ask me)
//// sizeof(digits) / sizeof(digits[0])
static bool isNumber(string str) {
char c = str.at(0);
for (int i = 0; i < sizeof(digits) / sizeof(digits[0]); i++) {
if (c == digits[i]) {
return true;
}
}
return false;
}
//dummy function
string getVariable(const string& str) {
return "VAR";
}
string handleStrConcatenation(const string& str, string(*getVariable)(const string& variableName)) {
string fullString = "";
// This is just the seperate parts of the string str
string currString = "";
int val = 0;
bool num = false;
for (int i = 0; i < str.length(); i++) {
if (str.at(i) == '"' || str.at(i) == '\'') {
// If it's the end of the quotation mark
if (handling == quotes) {
fullString += currString;
handling = none;
currString = "";
continue;
}
handling = quotes;
continue;
}
// If it's the first character, then it's probably a variable so print it
else if (i == 0) {
handling = HANDLER::variableormethod;
}
if (handling == quotes) {
//cout << str.at(i);
// If it's a special character (\)
if (str.at(i) == '\\') {
// Then, add that character to the string
i++;
if (str.at(i) == 'n') {
currString += '\n';
}
continue;
}
currString += str.at(i);
continue;
}
else if (handling == HANDLER::additionorconcatenation) {
if (str.at(i) != ' ') {
handling = HANDLER::variableormethod;
}
}
// Here, it's not quotes so it may be a method, variable or class
else if (handling == HANDLER::variableormethod) {
if (str.at(i) == ' ') {
handling = none;
cout << currString << "test\n";
if (isNumber(currString)) {
// cout << currString << " \n";
// fullString += currString;
val += stoi(currString);
num = true;
}
else {
fullString += getVariable(currString);
}
currString = "";
continue;
}
currString += str.at(i);
}
if (str.at(i) == '+') {
handling = HANDLER::additionorconcatenation;
}
}
// If it the concatenation abrubtly ends (which it will), then add the last currString
// Or as a neat trick just add a space at the end or something to make the loop continue
if (currString != "") {
if (handling == HANDLER::variableormethod) {
if (isNumber(currString)) {
fullString += currString;
}
else {
fullString += getVariable(currString);
}
}
}
handling = HANDLER::none;
if (num) {
fullString = to_string(val);
}
return fullString;
}
array<vector<string>, 8> handleParentheses(const string& str) {
// vector<string> order;
array<vector<string>, 8> order;
// max of 10 parentheses deep
// order.reserve(10);
string currWord;
int depth = 0;
for (char ch : str) {
// cout << depth << " -> " << ch << '\n';
if (ch == ')') {
// cout << currWord << depth << '\n';
order[depth].push_back(currWord);
currWord = "";
depth--;
}
else if (ch == '(') {
// reserved character
// currWord += '\t';
currWord += '■';
order[depth].push_back(currWord);
currWord = "";
depth++;
}
else {
currWord += ch;
}
}
for (int i = order.size(); i >= 0; i--) {
if (!order[i].empty()) {
for (int z = 0; z < order.at(i).size() - 1; z++) {
cout << "i: " << i << " z: " << z << " -> " << order.at(i)[z] << '\n';
order.at(i)[z] = handleStrConcatenation(order.at(i)[z], &getVariable);
}
}
}
cout << "------\n";
// If there's any remaining word after the loop, handle it
// if (!currWord.empty()) {
// order.push_back(currWord);
// }
return order;
}
string parseParentheses(const string& str, string(*getVariable)(const string& variableName)) {
string fullString = "";
// This is just the seperate parts of the string str
string currString = "";
for (char ch : str) {
if (handling == HANDLER::none) {
currString += ch;
if (ch == '+') {
}
}
}
// If it the concatenation abrubtly ends (which it will), then add the last currString
// Or as a neat trick just add a space at the end or something to make the loop continue
// if (currString != "") {
// if (handling == HANDLER::variableormethod) {
// // Just check if first character of the string is a number
// if (isNumber(currString.at(0))) {
// fullString += currString;
// }
// else {
// fullString += getVariable(currString);
// }
// }
// }
handling = HANDLER::none;
return fullString;
}
//int main() {
// array<vector<string>, 8> vec = handleParentheses("5 + (5 + 5) + int(str('5' + '5'))");
// for (int i = vec.size() - 1; i >= 0; i--) {
// if (vec[i + 1].size() != 0) {
// vector<string> vec2 = vec.at(i);
// // cout << i << " -> " << parseParentheses(vec.at(i), &getVariable) << '\n';
// for (int z = 0; z < vec2.size(); z++) {
// cout << "i: " << i << " z: " << z << " -> " << vec2[z] << '\n';
// }
// }
// }
//}