~adrian-arroyocalle/freerct/freerct

« back to all changes in this revision

Viewing changes to rcdgen/src/ast.h

  • Committer: charlespigott at googlemail
  • Date: 2013-07-09 17:30:05 UTC
  • Revision ID: svn-v4:d3bf82d2-0c16-e458-5408-27be57bd1276:trunk:780
-Codechange: Reorganise the project files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id$ */
2
 
 
3
 
/*
4
 
 * This file is part of FreeRCT.
5
 
 * FreeRCT is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6
 
 * FreeRCT is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7
 
 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with FreeRCT. If not, see <http://www.gnu.org/licenses/>.
8
 
 */
9
 
 
10
 
/** @file ast.h AST data structures. */
11
 
 
12
 
#ifndef AST_H
13
 
#define AST_H
14
 
 
15
 
#include <list>
16
 
#include <string>
17
 
#include "../../src/reference_count.h"
18
 
 
19
 
/** Position in a source file. */
20
 
class Position {
21
 
public:
22
 
        Position();
23
 
        Position(const char *filename, int line);
24
 
        Position(std::string &filename, int line);
25
 
        Position(const Position &pos);
26
 
        Position &operator=(const Position &pos);
27
 
        ~Position();
28
 
 
29
 
        const char *ToString() const;
30
 
 
31
 
        std::string filename; ///< File containing the line.
32
 
        int line;             ///< Number of the line referred to.
33
 
};
34
 
 
35
 
/** A Symbol in a 'symboltable'. */
36
 
struct Symbol {
37
 
        const char *name; ///< Name of the symbol.
38
 
        int value;        ///< Value of the symbol.
39
 
};
40
 
 
41
 
class Expression;
42
 
 
43
 
/** Counted reference to an expression. */
44
 
typedef DataReference<Expression> ExpressionRef;
45
 
 
46
 
/** Base class of expressions. */
47
 
class Expression : public RefCounter {
48
 
public:
49
 
        Expression(const Position &pos);
50
 
 
51
 
        virtual ExpressionRef Evaluate(const Symbol *symbols) const = 0;
52
 
 
53
 
        const Position pos; ///< %Position of the expression.
54
 
 
55
 
protected:
56
 
        virtual ~Expression();
57
 
};
58
 
 
59
 
/** A sequence of expressions. */
60
 
class ExpressionList {
61
 
public:
62
 
        ExpressionList();
63
 
        ~ExpressionList();
64
 
 
65
 
        std::list<ExpressionRef> exprs; ///< The sequence of expressions.
66
 
};
67
 
 
68
 
/** Unary operator expression. */
69
 
class UnaryOperator : public Expression {
70
 
public:
71
 
        UnaryOperator(const Position &pos, int oper, ExpressionRef &child);
72
 
 
73
 
        /* virtual */ ExpressionRef Evaluate(const Symbol *symbols) const;
74
 
 
75
 
        int oper; ///< Operation performed, currently only \c '-' (unary negation is supported).
76
 
        ExpressionRef child; ///< Child expression (should be numeric).
77
 
 
78
 
protected:
79
 
        /* virtual */ ~UnaryOperator();
80
 
};
81
 
 
82
 
/** String literal elementary expression node. */
83
 
class StringLiteral : public Expression {
84
 
public:
85
 
        StringLiteral(const Position &pos, char *text);
86
 
 
87
 
        /* virtual */ ExpressionRef Evaluate(const Symbol *symbols) const;
88
 
 
89
 
        char *CopyText() const;
90
 
 
91
 
        char *text; ///< Text of the string literal (decoded).
92
 
 
93
 
protected:
94
 
        /* virtual */ ~StringLiteral();
95
 
};
96
 
 
97
 
/** Identifier elementary expression node. */
98
 
class IdentifierLiteral : public Expression {
99
 
public:
100
 
        IdentifierLiteral(const Position &pos, char *name);
101
 
 
102
 
        /* virtual */ ExpressionRef Evaluate(const Symbol *symbols) const;
103
 
 
104
 
        char *name; ///< The identifier of the expression.
105
 
 
106
 
protected:
107
 
        /* virtual */ ~IdentifierLiteral();
108
 
};
109
 
 
110
 
/** Number literal elementary expression node. */
111
 
class NumberLiteral : public Expression {
112
 
public:
113
 
        NumberLiteral(const Position &pos, long long value);
114
 
 
115
 
        /* virtual */ ExpressionRef Evaluate(const Symbol *symbols) const;
116
 
 
117
 
        long long value; ///< Value of the number literal.
118
 
 
119
 
protected:
120
 
        /* virtual */ ~NumberLiteral();
121
 
};
122
 
 
123
 
/** Bit set expression ('or' of '1 << arg'). */
124
 
class BitSet : public Expression {
125
 
public:
126
 
        BitSet(const Position &pos, ExpressionList *args);
127
 
 
128
 
        /* virtual */ ExpressionRef Evaluate(const Symbol *symbols) const;
129
 
 
130
 
        ExpressionList *args; ///< Arguments of the bitset, if any.
131
 
 
132
 
protected:
133
 
        /* virtual */ ~BitSet();
134
 
};
135
 
 
136
 
/** Base class for labels of named values. */
137
 
class Name {
138
 
public:
139
 
        Name();
140
 
        virtual ~Name();
141
 
 
142
 
        virtual const Position &GetPosition() const = 0;
143
 
