forked from gfwilliams/tiny-js
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTinyJS_SyntaxTree.h
298 lines (240 loc) · 7.99 KB
/
TinyJS_SyntaxTree.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
#include "TinyJS.h"
#include <string.h>
#include <vector>
#include <cstdlib>
#include <assert.h>
#pragma once
class CSyntaxNode
{
public:
CSyntaxNode();
virtual ~CSyntaxNode();
virtual void emit(std::ostream& out, const std::string indentation = "") = 0;
// returns true if this node is the kind that should have a semicolon after it.
// since returns and declarations are statement-types but require semicolons,
// and the latter of which can appear in for statements, it cannot emit a semicolon
// itself. plus this is a little more "OO".
virtual bool semicolonizable() = 0;
protected:
CSyntaxNode* node;
};
// these two classes serve no purpose except to divide the two
// sides of the syntax tree.
class CSyntaxStatement : public CSyntaxNode
{
public:
virtual bool semicolonizable() { return false; }
};
class CSyntaxExpression : public CSyntaxNode
{
public:
virtual bool semicolonizable() { return true; }
virtual std::string lvaluePath() { assert(0); return std::string(); }
};
class CSyntaxSequence : public CSyntaxStatement
{
public:
CSyntaxSequence(CSyntaxNode* front, CSyntaxNode* last);
~CSyntaxSequence();
/// converts the sequence (and any subsequences) into a serial vector of statements.
/// Using this function will also "disown" all the statements in this sequence if
/// disown_children is true, ie the leaves of the tree represented by this sequence
/// will no longer be deleted when this sequence is deleted.
std::vector<CSyntaxNode*> normalize(bool disown_children = true);
CSyntaxNode* first() { return node; }
CSyntaxNode* second() { return last; }
virtual void emit(std::ostream& out, const std::string indentation = "");
private:
CSyntaxNode* last;
bool disowned;
};
class CSyntaxIf : public CSyntaxStatement
{
public:
CSyntaxIf(CSyntaxExpression* expr, CSyntaxNode* body, CSyntaxNode* else_);
CSyntaxIf(CSyntaxExpression* expr, CSyntaxNode* body);
~CSyntaxIf();
virtual void emit(std::ostream& out, const std::string indentation = "");
private:
CSyntaxExpression* expr;
CSyntaxNode* else_;
};
class CSyntaxWhile : public CSyntaxStatement
{
public:
CSyntaxWhile(CSyntaxExpression* expr, CSyntaxNode* body);
~CSyntaxWhile();
virtual void emit(std::ostream& out, const std::string indentation = "");
private:
CSyntaxExpression* expr;
};
class CSyntaxFor : public CSyntaxStatement
{
public:
CSyntaxFor(CSyntaxNode* init, CSyntaxExpression* cond, CSyntaxExpression* update, CSyntaxNode* body);
~CSyntaxFor();
virtual void emit(std::ostream& out, const std::string indentation = "");
private:
CSyntaxNode* init;
CSyntaxExpression* cond;
CSyntaxExpression* update;
};
class CSyntaxFactor : public CSyntaxExpression
{
public:
enum FACTOR_TYPES
{
F_TYPE_INT = 1,
F_TYPE_DOUBLE = 2,
F_TYPE_STRING = 4,
F_TYPE_IDENTIFIER = 8
};
CSyntaxFactor(std::string val);
bool isValueType() { return factorType & (F_TYPE_INT | F_TYPE_DOUBLE | F_TYPE_STRING); }
std::string getRawValue() { return value; }
double getDouble() { if(factorType != F_TYPE_DOUBLE) return getInt(); return std::strtod(value.c_str(), 0); }
int getInt() { if(factorType != F_TYPE_INT) return 0; return std::strtol(value.c_str(), 0, 0); }
virtual void emit(std::ostream& out, const std::string indentation = "");
protected:
std::string value;
int factorType;
};
class CSyntaxID : public CSyntaxFactor
{
public:
CSyntaxID(std::string id);
virtual void emit(std::ostream& out, const std::string indentation = "");
std::string getName() { return value; }
std::string lvaluePath() { return getName(); }
};
class CSyntaxFunction : public CSyntaxExpression
{
public:
CSyntaxFunction(CSyntaxID* name, std::vector<CSyntaxID*>& arguments, CSyntaxStatement* body);
~CSyntaxFunction();
virtual void emit(std::ostream& out, const std::string indentation = "");
CSyntaxID* getName();
private:
CSyntaxID* name;
std::vector<CSyntaxID*> arguments;
void generateRandomId();
};
class CSyntaxFunctionCall : public CSyntaxExpression
{
public:
CSyntaxFunctionCall(CSyntaxExpression* name, std::vector<CSyntaxExpression*> arguments, std::string originalArguments);
~CSyntaxFunctionCall();
virtual void emit(std::ostream& out, const std::string indentation = "");
protected:
std::vector<CSyntaxExpression*> actuals;
std::string origString;
};
class CSyntaxReturn : public CSyntaxStatement
{
public:
CSyntaxReturn(CSyntaxExpression* value);
virtual void emit(std::ostream& out, const std::string indentation = "");
virtual bool semicolonizable() { return true; }
};
class CSyntaxAssign : public CSyntaxExpression
{
public:
// this has to be an expression on the lhs to deal with things like
// var some.path.to.thing = something;
// or things like
// a["myfavoriteindex"] = something;
// the thing on the lhs is an l-value, but it is an expression that evaluates
// to one.
CSyntaxAssign(int op, CSyntaxExpression* lvalue, CSyntaxExpression* rvalue);
~CSyntaxAssign();
virtual void emit(std::ostream& out, const std::string indentation = "");
private:
int op;
CSyntaxExpression* lval;
};
class CSyntaxDefinition : public CSyntaxStatement
{
public:
CSyntaxDefinition(CSyntaxExpression* lvalue, CSyntaxExpression* rvalue);
~CSyntaxDefinition();
virtual void emit(std::ostream& out, const std::string indentation = "");
virtual bool semicolonizable() { return true; }
private:
CSyntaxExpression* lval;
};
class CSyntaxTernaryOperator : public CSyntaxExpression
{
public:
CSyntaxTernaryOperator(int op, CSyntaxExpression* cond, CSyntaxExpression* b1, CSyntaxExpression* b2);
~CSyntaxTernaryOperator();
virtual void emit(std::ostream& out, const std::string indentation = "");
private:
int op;
CSyntaxExpression* b1;
CSyntaxExpression* b2;
};
class CSyntaxBinaryOperator : public CSyntaxExpression
{
public:
CSyntaxBinaryOperator(int op, CSyntaxExpression* left, CSyntaxExpression* right);
~CSyntaxBinaryOperator();
bool canBeLval() { return op == '.' || op == '['; }
virtual void emit(std::ostream& out, const std::string indentation = "");
virtual std::string lvaluePath();
private:
int op;
CSyntaxExpression* right;
std::string randomArrayName = "";
void generateRandomID();
};
class CSyntaxCondition : public CSyntaxBinaryOperator
{
public:
CSyntaxCondition(int op, CSyntaxExpression* left, CSyntaxExpression* right);
virtual void emit(std::ostream& out, const std::string indentation = "");
};
class CSyntaxRelation : public CSyntaxBinaryOperator
{
public:
CSyntaxRelation(int rel, CSyntaxExpression* left, CSyntaxExpression* right);
virtual void emit(std::ostream& out, const std::string indentation = "");
};
class CSyntaxUnaryOperator : public CSyntaxExpression
{
public:
CSyntaxUnaryOperator(int op, CSyntaxExpression* expr);
virtual void emit(std::ostream& out, const std::string indentation = "");
private:
int op;
};
class CScriptSyntaxTree
{
public:
CScriptSyntaxTree(CScriptLex* lexer);
CScriptSyntaxTree(const std::string& buffer);
~CScriptSyntaxTree();
void parse();
void compile(std::ostream& out);
protected:
CScriptLex* lexer;
bool lexerOwned;
CSyntaxNode* root;
// taken from TinyJS.h
// parsing - in order of precedence
std::vector<CSyntaxExpression*> functionCall();
CSyntaxExpression* factor();
CSyntaxExpression* unary();
CSyntaxExpression* term();
CSyntaxExpression* expression();
CSyntaxExpression* shift();
CSyntaxExpression* condition();
CSyntaxExpression* logic();
CSyntaxExpression* ternary();
CSyntaxExpression* base();
// can return null for blocks like "{ }" or possibly "{ ;* }"
CSyntaxStatement* block();
CSyntaxNode* statement();
// parsing utility functions
CSyntaxFunction* parseFunctionDefinition();
std::vector<CSyntaxID*> parseFunctionArguments();
};