4 * Created on: Aug 26, 2012
8 #include "Interpreter.h"
10 #include "ParsedSource.h"
11 #include "../battle/Hero.h"
12 #include "../battle/Monster.h"
13 #include "../battle/PartyLayout.h"
14 #include "../battle/Resources.h"
15 #include "../common/Ikari.h"
16 #include "../common/Item.h"
17 #include "../common/Spell.h"
18 #include "../common/Stats.h"
19 #include "../common/TargetingMode.h"
20 #include "../graphics/ComplexAnimation.h"
21 #include "../graphics/Font.h"
22 #include "../graphics/Frame.h"
23 #include "../graphics/Gauge.h"
24 #include "../graphics/Menu.h"
25 #include "../graphics/SimpleAnimation.h"
26 #include "../graphics/Sprite.h"
30 #include <SDL_image.h>
33 using battle::Monster;
34 using battle::PartyLayout;
39 using common::TargetingMode;
40 using graphics::Animation;
41 using graphics::Color;
43 using graphics::Frame;
44 using graphics::Gauge;
45 using graphics::ComplexAnimation;
46 using graphics::SimpleAnimation;
47 using graphics::Sprite;
48 using geometry::Vector;
56 Interpreter::~Interpreter() {
57 for (std::map<string, SDL_Surface *>::const_iterator i(imageCache.begin()), end(imageCache.end()); i != end; ++i) {
58 SDL_FreeSurface(i->second);
63 const Interpreter::ParsedDefinition &Interpreter::GetDefinition(const string &identifier) const {
64 return parsedDefinitions.at(identifier);
68 void *Interpreter::GetObject(int typeId, const std::string &name) {
69 std::map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
70 if (i != parsedDefinitions.end()) {
71 const TypeDescription &requested(TypeDescription::Get(typeId));
72 const TypeDescription &actual(TypeDescription::Get(i->second.type));
73 if (requested.TypeId() == actual.TypeId()) {
74 return values[actual.TypeId()][i->second.id];
75 } else if (actual.IsSubtypeOf(requested)) {
76 char *sub(reinterpret_cast<char *>(values[actual.TypeId()][i->second.id]));
77 std::ptrdiff_t offset(actual.SupertypeOffset(requested));
80 throw Error("cannot cast " + actual.TypeName() + " to " + requested.TypeName());
83 throw Error("access to undefined object " + name);
88 void Interpreter::ReadSource() {
89 for (set<string>::const_iterator i(source.Exports().begin()), end(source.Exports().end()); i != end; ++i) {
90 ReadDefinition(source.GetDefinition(*i));
94 void Interpreter::ReadDefinition(const Definition &dfn) {
95 if (parsedDefinitions.find(dfn.Identifier()) != parsedDefinitions.end()) {
98 if (dfn.HasLiteralValue()) {
105 void Interpreter::ReadLiteral(const Definition &dfn) {
106 const string &typeName(dfn.GetLiteral()->IsArray() ? "Array" : dfn.GetLiteral()->GetTypeName());
107 int typeId(TypeDescription::GetTypeId(typeName));
108 int id(values[typeId].size());
109 const TypeDescription &td(TypeDescription::Get(typeId));
111 (dfn.GetLiteral()->GetType() == Literal::PATH
112 || dfn.GetLiteral()->GetType() == Literal::STRING)
113 ? dfn.GetLiteral()->GetString().size() : td.Size());
114 char *object(alloc.Alloc(size));
115 if (dfn.GetLiteral()->GetType() == Literal::OBJECT) {
116 ReadObject(typeId, id, object, *dfn.GetLiteral()->GetProperties());
118 ReadLiteral(typeId, id, object, *dfn.GetLiteral());
120 values[typeId].push_back(object);
121 parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, typeId, id)));
124 void Interpreter::ReadLiteral(int typeId, int id, char *object, const Literal &literal) {
125 switch (literal.GetType()) {
126 case Literal::ARRAY_VALUES:
127 throw Error("named value arrays are not supported, sorry");
129 case Literal::ARRAY_PROPS:
130 throw Error("named property list arrays are not supported, sorry");
132 case Literal::BOOLEAN:
133 new (object) bool(literal.GetBoolean());
136 new (object) Color(literal.GetRed(), literal.GetGreen(), literal.GetBlue(), literal.GetAlpha());
138 case Literal::NUMBER:
139 new (object) int(literal.GetNumber());
142 std::memcpy(object, literal.GetString().c_str(), literal.GetString().size());
143 object[literal.GetString().size()] = '\0';
145 case Literal::SCRIPT:
146 throw Error("script compiler not implemented");
147 case Literal::STRING:
148 std::memcpy(object, literal.GetString().c_str(), literal.GetString().size());
149 object[literal.GetString().size()] = '\0';
151 case Literal::VECTOR:
152 new (object) Vector<int>(literal.GetX(), literal.GetY());
154 case Literal::OBJECT:
155 throw Error("illogical branch: read literal object as non-object literal");
160 void *Interpreter::GetObject(int typeId, const Value &v) {
162 if (v.GetLiteral().IsObject()) {
163 int typeId(TypeDescription::GetTypeId(v.GetLiteral().GetTypeName()));
164 const TypeDescription &td(TypeDescription::Get(typeId));
165 char *object(alloc.Alloc(td.Size()));
166 td.Construct(object);
167 int id(values[typeId].size());
168 values[typeId].push_back(object);
169 ReadObject(typeId, id, object, *v.GetLiteral().GetProperties());
172 int typeId(0), id(0);
173 switch (v.GetLiteral().GetType()) {
174 case Literal::ARRAY_VALUES:
175 throw Error("cannot copy value arrays, sorry");
177 case Literal::ARRAY_PROPS:
178 throw Error("cannot copy property list arrays, sorry");
180 case Literal::BOOLEAN:
182 typeId = TypeDescription::GetTypeId("Boolean");
183 id = values[typeId].size();
184 const TypeDescription &td(TypeDescription::Get(typeId));
185 char *object(alloc.Alloc(td.Size()));
186 values[typeId].push_back(new (object) bool(v.GetLiteral().GetBoolean()));
191 typeId = TypeDescription::GetTypeId("Color");
192 id = values[typeId].size();
193 const TypeDescription &td(TypeDescription::Get(typeId));
194 char *object(alloc.Alloc(td.Size()));
195 values[typeId].push_back(new (object) Color(v.GetLiteral().GetRed(), v.GetLiteral().GetGreen(), v.GetLiteral().GetBlue(), v.GetLiteral().GetAlpha()));
198 case Literal::NUMBER:
200 typeId = TypeDescription::GetTypeId("Number");
201 id = values[typeId].size();
202 const TypeDescription &td(TypeDescription::Get(typeId));
203 char *object(alloc.Alloc(td.Size()));
204 values[typeId].push_back(new (object) int(v.GetLiteral().GetNumber()));
209 typeId = TypeDescription::GetTypeId("Path");
210 id = values[typeId].size();
211 char *str(alloc.Alloc(v.GetLiteral().GetString().size() + 1));
212 std::memcpy(str, v.GetLiteral().GetString().c_str(), v.GetLiteral().GetString().size());
213 str[v.GetLiteral().GetString().size()] = '\0';
214 values[typeId].push_back(str);
217 case Literal::SCRIPT:
218 throw Error("script compiler not implemented");
219 case Literal::STRING:
221 typeId = TypeDescription::GetTypeId("String");
222 id = values[typeId].size();
223 char *str(alloc.Alloc(v.GetLiteral().GetString().size() + 1));
224 std::memcpy(str, v.GetLiteral().GetString().c_str(), v.GetLiteral().GetString().size());
225 str[v.GetLiteral().GetString().size()] = '\0';
226 values[typeId].push_back(str);
229 case Literal::VECTOR:
231 typeId = TypeDescription::GetTypeId("Vector");
232 id = values[typeId].size();
233 const TypeDescription &td(TypeDescription::Get(typeId));
234 char *object(alloc.Alloc(td.Size()));
235 values[typeId].push_back(new (object) Vector<int>(v.GetLiteral().GetX(), v.GetLiteral().GetY()));
238 case Literal::OBJECT:
240 typeId = TypeDescription::GetTypeId(v.GetLiteral().GetTypeName());
241 const TypeDescription &td(TypeDescription::Get(typeId));
242 id = values[typeId].size();
243 char *object(alloc.Alloc(td.Size()));
244 td.Construct(object);
245 ReadObject(typeId, id, object, *v.GetLiteral().GetProperties());
249 return values[typeId][id];
252 ReadDefinition(source.GetDefinition(v.GetIdentifier()));
253 return GetObject(typeId, v.GetIdentifier());
258 void Interpreter::ReadObject(const Definition &dfn) {
259 int typeId(TypeDescription::GetTypeId(dfn.TypeName()));
260 const TypeDescription &td(TypeDescription::Get(typeId));
261 int id(values[typeId].size());
262 char *object(alloc.Alloc(td.Size()));
263 td.Construct(object);
264 values[typeId].push_back(object);
265 ReadObject(typeId, id, object, *dfn.GetProperties());
266 parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, typeId, id)));
270 void Interpreter::ReadObject(int typeId, int id, char *object, const PropertyList &props) {
271 const TypeDescription &td(TypeDescription::Get(typeId));
272 for (PropertyList::ConstIterator i(props.Begin()), end(props.End()); i != end; ++i) {
273 const FieldDescription &fd(td.GetField(i->first));
274 const TypeDescription &fieldType(TypeDescription::Get(fd.TypeId()));
275 if (CanLink(*i->second)) {
276 char *dest(object + fd.Offset());
277 if (fd.IsAggregate()) {
278 int arraySize(i->second->GetLiteral().ArraySize());
279 char *aggregate(alloc.Alloc(fieldType.Size() * arraySize));
280 char *iter(aggregate);
281 if (i->second->GetLiteral().GetType() == Literal::ARRAY_PROPS) {
282 const vector<PropertyList *> &list(i->second->GetLiteral().GetPropertyLists());
283 for (vector<PropertyList *>::const_iterator j(list.begin()), end(list.end()); j != end; ++j, iter += fieldType.Size()) {
284 fieldType.Construct(iter);
285 ReadObject(fieldType.TypeId(), -1, iter, **j);
288 const vector<Value *> &list(i->second->GetLiteral().GetValues());
289 for (vector<Value *>::const_iterator j(list.begin()), end(list.end()); j != end; ++j, iter += fieldType.Size()) {
290 fieldType.Construct(iter);
291 ReadLiteral(fieldType.TypeId(), -1, iter, (*j)->GetLiteral());
294 if (fd.IsReferenced()) {
295 std::memcpy(dest, &aggregate, sizeof(char *));
296 dest += sizeof(char *);
297 std::memcpy(dest, &arraySize, sizeof(int));
299 throw Error("aggregate type fields must be referenced");
301 } else if (i->second->IsLiteral() && !fd.IsReferenced()) {
303 if (i->second->GetLiteral().IsObject()) {
304 ReadObject(fd.TypeId(), -1, dest, *i->second->GetLiteral().GetProperties());
306 ReadLiteral(fd.TypeId(), -1, dest, i->second->GetLiteral());
309 char *src(reinterpret_cast<char *>(GetObject(fd.TypeId(), *i->second)));
310 if (fd.TypeId() == TypeDescription::GetTypeId("Image")) {
311 src = reinterpret_cast<char *>(GetImage(src));
313 if (fd.IsReferenced()) {
314 std::memcpy(dest, &src, sizeof(char *));
316 std::memcpy(dest, src, fieldType.Size());
320 Postpone(typeId, id, fd.Offset(), i->second->GetIdentifier(), fd.TypeId(), !fd.IsReferenced());
327 SDL_Surface *Interpreter::GetImage(const string &path) {
328 std::map<string, SDL_Surface *>::const_iterator result(imageCache.find(path));
329 if (result != imageCache.end()) {
330 return result->second;
332 SDL_Surface *image(IMG_Load(path.c_str()));
333 imageCache.insert(make_pair(path, image));
339 bool Interpreter::CanLink(const Value &v) const {
340 return v.IsLiteral() || source.IsDefined(v.GetIdentifier());
343 void Interpreter::Postpone(int type, int id, std::ptrdiff_t offset, const std::string &identifier, int linkedType, bool inlined) {
344 char *str(alloc.Alloc(identifier.size() + 1));
345 std::memcpy(str, identifier.c_str(), identifier.size());
346 str[identifier.size()] = '\0';
347 postponedDefinitions.push_back(PostponedDefinition(type, id, offset, str, linkedType, inlined));
351 void Interpreter::CreateTypeDescriptions() {
353 TypeDescription &td(TypeDescription::CreateOrGet("Boolean"));
354 td.SetDescription("Logical value which can be either true or false.");
355 td.SetSize(sizeof(bool));
358 TypeDescription &td(TypeDescription::CreateOrGet("Color"));
360 "A color in RGB format with an optional alpha channel.\n"
361 "Components range from 0 to 255.\n"
362 "Alpha defaults to 255 if omitted.");
363 td.SetSize(sizeof(Color));
366 TypeDescription &td(TypeDescription::CreateOrGet("Image"));
367 td.SetDescription("Path to a PNG file with image data.");
368 td.SetSize(sizeof(SDL_Surface));
371 TypeDescription &td(TypeDescription::CreateOrGet("Number"));
372 td.SetDescription("A signed integer.");
373 td.SetSize(sizeof(int));
376 int stringId(TypeDescription::GetTypeId("String"));
377 TypeDescription &td(TypeDescription::CreateOrGet("Path"));
378 td.SetDescription("A path in the filesystem which is interpreted relative to the source file's location.");
380 td.AddSupertype(stringId, 0);
383 TypeDescription &td(TypeDescription::CreateOrGet("String"));
384 td.SetDescription("Some characters.");
388 TypeDescription &td(TypeDescription::CreateOrGet("Vector"));
389 td.SetDescription("A pair of numbers usually describing a 2D translation or offset.");
390 td.SetSize(sizeof(Vector<int>));