        virtual int GetNameCount() const = 0;
144
 
};
145
 
 
146
 
/** Label of a named value containing a single name. */
147
 
class SingleName : public Name {
148
 
public:
149
 
        SingleName(const Position &pos, char *name);
150
 
        /* virtual */ ~SingleName();
151
 
 
152
 
        /* virtual */ const Position &GetPosition() const;
153
 
        /* virtual */ int GetNameCount() const;
154
 
 
155
 
        const Position pos; ///< %Position of the label.
156
 
        char *name;         ///< The label itself.
157
 
};
158
 
 
159
 
/** Somewhat generic class for storing an identifier and its position. */
160
 
class IdentifierLine {
161
 
public:
162
 
        IdentifierLine(const Position &pos, char *name);
163
 
        IdentifierLine(const IdentifierLine &il);
164
 
        IdentifierLine &operator=(const IdentifierLine &il);
165
 
        ~IdentifierLine();
166
 
 
167
 
        const Position &GetPosition() const;
168
 
        bool IsValid() const;
169
 
 
170
 
        Position pos; ///< %Position of the label.
171
 
        char *name;   ///< The label itself.
172
 
};
173
 
 
174
 
/** A row of identifiers. */
175
 
class NameRow {
176
 
public:
177
 
        NameRow();
178
 
        ~NameRow();
179
 
 
180
 
        const Position &GetPosition() const;
181
 
        int GetNameCount() const;
182
 
 
183
 
        std::list<IdentifierLine *> identifiers; ///< Identifiers in this row.
184
 
};
185
 
 
186
 
/** a 2D table of identifiers. */
187
 
class NameTable : public Name {
188
 
public:
189
 
        NameTable();
190
 
        /* virtual */ ~NameTable();
191
 
 
192
 
        /* virtual */ const Position &GetPosition() const;
193
 
        /* virtual */ int GetNameCount() const;
194
 
 
195
 
        std::list<NameRow *> rows; ///< Rows of the table.
196
 
};
197
 
 
198
 
class NamedValueList;
199
 
class NodeGroup;
200
 
class ExpressionGroup;
201
 
 
202
 
/** Base class of the value part of a named value. */
203
 
class Group {
204
 
public:
205
 
        Group();
206
 
        virtual ~Group();
207
 
 
208
 
        virtual const Position &GetPosition() const = 0;
209
 
        virtual NodeGroup *CastToNodeGroup();
210
 
        virtual ExpressionGroup *CastToExpressionGroup();
211
 
};
212
 
 
213
 
/** Value part consisting of a node. */
214
 
class NodeGroup : public Group {
215
 
public:
216
 
        NodeGroup(const Position &pos, char *name, ExpressionList *exprs, NamedValueList *values);
217
 
        /* virtual */ ~NodeGroup();
218
 
 
219
 
        /* virtual */ const Position &GetPosition() const;
220
 
        /* virtual */ NodeGroup *CastToNodeGroup();
221
 
 
222
 
        void HandleImports();
223
 
 
224
 
        const Position pos;     ///< %Position of the node name.
225
 
        char *name;             ///< Node name itself.
226
 
        ExpressionList *exprs;  ///< Parameters of the node.
227
 
        NamedValueList *values; ///< Named values of the node.
228
 
};
229
 
 
230
 
/** Value part of a group consisting of an expression. */
231
 
class ExpressionGroup : public Group {
232
 
public:
233
 
        ExpressionGroup(ExpressionRef &expr);
234
 
        /* virtual */ ~ExpressionGroup();
235
 
 
236
 
        /* virtual */ const Position &GetPosition() const;
237
 
        /* virtual */ ExpressionGroup *CastToExpressionGroup();
238
 
 
239
 
        ExpressionRef expr; ///< %Expression to store.
240
 
};
241
 
 
242
 
/** Base class for named values. */
243
 
class BaseNamedValue {
244
 
public:
245
 
        BaseNamedValue();
246
 
        virtual ~BaseNamedValue();
247
 
 
248
 
        virtual void HandleImports() = 0;
249
 
};
250
 
 
251
 
/** A value with a name. */
252
 
class NamedValue : public BaseNamedValue {
253
 
public:
254
 
        NamedValue(Name *name, Group *group);
255
 
        /* virtual */ ~NamedValue();
256
 
 
257
 
        /* virtual */ void HandleImports();
258
 
 
259
 
        Name *name;   ///< %Name part, may be \c NULL.
260
 
        Group *group; ///< Value part.
261
 
};
262
 
 
263
 
/** Node to import another file. */
264
 
class ImportValue : public BaseNamedValue {
265
 
public:
266
 
        ImportValue(const Position &pos, char *filename);
267
 
        /* virtual */ ~ImportValue();
268
 
 
269
 
        /* virtual */ void HandleImports();
270
 
 
271
 
        const Position pos; ///< %Position of the import.
272
 
        char *filename;     ///< Name of the file to import.
273
 
};
274
 
 
275
 
/** Sequence of named values. */
276
 
class NamedValueList {
277
 
public:
278
 
        NamedValueList();
279
 
        ~NamedValueList();
280
 
 
281
 
        void HandleImports();
282
 
 
283
 
        std::list<BaseNamedValue *> values; ///< Named values in the sequence.
284
 
};
285
 
 
286
 
NamedValueList *LoadFile(const char *filename, int line = 0);
287
 
 
288
 
#endif
289