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/Script.h"
18 #include "../common/Spell.h"
19 #include "../common/Stats.h"
20 #include "../common/TargetingMode.h"
21 #include "../graphics/ComplexAnimation.h"
22 #include "../graphics/Font.h"
23 #include "../graphics/Frame.h"
24 #include "../graphics/Gauge.h"
25 #include "../graphics/Menu.h"
26 #include "../graphics/SimpleAnimation.h"
27 #include "../graphics/Sprite.h"
31 #include <SDL_image.h>
34 using battle::Monster;
35 using battle::PartyLayout;
41 using common::TargetingMode;
42 using graphics::Animation;
43 using graphics::Color;
45 using graphics::Frame;
46 using graphics::Gauge;
47 using graphics::ComplexAnimation;
48 using graphics::SimpleAnimation;
49 using graphics::Sprite;
50 using geometry::Vector;
58 Interpreter::~Interpreter() {
59 for (std::map<string, SDL_Surface *>::const_iterator i(imageCache.begin()), end(imageCache.end()); i != end; ++i) {
60 SDL_FreeSurface(i->second);
65 const Interpreter::ParsedDefinition &Interpreter::GetDefinition(const string &identifier) const {
66 return parsedDefinitions.at(identifier);
70 void *Interpreter::GetObject(int typeId, const std::string &name) {
71 std::map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
72 if (i != parsedDefinitions.end()) {
73 const TypeDescription &requested(TypeDescription::Get(typeId));
74 const TypeDescription &actual(TypeDescription::Get(i->second.type));
75 if (requested.TypeId() == actual.TypeId()) {
76 return values[actual.TypeId()][i->second.id];
77 } else if (actual.IsSubtypeOf(requested)) {
78 char *sub(reinterpret_cast<char *>(values[actual.TypeId()][i->second.id]));
79 std::ptrdiff_t offset(actual.SupertypeOffset(requested));
82 throw Error("cannot cast " + actual.TypeName() + " to " + requested.TypeName());
85 throw Error("access to undefined object " + name);
90 void Interpreter::ReadSource() {
91 for (set<string>::const_iterator i(source.Exports().begin()), end(source.Exports().end()); i != end; ++i) {
92 ReadDefinition(source.GetDefinition(*i));
96 void Interpreter::ReadDefinition(const Definition &dfn) {
97 if (parsedDefinitions.find(dfn.Identifier()) != parsedDefinitions.end()) {
100 if (dfn.HasLiteralValue()) {
107 void Interpreter::ReadLiteral(const Definition &dfn) {
108 const string &typeName(dfn.GetLiteral()->IsArray() ? "Array" : dfn.GetLiteral()->GetTypeName());
109 int typeId(TypeDescription::GetTypeId(typeName));
110 int id(values[typeId].size());
111 const TypeDescription &td(TypeDescription::Get(typeId));
113 (dfn.GetLiteral()->GetType() == Literal::PATH
114 || dfn.GetLiteral()->GetType() == Literal::STRING)
115 ? dfn.GetLiteral()->GetString().size() : td.Size());
116 char *object(alloc.Alloc(size));
117 if (dfn.GetLiteral()->GetType() == Literal::OBJECT) {
118 ReadObject(typeId, id, object, *dfn.GetLiteral()->GetProperties());
120 ReadLiteral(typeId, id, object, *dfn.GetLiteral());
122 values[typeId].push_back(object);
123 parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, typeId, id)));
126 void Interpreter::ReadLiteral(int typeId, int id, char *object, const Literal &literal) {
127 switch (literal.GetType()) {
128 case Literal::ARRAY_VALUES:
129 throw Error("named value arrays are not supported, sorry");
131 case Literal::ARRAY_PROPS:
132 throw Error("named property list arrays are not supported, sorry");
134 case Literal::BOOLEAN:
135 new (object) bool(literal.GetBoolean());
138 new (object) Color(literal.GetRed(), literal.GetGreen(), literal.GetBlue(), literal.GetAlpha());
140 case Literal::NUMBER:
141 new (object) int(literal.GetNumber());
144 std::memcpy(object, literal.GetString().c_str(), literal.GetString().size());
145 object[literal.GetString().size()] = '\0';
147 case Literal::SCRIPT:
149 ReadScript(literal.GetScript(), reinterpret_cast<Script *>(object));
151 case Literal::STRING:
152 std::memcpy(object, literal.GetString().c_str(), literal.GetString().size());
153 object[literal.GetString().size()] = '\0';
155 case Literal::VECTOR:
156 new (object) Vector<int>(literal.GetX(), literal.GetY());
158 case Literal::OBJECT:
159 throw Error("illogical branch: read literal object as non-object literal");
164 void *Interpreter::GetObject(int typeId, const Value &v) {
166 if (v.GetLiteral().IsObject()) {
167 int typeId(TypeDescription::GetTypeId(v.GetLiteral().GetTypeName()));
168 const TypeDescription &td(TypeDescription::Get(typeId));
169 char *object(alloc.Alloc(td.Size()));
170 td.Construct(object);
171 int id(values[typeId].size());
172 values[typeId].push_back(object);
173 ReadObject(typeId, id, object, *v.GetLiteral().GetProperties());
176 int typeId(0), id(0);
177 switch (v.GetLiteral().GetType()) {
178 case Literal::ARRAY_VALUES:
179 throw Error("cannot copy value arrays, sorry");
181 case Literal::ARRAY_PROPS:
182 throw Error("cannot copy property list arrays, sorry");
184 case Literal::BOOLEAN:
186 typeId = TypeDescription::GetTypeId("Boolean");
187 id = values[typeId].size();
188 const TypeDescription &td(TypeDescription::Get(typeId));
189 char *object(alloc.Alloc(td.Size()));
190 values[typeId].push_back(new (object) bool(v.GetLiteral().GetBoolean()));
195 typeId = TypeDescription::GetTypeId("Color");
196 id = values[typeId].size();
197 const TypeDescription &td(TypeDescription::Get(typeId));
198 char *object(alloc.Alloc(td.Size()));
199 values[typeId].push_back(new (object) Color(v.GetLiteral().GetRed(), v.GetLiteral().GetGreen(), v.GetLiteral().GetBlue(), v.GetLiteral().GetAlpha()));
202 case Literal::NUMBER:
204 typeId = TypeDescription::GetTypeId("Number");
205 id = values[typeId].size();
206 const TypeDescription &td(TypeDescription::Get(typeId));
207 char *object(alloc.Alloc(td.Size()));
208 values[typeId].push_back(new (object) int(v.GetLiteral().GetNumber()));
213 typeId = TypeDescription::GetTypeId("Path");
214 id = values[typeId].size();
215 char *str(alloc.Alloc(v.GetLiteral().GetString().size() + 1));
216 std::memcpy(str, v.GetLiteral().GetString().c_str(), v.GetLiteral().GetString().size());
217 str[v.GetLiteral().GetString().size()] = '\0';
218 values[typeId].push_back(str);
221 case Literal::SCRIPT:
223 typeId = TypeDescription::GetTypeId("Script");
224 id = values[typeId].size();
225 values[typeId].push_back(ReadScript(v.GetLiteral().GetScript()));
228 case Literal::STRING:
230 typeId = TypeDescription::GetTypeId("String");
231 id = values[typeId].size();
232 char *str(alloc.Alloc(v.GetLiteral().GetString().size() + 1));
233 std::memcpy(str, v.GetLiteral().GetString().c_str(), v.GetLiteral().GetString().size());
234 str[v.GetLiteral().GetString().size()] = '\0';
235 values[typeId].push_back(str);
238 case Literal::VECTOR:
240 typeId = TypeDescription::GetTypeId("Vector");
241 id = values[typeId].size();
242 const TypeDescription &td(TypeDescription::Get(typeId));
243 char *object(alloc.Alloc(td.Size()));
244 values[typeId].push_back(new (object) Vector<int>(v.GetLiteral().GetX(), v.GetLiteral().GetY()));
247 case Literal::OBJECT:
249 typeId = TypeDescription::GetTypeId(v.GetLiteral().GetTypeName());
250 const TypeDescription &td(TypeDescription::Get(typeId));
251 id = values[typeId].size();
252 char *object(alloc.Alloc(td.Size()));
253 td.Construct(object);
254 ReadObject(typeId, id, object, *v.GetLiteral().GetProperties());
258 return values[typeId][id];
261 ReadDefinition(source.GetDefinition(v.GetIdentifier()));
262 return GetObject(typeId, v.GetIdentifier());
267 void Interpreter::ReadObject(const Definition &dfn) {
268 int typeId(TypeDescription::GetTypeId(dfn.TypeName()));
269 const TypeDescription &td(TypeDescription::Get(typeId));
270 int id(values[typeId].size());
271 char *object(alloc.Alloc(td.Size()));
272 td.Construct(object);
273 values[typeId].push_back(object);
274 ReadObject(typeId, id, object, *dfn.GetProperties());
275 parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, typeId, id)));
279 void Interpreter::ReadObject(int typeId, int id, char *object, const PropertyList &props) {
280 const TypeDescription &td(TypeDescription::Get(typeId));
281 for (PropertyList::ConstIterator i(props.Begin()), end(props.End()); i != end; ++i) {
282 const FieldDescription &fd(td.GetField(i->first));
283 const TypeDescription &fieldType(TypeDescription::Get(fd.TypeId()));
284 if (CanLink(*i->second)) {
285 char *dest(object + fd.Offset());
286 if (fd.IsAggregate()) {
287 int arraySize(i->second->GetLiteral().ArraySize());
288 char *aggregate(alloc.Alloc(fieldType.Size() * arraySize));
289 char *iter(aggregate);
290 if (i->second->GetLiteral().GetType() == Literal::ARRAY_PROPS) {
291 const vector<PropertyList *> &list(i->second->GetLiteral().GetPropertyLists());
292 for (vector<PropertyList *>::const_iterator j(list.begin()), end(list.end()); j != end; ++j, iter += fieldType.Size()) {
293 fieldType.Construct(iter);
294 ReadObject(fieldType.TypeId(), -1, iter, **j);
297 const vector<Value *> &list(i->second->GetLiteral().GetValues());
298 for (vector<Value *>::const_iterator j(list.begin()), end(list.end()); j != end; ++j, iter += fieldType.Size()) {
299 fieldType.Construct(iter);
300 ReadLiteral(fieldType.TypeId(), -1, iter, (*j)->GetLiteral());
303 if (fd.IsReferenced()) {
304 std::memcpy(dest, &aggregate, sizeof(char *));
305 dest += sizeof(char *);
306 std::memcpy(dest, &arraySize, sizeof(int));
308 throw Error("aggregate type fields must be referenced");
310 } else if (i->second->IsLiteral() && !fd.IsReferenced()) {
312 if (i->second->GetLiteral().IsObject()) {
313 ReadObject(fd.TypeId(), -1, dest, *i->second->GetLiteral().GetProperties());
315 ReadLiteral(fd.TypeId(), -1, dest, i->second->GetLiteral());
318 char *src(reinterpret_cast<char *>(GetObject(fd.TypeId(), *i->second)));
319 if (fd.TypeId() == TypeDescription::GetTypeId("Image")) {
320 src = reinterpret_cast<char *>(GetImage(src));
322 if (fd.IsReferenced()) {
323 std::memcpy(dest, &src, sizeof(char *));
325 std::memcpy(dest, src, fieldType.Size());
329 Postpone(typeId, id, fd.Offset(), i->second->GetIdentifier(), fd.TypeId(), !fd.IsReferenced());
336 void Interpreter::ReadScript(const std::vector<ScriptToken *> &s, Script *script) {
338 for (vector<ScriptToken *>::const_iterator i(s.begin()), end(s.end()); i != end; ++i) {
339 if ((*i)->GetType() != ScriptToken::COMMAND) {
340 throw Error("unexpected script token");
343 const string &cmd((*i)->CommandName());
347 throw Error("unexpected script end after move");
349 const string ®((*i)->RegisterName());
352 size += sizeof(void *);
358 size += sizeof(Vector<int>);
361 throw Error("unknown register " + reg);
365 throw Error("unexpected script end after move");
367 } else if (cmd == "add") {
370 throw Error("unexpected script end after add");
372 const string ®((*i)->RegisterName());
378 size += sizeof(Vector<int>);
381 throw Error("expected register after add " + reg);
385 throw Error("unexpected script end after add");
387 } else if (cmd == "mod") {
390 throw Error("unexpected script end after mod");
395 throw Error("unexpected script end after mod");
397 } else if (cmd == "rand") {
400 throw Error("unexpected script end after rand");
405 throw Error("unexpected script end after rand");
407 } else if (cmd == "sysc") {
410 throw Error("unknown command " + cmd);
414 unsigned char *text(reinterpret_cast<unsigned char *>(alloc.Alloc(size)));
416 for (vector<ScriptToken *>::const_iterator i(s.begin()), end(s.end()); i != end; ++i) {
417 if ((*i)->GetType() != ScriptToken::COMMAND) {
418 throw Error("unexpected script token");
420 const string &cmd((*i)->CommandName());
423 const string ®((*i)->RegisterName());
426 text[cursor] = Script::CODE_MOVE_A0;
428 ReadScriptAddress(**i, text + cursor);
429 cursor += sizeof(void *);
430 } else if (reg == "a1") {
431 text[cursor] = Script::CODE_MOVE_A1;
433 ReadScriptAddress(**i, text + cursor);
434 cursor += sizeof(void *);
435 } else if (reg == "i0") {
436 text[cursor] = Script::CODE_MOVE_I0;
438 ReadScriptInteger(**i, text + cursor);
439 cursor += sizeof(int);
440 } else if (reg == "i1") {
441 text[cursor] = Script::CODE_MOVE_I1;
443 ReadScriptInteger(**i, text + cursor);
444 cursor += sizeof(int);
445 } else if (reg == "v0") {
446 text[cursor] = Script::CODE_MOVE_V0;
448 ReadScriptVector(**i, text + cursor);
449 cursor += sizeof(Vector<int>);
450 } else if (reg == "v1") {
451 text[cursor] = Script::CODE_MOVE_V1;
453 ReadScriptVector(**i, text + cursor);
454 cursor += sizeof(Vector<int>);
456 throw Error("unknown register " + reg);
458 } else if (cmd == "add") {
460 const string ®((*i)->RegisterName());
463 text[cursor] = Script::CODE_ADD_I0;
465 ReadScriptInteger(**i, text + cursor);
466 cursor += sizeof(int);
467 } else if (reg == "i1") {
468 text[cursor] = Script::CODE_ADD_I1;
470 ReadScriptInteger(**i, text + cursor);
471 cursor += sizeof(int);
472 } else if (reg == "v0") {
473 text[cursor] = Script::CODE_ADD_V0;
475 ReadScriptVector(**i, text + cursor);
476 cursor += sizeof(Vector<int>);
477 } else if (reg == "v1") {
478 text[cursor] = Script::CODE_ADD_V1;
480 ReadScriptVector(**i, text + cursor);
481 cursor += sizeof(Vector<int>);
483 throw Error("unexpected register " + reg);
485 } else if (cmd == "mod") {
487 const string ®((*i)->RegisterName());
490 text[cursor] = Script::CODE_MOD_I0;
492 ReadScriptInteger(**i, text + cursor);
493 cursor += sizeof(int);
494 } else if (reg == "i1") {
495 text[cursor] = Script::CODE_MOD_I1;
497 ReadScriptInteger(**i, text + cursor);
498 cursor += sizeof(int);
500 throw Error("unexpected register " + reg);
502 } else if (cmd == "rand") {
504 const string ®((*i)->RegisterName());
506 text[cursor] = Script::CODE_RAND_I0;
508 } else if (reg == "i1") {
509 text[cursor] = Script::CODE_RAND_I1;
512 throw Error("unexpected register " + reg);
514 } else if (cmd == "sysc") {
515 text[cursor] = Script::CODE_SYSCALL;
518 throw Error("unknown command " + cmd);
523 script->textlen = size;
526 char *Interpreter::ReadScript(const vector<ScriptToken *> &s) {
527 char *mem(alloc.Alloc(sizeof(Script)));
529 Script *script(reinterpret_cast<Script *>(mem));
530 ReadScript(s, script);
534 void Interpreter::ReadScriptAddress(const ScriptToken &t, unsigned char *dest) {
535 if (t.GetType() != ScriptToken::IDENTIFIER) {
536 throw Error("expected identifier for address");
538 if (source.IsDefined(t.GetIdentifier())) {
539 const ParsedDefinition &def(GetDefinition(t.GetIdentifier()));
540 void *addr(GetObject(def.type, t.GetIdentifier()));
541 *reinterpret_cast<void **>(dest) = addr;
543 throw Error("postponing values in scripts not implemented");
547 void Interpreter::ReadScriptInteger(const ScriptToken &t, unsigned char *dest) {
548 if (t.GetType() == ScriptToken::IDENTIFIER) {
549 if (source.IsDefined(t.GetIdentifier())) {
550 void *num(GetObject(TypeDescription::GetTypeId("Number"), t.GetIdentifier()));
551 *reinterpret_cast<int *>(dest) = *reinterpret_cast<int *>(num);
553 throw Error("postponing values in scripts not implemented");
555 } else if (t.GetType() == ScriptToken::LITERAL) {
556 *reinterpret_cast<int *>(dest) = t.GetLiteral()->GetNumber();
558 throw Error("expected identifier or integer literal");
562 void Interpreter::ReadScriptVector(const ScriptToken &t, unsigned char *dest) {
563 if (t.GetType() == ScriptToken::IDENTIFIER) {
564 if (source.IsDefined(t.GetIdentifier())) {
565 void *vec(GetObject(TypeDescription::GetTypeId("Vector"), t.GetIdentifier()));
566 *reinterpret_cast<Vector<int> *>(dest) = *reinterpret_cast<Vector<int> *>(vec);
568 throw Error("postponing values in scripts not implemented");
570 } else if (t.GetType() == ScriptToken::LITERAL) {
571 *reinterpret_cast<Vector<int> *>(dest) = Vector<int>(t.GetLiteral()->GetX(), t.GetLiteral()->GetY());
573 throw Error("expected identifier or vector literal");
578 SDL_Surface *Interpreter::GetImage(const string &path) {
579 std::map<string, SDL_Surface *>::const_iterator result(imageCache.find(path));
580 if (result != imageCache.end()) {
581 return result->second;
583 SDL_Surface *image(IMG_Load(path.c_str()));
584 imageCache.insert(make_pair(path, image));
590 bool Interpreter::CanLink(const Value &v) const {
591 return v.IsLiteral() || source.IsDefined(v.GetIdentifier());
594 void Interpreter::Postpone(int type, int id, std::ptrdiff_t offset, const std::string &identifier, int linkedType, bool inlined) {
595 char *str(alloc.Alloc(identifier.size() + 1));
596 std::memcpy(str, identifier.c_str(), identifier.size());
597 str[identifier.size()] = '\0';
598 postponedDefinitions.push_back(PostponedDefinition(type, id, offset, str, linkedType, inlined));
602 void Interpreter::CreateTypeDescriptions() {
604 TypeDescription &td(TypeDescription::CreateOrGet("Boolean"));
605 td.SetDescription("Logical value which can be either true or false.");
606 td.SetSize(sizeof(bool));
609 TypeDescription &td(TypeDescription::CreateOrGet("Color"));
611 "A color in RGB format with an optional alpha channel.\n"
612 "Components range from 0 to 255.\n"
613 "Alpha defaults to 255 if omitted.");
614 td.SetSize(sizeof(Color));
617 TypeDescription &td(TypeDescription::CreateOrGet("Image"));
618 td.SetDescription("Path to a PNG file with image data.");
619 td.SetSize(sizeof(SDL_Surface));
622 TypeDescription &td(TypeDescription::CreateOrGet("Number"));
623 td.SetDescription("A signed integer.");
624 td.SetSize(sizeof(int));
627 int stringId(TypeDescription::GetTypeId("String"));
628 TypeDescription &td(TypeDescription::CreateOrGet("Path"));
629 td.SetDescription("A path in the filesystem which is interpreted relative to the source file's location.");
631 td.AddSupertype(stringId, 0);
634 TypeDescription &td(TypeDescription::CreateOrGet("Script"));
635 td.SetDescription("Collection of commands that define a behaviour.");
636 td.SetSize(sizeof(Script));
639 TypeDescription &td(TypeDescription::CreateOrGet("String"));
640 td.SetDescription("Some characters.");
644 TypeDescription &td(TypeDescription::CreateOrGet("Vector"));
645 td.SetDescription("A pair of numbers usually describing a 2D translation or offset.");
646 td.SetSize(sizeof(Vector<int>));