-
Notifications
You must be signed in to change notification settings - Fork 665
/
ASTNode.cpp
207 lines (190 loc) · 4.64 KB
/
ASTNode.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
#include "ASTNode.h"
#include "bytecode.h"
/* ASTNodeList */
void ASTNodeList::removeLast()
{
list_t::iterator it = m_nodes.end();
--it;
m_nodes.erase(it);
}
void ASTNodeList::removeFirst()
{
m_nodes.erase(m_nodes.begin());
}
/* ASTUnary */
const char* ASTUnary::op_str() const
{
static const char* s_op_strings[] = {
"+", "-", "~", "not "
};
return s_op_strings[op()];
}
/* ASTBinary */
const char* ASTBinary::op_str() const
{
static const char* s_op_strings[] = {
".", " ** ", " * ", " / ", " // ", " % ", " + ", " - ",
" << ", " >> ", " & ", " ^ ", " | ", " and ", " or ", " @ ",
" += ", " -= ", " *= ", " /= ", " %= ", " **= ", " <<= ",
" >>= ", " &= ", " ^= ", " |= ", " //= ", " @= ", " <INVALID> "
};
return s_op_strings[op()];
}
ASTBinary::BinOp ASTBinary::from_opcode(int opcode)
{
switch (opcode) {
case Pyc::BINARY_ADD:
return BIN_ADD;
case Pyc::BINARY_AND:
return BIN_AND;
case Pyc::BINARY_DIVIDE:
return BIN_DIVIDE;
case Pyc::BINARY_FLOOR_DIVIDE:
return BIN_FLOOR_DIVIDE;
case Pyc::BINARY_LSHIFT:
return BIN_LSHIFT;
case Pyc::BINARY_MODULO:
return BIN_MODULO;
case Pyc::BINARY_MULTIPLY:
return BIN_MULTIPLY;
case Pyc::BINARY_OR:
return BIN_OR;
case Pyc::BINARY_POWER:
return BIN_POWER;
case Pyc::BINARY_RSHIFT:
return BIN_RSHIFT;
case Pyc::BINARY_SUBTRACT:
return BIN_SUBTRACT;
case Pyc::BINARY_TRUE_DIVIDE:
return BIN_DIVIDE;
case Pyc::BINARY_XOR:
return BIN_XOR;
case Pyc::BINARY_MATRIX_MULTIPLY:
return BIN_MAT_MULTIPLY;
case Pyc::INPLACE_ADD:
return BIN_IP_ADD;
case Pyc::INPLACE_AND:
return BIN_IP_AND;
case Pyc::INPLACE_DIVIDE:
return BIN_IP_DIVIDE;
case Pyc::INPLACE_FLOOR_DIVIDE:
return BIN_IP_FLOOR_DIVIDE;
case Pyc::INPLACE_LSHIFT:
return BIN_IP_LSHIFT;
case Pyc::INPLACE_MODULO:
return BIN_IP_MODULO;
case Pyc::INPLACE_MULTIPLY:
return BIN_IP_MULTIPLY;
case Pyc::INPLACE_OR:
return BIN_IP_OR;
case Pyc::INPLACE_POWER:
return BIN_IP_POWER;
case Pyc::INPLACE_RSHIFT:
return BIN_IP_RSHIFT;
case Pyc::INPLACE_SUBTRACT:
return BIN_IP_SUBTRACT;
case Pyc::INPLACE_TRUE_DIVIDE:
return BIN_IP_DIVIDE;
case Pyc::INPLACE_XOR:
return BIN_IP_XOR;
case Pyc::INPLACE_MATRIX_MULTIPLY:
return BIN_IP_MAT_MULTIPLY;
default:
return BIN_INVALID;
}
}
ASTBinary::BinOp ASTBinary::from_binary_op(int operand)
{
switch (operand) {
case 0:
return BIN_ADD;
case 1:
return BIN_AND;
case 2:
return BIN_FLOOR_DIVIDE;
case 3:
return BIN_LSHIFT;
case 4:
return BIN_MAT_MULTIPLY;
case 5:
return BIN_MULTIPLY;
case 6:
return BIN_MODULO;
case 7:
return BIN_OR;
case 8:
return BIN_POWER;
case 9:
return BIN_RSHIFT;
case 10:
return BIN_SUBTRACT;
case 11:
return BIN_DIVIDE;
case 12:
return BIN_XOR;
case 13:
return BIN_IP_ADD;
case 14:
return BIN_IP_AND;
case 15:
return BIN_IP_FLOOR_DIVIDE;
case 16:
return BIN_IP_LSHIFT;
case 17:
return BIN_MAT_MULTIPLY;
case 18:
return BIN_IP_MULTIPLY;
case 19:
return BIN_IP_MODULO;
case 20:
return BIN_IP_OR;
case 21:
return BIN_IP_POWER;
case 22:
return BIN_IP_RSHIFT;
case 23:
return BIN_IP_SUBTRACT;
case 24:
return BIN_IP_DIVIDE;
case 25:
return BIN_IP_XOR;
default:
return BIN_INVALID; // Return BIN_INVALID for out-of-range operand
}
}
/* ASTCompare */
const char* ASTCompare::op_str() const
{
static const char* s_cmp_strings[] = {
" < ", " <= ", " == ", " != ", " > ", " >= ", " in ", " not in ", " is ", " is not ",
"<EXCEPTION MATCH>", "<BAD>"
};
return s_cmp_strings[op()];
}
/* ASTKeyword */
const char* ASTKeyword::word_str() const
{
static const char* s_word_strings[] = {
"pass", "break", "continue"
};
return s_word_strings[key()];
}
/* ASTBlock */
void ASTBlock::removeLast()
{
list_t::iterator it = m_nodes.end();
--it;
m_nodes.erase(it);
}
void ASTBlock::removeFirst()
{
m_nodes.erase(m_nodes.begin());
}
const char* ASTBlock::type_str() const
{
static const char* s_type_strings[] = {
"", "if", "else", "elif", "try", "CONTAINER", "except",
"finally", "while", "for", "with", "async for"
};
return s_type_strings[blktype()];
}