forked from gfwilliams/tiny-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.cpp
510 lines (418 loc) · 13.2 KB
/
ast.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
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/*
* File: ast.cpp
* Author: ghernan
*
* Abstract Syntax Tree classes
*
* Created on November 30, 2016, 7:56 PM
*/
#include "ascript_pch.hpp"
#include "ast.h"
#include "jsArray.h"
using namespace std;
//Empty children list constant
const AstNodeList AstNode::ms_noChildren;
// Constructor functions.
//
////////////////////////////////
Ref<AstNode> astCreateScript(ScriptPosition pos)
{
return refFromNew( new AstBranchNode(AST_SCRIPT, pos));
}
Ref<AstNode> astCreateBlock(CScriptToken token)
{
return refFromNew( new AstBranchNode(AST_BLOCK, token.getPosition()));
}
Ref<AstNode> astCreateIf (ScriptPosition pos,
Ref<AstNode> condition,
Ref<AstNode> thenSt,
Ref<AstNode> elseSt)
{
auto result = refFromNew( new AstBranchNode(AST_IF, pos));
result->addChild(condition);
result->addChild(thenSt);
result->addChild(elseSt);
return result;
}
Ref<AstNode> astCreateConditional ( ScriptPosition pos,
Ref<AstNode> condition,
Ref<AstNode> thenExpr,
Ref<AstNode> elseExpr)
{
auto result = refFromNew( new AstBranchNode(AST_CONDITIONAL, pos));
result->addChild(condition);
result->addChild(thenExpr);
result->addChild(elseExpr);
return result;
}
Ref<AstNode> astCreateFor (ScriptPosition pos,
Ref<AstNode> initSt,
Ref<AstNode> condition,
Ref<AstNode> incrementSt,
Ref<AstNode> body)
{
auto result = refFromNew( new AstBranchNode(AST_FOR, pos));
result->addChild(initSt);
result->addChild(condition);
result->addChild(incrementSt);
result->addChild(body);
return result;
}
Ref<AstNode> astCreateForEach (ScriptPosition pos,
Ref<AstNode> itemDeclaration,
Ref<AstNode> sequenceExpr,
Ref<AstNode> body)
{
auto result = refFromNew( new AstBranchNode(AST_FOR_EACH, pos));
result->addChild(itemDeclaration);
result->addChild(sequenceExpr);
result->addChild(body);
return result;
}
Ref<AstNode> astCreateReturn (ScriptPosition pos, Ref<AstNode> expr)
{
auto result = refFromNew( new AstBranchNode(AST_RETURN, pos));
result->addChild(expr);
return result;
}
Ref<AstNode> astCreateAssignment(ScriptPosition pos,
int opCode,
Ref<AstNode> lexpr,
Ref<AstNode> rexpr)
{
auto result = refFromNew( new AstOperator(AST_ASSIGNMENT, pos, opCode));
result->addChild(lexpr);
result->addChild(rexpr);
return result;
}
Ref<AstNode> astCreatePrefixOp(CScriptToken token, Ref<AstNode> rexpr)
{
auto result = refFromNew( new AstOperator(AST_PREFIXOP,
token.getPosition(),
token.type()));
result->addChild(rexpr);
return result;
}
Ref<AstNode> astCreatePostfixOp(CScriptToken token, Ref<AstNode> lexpr)
{
auto result = refFromNew( new AstOperator(AST_POSTFIXOP,
token.getPosition(),
token.type()));
result->addChild(lexpr);
return result;
}
Ref<AstNode> astCreateBinaryOp(CScriptToken token,
Ref<AstNode> lexpr,
Ref<AstNode> rexpr)
{
auto result = refFromNew( new AstOperator(AST_BINARYOP,
token.getPosition(),
token.type()));
result->addChild(lexpr);
result->addChild(rexpr);
return result;
}
Ref<AstNode> astCreateFnCall(ScriptPosition pos, Ref<AstNode> fnExpr)
{
auto result = refFromNew( new AstBranchNode(AST_FNCALL, pos));
result->addChild(fnExpr);
return result;
}
/**
* Creates an array literal AST node.
* @param pos
* @return
*/
Ref<AstNode> astCreateArray(ScriptPosition pos)
{
return refFromNew( new AstBranchNode(AST_ARRAY, pos));
}
Ref<AstNode> astCreateArrayAccess(ScriptPosition pos,
Ref<AstNode> arrayExpr,
Ref<AstNode> indexExpr)
{
auto result = refFromNew( new AstBranchNode(AST_ARRAY_ACCESS, pos));
result->addChild(arrayExpr);
result->addChild(indexExpr);
return result;
}
Ref<AstNode> astCreateMemberAccess(ScriptPosition pos,
Ref<AstNode> objExpr,
Ref<AstNode> identifier)
{
auto result = refFromNew( new AstBranchNode(AST_MEMBER_ACCESS, pos));
result->addChild(objExpr);
result->addChild(identifier);
return result;
}
Ref<AstNode> astCreateVar (const ScriptPosition& pos,
const std::string& name,
Ref<AstNode> expr,
bool isConst)
{
auto result = refFromNew (new AstNamedBranch(isConst ? AST_CONST : AST_VAR, pos, name));
result->addChild(expr);
return result;
}
Ref<AstNode> astCreateActor(ScriptPosition pos, const std::string& name)
{
return refFromNew(new AstNamedBranch(AST_ACTOR, pos, name));
}
Ref<AstFunction> astCreateInputMessage(ScriptPosition pos, const std::string& name)
{
return refFromNew(new AstFunction(AST_INPUT, pos, name));
}
Ref<AstFunction> astCreateOutputMessage(ScriptPosition pos, const std::string& name)
{
return refFromNew(new AstFunction(AST_OUTPUT, pos, name));
}
Ref<AstNode> astCreateConnect(ScriptPosition pos,
Ref<AstNode> lexpr,
Ref<AstNode> rexpr)
{
auto result = refFromNew (new AstOperator(AST_CONNECT, pos, LEX_CONNECT));
result->addChild(lexpr);
result->addChild(rexpr);
return result;
}
Ref<AstNode> astCreateSend(ScriptPosition pos,
Ref<AstNode> lexpr,
Ref<AstNode> rexpr)
{
auto result = refFromNew (new AstOperator(AST_BINARYOP, pos, LEX_SEND));
result->addChild(lexpr);
result->addChild(rexpr);
return result;
}
Ref<AstNode> astCreateExtends (ScriptPosition pos,
const std::string& parentName)
{
return refFromNew (new AstNamedBranch(AST_EXTENDS, pos, parentName));
}
Ref<AstNode> astCreateExport (ScriptPosition pos, Ref<AstNode> child)
{
auto result = refFromNew(new AstBranchNode(AST_EXPORT, pos));
result->addChild(child);
return result;
}
Ref<AstNode> astCreateImport (ScriptPosition pos, Ref<AstNode> param)
{
auto result = refFromNew(new AstBranchNode(AST_IMPORT, pos));
result->addChild(param);
return result;
}
/**
* Gets the 'extends' node of a class node.
* @param node
* @return
*/
Ref<AstNode> astGetExtends(Ref<AstNode> node)
{
ASSERT (node->getType() == AST_CLASS);
if (!node->childExists(0))
return Ref<AstNode>();
auto child = node->children().front();
if (child->getType() != AST_EXTENDS)
return Ref<AstNode>();
else
return child;
}
/**
* Creates an 'AstLiteral' object from a source token.
* @param token
* @return
*/
Ref<AstLiteral> AstLiteral::create(CScriptToken token)
{
ASValue value;
switch (token.type())
{
case LEX_R_TRUE: value = jsTrue(); break;
case LEX_R_FALSE: value = jsFalse(); break;
case LEX_R_NULL: value = jsNull(); break;
case LEX_STR:
case LEX_INT:
case LEX_FLOAT:
value = createConstant(token);
break;
default:
ASSERT(!"Invalid token for a literal");
}
return refFromNew(new AstLiteral(token.getPosition(), value));
}
/**
* Creates a literal from an integer
* @param pos
* @param value
* @return
*/
Ref<AstLiteral> AstLiteral::create(ScriptPosition pos, int value)
{
return refFromNew(new AstLiteral(pos, jsInt(value)));
}
/**
* Creates a 'null' literal
* @param pos
* @return
*/
Ref<AstLiteral> AstLiteral::createNull(ScriptPosition pos)
{
return refFromNew(new AstLiteral(pos, jsNull()));
}
/**
* Gets the 'extends' node of a class. The 'extends' node contains inheritance
* information.
* @return The node or a NULL pointer if not present.
*/
Ref<AstNamedBranch> AstClassNode::getExtendsNode()const
{
if (this->childExists(0))
{
auto child = this->children().front();
if (child->getType() == AST_EXTENDS)
return child.staticCast<AstNamedBranch>();
}
return Ref<AstNamedBranch>();
}
/**
* Transforms an AST statement into a Javascript object.
* This particular version creates an object containing all its children
* @return
*/
ASValue AstNode::toJS()const
{
Ref<JSObject> obj = JSObject::create();
obj->writeField("a_type", jsString(astTypeToString(getType())), false);
const string name = getName();
if (!name.empty())
obj->writeField("b_name", jsString(name), false);
const AstNodeList& c = children();
if (!c.empty())
obj->writeField("z_children", toJSArray(c)->value(), false);
const auto value = getValue();
if (!value.isNull())
obj->writeField("v_value", value, false);
return obj->value();
}
/**
* Function declaration to JSValue
* @return
*/
ASValue AstFunction::toJS()const
{
Ref<JSObject> obj = AstNode::toJS().staticCast<JSObject>();
obj->writeField("c_parameters", JSArray::createStrArray(m_params)->value(), false);
if (m_code.notNull())
obj->writeField("d_code", m_code->toJS(), false);
return obj->value();
}
/**
* Class node to javascript object
* @return
*/
ASValue AstClassNode::toJS()const
{
Ref<JSObject> obj = AstNamedBranch::toJS().staticCast<JSObject>();
obj->writeField("c_parameters", JSArray::createStrArray(m_params)->value(), false);
return obj->value();
}
/**
* Actor node to javascript object
* @return
*/
ASValue AstActor::toJS()const
{
Ref<JSObject> obj = AstNamedBranch::toJS().staticCast<JSObject>();
obj->writeField("c_parameters", JSArray::createStrArray(m_params)->value(), false);
return obj->value();
}
/**
* Operator to JSValue
* @return
*/
ASValue AstOperator::toJS()const
{
Ref<JSObject> obj = AstBranchNode::toJS().staticCast<JSObject>();
obj->writeField("d_operator", jsString(getTokenStr(code)), false);
return obj->value();
}
/**
* Object literal to JSValue
* @return
*/
ASValue AstObject::toJS()const
{
Ref<JSObject> obj = JSObject::create();
Ref<JSObject> props = JSObject::create();
obj->writeField("a_type", jsString(astTypeToString(getType())), false);
obj->writeField("b_properties", props->value(), false);
PropertyList::const_iterator it;
for (it = m_properties.begin(); it != m_properties.end(); ++it)
props->writeField(it->name, it->expr->toJS(), false);
return obj->value();
}
/**
* Transforms a list of AstNodes into a Javascript Array.
* @param statements
* @return
*/
Ref<JSArray> toJSArray (const AstNodeList& statements)
{
Ref<JSArray> result = JSArray::create();
for (size_t i = 0; i < statements.size(); ++i)
{
if (statements[i].notNull())
result->push( statements[i]->toJS() );
else
result->push(jsNull());
}
return result;
}
/**
* Gets the string representation of an AST type
* @param type
* @return
*/
std::string astTypeToString(AstNodeTypes type)
{
typedef map<AstNodeTypes, string> TypesMap;
static TypesMap types;
if (types.empty())
{
types[AST_SCRIPT] = "AST_SCRIPT";
types[AST_BLOCK] = "AST_BLOCK";
types[AST_VAR] = "AST_VAR";
types[AST_CONST] = "AST_CONST";
types[AST_IF] = "AST_IF";
types[AST_FOR] = "AST_FOR";
types[AST_FOR_EACH] = "AST_FOR_EACH";
types[AST_RETURN] = "AST_RETURN";
types[AST_FUNCTION] = "AST_FUNCTION";
types[AST_ASSIGNMENT] = "AST_ASSIGNMENT";
types[AST_FNCALL] = "AST_FNCALL";
types[AST_LITERAL] = "AST_LITERAL";
types[AST_IDENTIFIER] = "AST_IDENTIFIER";
types[AST_ARRAY] = "AST_ARRAY";
types[AST_OBJECT] = "AST_OBJECT";
types[AST_ARRAY_ACCESS] = "AST_ARRAY_ACCESS";
types[AST_MEMBER_ACCESS] = "AST_MEMBER_ACCESS";
types[AST_CONDITIONAL] = "AST_CONDITIONAL";
types[AST_BINARYOP] = "AST_BINARYOP";
types[AST_PREFIXOP] = "AST_PREFIXOP";
types[AST_POSTFIXOP] = "AST_POSTFIXOP";
types[AST_ACTOR] = "AST_ACTOR";
types[AST_CONNECT] = "AST_CONNECT";
types[AST_INPUT] = "AST_INPUT";
types[AST_OUTPUT] = "AST_OUTPUT";
types[AST_CLASS] = "AST_CLASS";
types[AST_EXTENDS] = "AST_EXTENDS";
types[AST_EXPORT] = "AST_EXPORT";
types[AST_IMPORT] = "AST_IMPORT";
//types[AST_TYPES_COUNT] = "AST_TYPES_COUNT";
}
TypesMap::const_iterator it = types.find(type);
if (it != types.end())
return it->second;
else
return "BAD_AST_TYPE";
}