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/TargetingMode.h"
19 #include "../graphics/ComplexAnimation.h"
20 #include "../graphics/Font.h"
21 #include "../graphics/Frame.h"
22 #include "../graphics/Gauge.h"
23 #include "../graphics/Menu.h"
24 #include "../graphics/SimpleAnimation.h"
25 #include "../graphics/Sprite.h"
29 #include <SDL_image.h>
32 using battle::Monster;
33 using battle::PartyLayout;
38 using common::TargetingMode;
39 using graphics::Animation;
40 using graphics::Color;
42 using graphics::Frame;
43 using graphics::Gauge;
44 using graphics::ComplexAnimation;
45 using graphics::SimpleAnimation;
46 using graphics::Sprite;
47 using geometry::Vector;
56 Interpreter::~Interpreter() {
57 for (vector<PostponedDefinition>::const_iterator i(postponedDefinitions.begin()), end(postponedDefinitions.end()); i != end; ++i) {
60 for (map<string, SDL_Surface *>::const_iterator i(imageCache.begin()), end(imageCache.end()); i != end; ++i) {
61 SDL_FreeSurface(i->second);
63 // TODO: maybe need to reverse the array deletion check if most objects turn out to be arrays (of char)
64 for (map<int, vector<void *> >::const_iterator i(values.begin()), end(values.end()); i != end; ++i) {
65 for (vector<void *>::const_iterator j(i->second.begin()), end(i->second.end()); j != end; ++j) {
66 delete[] reinterpret_cast<char *>(*j);
72 void *Interpreter::GetObject(int typeId, const std::string &name) {
73 map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
74 if (i != parsedDefinitions.end()) {
75 const TypeDescription &requested(TypeDescription::Get(typeId));
76 const TypeDescription &actual(TypeDescription::Get(i->second.type));
77 if (requested.TypeId() == actual.TypeId()) {
78 return values[actual.TypeId()][i->second.id];
79 } else if (actual.IsSubtypeOf(requested)) {
80 char *sub(reinterpret_cast<char *>(values[actual.TypeId()][i->second.id]));
81 std::ptrdiff_t offset(actual.SupertypeOffset(requested));
84 throw Error("cannot cast " + actual.TypeName() + " to " + requested.TypeName());
87 throw Error("access to undefined object " + name);
92 void Interpreter::ReadSource() {
93 for (set<string>::const_iterator i(source.Exports().begin()), end(source.Exports().end()); i != end; ++i) {
94 ReadDefinition(source.GetDefinition(*i));
98 void Interpreter::ReadDefinition(const Definition &dfn) {
99 if (parsedDefinitions.find(dfn.Identifier()) != parsedDefinitions.end()) {
102 if (dfn.HasLiteralValue()) {
109 void Interpreter::ReadLiteral(const Definition &dfn) {
110 switch (dfn.GetLiteral()->GetType()) {
111 case Literal::ARRAY_VALUES:
112 throw Error("named value arrays are not supported, sorry");
114 case Literal::ARRAY_PROPS:
115 throw Error("named property list arrays are not supported, sorry");
117 case Literal::BOOLEAN:
119 int typeId(TypeDescription::GetTypeId("Boolean"));
120 values[typeId].push_back(new bool(dfn.GetLiteral()->GetBoolean()));
121 parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, typeId, values[typeId].size() - 1)));
126 int typeId(TypeDescription::GetTypeId("Color"));
127 values[typeId].push_back(new Color(dfn.GetLiteral()->GetRed(), dfn.GetLiteral()->GetGreen(), dfn.GetLiteral()->GetBlue(), dfn.GetLiteral()->GetAlpha()));
128 parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, typeId, values[typeId].size() - 1)));
131 case Literal::NUMBER:
133 int typeId(TypeDescription::GetTypeId("Number"));
134 values[typeId].push_back(new int(dfn.GetLiteral()->GetNumber()));
135 parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, typeId, values[typeId].size() - 1)));
140 int typeId(TypeDescription::GetTypeId("Path"));
141 char *str(new char[dfn.GetLiteral()->GetString().size() + 1]);
142 std::memcpy(str, dfn.GetLiteral()->GetString().c_str(), dfn.GetLiteral()->GetString().size());
143 str[dfn.GetLiteral()->GetString().size()] = '\0';
144 values[typeId].push_back(str);
145 parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, typeId, values[typeId].size() - 1)));
148 case Literal::STRING:
150 int typeId(TypeDescription::GetTypeId("String"));
151 char *str(new char[dfn.GetLiteral()->GetString().size() + 1]);
152 std::memcpy(str, dfn.GetLiteral()->GetString().c_str(), dfn.GetLiteral()->GetString().size());
153 str[dfn.GetLiteral()->GetString().size()] = '\0';
154 values[typeId].push_back(str);
155 parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, typeId, values[typeId].size() - 1)));
158 case Literal::VECTOR:
160 int typeId(TypeDescription::GetTypeId("Vector"));
161 values[typeId].push_back(new Vector<int>(dfn.GetLiteral()->GetX(), dfn.GetLiteral()->GetY()));
162 parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, typeId, values[typeId].size() - 1)));
165 case Literal::OBJECT:
172 void *Interpreter::GetObject(int typeId, const Value &v) {
174 if (v.GetLiteral().IsObject()) {
175 int typeId(TypeDescription::GetTypeId(v.GetLiteral().GetTypeName()));
176 const TypeDescription &td(TypeDescription::Get(typeId));
177 char *object(new char[td.Size()]);
178 td.Construct(object);
179 int id(values[typeId].size());
180 values[typeId].push_back(object);
181 ReadObject(typeId, id, object, *v.GetLiteral().GetProperties());
184 int typeId(0), id(0);
185 switch (v.GetLiteral().GetType()) {
186 case Literal::ARRAY_VALUES:
187 throw Error("cannot copy value arrays, sorry");
189 case Literal::ARRAY_PROPS:
190 throw Error("cannot copy property list arrays, sorry");
192 case Literal::BOOLEAN:
194 typeId = TypeDescription::GetTypeId("Boolean");
195 id = values[typeId].size();
196 const TypeDescription &td(TypeDescription::Get(typeId));
197 char *object(new char[td.Size()]);
198 values[typeId].push_back(new (object) bool(v.GetLiteral().GetBoolean()));
203 typeId = TypeDescription::GetTypeId("Color");
204 id = values[typeId].size();
205 const TypeDescription &td(TypeDescription::Get(typeId));
206 char *object(new char[td.Size()]);
207 values[typeId].push_back(new (object) Color(v.GetLiteral().GetRed(), v.GetLiteral().GetGreen(), v.GetLiteral().GetBlue(), v.GetLiteral().GetAlpha()));
210 case Literal::NUMBER:
212 typeId = TypeDescription::GetTypeId("Number");
213 id = values[typeId].size();
214 const TypeDescription &td(TypeDescription::Get(typeId));
215 char *object(new char[td.Size()]);
216 values[typeId].push_back(new (object) int(v.GetLiteral().GetNumber()));
221 typeId = TypeDescription::GetTypeId("Path");
222 id = values[typeId].size();
223 char *str(new char[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::STRING:
231 typeId = TypeDescription::GetTypeId("String");
232 id = values[typeId].size();
233 char *str(new char[v.GetLiteral().GetString().size() + 1]);
234 std::memcpy(str, v.GetLiteral().GetString().c_str(), v.GetLiteral().GetString().size());
235 str[v.GetLiteral().GetString().size()] = '\0';
236 values[typeId].push_back(str);
239 case Literal::VECTOR:
241 typeId = TypeDescription::GetTypeId("Vector");
242 id = values[typeId].size();
243 const TypeDescription &td(TypeDescription::Get(typeId));
244 char *object(new char[td.Size()]);
245 values[typeId].push_back(new (object) Vector<int>(v.GetLiteral().GetX(), v.GetLiteral().GetY()));
248 case Literal::OBJECT:
250 typeId = TypeDescription::GetTypeId(v.GetLiteral().GetTypeName());
251 const TypeDescription &td(TypeDescription::Get(typeId));
252 id = values[typeId].size();
253 char *object(new char[td.Size()]);
254 td.Construct(object);
255 ReadObject(typeId, id, object, *v.GetLiteral().GetProperties());
259 return values[typeId][id];
262 ReadDefinition(source.GetDefinition(v.GetIdentifier()));
263 return GetObject(typeId, v.GetIdentifier());
268 void Interpreter::ReadObject(const Definition &dfn) {
269 int typeId(TypeDescription::GetTypeId(dfn.TypeName()));
270 const TypeDescription &td(TypeDescription::Get(typeId));
271 int id(values[typeId].size());
272 char *object(new char[td.Size()]);
273 td.Construct(object);
274 values[typeId].push_back(object);
275 ReadObject(typeId, id, object, *dfn.GetProperties());
276 parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, typeId, id)));
280 void Interpreter::ReadObject(int typeId, int id, char *object, const PropertyList &props) {
281 const TypeDescription &td(TypeDescription::Get(typeId));
282 for (PropertyList::ConstIterator i(props.Begin()), end(props.End()); i != end; ++i) {
283 const FieldDescription &fd(td.GetField(i->first));
284 const TypeDescription &fieldType(TypeDescription::Get(fd.TypeId()));
285 if (CanLink(*i->second)) {
286 char *dest(object + fd.Offset());
287 if (fd.IsAggregate()) {
288 if (i->second->GetLiteral().GetType() != Literal::ARRAY_PROPS) {
289 throw Error("unsupported aggregate type");
291 int arraySize(i->second->GetLiteral().ArraySize());
292 char *aggregate(new char[fieldType.Size() * arraySize]);
293 char *iter(aggregate);
294 vector<PropertyList *> list(i->second->GetLiteral().GetPropertyLists());
295 for (vector<PropertyList *>::const_iterator j(list.begin()), end(list.end()); j != end; ++j, iter += fieldType.Size()) {
296 fieldType.Construct(iter);
297 ReadObject(fieldType.TypeId(), -1, iter, **j);
299 if (fd.IsReferenced()) {
300 std::memcpy(dest, &aggregate, sizeof(char *));
301 dest += sizeof(char *);
302 std::memcpy(dest, &arraySize, sizeof(int));
304 throw Error("aggregate type fields must be referenced");
307 char *src(reinterpret_cast<char *>(GetObject(fd.TypeId(), *i->second)));
308 if (fd.IsReferenced()) {
309 std::memcpy(dest, &src, sizeof(char *));
311 std::memcpy(dest, src, fieldType.Size());
315 Postpone(typeId, id, fd.Offset(), i->second->GetIdentifier(), fd.TypeId());
321 bool Interpreter::CanLink(const Value &v) const {
322 return v.IsLiteral() || source.IsDefined(v.GetIdentifier());
325 void Interpreter::Postpone(int type, int id, std::ptrdiff_t offset, const std::string &identifier, int linkedType) {
326 char *str(new char[identifier.size() + 1]);
327 std::memcpy(str, identifier.c_str(), identifier.size());
328 str[identifier.size()] = '\0';
329 postponedDefinitions.push_back(PostponedDefinition(type, id, offset, str, linkedType));
333 void Interpreter::CreateTypeDescriptions() {
335 TypeDescription &td(TypeDescription::CreateOrGet("Boolean"));
336 td.SetSize(sizeof(bool));
339 TypeDescription &td(TypeDescription::CreateOrGet("Color"));
340 td.SetSize(sizeof(Color));
343 TypeDescription &td(TypeDescription::CreateOrGet("Image"));
344 td.SetSize(sizeof(SDL_Surface));
347 TypeDescription &td(TypeDescription::CreateOrGet("Number"));
348 td.SetSize(sizeof(int));
351 int stringId(TypeDescription::GetTypeId("String"));
352 TypeDescription &td(TypeDescription::CreateOrGet("Path"));
354 td.AddSupertype(stringId, 0);
357 TypeDescription &td(TypeDescription::CreateOrGet("String"));
361 TypeDescription &td(TypeDescription::CreateOrGet("Vector"));
362 td.SetSize(sizeof(Vector<int>));