forked from gfwilliams/tiny-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.h
493 lines (408 loc) · 11.1 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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/*
* File: ast.h
* Author: ghernan
*
* Abstract Syntax Tree classes
*
* Created on November 30, 2016, 7:56 PM
*/
#ifndef AST_H
#define AST_H
#pragma once
#include "RefCountObj.h"
#include "jsLexer.h"
#include "jsVars.h"
#include <vector>
#include <map>
/**
* AST node types enumeration
*/
enum AstNodeTypes
{
AST_SCRIPT
,AST_BLOCK
,AST_VAR
,AST_CONST
,AST_IF
,AST_FOR
,AST_FOR_EACH
,AST_RETURN
,AST_FUNCTION
,AST_ASSIGNMENT
,AST_FNCALL
//,AST_NEWCALL
,AST_LITERAL
,AST_IDENTIFIER
,AST_ARRAY
,AST_OBJECT
,AST_ARRAY_ACCESS
,AST_MEMBER_ACCESS
,AST_CONDITIONAL
,AST_BINARYOP
,AST_PREFIXOP
,AST_POSTFIXOP
,AST_ACTOR
,AST_CONNECT
,AST_INPUT
,AST_OUTPUT
,AST_CLASS
,AST_EXTENDS
,AST_EXPORT
,AST_IMPORT
,AST_TYPES_COUNT
};
class AstNode;
class AstFunction;
class JSArray;
typedef std::vector <Ref<AstNode> > AstNodeList;
//AST conversion functions (mainly for AST /parser debugging)
Ref<JSArray> toJSArray (const AstNodeList& statements);
std::string toJSON (const AstNodeList& statements);
std::string astTypeToString(AstNodeTypes type);
//Constructor functions
Ref<AstNode> astCreateScript(ScriptPosition pos);
Ref<AstNode> astCreateBlock(CScriptToken token);
Ref<AstNode> astCreateIf (ScriptPosition pos,
Ref<AstNode> condition,
Ref<AstNode> thenSt,
Ref<AstNode> elseSt);
Ref<AstNode> astCreateConditional ( ScriptPosition pos,
Ref<AstNode> condition,
Ref<AstNode> thenExpr,
Ref<AstNode> elseExpr);
Ref<AstNode> astCreateFor (ScriptPosition pos,
Ref<AstNode> initSt,
Ref<AstNode> condition,
Ref<AstNode> incrementSt,
Ref<AstNode> body);
Ref<AstNode> astCreateForEach (ScriptPosition pos,
Ref<AstNode> itemDeclaration,
Ref<AstNode> sequenceExpr,
Ref<AstNode> body);
Ref<AstNode> astCreateReturn (ScriptPosition pos, Ref<AstNode> expr);
Ref<AstNode> astCreateAssignment(ScriptPosition pos,
int opCode,
Ref<AstNode> lexpr,
Ref<AstNode> rexpr);
Ref<AstNode> astCreatePrefixOp(CScriptToken token, Ref<AstNode> rexpr);
Ref<AstNode> astCreatePostfixOp(CScriptToken token, Ref<AstNode> lexpr);
Ref<AstNode> astCreateBinaryOp(CScriptToken token,
Ref<AstNode> lexpr,
Ref<AstNode> rexpr);
Ref<AstNode> astCreateFnCall(ScriptPosition pos, Ref<AstNode> fnExpr);
//Ref<AstNode> astToNewCall(Ref<AstNode> callExpr);
Ref<AstNode> astCreateArray(ScriptPosition pos);
Ref<AstNode> astCreateArrayAccess(ScriptPosition pos,
Ref<AstNode> arrayExpr,
Ref<AstNode> indexExpr);
Ref<AstNode> astCreateMemberAccess(ScriptPosition pos,
Ref<AstNode> objExpr,
Ref<AstNode> identifier);
Ref<AstNode> astCreateVar (const ScriptPosition& pos,
const std::string& name,
Ref<AstNode> expr,
bool isConst);
Ref<AstFunction> astCreateInputMessage(ScriptPosition pos, const std::string& name);
Ref<AstFunction> astCreateOutputMessage(ScriptPosition pos, const std::string& name);
Ref<AstNode> astCreateConnect(ScriptPosition pos,
Ref<AstNode> lexpr,
Ref<AstNode> rexpr);
Ref<AstNode> astCreateSend (ScriptPosition pos,
Ref<AstNode> lexpr,
Ref<AstNode> rexpr);
Ref<AstNode> astCreateExtends (ScriptPosition pos,
const std::string& parentName);
Ref<AstNode> astCreateExport (ScriptPosition pos, Ref<AstNode> child);
Ref<AstNode> astCreateImport (ScriptPosition pos, Ref<AstNode> param);
Ref<AstNode> astGetExtends(Ref<AstNode> node);
/**
* Base class for AST nodes
*/
class AstNode : public RefCountObj
{
public:
virtual const AstNodeList& children()const
{
return ms_noChildren;
}
virtual const std::string getName()const
{
return "";
}
virtual ASValue getValue()const
{
return jsNull();
}
virtual void addChild(Ref<AstNode> child)
{
ASSERT(!"addChildren unsupported");
}
virtual void addParam(const std::string& paramName)
{
ASSERT(!"addParam unsupported");
}
typedef std::vector<std::string> Params;
virtual const Params& getParams()const
{
static const Params noParams;
return noParams;
}
bool childExists(size_t index)const
{
const AstNodeList& c = children();
if (index < c.size())
return c[index].notNull();
else
return false;
}
const ScriptPosition& position()const
{
return m_position;
}
virtual ASValue toJS()const;
AstNodeTypes getType()const
{
return m_type;
}
protected:
static const AstNodeList ms_noChildren;
const ScriptPosition m_position;
const AstNodeTypes m_type;
AstNode(AstNodeTypes type, const ScriptPosition& pos) :
m_position(pos), m_type(type)
{
}
virtual ~AstNode()
{
}
};
/**
* Base class for AST nodes which contain children nodes.
*/
class AstBranchNode : public AstNode
{
public:
virtual const AstNodeList& children()const
{
return m_children;
}
virtual void addChild(Ref<AstNode> child)
{
m_children.push_back(child);
}
AstBranchNode(AstNodeTypes type, const ScriptPosition& pos) : AstNode(type, pos)
{
}
protected:
AstNodeList m_children;
};
/**
* Class for branch nodes which are also named
*/
class AstNamedBranch : public AstBranchNode
{
public:
virtual const std::string getName()const
{
return m_name;
}
AstNamedBranch(AstNodeTypes type, const ScriptPosition& pos, const std::string& _name)
: AstBranchNode(type, pos), m_name(_name)
{
}
protected:
const std::string m_name;
};
/**
* AST node for function definitions
*/
class AstFunction : public AstNode
{
public:
static Ref<AstFunction> create(ScriptPosition position,
const std::string& name)
{
return refFromNew (new AstFunction(AST_FUNCTION, position, name));
}
void setCode(Ref<AstNode> code)
{
m_code = code;
}
Ref<AstNode> getCode()const
{
return m_code;
}
virtual const std::string getName()const
{
return m_name;
}
virtual void addParam(const std::string& paramName)
{
m_params.push_back(paramName);
}
virtual const Params& getParams()const
{
return m_params;
}
virtual ASValue toJS()const;
AstFunction(AstNodeTypes type, ScriptPosition position, const std::string& name) :
AstNode(type, position),
m_name(name)
{
}
protected:
const std::string m_name;
Params m_params;
Ref<AstNode> m_code;
};
/**
* AST node for actor definitions
*/
class AstActor : public AstNamedBranch
{
public:
static Ref<AstActor> create(ScriptPosition position,
const std::string& name)
{
return refFromNew (new AstActor(position, name));
}
virtual void addParam(const std::string& paramName)
{
m_params.push_back(paramName);
}
virtual const Params& getParams()const
{
return m_params;
}
virtual ASValue toJS()const;
protected:
AstActor(ScriptPosition position, const std::string& name) :
AstNamedBranch(AST_ACTOR, position, name)
{
}
Params m_params;
};
/**
* Base class for all AST nodes which represent operators.
*/
class AstOperator : public AstBranchNode
{
public:
const int code;
virtual ASValue toJS()const;
AstOperator (AstNodeTypes type, ScriptPosition position, int opCode) :
AstBranchNode (type, position), code (opCode)
{
}
};
/**
* AST node for primitive types literals (Number, String, Boolean)
*/
class AstLiteral : public AstNode
{
public:
static Ref<AstLiteral> create(CScriptToken token);
static Ref<AstLiteral> create(ScriptPosition pos, int value);
static Ref<AstLiteral> createNull(ScriptPosition pos);
virtual ASValue getValue()const
{
return m_value;
}
protected:
AstLiteral (ScriptPosition position, ASValue val) :
AstNode(AST_LITERAL, position),
m_value (val)
{
}
const ASValue m_value;
};
/**
* AST node for identifiers (variable names, function names, members...)
*/
class AstIdentifier : public AstNode
{
public:
static Ref<AstIdentifier> create(CScriptToken token)
{
return refFromNew(new AstIdentifier(token));
}
virtual const std::string getName()const
{
return m_name;
}
protected:
AstIdentifier (CScriptToken token) :
AstNode(AST_IDENTIFIER, token.getPosition()),
m_name (token.text())
{
}
const std::string m_name;
};
/**
* AST node for object literals.
*/
class AstObject : public AstNode
{
public:
/**
* Object property structure
*/
struct Property
{
std::string name;
Ref<AstNode> expr;
bool isConst;
};
typedef std::vector<Property> PropertyList;
static Ref<AstObject> create(ScriptPosition pos)
{
return refFromNew (new AstObject(pos));
}
void addProperty (const std::string name, Ref<AstNode> expr, bool isConst)
{
Property prop;
prop.name = name;
prop.expr = expr;
prop.isConst = isConst;
m_properties.push_back(prop);
}
const PropertyList & getProperties()const
{
return m_properties;
}
virtual ASValue toJS()const;
protected:
AstObject(ScriptPosition pos) : AstNode(AST_OBJECT, pos)
{
}
PropertyList m_properties;
};
/**
* AST node for class definitions
*/
class AstClassNode : public AstNamedBranch
{
public:
static Ref<AstClassNode> create(ScriptPosition position,
const std::string& name)
{
return refFromNew (new AstClassNode(position, name));
}
virtual void addParam(const std::string& paramName)
{
m_params.push_back(paramName);
}
virtual const Params& getParams()const
{
return m_params;
}
Ref<AstNamedBranch> getExtendsNode()const;
virtual ASValue toJS()const;
protected:
AstClassNode(ScriptPosition position, const std::string& name) :
AstNamedBranch(AST_CLASS, position, name)
{
}
Params m_params;
};
#endif /* AST_H */