-
Notifications
You must be signed in to change notification settings - Fork 0
/
type_utils.h
308 lines (281 loc) · 4.14 KB
/
type_utils.h
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
#ifndef _TYPE_UTILS_H
#define _TYPE_UTILS_H
#include <string>
using std::string;
/* enumeration types */
/* the following enums are used in JackTokenizer */
// because some keywords are using the same name
// than #define values, we can't use them as is (C-preprocessor sucks)
enum TYPE_TOKEN {
TOK_KEYWORD,
TOK_SYMBOL,
TOK_IDENTIFIER,
TOK_INT_CONST,
TOK_STRING_CONST,
TOK_EMPTY
};
enum TYPE_KEYWORD {
KW_CLASS,
KW_METHOD,
KW_FUNCTION,
KW_CONSTRUCTOR,
KW_INT,
KW_BOOLEAN,
KW_CHAR,
KW_VOID,
KW_VAR,
KW_STATIC,
KW_FIELD,
KW_LET,
KW_DO,
KW_IF,
KW_ELSE,
KW_WHILE,
KW_RETURN,
KW_TRUE,
KW_FALSE,
KW_NULL,
KW_THIS,
KW_UNKNOWN
};
/* the following enums are used in JackCompiler */
enum KIND {
K_STATIC,
K_FIELD,
K_ARG,
K_VAR,
K_NONE
};
enum SEGMENT {
SEG_CONST,
SEG_ARG,
SEG_LOCAL,
SEG_STATIC,
SEG_THIS,
SEG_THAT,
SEG_POINTER,
SEG_TEMP
};
enum COMMAND {
C_ADD,
C_SUB,
C_NEG,
C_EQ,
C_GT,
C_LT,
C_AND,
C_OR,
C_NOT
};
/* utils functions */
inline string token_to_string(TYPE_TOKEN tok)
{
switch ( tok )
{
case TOK_KEYWORD:
return "keyword";
case TOK_SYMBOL:
return "symbol";
case TOK_IDENTIFIER:
return "identifier";
case TOK_INT_CONST:
return "integer";
case TOK_STRING_CONST:
return "string";
case TOK_EMPTY:
return "empty";
}
}
inline string keyword_to_string(TYPE_KEYWORD tk)
{
switch ( tk )
{
case KW_CLASS:
return "class";
case KW_METHOD:
return "method";
case KW_FUNCTION:
return "function";
case KW_CONSTRUCTOR:
return "constructor";
case KW_INT:
return "int";
case KW_BOOLEAN:
return "boolean";
case KW_CHAR:
return "char";
case KW_VOID:
return "void";
case KW_VAR:
return "var";
case KW_STATIC:
return "static";
case KW_FIELD:
return "field";
case KW_LET:
return "let";
case KW_DO:
return "do";
case KW_IF:
return "if";
case KW_ELSE:
return "else";
case KW_WHILE:
return "while";
case KW_RETURN:
return "return";
case KW_TRUE:
return "true";
case KW_FALSE:
return "false";
case KW_NULL:
return "null";
case KW_THIS:
return "this";
case KW_UNKNOWN:
return "unknown";
}
}
inline string kind_to_string(KIND k)
{
switch ( k )
{
case K_STATIC:
return "static";
case K_FIELD:
return "field";
case K_ARG:
return "arg";
case K_VAR:
return "var";
case K_NONE:
return "none";
}
}
inline SEGMENT kind_to_segment(KIND k)
{
switch ( k )
{
case K_STATIC:
return SEG_STATIC;
case K_FIELD:
return SEG_THIS;
case K_ARG:
return SEG_ARG;
case K_VAR:
return SEG_LOCAL;
}
}
inline KIND keyword_to_kind(TYPE_KEYWORD tk)
{
if (tk == KW_STATIC)
{
return K_STATIC;
}
else if (tk == KW_FIELD)
{
return K_FIELD;
}
else if (tk == KW_VAR)
{
return K_VAR;
}
// this is an ugly hack :(
else
{
return K_ARG;
}
}
inline string segment_to_string(SEGMENT seg)
{
switch ( seg )
{
case SEG_CONST:
return "constant";
case SEG_ARG:
return "argument";
case SEG_LOCAL:
return "local";
case SEG_STATIC:
return "static";
case SEG_THIS:
return "this";
case SEG_THAT:
return "that";
case SEG_POINTER:
return "pointer";
case SEG_TEMP:
return "temp";
}
}
inline string command_to_string(COMMAND cmd)
{
switch ( cmd )
{
case C_ADD:
return "add";
case C_SUB:
return "sub";
case C_NEG:
return "neg";
case C_EQ:
return "eq";
case C_GT:
return "gt";
case C_LT:
return "lt";
case C_AND:
return "and";
case C_OR:
return "or";
case C_NOT:
return "not";
}
}
inline COMMAND char_to_op(char cmd)
{
switch ( cmd )
{
case '+':
return C_ADD;
case '-':
return C_SUB;
case '=':
return C_EQ;
case '>':
return C_GT;
case '<':
return C_LT;
case '&':
return C_AND;
case '|':
return C_OR;
}
}
inline COMMAND char_to_unaryOp(char cmd)
{
switch ( cmd )
{
case '-':
return C_NEG;
case '~':
return C_NOT;
}
}
struct SubroutineInfo {
public:
string type;
string kind;
int nArgs;
SubroutineInfo():type(""), kind(""), nArgs(0) {}
SubroutineInfo(string t, string k, int i):type(t), kind(k), nArgs(i) {}
};
struct SymbolInfo {
public:
string type;
KIND kind;
int index;
SymbolInfo():type(""), kind(K_NONE), index(0) {}
SymbolInfo(string t, KIND k, int i):type(t), kind(k), index(i) {}
};
#endif