-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAST.h
More file actions
401 lines (320 loc) · 10.1 KB
/
AST.h
File metadata and controls
401 lines (320 loc) · 10.1 KB
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
#pragma once
#include <vector>
#include <string>
#include <string.h>
#include "glm/vec2.hpp"
#include "glm/vec3.hpp"
#include "glm/vec4.hpp"
#include "Location.h"
#include "Common.h"
namespace LSystem
{
namespace AST
{
class Name;
class QualifiedName;
class NameList;
class Namespace;
class NamespaceList;
class ImportList;
class Rule;
class RuleList;
class RuleDefs;
class Predecessor;
class Successor;
class Expression;
class ModuleDefList;
class ModuleDef;
class ModuleExp;
class Grammar;
enum RuleKind
{
Replace,
Decompose,
Transform
};
typedef std::vector<Expression*> Expressions;
class ASTNode
{
public:
ASTNode(const Location &aLocation);
virtual ~ASTNode(void);
const Location& GetLocation() const { return mLocation; }
protected:
Location mLocation;
};
class NamespaceList : public ASTNode
{
public:
typedef std::vector<Namespace*> Namespaces;
NamespaceList(const Location &aLocation, Namespace* aNamespace);
~NamespaceList();
void AddNamespace(Namespace* aNamespace);
const Namespaces& GetNamespaces() const { return mNamespaces; }
private:
Namespaces mNamespaces;
};
class Namespace : public ASTNode
{
public:
Namespace(const Location &aLocation, Name* aName, NamespaceList* aNamespaces, RuleDefs* aRuleDefs);
const Name* GetName() const { return mName; }
const RuleDefs* GetRuleDefs() const { return mRuleDefs; }
const NamespaceList* GetNamespaces() const { return mNamespaces; }
void AddNamespace(Namespace* aNamespace);
private:
Name* mName;
NamespaceList* mNamespaces;
RuleDefs* mRuleDefs;
};
class Grammar : public ASTNode
{
public:
Grammar(const Location &aLocation, ImportList* aImports, NamespaceList* aNamespaceList, Successor* aAxiom);
virtual ~Grammar();
const Successor* GetAxiom() const { return mAxiom; }
const NamespaceList* GetNamespaceList() const { return mNamespaceList; }
const ImportList* GetImports() const { return mImports; }
private:
ImportList* mImports;
Successor* mAxiom;
NamespaceList* mNamespaceList;
};
class Name : public ASTNode
{
public:
Name(const Location &aLocation, const char* aValue);
const std::string& GetValue() const { return mValue; }
private:
std::string mValue;
};
class QualifiedName : public ASTNode
{
public:
typedef std::vector<std::string> Values;
QualifiedName(const Location &aLocation, const char* aValue);
void AddName(const char* aValue);
const Values& GetValues() const { return mValues; }
const std::string& GetFullValue() const { return mFullValue; }
const std::string& GetNamePart() const { return mNamePart; }
bool IsAbsolute() const { return mValues.size() > 1; }
private:
Values mValues;
std::string mFullValue;
std::string mNamePart;
};
class ArgumentSpec : public ASTNode
{
public:
ArgumentSpec(Name* aName, ArgumentType::Type aType);
virtual ~ArgumentSpec();
inline const Name* GetName() const { return mName; }
inline ArgumentType::Type GetType() const { return mType; }
private:
Name* mName;
ArgumentType::Type mType;
};
class ImportList : public ASTNode
{
public:
typedef std::vector<QualifiedName*> Imports;
ImportList(const Location& aLocation, QualifiedName* aImport);
~ImportList();
void AddImport(QualifiedName* aQualifiedName);
inline const Imports& GetImports() const { return mImports; }
private:
Imports mImports;
};
class Rule : public ASTNode
{
public:
Rule(RuleKind aKind, Name* aLabel, float aProbability, Predecessor* aPredecessor, Expression* aCondition, Successor* aSuccessor);
virtual ~Rule();
RuleKind GetKind() const { return mKind; }
const Name* GetLabel() const { return mLabel; }
const Predecessor* GetPredecessor() const { return mPredecessor; }
const Expression* GetCondition() const { return mCondition; }
const Successor* GetSuccessor() const { return mSuccessor; }
float GetProbability() const { return mProbability; }
private:
RuleKind mKind;
Name* mLabel;
Predecessor* mPredecessor;
Expression* mCondition;
Successor* mSuccessor;
float mProbability;
};
class RuleDefs : public ASTNode
{
public:
RuleDefs(const Location &aLocation, NameList* aVariables, NameList* aConstants, RuleList* aRules);
const NameList* GetVariables() const { return mVariables; }
const NameList* GetConstants() const { return mConstants; }
const RuleList* GetRules() const { return mRules; }
private:
NameList* mVariables;
NameList* mConstants;
RuleList* mRules;
};
class RuleList : public ASTNode
{
public:
typedef std::vector<Rule*> Rules;
RuleList(const Location &aLocation);
virtual ~RuleList(void);
void AddRule(Rule* aRule);
const Rules& GetRules() const { return mRules; }
private:
Rules mRules;
};
class RuleSections : public ASTNode
{
public:
RuleSections(const Location& aLocation);
RuleSections(RuleList* aPlainRules, RuleList* aDecompositions, RuleList* aHomomorphisms);
virtual ~RuleSections();
void SetPlainRules(RuleList* aRules);
void SetHomomorphisms(RuleList* aRules);
void SetDecompositions(RuleList* aRules);
inline const RuleList* GetPlainRules() const { return mPlainRules; }
inline const RuleList* GetHomomorphisms() const { return mHomomorphisms; }
inline const RuleList* GetDecompositions() const { return mDecompositions; }
private:
RuleList* mPlainRules;
RuleList* mDecompositions;
RuleList* mHomomorphisms;
};
class NameList : public ASTNode
{
public:
typedef std::vector<const Name *> Names;
NameList(const Location &aLocation);
virtual ~NameList(void);
void AddName(Name* aName);
void AddNameList(NameList* aNames);
const Names& GetNames() const { return mNames; }
private:
Names mNames;
};
class ArgumentSpecList : public ASTNode
{
public:
typedef std::vector<ArgumentSpec *> ArgumentSpecs;
ArgumentSpecList();
virtual ~ArgumentSpecList(void);
void AddArgumentSpec(ArgumentSpec* aArgumentSpec);
void AddArgumentSpecList(ArgumentSpecList* aArgumentSpecs);
const ArgumentSpecs& GetArgumentSpecs() const { return mArgumentSpecs; }
private:
ArgumentSpecs mArgumentSpecs;
};
class Predecessor : public ASTNode
{
public:
Predecessor(ModuleDefList* aLeftContext, ModuleDef* aVariable, ModuleDefList* aRightContext);
virtual ~Predecessor(void);
const ModuleDefList* GetLeftContext() const { return mLeftContext; }
const ModuleDef* GetVariable() const { return mVariable; }
const ModuleDefList* GetRightContext() const { return mRightContext; }
private:
ModuleDefList* mLeftContext;
ModuleDef* mVariable;
ModuleDefList* mRightContext;
};
class Successor : public ASTNode
{
public:
typedef std::vector<ModuleExp *> Successors;
Successor(const Location &aLocation);
virtual ~Successor();
void AddModuleExp(ModuleExp* aModule);
void MergeSuccessor(Successor* aSuccessor);
const Successors& GetSuccessors() const { return mSuccessors; }
private:
Successors mSuccessors;
};
class ModuleDefList : public ASTNode
{
public:
typedef std::vector<ModuleDef *> ModuleDefs;
ModuleDefList(const Location &aLocation);
virtual ~ModuleDefList();
void AddModuleDef(ModuleDef* aModuleDef);
void MergeModuleDefList(ModuleDefList* aModuleDefList);
const ModuleDefs& GetModuleDefs() const { return mModuleDefs; }
private:
ModuleDefs mModuleDefs;
};
class ModuleExp : public ASTNode
{
public:
ModuleExp(QualifiedName* aName, Expression* aParameters);
virtual ~ModuleExp();
inline const QualifiedName* GetName() const { return mName; }
inline const Expression* GetParameters() const { return mParameters; }
private:
QualifiedName* mName;
Expression* mParameters;
};
// Module definition class
// Holds name token and parameter list definition.
class ModuleDef : public ASTNode
{
public:
ModuleDef(QualifiedName* aName, ArgumentSpecList* aArguments);
virtual ~ModuleDef();
inline const QualifiedName* GetName() const { return mName; }
inline const ArgumentSpecList* GetArguments() const { return mArguments; }
private:
QualifiedName* mName;
ArgumentSpecList* mArguments;
};
class Expression : public ASTNode
{
public:
typedef enum
{
FuncCall, ExpressionList, UnaryPlus, UnaryMinus,
Negate, Not, Multiply, Divide,
Remainder, Add, Subtract, ShiftLeft,
ShiftRight, LessThan, GreaterThan, LessOrEqual,
GreaterOrEqual, Equals, NotEquals, BitwiseAnd,
BitwiseOr, And, Or, XOr,
Conditional, Integer, Float, Boolean,
Vec2, Vec3, Vec4, QualifiedName,
IntType, FloatType, BoolType, Vec2Type,
Vec3Type, Vec4Type, TypeCast, MemberSelect,
MaxOpCode,
} Type;
Expression(Type aOpCode, const Location &aLocation);
Expression(const Location &aLocation, int aValue );
Expression(const Location &aLocation, bool aValue );
Expression(const Location &aLocation, float aValue );
Expression(const Location &aLocation, glm::vec2 aValue );
Expression(const Location &aLocation, glm::vec3 aValue );
Expression(const Location &aLocation, glm::vec4 aValue );
Expression(const Location &aLocation, const char* aName );
virtual ~Expression();
const Expressions& GetOperands() const { return mOperands; }
Type GetOpCode() const { return mOpCode; }
inline int GetIntegerValue() const { return mIntegerValue; }
inline float GetFloatValue() const { return mFloatValue; }
inline bool GetBoolValue() const { return mBoolValue; }
inline glm::vec2 GetVec2Value() const { return mVec2Value; }
inline glm::vec3 GetVec3Value() const { return mVec3Value; }
inline glm::vec4 GetVec4Value() const { return mVec4Value; }
inline const std::string GetName() const { return mName; }
int AddOperand(Expression* aOperand);
private:
Type mOpCode;
Expressions mOperands;
std::string mName;
int mIntegerValue;
float mFloatValue;
bool mBoolValue;
glm::vec2 mVec2Value;
glm::vec3 mVec3Value;
glm::vec4 mVec4Value;
};
}
}