Repository navigation
Expand file tree
/
Copy pathcse_structs.h
More file actions
324 lines (309 loc) · 8.1 KB
/
Copy pathcse_structs.h
File metadata and controls
324 lines (309 loc) · 8.1 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
#include <iostream>
#include <vector>
#include <string>
using namespace std;
//! Different types of "Base *" types
// lambda: data_type functions (Read below)
// eta: same structure as functions (Read below)
// delta: arg_int = index on the array of control_structures array
// gamma: -
// operator: arg_str = operator
// identifier: arg_str = identifier name
// environment: prev = parent environment
// beta: -
// tau: arg_int = size
// ystar: -
// boolean: arg_str = "true" or "false"
//! How different data types are stored
// string: type = "string", arg_str = value
// tuple: type = "tuple", children = values as Base *
// integer: type = "integer", arg_int = value
// boolean: type = "boolean", arg_str = "true" or "false"
// function: type = "lambda", arg_str = name, arg_int = stored index, children = function parameters, prev = called environment
// dummy: type = "dummy" Eg. actual value of each print() output
class Base // Each node in the control stack and the stack stack
{
public:
string type = "none";
string arg_str = "";
int arg_int;
Base *prev = nullptr;
vector<Base *> children = {};
// Constructors
Base(string type) : type(type){};
Base(string type, int arg_int) : type(type), arg_int(arg_int){}; // Eg: integer
Base(string type, string arg_str) : type(type), arg_str(arg_str){}; // Eg: identifier
Base(string type, string arg_str, int arg_int) : type(type), arg_str(arg_str), arg_int(arg_int){};
Base(string type, string arg_str, Base *prev) : type(type), arg_str(arg_str), prev(prev){};
Base(string type, Base *prev) : type(type), prev(prev){};
// Destructor
~Base()
{
for (auto &child : children)
{
delete child;
};
delete prev;
}
};
stack<Base *> control_stk; // Working control stack
stack<Base *> stack_stk; // Working stack stack
stack<Base *> parsing_env; // Stored environments
vector<vector<Base *>> control_structures; // Stores each function present in the syntax tree
// In built functions
void add_in_built_to_env(Base *env)
{
// Defining internal variables
//? env->children.push_back(new Base("identifier", "hundred", new Base("integer", 100)));
// Defining inbuilt functions
env->children.push_back(new Base("identifier", "Print", new Base("lambda", -1)));
env->children.push_back(new Base("identifier", "print", new Base("lambda", -1)));
env->children.push_back(new Base("identifier", "Isinteger", new Base("lambda", -2)));
env->children.push_back(new Base("identifier", "Isstring", new Base("lambda", -3)));
env->children.push_back(new Base("identifier", "Istuple", new Base("lambda", -4)));
env->children.push_back(new Base("identifier", "Isfunction", new Base("lambda", -5)));
env->children.push_back(new Base("identifier", "Isdummy", new Base("lambda", -6)));
env->children.push_back(new Base("identifier", "Stem", new Base("lambda", -7)));
env->children.push_back(new Base("identifier", "Stern", new Base("lambda", -8)));
env->children.push_back(new Base("identifier", "Conc", new Base("lambda", -9)));
env->children.push_back(new Base("identifier", "Order", new Base("lambda", -10)));
env->children.push_back(new Base("identifier", "Null", new Base("lambda", -11)));
env->children.push_back(new Base("identifier", "ItoS", new Base("lambda", -12)));
}
void print_Base(Base *env) // Printing a Base structure
{
if (env->type == "tuple")
{
cout << "(";
if (env->children.size() > 0)
{
print_Base(env->children[0]);
}
for (int i = 1; i < env->children.size(); i++)
{
cout << ", ";
print_Base(env->children[i]);
}
cout << ")";
}
else if (env->type == "integer")
{
cout << env->arg_int;
}
else if (env->type == "boolean")
{
cout << env->arg_str;
}
else if (env->type == "string")
{
for (int i = 0; i < env->arg_str.size(); i++)
{
if (env->arg_str[i] == '\\')
{
i++;
if (i < env->arg_str.size())
{
if (env->arg_str[i] == 'n')
{
cout << "\n";
}
else if (env->arg_str[i] == 't')
{
cout << "\t";
}
else if (env->arg_str[i] == 'b')
{
cout << "\b";
}
else if (env->arg_str[i] == '\\')
{
cout << "\\";
}
else if (env->arg_str[i] == '"')
{
cout << "\"";
}
else if (env->arg_str[i] == '\'')
{
cout << "\'";
}
else
{
string sub = env->arg_str.substr(i - 1, 2);
cout << sub;
}
}
else
{
cout << "\\";
}
}
else
{
cout << env->arg_str[i];
}
}
}
}
// Runs the suitable index based on the index of the inbuilt function
void in_built_functions(Base *func, Base *func_args)
{
int val = func->arg_int;
if (val == -1)
{
print_Base(func_args);
cout << endl;
stack_stk.push(new Base("dummy"));
}
else if (val == -2)
{
if (func_args->type == "integer")
{
stack_stk.push(new Base("boolean", "true"));
}
else
{
stack_stk.push(new Base("boolean", "false"));
}
}
else if (val == -3)
{
if (func_args->type == "string")
{
stack_stk.push(new Base("boolean", "true"));
}
else
{
stack_stk.push(new Base("boolean", "false"));
}
}
else if (val == -4)
{
if (func_args->type == "tuple")
{
stack_stk.push(new Base("boolean", "true"));
}
else
{
stack_stk.push(new Base("boolean", "false"));
}
}
else if (val == -5)
{
if (func_args->type == "lambda")
{
stack_stk.push(new Base("boolean", "true"));
}
else
{
stack_stk.push(new Base("boolean", "false"));
}
}
else if (val == -6)
{
if (func_args->type == "dummy")
{
stack_stk.push(new Base("boolean", "true"));
}
else
{
stack_stk.push(new Base("boolean", "false"));
}
}
else if (val == -7)
{
if (func_args->type != "string")
{
cout << "Expect a string with Stem" << endl;
throw "Error";
}
if (func_args->arg_str.size() == 0)
{
cout << "Expect a string atleast of size 1 with Stem" << endl;
throw "Error";
}
stack_stk.push(new Base("string", func_args->arg_str.substr(0, 1)));
}
else if (val == -8)
{
if (func_args->type != "string")
{
cout << "Expect a string with Stern" << endl;
throw "Error";
}
if (func_args->arg_str.size() == 0)
{
cout << "Expect a string atleast of size 1 with Stern" << endl;
throw "Error";
}
stack_stk.push(new Base("string", func_args->arg_str.substr(1)));
}
else if (val == -9)
{
if (func_args->type != "string" || stack_stk.top()->type != "string")
{
cout << "Expect two strings with Conc" << endl;
throw "Error";
}
Base *temp = stack_stk.top();
control_stk.pop();
stack_stk.pop();
string output = func_args->arg_str + temp->arg_str;
stack_stk.push(new Base("string", output));
}
else if (val == -10)
{
if (func_args->type != "tuple")
{
cout << "Expect a tuple with Order" << endl;
throw "Error";
}
stack_stk.push(new Base("integer", func_args->children.size()));
}
else if (val == -11)
{
if (func_args->type != "tuple")
{
cout << "Expect a tuple with Null" << endl;
throw "Error";
}
if (func_args->children.size() == 0)
{
stack_stk.push(new Base("boolean", "true"));
}
else
{
stack_stk.push(new Base("boolean", "false"));
}
}
else if (val == -12)
{
if (func_args->type != "integer")
{
cout << "Expect an integer with ItoS" << endl;
throw "Error";
}
stack_stk.push(new Base("string", to_string(func_args->arg_int)));
}
}
void clear_stacks()
{
while (!ast_bu.empty())
{
Node *temp = ast_bu.top();
ast_bu.pop();
delete temp;
}
// while (!tokens.empty())
// {
// Token temp = tokens.back();
// tokens.pop_back();
// delete &temp;
// }
while (!dt_bu.empty())
{
string temp = dt_bu.back();
dt_bu.pop_back();
delete &temp;
}
}