forked from google/jsonnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.h
408 lines (352 loc) · 9.73 KB
/
ast.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
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
/*
Copyright 2015 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef AST_H
#define AST_H
#include <cstdlib>
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include "lexer.h"
enum ASTType {
AST_APPLY,
AST_ARRAY,
AST_BINARY,
AST_BUILTIN_FUNCTION,
AST_CONDITIONAL,
AST_ERROR,
AST_FUNCTION,
AST_IMPORT,
AST_IMPORTSTR,
AST_INDEX,
AST_LOCAL,
AST_LITERAL_BOOLEAN,
AST_LITERAL_NULL,
AST_LITERAL_NUMBER,
AST_LITERAL_STRING,
AST_OBJECT,
AST_OBJECT_COMPOSITION,
AST_SELF,
AST_SUPER,
AST_UNARY,
AST_VAR
};
/** Represents a variable / parameter / field name. */
struct Identifier {
std::string name;
Identifier(const std::string &name)
: name(name)
{ }
};
static inline std::ostream &operator<<(std::ostream &o, const Identifier *id)
{
o << id->name;
return o;
}
/** All AST nodes are subtypes of this class.
*/
struct AST {
LocationRange location;
ASTType type;
std::vector<const Identifier *> freeVariables;
AST(const LocationRange &location, ASTType type)
: location(location), type(type)
{
}
virtual ~AST(void)
{
}
};
/** Represents function calls. */
struct Apply : public AST {
AST *target;
std::vector<AST*> arguments;
Apply(const LocationRange &lr, AST *target, const std::vector<AST*> &arguments)
: AST(lr, AST_APPLY), target(target), arguments(arguments)
{ }
};
/** Represents array constructors [1, 2, 3]. */
struct Array : public AST {
std::vector<AST*> elements;
Array(const LocationRange &lr, const std::vector<AST*> &elements)
: AST(lr, AST_ARRAY), elements(elements)
{ }
};
enum BinaryOp {
BOP_MULT,
BOP_DIV,
BOP_PLUS,
BOP_MINUS,
BOP_SHIFT_L,
BOP_SHIFT_R,
BOP_GREATER,
BOP_GREATER_EQ,
BOP_LESS,
BOP_LESS_EQ,
BOP_MANIFEST_EQUAL,
BOP_MANIFEST_UNEQUAL,
BOP_BITWISE_AND,
BOP_BITWISE_XOR,
BOP_BITWISE_OR,
BOP_AND,
BOP_OR
};
static inline std::string bop_string (BinaryOp bop)
{
switch (bop) {
case BOP_MULT: return "*";
case BOP_DIV: return "/";
case BOP_PLUS: return "+";
case BOP_MINUS: return "-";
case BOP_SHIFT_L: return "<<";
case BOP_SHIFT_R: return ">>";
case BOP_GREATER: return ">";
case BOP_GREATER_EQ: return ">=";
case BOP_LESS: return "<";
case BOP_LESS_EQ: return "<=";
case BOP_MANIFEST_EQUAL: return "==";
case BOP_MANIFEST_UNEQUAL: return "!=";
case BOP_BITWISE_AND: return "&";
case BOP_BITWISE_XOR: return "^";
case BOP_BITWISE_OR: return "|";
case BOP_AND: return "&&";
case BOP_OR: return "||";
default:
std::cerr << "INTERNAL ERROR: Unrecognised binary operator: " << bop << std::endl;
std::abort();
}
}
/** Represents binary operators. */
struct Binary : public AST {
AST *left;
BinaryOp op;
AST *right;
Binary(const LocationRange &lr, AST *left, BinaryOp op, AST *right)
: AST(lr, AST_BINARY), left(left), op(op), right(right)
{ }
};
/** Represents built-in functions.
*
* There is no parse rule to build this AST. Instead, it is used to build the std object in the
* interpreter.
*/
struct BuiltinFunction : public AST {
unsigned long id;
std::vector<const Identifier*> params;
BuiltinFunction(const LocationRange &lr, unsigned long id,
const std::vector<const Identifier*> ¶ms)
: AST(lr, AST_BUILTIN_FUNCTION), id(id), params(params)
{ }
};
/** Represents if then else. */
struct Conditional : public AST {
AST *cond;
AST *branchTrue;
AST *branchFalse;
Conditional(const LocationRange &lr, AST *cond, AST *branchTrue, AST *branchFalse)
: AST(lr, AST_CONDITIONAL), cond(cond), branchTrue(branchTrue), branchFalse(branchFalse)
{ }
};
/** Represents error e. */
struct Error : public AST {
AST *expr;
Error(const LocationRange &lr, AST *expr)
: AST(lr, AST_ERROR), expr(expr)
{ }
};
/** Represents function calls. */
struct Function : public AST {
std::vector<const Identifier*> parameters;
AST *body;
Function(const LocationRange &lr, const std::vector<const Identifier*> ¶meters, AST *body)
: AST(lr, AST_FUNCTION), parameters(parameters), body(body)
{ }
};
/** Represents import "file". */
struct Import : public AST {
std::string file;
Import(const LocationRange &lr, const std::string &file)
: AST(lr, AST_IMPORT), file(file)
{ }
};
/** Represents importstr "file". */
struct Importstr : public AST {
std::string file;
Importstr(const LocationRange &lr, const std::string &file)
: AST(lr, AST_IMPORTSTR), file(file)
{ }
};
/** Represents both e[e] and the syntax sugar e.f. */
struct Index : public AST {
AST *target;
AST *index;
Index(const LocationRange &lr, AST *target, AST *index)
: AST(lr, AST_INDEX), target(target), index(index)
{ }
};
/** Represents local x = e; e. */
struct Local : public AST {
typedef std::map<const Identifier*, AST*> Binds;
Binds binds;
AST *body;
Local(const LocationRange &lr, const Binds &binds, AST *body)
: AST(lr, AST_LOCAL), binds(binds), body(body)
{ }
};
/** Represents true and false. */
struct LiteralBoolean : public AST {
bool value;
LiteralBoolean(const LocationRange &lr, bool value)
: AST(lr, AST_LITERAL_BOOLEAN), value(value)
{ }
};
/** Represents the null keyword. */
struct LiteralNull : public AST {
LiteralNull(const LocationRange &lr)
: AST(lr, AST_LITERAL_NULL)
{ }
};
/** Represents JSON numbers. */
struct LiteralNumber : public AST {
double value;
LiteralNumber(const LocationRange &lr, double value)
: AST(lr, AST_LITERAL_NUMBER), value(value)
{ }
};
/** Represents JSON strings. */
struct LiteralString : public AST {
std::string value;
LiteralString(const LocationRange &lr, const std::string &value)
: AST(lr, AST_LITERAL_STRING), value(value)
{ }
};
/** Represents object constructors { f: e ... }. */
struct Object : public AST {
struct Field {
enum Hide {
INHERIT, // f: v
HIDDEN, // f:: v
VISIBLE // f::: v
};
AST *name;
enum Hide hide;
AST *body;
Field(AST *name, enum Hide hide, AST *body)
: name(name), hide(hide), body(body)
{ }
};
typedef std::list<Field> Fields;
Fields fields;
Object(const LocationRange &lr, const Fields &fields)
: AST(lr, AST_OBJECT), fields(fields)
{ }
};
/** Represents object composition { [e]: e for x in e }. */
struct ObjectComposition : public AST {
AST *field;
AST *value;
const Identifier *id;
AST *array;
ObjectComposition(const LocationRange &lr, AST *field, AST *value,
const Identifier *id, AST *array)
: AST(lr, AST_OBJECT_COMPOSITION), field(field), value(value), id(id), array(array)
{ }
};
/** Represents the self keyword. */
struct Self : public AST {
Self(const LocationRange &lr)
: AST(lr, AST_SELF)
{ }
};
/** Represents the super keyword. */
struct Super : public AST {
Super(const LocationRange &lr)
: AST(lr, AST_SUPER)
{ }
};
enum UnaryOp {
UOP_NOT,
UOP_BITWISE_NOT,
UOP_PLUS,
UOP_MINUS
};
static inline std::string uop_string (UnaryOp uop)
{
switch (uop) {
case UOP_PLUS: return "+";
case UOP_MINUS: return "-";
case UOP_BITWISE_NOT: return "~";
case UOP_NOT: return "!";
default:
std::cerr << "INTERNAL ERROR: Unrecognised unary operator: " << uop << std::endl;
std::abort();
}
}
/** Represents unary operators. */
struct Unary : public AST {
UnaryOp op;
AST *expr;
Unary(const LocationRange &lr, UnaryOp op, AST *expr)
: AST(lr, AST_UNARY), op(op), expr(expr)
{ }
};
/** Represents variables. */
struct Var : public AST {
const Identifier *id;
const Identifier *original;
Var(const LocationRange &lr, const Identifier *id)
: AST(lr, AST_VAR), id(id), original(id)
{ }
Var(const LocationRange &lr, const Identifier *id, const Identifier *original)
: AST(lr, AST_VAR), id(id), original(original)
{ }
};
/** Allocates ASTs on demand, frees them in its destructor.
*/
class Allocator {
std::map<std::string, const Identifier*> internedIdentifiers;
std::vector<AST*> allocated;
public:
template <class T, class... Args> T* make(Args... args)
{
auto r = new T(args...);
allocated.push_back(r);
return r;
}
/** Returns interned identifiers.
*
* The location used in the Identifier AST is that of the first one parsed.
*/
const Identifier *makeIdentifier(const std::string &name)
{
auto it = internedIdentifiers.find(name);
if (it != internedIdentifiers.end()) {
return it->second;
}
auto r = new Identifier(name);
internedIdentifiers[name] = r;
return r;
}
~Allocator()
{
for (auto x : allocated) {
delete x;
}
allocated.clear();
for (auto x : internedIdentifiers) {
delete x.second;
}
internedIdentifiers.clear();
}
};
#endif