forked from gfwilliams/tiny-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsemanticCheck.cpp
383 lines (325 loc) · 10.1 KB
/
semanticCheck.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
/*
* File: semanticCheck.cpp
* Author: ghernan
*
* Semantic check code for AsyncScript
*
* Created on December 17, 2016, 10:54 PM
*/
#include "ascript_pch.hpp"
#include "OS_support.h"
#include "semanticCheck.h"
#include "ScriptException.h"
#include <string>
#include <set>
#include <vector>
using namespace std;
/**
* State of the semantic analysis process
*/
struct SemCheckState
{
StringSet definedClasses;
vector<Ref<AstNode> > nodeStack;
bool isClassDefined (const std::string& name)const
{
return definedClasses.find(name) != definedClasses.end();
}
};
//Forward declarations
typedef void (*SemcheckFN)(Ref<AstNode> node, SemCheckState* pState);
void semCheck (Ref<AstNode> node, SemCheckState* pState);
void childrenSemCheck (Ref<AstNode> node, SemCheckState* pState);
void varSemCheck (Ref<AstNode> node, SemCheckState* pState);
void functionSemCheck (Ref<AstNode> node, SemCheckState* pState);
void assignmentSemCheck (Ref<AstNode> node, SemCheckState* pState);
void postfixOpSemCheck (Ref<AstNode> node, SemCheckState* pState);
void prefixOpSemCheck (Ref<AstNode> node, SemCheckState* pState);
void objectSemCheck (Ref<AstNode> node, SemCheckState* pState);
void classSemCheck (Ref<AstNode> node, SemCheckState* pState);
void exportSemCheck (Ref<AstNode> node, SemCheckState* pState);
void importSemCheck (Ref<AstNode> node, SemCheckState* pState);
void checkReservedNames (const std::string& name, ScriptPosition pos, const char* errorMsg);
void checkReservedNames (Ref<AstNode> node, const char* errorMsg);
Ref<AstNode> getPrevSibling(Ref<AstNode> node, SemCheckState* pState);
Ref<AstNode> getParent(SemCheckState* pState);
AstNodeTypes getParentType(SemCheckState* pState);
/**
* Check:
* - Var declaration
* - Function name
* - Assignment
* - Function parameter names
* - PostFix and prefix increment operators
*/
/**
* Semantic analysis pass entry point. It throws a 'CScriptException' on the first
* error it finds.
* @param script
*/
void semanticCheck(Ref<AstNode> script)
{
ASSERT (script->getType() == AST_SCRIPT);
SemCheckState state;
state.nodeStack.push_back(script);
childrenSemCheck(script, &state);
}
/**
* Performs semantic analysis on an AST node.
* @param node
* @param pState
*/
void semCheck (Ref<AstNode> node, SemCheckState* pState)
{
static SemcheckFN types[AST_TYPES_COUNT] = {NULL, NULL};
if (types [0] == NULL)
{
types [AST_SCRIPT] = childrenSemCheck;
types [AST_BLOCK] = childrenSemCheck;
types [AST_VAR] = varSemCheck;
types [AST_CONST] = varSemCheck;
types [AST_IF] = childrenSemCheck;
types [AST_FOR] = childrenSemCheck;
types [AST_FOR_EACH] = childrenSemCheck;
types [AST_RETURN] = childrenSemCheck;
types [AST_FUNCTION] = functionSemCheck;
types [AST_ASSIGNMENT] = assignmentSemCheck;
types [AST_FNCALL] = childrenSemCheck;
// types [AST_NEWCALL] = childrenSemCheck;
types [AST_LITERAL] = childrenSemCheck;
types [AST_IDENTIFIER] = childrenSemCheck;
types [AST_ARRAY] = childrenSemCheck;
types [AST_OBJECT] = objectSemCheck;
types [AST_ARRAY_ACCESS] = childrenSemCheck;
types [AST_MEMBER_ACCESS] = childrenSemCheck;
types [AST_CONDITIONAL] = childrenSemCheck;
types [AST_BINARYOP] = childrenSemCheck;
types [AST_PREFIXOP] = prefixOpSemCheck;
types [AST_POSTFIXOP] = postfixOpSemCheck;
types [AST_ACTOR] = childrenSemCheck;
types [AST_CONNECT] = childrenSemCheck;
types [AST_INPUT] = childrenSemCheck;
types [AST_OUTPUT] = childrenSemCheck;
types [AST_CLASS] = classSemCheck;
types [AST_EXTENDS] = childrenSemCheck;
types [AST_EXPORT] = exportSemCheck;
types [AST_IMPORT] = importSemCheck;
}
pState->nodeStack.push_back(node);
types[node->getType()](node, pState);
ASSERT(!pState->nodeStack.empty());
pState->nodeStack.pop_back();
}
/**
* Performs semantic analysis on every children of the current AST node.
* @param node
* @param pState
* @return number of children nodes processed
*/
void childrenSemCheck (Ref<AstNode> node, SemCheckState* pState)
{
const AstNodeList& children = node->children();
for (size_t i=0; i < children.size(); ++i)
{
if (children[i].notNull())
semCheck (children[i], pState);
}
}
/**
* Semantic analysis for 'var' declarations
* @param node
* @param pState
*/
void varSemCheck (Ref<AstNode> node, SemCheckState* pState)
{
checkReservedNames (node, "Invalid variable name");
childrenSemCheck(node, pState);
}
/**
* Semantic analysis for function definition nodes.
* @param node
* @param pState
*/
void functionSemCheck (Ref<AstNode> node, SemCheckState* pState)
{
Ref<AstFunction> fnNode = node.staticCast<AstFunction>();
checkReservedNames (node, "Invalid function name");
auto params = fnNode->getParams();
for (size_t i = 0; i < params.size(); ++i)
checkReservedNames (params[i], node->position(), "Invalid parameter name: %s");
SemCheckState fnState (*pState);
semCheck (fnNode->getCode(), &fnState);
}
/**
* Semantic analysis for assignment nodes
* @param node
* @param pState
*/
void assignmentSemCheck (Ref<AstNode> node, SemCheckState* pState)
{
checkReservedNames (node->children().front(), "Cannot write to: %s");
childrenSemCheck(node, pState);
}
/**
* Semantic analysis for assignment nodes
* @param node
* @param pState
*/
void postfixOpSemCheck (Ref<AstNode> node, SemCheckState* pState)
{
checkReservedNames (node->children().front(), "Cannot write to: %s");
childrenSemCheck(node, pState);
}
/**
* Semantic analysis for assignment nodes
* @param node
* @param pState
*/
void prefixOpSemCheck (Ref<AstNode> node, SemCheckState* pState)
{
const auto opNode = node.staticCast<AstOperator>();
const auto opCode = opNode->code;
if (opCode == LEX_PLUSPLUS || opCode == LEX_MINUSMINUS )
checkReservedNames (node->children().front(), "Cannot write to: %s");
childrenSemCheck(node, pState);
}
void objectSemCheck(Ref<AstNode> node, SemCheckState* pState)
{
Ref<AstObject> objNode = node.staticCast<AstObject>();
auto props = objNode->getProperties();
set<string> usedNames;
for (size_t i = 0; i < props.size(); ++i)
{
if (usedNames.count(props[i].name) == 0)
usedNames.insert(props[i].name);
else
{
errorAt (props[i].expr->position(),
"Duplicated key in object: %s",
props[i].name.c_str());
}
}
}
/**
* Semantic check for class definitions.
* @param node
* @param pState
*/
void classSemCheck (Ref<AstNode> node, SemCheckState* pState)
{
auto classNode = node.staticCast<AstClassNode>();
auto name = classNode->getName();
checkReservedNames (node, "Invalid class name");
if (pState->isClassDefined(name))
errorAt(node->position(), "'%s' class already defined. Use another name", name.c_str());
auto extends = classNode->getExtendsNode();
if (extends.notNull())
{
auto parentName = extends->getName();
if (!pState->isClassDefined(parentName))
errorAt(extends->position(), "Base class '%s' not defined.", parentName.c_str());
}
SemCheckState classState (*pState);
childrenSemCheck(node, &classState);
}
/**
* Semantic check for 'export' modifiers.
* @param node
* @param pState
*/
void exportSemCheck (Ref<AstNode> node, SemCheckState* pState)
{
if (getParentType(pState) != AST_SCRIPT)
errorAt(node->position(), "Exported symbols must be globals");
}
/**
* Semantic check for 'import' statements.
* @param node
* @param pState
*/
void importSemCheck (Ref<AstNode> node, SemCheckState* pState)
{
if (getParentType(pState) != AST_SCRIPT)
errorAt(node->position(), "Import statements must be top-level elements");
auto prev = getPrevSibling(node, pState);
if (prev.notNull() && prev->getType() != AST_IMPORT)
errorAt(node->position(), "Import statements must come before any other statements");
}
/**
* Checks that the name is not among the reserved names
* @param name
* @param pos
* @param errorMsg
*/
void checkReservedNames (const std::string& name, ScriptPosition pos, const char* errorMsg)
{
static set <string> reserved;
if (reserved.empty())
{
//TODO: probably 'this' would need more checks.
reserved.insert("this");
reserved.insert("arguments");
reserved.insert("eval");
}
if (reserved.count(name) > 0)
errorAt(pos, errorMsg, name.c_str());
}
/**
* Checks that node name is not among reserved names.
* @param node
* @param errorMsg
*/
void checkReservedNames (Ref<AstNode> node, const char* errorMsg)
{
checkReservedNames(node->getName(), node->position(), errorMsg);
}
/**
* Gets the previous sibling, if any.
*
* @param pState
* @return If there is no previous sibling, it will return null.
*/
Ref<AstNode> getPrevSibling(Ref<AstNode> node, SemCheckState* pState)
{
auto parent = getParent(pState);
if (parent.notNull())
{
const auto &children = parent->children();
for (size_t i = 0; i < children.size(); ++i)
{
if (node == children[i])
{
if (i > 0)
return children[i-1];
else
break;
}
}
}
return Ref<AstNode>();
}
/**
* Gets the parent node of the current node.
* @param pState
* @return
*/
Ref<AstNode> getParent(SemCheckState* pState)
{
if (pState->nodeStack.size() > 1)
return *(pState->nodeStack.rbegin()+1);
else
return Ref<AstNode>();
}
/**
* Gets the type of the parent node.
* @param pState
* @return If the current node has no parent, it returns AST_TYPES_COUNT.
*/
AstNodeTypes getParentType(SemCheckState* pState)
{
auto parent = getParent(pState);
if (parent.notNull())
return parent->getType();
else
return AST_TYPES_COUNT;
}