/
expression.h
144 lines (115 loc) · 3.99 KB
/
expression.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
/*
* The Doomsday Engine Project -- libdeng2
*
* Copyright (c) 2004-2012 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* This program 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; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LIBDENG2_EXPRESSION_H
#define LIBDENG2_EXPRESSION_H
#include "../ISerializable"
#include <QFlags>
namespace de
{
class Evaluator;
class Value;
class Record;
/**
* Base class for expressions.
*
* @note All expression classes must call the serialization methods of this class
* so that the expression flags are properly serialized.
*
* @ingroup script
*/
class Expression : public ISerializable
{
public:
/// Deserialization of an expression failed. @ingroup errors
DENG2_ERROR(DeserializationError);
// Flags for evaluating expressions.
// Note: these are serialized as is, so don't change the existing values.
enum Flag
{
/// Evaluates to a value. In conjunction with IMPORT, causes the imported
/// record to be copied to the local namespace.
ByValue = 0x1,
/// Evaluates to a reference.
ByReference = 0x2,
/// If missing, create a new variable.
NewVariable = 0x4,
/// If missing, create a new subrecord (i.e., Variable that owns a Record).
NewSubrecord = 0x8,
/// Identifier must exist and will be deleted.
Delete = 0x10,
/// Imports an external namespace into the local namespace (as a reference).
Import = 0x20,
/// Look for object in local namespace only.
LocalOnly = 0x40,
/// If the identifier is in scope, returns a reference to the process's
/// throwaway variable.
ThrowawayIfInScope = 0x80,
/// Identifier must not already exist in scope.
NotInScope = 0x100,
/// Variable will be set to read-only mode.
ReadOnly = 0x200,
/// Variable will be raised into a higher namespace.
Export = 0x400
};
Q_DECLARE_FLAGS(Flags, Flag)
public:
virtual ~Expression();
virtual void push(Evaluator &evaluator, Record *names = 0) const;
virtual Value *evaluate(Evaluator &evaluator) const = 0;
/**
* Returns the flags of the expression.
*/
Flags const &flags () const;
/**
* Sets the flags of the expression.
*/
void setFlags(Flags f);
/**
* Subclasses must call this in their serialization method.
*/
void operator >> (Writer &to) const;
/**
* Subclasses must call this in their deserialization method.
*/
void operator << (Reader &from);
public:
/**
* Constructs an expression by deserializing one from a reader.
*
* @param reader Reader.
*
* @return The deserialized expression. Caller gets ownership.
*/
static Expression *constructFrom(Reader &reader);
protected:
typedef dbyte SerialId;
enum SerialIds {
ARRAY,
BUILT_IN,
CONSTANT,
DICTIONARY,
NAME,
OPERATOR
};
private:
Flags _flags;
};
Q_DECLARE_OPERATORS_FOR_FLAGS(Expression::Flags)
}
#endif /* LIBDENG2_EXPRESSION_H */