-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
scriptedinfo.cpp
206 lines (181 loc) · 6 KB
/
scriptedinfo.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
/** @file scriptedinfo.cpp Info document tree with script context.
*
* @authors Copyright (c) 2013 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>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</small>
*/
#include "de/ScriptedInfo"
#include "de/Script"
#include "de/Process"
#include "de/ArrayValue"
#include "de/RecordValue"
namespace de {
DENG2_PIMPL(ScriptedInfo)
{
Info info; ///< Original full parsed contents.
QScopedPointer<Script> script; ///< Current script being executed.
Process process; ///< Execution context.
String sourcePath;
Instance(Public *i) : Base(i)
{}
void clear()
{
info.clear();
process.clear();
script.reset();
}
/**
* Iterates through the parsed Info contents and processes each element.
* Script blocks are parsed as Doomsday Script and executed in the local
* process. Key/value elements become variables (and values) in the
* process's global namespace.
*/
void processAll()
{
processBlock(info.root());
LOG_DEBUG("Processed contents:\n") << process.globals().asText();
}
void processElement(Info::Element const *element)
{
if(element->isBlock())
{
processBlock(*static_cast<Info::BlockElement const *>(element));
}
else if(element->isKey())
{
processKey(*static_cast<Info::KeyElement const *>(element));
}
else if(element->isList())
{
processList(*static_cast<Info::ListElement const *>(element));
}
}
void processBlock(Info::BlockElement const &block)
{
if(Info::Element *condition = block.find("condition"))
{
// Any block will be ignored if its condition is false.
QScopedPointer<Value> result(evaluate(condition->values().first()));
if(result.isNull() || result->isFalse())
{
return;
}
}
if(block.blockType() == "script")
{
DENG2_ASSERT(block.find("script") != 0);
DENG2_ASSERT(process.state() == Process::Stopped);
script.reset(new Script(block.find("script")->values().first()));
script->setPath(sourcePath); // where the source comes from
process.run(*script);
process.execute();
}
else
{
// Block type placed into a special variable (only with named blocks, though).
if(!block.name().isEmpty())
{
String varName = variableName(block).concatenatePath("__type__", '.');
process.globals().add(varName) = new TextValue(block.blockType());
}
foreach(Info::Element const *sub, block.contentsInOrder())
{
if(sub->name() == "condition")
{
// Skip special elements.
continue;
}
if(sub->name() == ":" && !block.name().isEmpty())
{
// Inheritance.
String target = sub->values().first();
process.globals().add(variableName(block).concatenatePath("__inherit__", '.')) =
new TextValue(target);
// Copy all present members of the target record.
process.globals().subrecord(variableName(block))
.copyMembersFrom(process.globals()[target].value<RecordValue>().dereference());
continue;
}
processElement(sub);
}
}
}
String variableName(Info::Element const &element)
{
String varName = element.name();
for(Info::BlockElement *b = element.parent(); b != 0; b = b->parent())
{
if(!b->name().isEmpty())
{
varName = b->name().concatenatePath(varName, '.');
}
}
return varName;
}
static bool isEvaluatable(String const &value)
{
return value.startsWith('$');
}
Value *evaluate(String const &source)
{
script.reset(new Script(source));
process.run(*script);
process.execute();
return process.context().evaluator().result().duplicate();
}
Value *makeValue(String const &rawValue)
{
if(isEvaluatable(rawValue))
{
return evaluate(rawValue.substr(1));
}
return new TextValue(rawValue);
}
void processKey(Info::KeyElement const &key)
{
QScopedPointer<Value> v(makeValue(key.value()));
process.globals().add(variableName(key)) = v.take();
}
void processList(Info::ListElement const &list)
{
ArrayValue* av = new ArrayValue;
foreach(QString v, list.values())
{
*av << makeValue(v);
}
process.globals().addArray(variableName(list), av);
}
};
ScriptedInfo::ScriptedInfo() : d(new Instance(this))
{}
void ScriptedInfo::clear()
{
d->clear();
}
void ScriptedInfo::parse(String const &source)
{
d->clear();
d->info.parse(source);
d->processAll();
}
void ScriptedInfo::parse(File const &file)
{
d->sourcePath = file.path();
parse(String::fromUtf8(Block(file)));
}
Value *ScriptedInfo::evaluate(String const &source)
{
return d->evaluate(source);
}
} // namespace de