-
Notifications
You must be signed in to change notification settings - Fork 3
/
nodes.cpp
412 lines (361 loc) · 6.81 KB
/
nodes.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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
enum nodetype{
N_INT,
N_FLOAT,
N_BINOP,
N_IDENT,
N_STR,
N_ASSIGN,
N_UNARY,
N_FUNC,
N_CALL,
N_TRUE,
N_FALSE,
N_NONE,
N_CLASS,
N_DOT,
N_DOTASSIGN,
N_DOTCALL,
N_RETURN,
N_LIST, //
N_TUPLE, // Same internally (List)
N_DICT,
N_CONTROL,
N_IF,
N_ELSE,
N_SUBSCR,
N_RAISE,
N_STORE_SUBSCR,
N_TRY_EXCEPT_FINALLY,
N_TRY,
N_EXCEPT,
N_FINALLY,
N_FOR,
N_BREAK, //No data struct
N_CONTINUE, //No data struct
N_WHILE,
N_MULTIIDENT,
N_IMPORT,
N_FROM,
N_SLICE,
N_STORE_SLICE,
N_DEL,
N_GLBL_IDENT,
N_FSTRING,
N_ASSERT,
N_NONLOCAL, //No data struct
N_TERNARY,
N_DECORATOR,
N_ANONIDENT,
N_ANONASSIGN, //No data struct
N_ANONNONLOCAL, //No data struct
N_ANONGLBL_IDENT, //No data struct
N_ANONDOT,
N_YIELD,
N_SET, //No data struct (List)
N_WITH,
N_LISTCOMP,
N_TUPLECOMP, //No data struct (N_TUPLECOMP)
N_SETCOMP, //No data struct (N_TUPLECOMP)
N_DICTCOMP,
N_BSTRING,
N_BIN, //No data struct (N_INTLIT)
N_HEX, //No data struct (N_INTLIT)
N_OCTAL, //No data struct (N_INTLIT)
};
enum precedence {
LOWEST = 1,
ASSIGN,
LOGICAL_OR,
LOGICAL_AND,
LOGICAL_NOT,
EQUALS,
LESSGREATER,
BITWISE_OR_PREC,
BITWISE_XOR_PREC,
BITWISE_AND_PREC,
BITWISE_SHIFT_PREC,
SUM,
PRODUCT,
BITWISE_NOT_PREC,
EXP,
CALL,
INDEX,
UNARY,
TERNARY,
};
enum precedence get_precedence(Token t){
switch (t.type){
case T_EQ:
case T_IADD:
case T_ISUB:
case T_IMUL:
case T_IDIV:
case T_IMOD:
case T_IPOW:
case T_IAMP:
case T_IVBAR:
case T_ILSH:
case T_IRSH:
case T_IFLDIV:
case T_IXOR:
return ASSIGN;
case T_OR:
return LOGICAL_OR;
case T_AND:
return LOGICAL_AND;
case T_NOT:
return LOGICAL_NOT;
case T_EE:
case T_NE:
case T_IS:
case T_IN:
return EQUALS;
case T_GT:
case T_LT:
case T_GTE:
case T_LTE:
return LESSGREATER;
case T_PLUS:
case T_MINUS:
return SUM;
case T_TILDE:
return BITWISE_NOT_PREC;
case T_MUL:
case T_DIV:
case T_PERCENT:
case T_FLDIV:
return PRODUCT;
case T_POW:
return EXP;
case T_LPAREN:
return CALL;
case T_LSQUARE:
return INDEX;
case T_AMPERSAND:
return BITWISE_AND_PREC;
case T_VBAR:
return BITWISE_OR_PREC;
case T_LSHIFT:
case T_RSHIFT:
return BITWISE_SHIFT_PREC;
case T_QMARK:
return TERNARY;
case T_CARET:
return BITWISE_XOR_PREC;
default:
return LOWEST;
}
}
struct Node{
void* node;
enum nodetype type;
Position* start;
Position* end;
};
struct IntLiteral{
string* literal;
};
struct FloatLiteral{
string* literal;
};
struct StringLiteral{
string* literal;
};
struct Identifier{
string* name;
};
struct BinOp{
Node* left;
enum token_type opr;
Node* right;
};
struct UnaryOp{
enum token_type opr;
Node* right;
};
struct Assign{
Node* name;
Node* right;
};
struct Func{
Node* name;
vector<Node*>* code;
vector<Node*>* args;
vector<Node*>* kwargs;
int type;
bool starargs;
bool starkwargs;
Node* stargs;
Node* stkwargs;
Node* rettp;
Node* doc;
};
struct Call{
Node* object;
vector<Node*>* args;
vector<Node*>* kwargs;
vector<int>* stargs;
vector<int>* stkwargs;
};
struct Class{
Node* name;
vector<Node*>* code;
vector<Node*>* bases;
Node* doc;
};
struct Dot{
vector<Node*>* names;
};
struct DotAssign{
Node* dot;
Node* right;
};
struct DotCall{
Node* dot;
vector<Node*>* args;
vector<Node*>* kwargs;
vector<int>* stargs;
vector<int>* stkwargs;
};
struct Return{
Node* node;
};
struct List{
vector<Node*>* list;
};
struct Dict{
vector<Node*>* keys;
vector<Node*>* vals;
};
struct If{
Node* expr;
vector<Node*>* code;
};
struct Else{
Node* base;
vector<Node*>* code;
};
struct Control{
vector<Node*>* bases;
};
struct Subscript{
Node* left;
Node* expr;
};
struct Raise{
Node* expr;
};
struct StoreSubscript{
Node* left;
Node* expr;
};
struct TryExceptFinally{
vector<Node*>* bases;
};
struct Try{
vector<Node*>* code;
};
struct Except{
Node* type;
Node* name;
vector<Node*>* code;
};
struct Finally{
vector<Node*>* code;
};
struct For{
vector<Node*>* code;
Node* ident;
Node* expr;
Node* elsen;
};
struct While{
vector<Node*>* code;
Node* expr;
Node* elsen;
};
struct MultiIdentifier{
vector<string*>* name;
};
struct Import{
vector<Node*>* libnames;
vector<Node*>* names;
};
struct From{
Node* name;
vector<Node*>* names;
};
struct Slice{
Node* left;
Node* right;
Node* step;
};
struct StoreSlice{
Node* left;
Node* expr;
};
struct Del{
Node* expr;
};
struct GlblIdent{
Node* name;
};
struct Assert{
Node* expr;
};
struct Ternary{
Node* left;
Node* expr1;
Node* expr2;
};
struct Decorator{
Node* name;
Node* function;
Node* decorator;
};
struct AnnotatedIdentifier{
string* name;
Node* tp;
};
struct AnnotatedDot{
vector<Node*>* names;
Node* tp;
};
struct Yield{
Node* expr;
};
struct With{
Node* expr;
Node* name;
vector<Node*>* code;
};
struct ListComp{
Node* left;
Node* ident;
Node* expr;
Node* condition;
};
struct DictComp{
Node* left;
Node* right;
Node* ident;
Node* expr;
Node* condition;
};
void destroy_node(struct Node* node){
if (node->type==N_INT){
delete ((IntLiteral*)(node->node))->literal;
}
else if (node->type==N_FLOAT){
delete ((FloatLiteral*)(node->node))->literal;
}
else if (node->type==N_BINOP){
destroy_node(((BinOp*)(node->node))->left);
destroy_node(((BinOp*)(node->node))->right);
fpl_free(&((BinOp*)(node->node))->opr);
}
delete node->start;
delete node->end;
fpl_free(node->node);
fpl_free(node);
}