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/>.
10
/** @file ast.h AST data structures. */
17
#include "../../src/reference_count.h"
19
/** Position in a source file. */
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);
29
const char *ToString() const;
31
std::string filename; ///< File containing the line.
32
int line; ///< Number of the line referred to.
35
/** A Symbol in a 'symboltable'. */
37
const char *name; ///< Name of the symbol.
38
int value; ///< Value of the symbol.
43
/** Counted reference to an expression. */
44
typedef DataReference<Expression> ExpressionRef;
46
/** Base class of expressions. */
47
class Expression : public RefCounter {
49
Expression(const Position &pos);
51
virtual ExpressionRef Evaluate(const Symbol *symbols) const = 0;
53
const Position pos; ///< %Position of the expression.
56
virtual ~Expression();
59
/** A sequence of expressions. */
60
class ExpressionList {
65
std::list<ExpressionRef> exprs; ///< The sequence of expressions.
68
/** Unary operator expression. */
69
class UnaryOperator : public Expression {
71
UnaryOperator(const Position &pos, int oper, ExpressionRef &child);
73
/* virtual */ ExpressionRef Evaluate(const Symbol *symbols) const;
75
int oper; ///< Operation performed, currently only \c '-' (unary negation is supported).
76
ExpressionRef child; ///< Child expression (should be numeric).
79
/* virtual */ ~UnaryOperator();
82
/** String literal elementary expression node. */
83
class StringLiteral : public Expression {
85
StringLiteral(const Position &pos, char *text);
87
/* virtual */ ExpressionRef Evaluate(const Symbol *symbols) const;
89
char *CopyText() const;
91
char *text; ///< Text of the string literal (decoded).
94
/* virtual */ ~StringLiteral();
97
/** Identifier elementary expression node. */
98
class IdentifierLiteral : public Expression {
100
IdentifierLiteral(const Position &pos, char *name);
102
/* virtual */ ExpressionRef Evaluate(const Symbol *symbols) const;
104
char *name; ///< The identifier of the expression.
107
/* virtual */ ~IdentifierLiteral();
110
/** Number literal elementary expression node. */
111
class NumberLiteral : public Expression {
113
NumberLiteral(const Position &pos, long long value);
115
/* virtual */ ExpressionRef Evaluate(const Symbol *symbols) const;
117
long long value; ///< Value of the number literal.
120
/* virtual */ ~NumberLiteral();
123
/** Bit set expression ('or' of '1 << arg'). */
124
class BitSet : public Expression {
126
BitSet(const Position &pos, ExpressionList *args);
128
/* virtual */ ExpressionRef Evaluate(const Symbol *symbols) const;
130
ExpressionList *args; ///< Arguments of the bitset, if any.
133
/* virtual */ ~BitSet();
136
/** Base class for labels of named values. */
142
virtual const Position &GetPosition() const = 0;
143
virtual int GetNameCount() const = 0;
146
/** Label of a named value containing a single name. */
147
class SingleName : public Name {
149
SingleName(const Position &pos, char *name);
150
/* virtual */ ~SingleName();
152
/* virtual */ const Position &GetPosition() const;
153
/* virtual */ int GetNameCount() const;
155
const Position pos; ///< %Position of the label.
156
char *name; ///< The label itself.
159
/** Somewhat generic class for storing an identifier and its position. */
160
class IdentifierLine {
162
IdentifierLine(const Position &pos, char *name);
163
IdentifierLine(const IdentifierLine &il);
164
IdentifierLine &operator=(const IdentifierLine &il);
167
const Position &GetPosition() const;
168
bool IsValid() const;
170
Position pos; ///< %Position of the label.
171
char *name; ///< The label itself.
174
/** A row of identifiers. */
180
const Position &GetPosition() const;
181
int GetNameCount() const;
183
std::list<IdentifierLine *> identifiers; ///< Identifiers in this row.
186
/** a 2D table of identifiers. */
187
class NameTable : public Name {
190
/* virtual */ ~NameTable();
192
/* virtual */ const Position &GetPosition() const;
193
/* virtual */ int GetNameCount() const;
195
std::list<NameRow *> rows; ///< Rows of the table.
198
class NamedValueList;
200
class ExpressionGroup;
202
/** Base class of the value part of a named value. */
208
virtual const Position &GetPosition() const = 0;
209
virtual NodeGroup *CastToNodeGroup();
210
virtual ExpressionGroup *CastToExpressionGroup();
213
/** Value part consisting of a node. */
214
class NodeGroup : public Group {
216
NodeGroup(const Position &pos, char *name, ExpressionList *exprs, NamedValueList *values);
217
/* virtual */ ~NodeGroup();
219
/* virtual */ const Position &GetPosition() const;
220
/* virtual */ NodeGroup *CastToNodeGroup();
222
void HandleImports();
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.
230
/** Value part of a group consisting of an expression. */
231
class ExpressionGroup : public Group {
233
ExpressionGroup(ExpressionRef &expr);
234
/* virtual */ ~ExpressionGroup();
236
/* virtual */ const Position &GetPosition() const;
237
/* virtual */ ExpressionGroup *CastToExpressionGroup();
239
ExpressionRef expr; ///< %Expression to store.
242
/** Base class for named values. */
243
class BaseNamedValue {
246
virtual ~BaseNamedValue();
248
virtual void HandleImports() = 0;
251
/** A value with a name. */
252
class NamedValue : public BaseNamedValue {
254
NamedValue(Name *name, Group *group);
255
/* virtual */ ~NamedValue();
257
/* virtual */ void HandleImports();
259
Name *name; ///< %Name part, may be \c NULL.
260
Group *group; ///< Value part.
263
/** Node to import another file. */
264
class ImportValue : public BaseNamedValue {
266
ImportValue(const Position &pos, char *filename);
267
/* virtual */ ~ImportValue();
269
/* virtual */ void HandleImports();
271
const Position pos; ///< %Position of the import.
272
char *filename; ///< Name of the file to import.
275
/** Sequence of named values. */
276
class NamedValueList {
281
void HandleImports();
283
std::list<BaseNamedValue *> values; ///< Named values in the sequence.
286
NamedValueList *LoadFile(const char *filename, int line = 0);