-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
nameexpression.cpp
311 lines (273 loc) · 10 KB
/
nameexpression.cpp
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
/*
* The Doomsday Engine Project -- libcore
*
* Copyright © 2004-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* LGPL: http://www.gnu.org/licenses/lgpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 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 Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#include "de/NameExpression"
#include "de/Evaluator"
#include "de/Process"
#include "de/TextValue"
#include "de/RefValue"
#include "de/ArrayValue"
#include "de/RecordValue"
#include "de/ScopeStatement"
#include "de/Writer"
#include "de/Reader"
#include "de/App"
#include "de/Module"
namespace de {
String const NameExpression::LOCAL_SCOPE = "-";
DENG2_PIMPL_NOREF(NameExpression)
{
String identifier;
String scopeIdentifier;
Instance(String const &id = "",
String const &scopeId = "")
: identifier(id)
, scopeIdentifier(scopeId)
{}
Variable *findInRecord(String const & name,
Record const & where,
Record *& foundIn,
bool lookInClass = true) const
{
if(where.hasMember(name))
{
// The name exists in this namespace. Even though the lookup was done as
// const, the caller expects non-const return values.
foundIn = const_cast<Record *>(&where);
return const_cast<Variable *>(&where[name]);
}
if(lookInClass && where.hasMember(Record::VAR_SUPER))
{
// The namespace is derived from another record. Let's look into each
// super-record in turn.
ArrayValue const &supers = where.geta(Record::VAR_SUPER);
for(int i = 0; i < int(supers.size()); ++i)
{
if(Variable *found = findInRecord(
name, supers.at(i).as<RecordValue>().dereference(), foundIn))
{
return found;
}
}
}
return 0;
}
Variable *findInNamespaces(String const & name,
Evaluator::Namespaces const &spaces,
bool localOnly,
Record *& foundInNamespace,
Record ** higherNamespace = 0)
{
DENG2_FOR_EACH_CONST(Evaluator::Namespaces, i, spaces)
{
Record &ns = **i;
if(Variable *variable =
findInRecord(name, ns, foundInNamespace,
// allow looking in class if local not required:
!localOnly))
{
// The name exists in this namespace.
// Also note the higher namespace (for export).
Evaluator::Namespaces::const_iterator next = i;
if(++next != spaces.end())
{
if(higherNamespace) *higherNamespace = *next;
}
return variable;
}
if(localOnly)
{
// Not allowed to look in outer scopes.
break;
}
}
return 0;
}
};
} // namespace de
namespace de {
NameExpression::NameExpression() : d(new Instance)
{}
NameExpression::NameExpression(String const &identifier, Flags const &flags,
String const &scopeIdentifier)
: d(new Instance(identifier, scopeIdentifier))
{
setFlags(flags);
}
String const &NameExpression::identifier() const
{
return d->identifier;
}
Value *NameExpression::evaluate(Evaluator &evaluator) const
{
//LOG_AS("NameExpression::evaluate");
//std::cout << "NameExpression::evaluator: " << _flags.to_string() << "\n";
LOGDEV_SCR_XVERBOSE_DEBUGONLY("evaluating name:\"%s\" flags:%x", d->identifier << flags());
// Collect the namespaces to search.
Evaluator::Namespaces spaces;
Record *foundInNamespace = 0;
Record *higherNamespace = 0;
Variable *variable = 0;
if(d->scopeIdentifier.isEmpty() || d->scopeIdentifier == LOCAL_SCOPE)
{
if(d->scopeIdentifier != LOCAL_SCOPE)
{
// This is the usual case: scope defined by the left side of the member
// operator, or if that is not specified, the context's namespace stack.
evaluator.namespaces(spaces);
}
else
{
// Start with the context's local namespace.
evaluator.process().namespaces(spaces);
}
variable = d->findInNamespaces(d->identifier, spaces, flags().testFlag(LocalOnly),
foundInNamespace, &higherNamespace);
}
else
{
// An explicit scope has been defined; try to find it first. Look in the current
// context of the process, ignoring any narrower scopes that may apply here.
evaluator.process().namespaces(spaces);
Variable *scope = d->findInNamespaces(d->scopeIdentifier, spaces, false, foundInNamespace);
if(!scope)
{
throw NotFoundError("NameExpression::evaluate",
"Scope '" + d->scopeIdentifier + "' not found");
}
// Locate the identifier from this scope, disregarding the regular
// namespace context.
variable = d->findInRecord(d->identifier, scope->valueAsRecord(),
foundInNamespace);
}
if(flags().testFlag(ThrowawayIfInScope) && variable)
{
foundInNamespace = 0;
variable = &evaluator.context().throwaway();
}
// If a new variable/record is required and one is in scope, we cannot continue.
if(flags().testFlag(NotInScope) && variable)
{
throw AlreadyExistsError("NameExpression::evaluate",
"Identifier '" + d->identifier + "' already exists");
}
// Create a new subrecord in the namespace? ("record xyz")
if(flags().testFlag(NewSubrecord) ||
(flags().testFlag(NewSubrecordIfNotInScope) && !variable))
{
// Replaces existing member with this identifier.
Record &record = spaces.front()->addRecord(d->identifier);
return new RecordValue(record);
}
// If nothing is found and we are permitted to create new variables, do so.
// Occurs when assigning into new variables.
if(!variable && flags().testFlag(NewVariable))
{
variable = new Variable(d->identifier);
// Add it to the local namespace.
spaces.front()->add(variable);
// Take note of the namespaces.
foundInNamespace = spaces.front();
if(!higherNamespace && spaces.size() > 1)
{
Evaluator::Namespaces::iterator i = spaces.begin();
higherNamespace = *(++i);
}
}
// Export variable into a higher namespace?
if(flags().testFlag(Export))
{
DENG2_ASSERT(!flags().testFlag(ThrowawayIfInScope));
if(!variable)
{
throw NotFoundError("NameExpression::evaluate",
"Cannot export nonexistent identifier '" + d->identifier + "'");
}
if(!higherNamespace)
{
throw NotFoundError("NameExpression::evaluate",
"No higher namespace for exporting '" + d->identifier + "' into");
}
if(higherNamespace != foundInNamespace && foundInNamespace)
{
foundInNamespace->remove(*variable);
higherNamespace->add(variable);
}
}
// Should we import a namespace?
if(flags() & Import)
{
Record *record = &App::scriptSystem().importModule(d->identifier,
evaluator.process().globals()["__file__"].value().asText());
// Overwrite any existing member with this identifier.
spaces.front()->add(variable = new Variable(d->identifier));
if(flags().testFlag(ByValue))
{
// Take a copy of the record ("import record").
*variable = new RecordValue(new Record(*record), RecordValue::OwnsRecord);
}
else
{
// The variable will merely reference the module.
*variable = new RecordValue(record);
}
return new RecordValue(record);
}
if(variable)
{
// Variables can be referred to by reference or value.
if(flags() & ByReference)
{
// Reference to the variable.
return new RefValue(variable);
}
else
{
// Variables evaluate to their values. As a special case, values may have
// ownership of their data. Here we don't want to duplicate the data, only
// reference it.
return variable->value().duplicateAsReference();
}
}
throw NotFoundError("NameExpression::evaluate", "Identifier '" + d->identifier +
"' does not exist");
}
void NameExpression::operator >> (Writer &to) const
{
to << SerialId(NAME);
Expression::operator >> (to);
to << d->identifier << d->scopeIdentifier;
}
void NameExpression::operator << (Reader &from)
{
SerialId id;
from >> id;
if(id != NAME)
{
/// @throw DeserializationError The identifier that species the type of the
/// serialized expression was invalid.
throw DeserializationError("NameExpression::operator <<", "Invalid ID");
}
Expression::operator << (from);
from >> d->identifier;
if(from.version() >= DENG2_PROTOCOL_1_15_0_NameExpression_with_scope_identifier)
{
from >> d->scopeIdentifier;
}
}
} // namespace de