-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.cpp
259 lines (247 loc) · 8.96 KB
/
token.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
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
//
// Created by Pro on 14/03/2021.
//
#include "include/parser.h"
#include "include/token.h"
#include <cassert>
#include <cctype>
const int GLOBAL_SCOPE = 5000;
const int ROW_SCOPE = 1000;
std::vector<std::pair<int, std::string> > cur_cmd;
int cnt = 1;
std::vector<int> rootsOfExpressions = {0};
std::vector<Node> tree = {Node(GLOBAL_SCOPE, "")};
void get_keyword(Token token){
cur_cmd.push_back({0, std::to_string(token.get_id())});
}
std::vector<char> expect_curly;
char residue = 0;
void get_special_character(char ch){
if (ch == '&' || ch == '|'){
if (residue != 0){
std::string s = "";
s += ch;
s += ch;
cur_cmd.emplace_back(1, s);
residue = 0;
return;
}
residue = ch;
return;
}
if (residue != 0){
std::cout << "FATAL: " << residue << " unexpected symbol" << std::endl;
assert(false);
}
std::string s = "";
s += ch;
cur_cmd.emplace_back(1, s);
if (ch == '{') expect_curly.push_back(0);
if (ch == '}'){
expect_curly.pop_back();
if (expect_curly.empty()) run_row();
return;
}
if (ch == ';' && expect_curly.empty()){
run_row();
}
if (ch == '~') flood_of();
}
void get_variables(std::string var){
bool is_dec = true;
bool is_float = false;
for (char ch:var){
if (ch == '.' && is_dec){
is_dec = false;
is_float = true;
}else if (isalpha(ch)){
is_dec = false;
is_float = false;
break;
}
}
if (is_dec){
cur_cmd.push_back({2, var});
}else if (is_float){
cur_cmd.push_back({3, var});
}else{
cur_cmd.push_back({4, var});
}
}
void run_row(){
std::vector<char> curl;
bool in_chain = false;
int cur_root = cnt;
tree[rootsOfExpressions.back()].children.push_back(cnt);
tree.push_back(Node(ROW_SCOPE, ""));
rootsOfExpressions.push_back(cnt);
cnt++;
std::vector<int> expectations = {0, 4};
for (const std::pair<int, std::string> x:cur_cmd){
bool ok = false;
for (int exp:expectations){
if (x.first == exp) ok = true;
}
if (!ok) assert(false);
switch (x.first){
case 0:
{
if (in_chain) assert(false);
if (stoi(x.second) >= 100){
tree.back() = Node(x.first, x.second);
expectations = {0, 1, 2, 3, 4};
}else {
tree.push_back(Node(x.first, x.second));
tree[rootsOfExpressions.back()].children.push_back(cnt);
tree.back().children.push_back(cnt + 1);
in_chain = true;
cnt++;
expectations = {4};
// if (stoi(x.second) < 10) expectations.push_back(0);
}
break;
}
case 1:
{
if (x.second == ","){
expectations = {0, 1, 2, 3, 4};
break;
}
if (x.second == ";"){
if (in_chain) assert(false);
if (tree[rootsOfExpressions.back()].val == "=") rootsOfExpressions.pop_back();
if (rootsOfExpressions.back() != cur_root) assert(false);
rootsOfExpressions.pop_back();
if (curl.size()){
cur_root = cnt;
tree[rootsOfExpressions.back()].children.push_back(cnt);
tree.push_back(Node(ROW_SCOPE, ""));
rootsOfExpressions.push_back(cnt);
cnt++;
expectations = {0, 1, 2, 3, 4};
}
break;
}
if (x.second == "(" || x.second == "[" || x.second == "{"){
tree.push_back(Node(x.first, x.second));
if (in_chain) in_chain = false;
else tree[rootsOfExpressions.back()].children.push_back(cnt);
rootsOfExpressions.push_back(cnt);
cnt++;
if (x.second == "{"){
curl.push_back(0);
cur_root = cnt;
tree[rootsOfExpressions.back()].children.push_back(cnt);
tree.push_back(Node(ROW_SCOPE, ""));
rootsOfExpressions.push_back(cnt);
cnt++;
}
}else if(x.second == ")" || x.second == "]" || x.second == "}"){
if (in_chain) assert(false);
std::string pr = tree[rootsOfExpressions.back()].val;
if (pr == "=" || pr == "<" || pr == "<=" || pr == ">" || pr == ">=" || pr == "==" || pr == "!=" || pr == "&&" || pr == "||") rootsOfExpressions.pop_back();
if ((x.second == ")" && tree[rootsOfExpressions.back()].val == "(") ||
(x.second == "]" && tree[rootsOfExpressions.back()].val == "[") ||
(x.second == "}" && rootsOfExpressions.size() > 1 && tree[rootsOfExpressions[rootsOfExpressions.size()-2]].val == "{")){
rootsOfExpressions.pop_back();
if (x.second == "}" && curl.size()){
curl.pop_back();
tree[rootsOfExpressions.back()].children.pop_back();
tree.pop_back();
cnt--;
rootsOfExpressions.pop_back();
rootsOfExpressions.pop_back();
cur_root = cnt;
tree[rootsOfExpressions.back()].children.push_back(cnt);
tree.push_back(Node(ROW_SCOPE, ""));
rootsOfExpressions.push_back(cnt);
cnt++;
}
}else{
std::cout << tree[rootsOfExpressions.back()].val << " expected, but " << x.second << " recognized" << std::endl;
assert(false);
}
}else if (x.second == "=" || x.second == "<" || x.second == ">" || x.second == "!" || x.second == "&&" || x.second == "||"){
if (x.second == "=" && tree.back().id == 1){
tree.back().val += x.second[0];
expectations = {0, 1, 2, 3, 4};
break;
}
tree.push_back(Node(x.first, x.second));
if (in_chain) in_chain = false;
else tree[rootsOfExpressions.back()].children.push_back(cnt);
rootsOfExpressions.push_back(cnt);
cnt++;
}else{
if (in_chain){
std::cout << "FATAL: '" << x.second << "' operator after '" << tree.back().val << "' not expected" << std::endl;
assert(false);
}
tree.push_back(Node(x.first, x.second));
tree[rootsOfExpressions.back()].children.push_back(cnt);
tree.back().children.push_back(cnt+1);
in_chain = true;
cnt++;
}
expectations = {0, 1, 2, 3, 4};
break;
}
case 2:
{
tree.push_back(Node(x.first, x.second));
if (in_chain){
in_chain = false;
}else{
tree[rootsOfExpressions.back()].children.push_back(cnt);
}
cnt++;
expectations = {1};
break;
}
case 3:
{
tree.push_back(Node(x.first, x.second));
if (in_chain){
in_chain = false;
}else{
tree[rootsOfExpressions.back()].children.push_back(cnt);
}
cnt++;
expectations = {1};
break;
}
case 4:
{
tree.push_back(Node(x.first, x.second));
if (in_chain){
in_chain = false;
}else{
tree[rootsOfExpressions.back()].children.push_back(cnt);
}
cnt++;
expectations = {1};
break;
}
default:
assert(false);
}
}
cur_cmd.clear();
}
void flood_of(){
getTokenTree(tree);
//clear memory
cur_cmd.shrink_to_fit();
rootsOfExpressions.clear();
rootsOfExpressions.shrink_to_fit();
tree.clear();
tree.shrink_to_fit();
}
//func f
// {
// print(get(Args, 0));
// if (get(Args, 0) >= 6){ return 0; }
// f(get(Args, 0)+1);
// }
//f(0);
//~