1 #include "Interpreter.h"
3 #include "ParsedSource.h"
4 #include "TypeDescription.h"
5 #include "../battle/Hero.h"
6 #include "../battle/Monster.h"
7 #include "../battle/PartyLayout.h"
8 #include "../battle/Resources.h"
9 #include "../common/Ikari.h"
10 #include "../common/Item.h"
11 #include "../common/Script.h"
12 #include "../common/Spell.h"
13 #include "../common/Stats.h"
14 #include "../common/TargetingMode.h"
15 #include "../graphics/ComplexAnimation.h"
16 #include "../graphics/Font.h"
17 #include "../graphics/Frame.h"
18 #include "../graphics/Gauge.h"
19 #include "../graphics/Menu.h"
20 #include "../graphics/SimpleAnimation.h"
21 #include "../graphics/Sprite.h"
25 #include <SDL_image.h>
28 using battle::Monster;
29 using battle::PartyLayout;
35 using common::TargetingMode;
36 using graphics::Animation;
37 using graphics::Color;
39 using graphics::Frame;
40 using graphics::Gauge;
41 using graphics::ComplexAnimation;
42 using graphics::SimpleAnimation;
43 using graphics::Sprite;
52 Interpreter::~Interpreter() {
53 for (std::map<string, SDL_Surface *>::const_iterator i(imageCache.begin()), end(imageCache.end()); i != end; ++i) {
54 SDL_FreeSurface(i->second);
59 const Interpreter::ParsedDefinition &Interpreter::GetDefinition(const string &identifier) {
60 std::map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(identifier));
61 if (i != parsedDefinitions.end()) {
63 } else if (source.IsDefined(identifier)) {
64 ReadDefinition(source.GetDefinition(identifier));
65 return parsedDefinitions.at(identifier);
67 throw Error("access to undefined object " + identifier);
71 const Interpreter::ParsedDefinition &Interpreter::GetDefinition(const string &identifier) const {
72 std::map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(identifier));
73 if (i != parsedDefinitions.end()) {
76 throw Error("access to undefined object " + identifier);
81 void *Interpreter::GetObject(
83 const std::string &name) {
84 std::map<string, ParsedDefinition>::const_iterator
85 i(parsedDefinitions.find(name));
86 if (i != parsedDefinitions.end()) {
87 const TypeDescription &requested =
88 TypeDescription::Get(typeId);
89 const TypeDescription &actual =
90 TypeDescription::Get(i->second.type);
91 if (requested.TypeId() == actual.TypeId()) {
92 return values[actual.TypeId()][i->second.id];
93 } else if (actual.IsSubtypeOf(requested)) {
94 char *sub = reinterpret_cast<char *>(
95 values[actual.TypeId()][i->second.id]);
96 std::ptrdiff_t offset =
97 actual.SupertypeOffset(requested);
100 throw Error("cannot cast " + actual.TypeName()
101 + " to " + requested.TypeName());
104 throw Error("access to undefined object " + name);
108 const void *Interpreter::GetObject(
110 const std::string &name) const {
111 std::map<string, ParsedDefinition>::const_iterator
112 i(parsedDefinitions.find(name));
113 if (i != parsedDefinitions.end()) {
114 const TypeDescription &requested =
115 TypeDescription::Get(typeId);
116 const TypeDescription &actual =
117 TypeDescription::Get(i->second.type);
118 if (requested.TypeId() == actual.TypeId()) {
119 return values.at(actual.TypeId()).at(i->second.id);
120 } else if (actual.IsSubtypeOf(requested)) {
121 char *sub = reinterpret_cast<char *>(
122 values.at(actual.TypeId()).at(i->second.id));
123 std::ptrdiff_t offset =
124 actual.SupertypeOffset(requested);
127 throw Error("cannot cast " + actual.TypeName()
128 + " to " + requested.TypeName());
131 throw Error("access to undefined object " + name);
136 void Interpreter::ReadSource() {
137 for (set<string>::const_iterator i(source.Exports().begin()), end(source.Exports().end()); i != end; ++i) {
138 ReadDefinition(source.GetDefinition(*i));
142 void Interpreter::ReadDefinition(const Definition &dfn) {
143 if (parsedDefinitions.find(dfn.Identifier()) != parsedDefinitions.end()) {
146 if (dfn.HasLiteralValue()) {
153 void Interpreter::ReadLiteral(const Definition &dfn) {
154 const string &typeName(dfn.GetLiteral()->IsArray() ? "Array" : dfn.GetLiteral()->GetTypeName());
155 int typeId(TypeDescription::GetTypeId(typeName));
156 int id(values[typeId].size());
157 const TypeDescription &td(TypeDescription::Get(typeId));
159 (dfn.GetLiteral()->GetType() == Literal::PATH
160 || dfn.GetLiteral()->GetType() == Literal::STRING)
161 ? dfn.GetLiteral()->GetString().size() : td.Size());
162 char *object(alloc.Alloc(size));
163 values[typeId].push_back(object);
164 parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, typeId, id)));
165 if (dfn.GetLiteral()->GetType() == Literal::OBJECT) {
166 ReadObject(typeId, id, object, *dfn.GetLiteral()->GetProperties());
168 ReadLiteral(typeId, id, object, *dfn.GetLiteral());
172 void Interpreter::ReadLiteral(int typeId, int id, char *object, const Literal &literal) {
173 switch (literal.GetType()) {
174 case Literal::ARRAY_VALUES:
175 case Literal::ARRAY_PROPS:
176 case Literal::ARRAY_IDENTS:
177 throw Error("named arrays are not supported, sorry");
179 case Literal::BOOLEAN:
180 new (object) bool(literal.GetBoolean());
183 new (object) Color(literal.GetRed(), literal.GetGreen(), literal.GetBlue(), literal.GetAlpha());
185 case Literal::NUMBER:
186 new (object) int(literal.GetNumber());
189 std::memcpy(object, literal.GetString().c_str(), literal.GetString().size());
190 object[literal.GetString().size()] = '\0';
192 case Literal::SCRIPT:
194 ReadScript(literal.GetScript(), reinterpret_cast<Script *>(object));
196 case Literal::STRING:
197 std::memcpy(object, literal.GetString().c_str(), literal.GetString().size());
198 object[literal.GetString().size()] = '\0';
200 case Literal::VECTOR:
201 new (object) Vector<int>(literal.GetX(), literal.GetY());
203 case Literal::OBJECT:
204 throw Error("illogical branch: read literal object as non-object literal");
209 void *Interpreter::GetObject(int typeId, const Value &v) {
211 if (v.GetLiteral().IsObject()) {
212 int typeId(TypeDescription::GetTypeId(v.GetLiteral().GetTypeName()));
213 const TypeDescription &td(TypeDescription::Get(typeId));
214 char *object(alloc.Alloc(td.Size()));
215 td.Construct(object);
216 int id(values[typeId].size());
217 values[typeId].push_back(object);
218 ReadObject(typeId, id, object, *v.GetLiteral().GetProperties());
221 int typeId(0), id(0);
222 switch (v.GetLiteral().GetType()) {
223 case Literal::ARRAY_VALUES:
224 case Literal::ARRAY_PROPS:
225 case Literal::ARRAY_IDENTS:
226 throw Error("cannot copy arrays, sorry");
228 case Literal::BOOLEAN:
230 typeId = TypeDescription::GetTypeId("Boolean");
231 id = values[typeId].size();
232 const TypeDescription &td(TypeDescription::Get(typeId));
233 char *object(alloc.Alloc(td.Size()));
234 values[typeId].push_back(new (object) bool(v.GetLiteral().GetBoolean()));
239 typeId = TypeDescription::GetTypeId("Color");
240 id = values[typeId].size();
241 const TypeDescription &td(TypeDescription::Get(typeId));
242 char *object(alloc.Alloc(td.Size()));
243 values[typeId].push_back(new (object) Color(v.GetLiteral().GetRed(), v.GetLiteral().GetGreen(), v.GetLiteral().GetBlue(), v.GetLiteral().GetAlpha()));
246 case Literal::NUMBER:
248 typeId = TypeDescription::GetTypeId("Number");
249 id = values[typeId].size();
250 const TypeDescription &td(TypeDescription::Get(typeId));
251 char *object(alloc.Alloc(td.Size()));
252 values[typeId].push_back(new (object) int(v.GetLiteral().GetNumber()));
257 typeId = TypeDescription::GetTypeId("Path");
258 id = values[typeId].size();
259 char *str(alloc.Alloc(v.GetLiteral().GetString().size() + 1));
260 std::memcpy(str, v.GetLiteral().GetString().c_str(), v.GetLiteral().GetString().size());
261 str[v.GetLiteral().GetString().size()] = '\0';
262 values[typeId].push_back(str);
265 case Literal::SCRIPT:
267 typeId = TypeDescription::GetTypeId("Script");
268 char *script(ReadScript(v.GetLiteral().GetScript()));
269 id = values[typeId].size();
270 values[typeId].push_back(script);
273 case Literal::STRING:
275 typeId = TypeDescription::GetTypeId("String");
276 id = values[typeId].size();
277 char *str(alloc.Alloc(v.GetLiteral().GetString().size() + 1));
278 std::memcpy(str, v.GetLiteral().GetString().c_str(), v.GetLiteral().GetString().size());
279 str[v.GetLiteral().GetString().size()] = '\0';
280 values[typeId].push_back(str);
283 case Literal::VECTOR:
285 typeId = TypeDescription::GetTypeId("Vector");
286 id = values[typeId].size();
287 const TypeDescription &td(TypeDescription::Get(typeId));
288 char *object(alloc.Alloc(td.Size()));
289 values[typeId].push_back(new (object) Vector<int>(v.GetLiteral().GetX(), v.GetLiteral().GetY()));
292 case Literal::OBJECT:
294 typeId = TypeDescription::GetTypeId(v.GetLiteral().GetTypeName());
295 const TypeDescription &td(TypeDescription::Get(typeId));
296 id = values[typeId].size();
297 char *object(alloc.Alloc(td.Size()));
298 td.Construct(object);
299 ReadObject(typeId, id, object, *v.GetLiteral().GetProperties());
303 return values[typeId][id];
306 ReadDefinition(source.GetDefinition(v.GetIdentifier()));
307 return GetObject(typeId, v.GetIdentifier());
312 void Interpreter::ReadObject(const Definition &dfn) {
313 int typeId(TypeDescription::GetTypeId(dfn.TypeName()));
314 const TypeDescription &td(TypeDescription::Get(typeId));
315 int id(values[typeId].size());
316 char *object(alloc.Alloc(td.Size()));
317 td.Construct(object);
318 values[typeId].push_back(object);
319 parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, typeId, id)));
320 ReadObject(typeId, id, object, *dfn.GetProperties());
324 void Interpreter::ReadObject(int typeId, int id, char *object, const PropertyList &props) {
325 const TypeDescription &td(TypeDescription::Get(typeId));
326 for (PropertyList::ConstIterator i(props.Begin()), end(props.End()); i != end; ++i) {
327 const FieldDescription &fd(td.GetField(i->first));
328 const TypeDescription &fieldType(TypeDescription::Get(fd.TypeId()));
329 if (CanLink(*i->second)) {
330 char *dest(object + fd.Offset());
331 if (fd.IsAggregate()) {
332 int arraySize(i->second->GetLiteral().ArraySize());
333 size_t memberSize = fd.IsReferenced() ? sizeof(char *) : fieldType.Size();
335 array.typeId = fd.TypeId();
336 array.size = arraySize * memberSize;
337 array.data = alloc.Alloc(array.size);
338 array.ref = fd.IsReferenced();
339 arrays.push_back(array);
340 char *iter = reinterpret_cast<char *>(array.data);
341 if (i->second->GetLiteral().GetType() == Literal::ARRAY_PROPS) {
342 const vector<PropertyList *> &list(i->second->GetLiteral().GetPropertyLists());
343 for (vector<PropertyList *>::const_iterator
344 j(list.begin()), end(list.end());
345 j != end; ++j, iter += memberSize) {
347 if (fd.IsReferenced()) {
348 member = alloc.Alloc(fieldType.Size());
349 *reinterpret_cast<char **>(iter) = member;
353 fieldType.Construct(member);
354 ReadObject(fieldType.TypeId(), -1, member, **j);
356 } else if (i->second->GetLiteral().GetType() == Literal::ARRAY_VALUES) {
357 const vector<Value *> &list(i->second->GetLiteral().GetValues());
358 for (vector<Value *>::const_iterator j(list.begin()), end(list.end());
359 j != end; ++j, iter += memberSize) {
361 if (fd.IsReferenced()) {
362 member = alloc.Alloc(fieldType.Size());
363 *reinterpret_cast<char **>(iter) = member;
367 fieldType.Construct(member);
368 ReadLiteral(fieldType.TypeId(), -1, member, (*j)->GetLiteral());
371 if (!fd.IsReferenced()) {
372 // TODO: implement inline identifier arrays
373 throw std::runtime_error("inline identifier arrays not implemented (yet)");
375 const vector<string> &list(i->second->GetLiteral().GetIdentifiers());
376 for (vector<string>::const_iterator j(list.begin()), end(list.end());
377 j != end; ++j, iter += memberSize) {
378 if (source.IsDefined(*j)) {
379 *reinterpret_cast<void **>(iter)
380 = GetObject(fd.TypeId(), *j);
382 Postpone(reinterpret_cast<char *>(array.data),
383 iter, *j, fd.TypeId(), false);
387 std::memcpy(dest, &array.data, sizeof(char *));
388 dest += sizeof(char *);
389 std::memcpy(dest, &arraySize, sizeof(int));
390 } else if (i->second->IsLiteral() && !fd.IsReferenced()) {
392 if (i->second->GetLiteral().IsObject()) {
393 ReadObject(fd.TypeId(), -1, dest, *i->second->GetLiteral().GetProperties());
395 ReadLiteral(fd.TypeId(), -1, dest, i->second->GetLiteral());
398 char *src(reinterpret_cast<char *>(GetObject(fd.TypeId(), *i->second)));
399 if (fd.TypeId() == TypeDescription::GetTypeId("Image")) {
400 src = reinterpret_cast<char *>(GetImage(src));
402 if (fd.IsReferenced()) {
403 std::memcpy(dest, &src, sizeof(char *));
405 std::memcpy(dest, src, fieldType.Size());
409 Postpone(object, object + fd.Offset(), i->second->GetIdentifier(), fd.TypeId(), !fd.IsReferenced(), fd.IsAggregate());
416 void Interpreter::ReadScript(const std::vector<ScriptToken *> &s, Script *script) {
417 std::map<string, int> labels;
419 for (vector<ScriptToken *>::const_iterator i(s.begin()), end(s.end()); i != end; ++i) {
420 if ((*i)->GetType() == ScriptToken::LABEL) {
421 if (labels.count((*i)->Label())) {
422 throw Error("duplicate label " + (*i)->Label());
424 labels[(*i)->Label()] = size;
426 } else if ((*i)->GetType() != ScriptToken::COMMAND) {
427 throw Error("unexpected script token");
429 size += sizeof(Script::Code);
430 const string &cmd((*i)->CommandName());
434 throw Error("unexpected script end after move");
436 const string ®((*i)->RegisterName());
437 if (reg.size() != 2) {
438 throw Error("invalid register name " + reg);
442 throw Error("unexpected script end after move");
444 if ((*i)->GetType() != ScriptToken::REGISTER) {
447 size += sizeof(void *);
453 size += sizeof(Vector<int>);
456 throw Error("unknown register " + reg);
459 } else if (cmd == "add") {
462 throw Error("unexpected script end after add");
464 const string ®((*i)->RegisterName());
465 if (reg.size() != 2) {
466 throw Error("invalid register name " + reg);
470 throw Error("unexpected script end after add");
472 if ((*i)->GetType() != ScriptToken::REGISTER) {
478 size += sizeof(Vector<int>);
481 throw Error("expected register after add " + reg);
484 } else if (cmd == "mod") {
487 throw Error("unexpected script end after mod");
491 throw Error("unexpected script end after mod");
493 if ((*i)->GetType() != ScriptToken::REGISTER) {
496 } else if (cmd == "rand") {
499 throw Error("unexpected script end after rand");
501 } else if (cmd == "cmp") {
504 throw Error("unexpected script end after cmp");
506 if ((*i)->GetType() != ScriptToken::REGISTER) {
511 throw Error("unexpected script end after cmp");
513 if ((*i)->GetType() != ScriptToken::REGISTER) {
516 } else if (cmd == "jmp") {
520 throw Error("unexpected script end after cmp");
522 } else if (cmd == "jeq") {
526 throw Error("unexpected script end after cmp");
528 } else if (cmd == "jne") {
532 throw Error("unexpected script end after cmp");
534 } else if (cmd == "jl") {
538 throw Error("unexpected script end after cmp");
540 } else if (cmd == "jle") {
544 throw Error("unexpected script end after cmp");
546 } else if (cmd == "jg") {
550 throw Error("unexpected script end after cmp");
552 } else if (cmd == "jge") {
556 throw Error("unexpected script end after cmp");
558 } else if (cmd == "sysc") {
561 throw Error("unknown command " + cmd);
565 char *text(alloc.Alloc(size));
567 for (vector<ScriptToken *>::const_iterator i(s.begin()), end(s.end()); i != end; ++i) {
568 if ((*i)->GetType() == ScriptToken::LABEL) {
571 if ((*i)->GetType() != ScriptToken::COMMAND) {
572 throw Error("unexpected script token");
574 const string &cmd((*i)->CommandName());
576 Script::Code &code(CreateScriptCode(Script::COMMAND_MOVE, text + cursor));
577 cursor += sizeof(Script::Code);
580 const string ®((*i)->RegisterName());
583 code.type = Script::TYPE_ADDRESS;
586 code.type = Script::TYPE_INTEGER;
589 code.type = Script::TYPE_VECTOR;
592 throw Error("invalid register " + reg);
594 int regnum(reg[1] - '0');
595 if (regnum < 0 || regnum > 6) {
596 throw Error("invalid register " + reg);
601 if ((*i)->GetType() == ScriptToken::REGISTER) {
602 string reg2((*i)->RegisterName());
603 if (reg[0] != reg2[0]) {
604 throw Error("mixed-type commands not allowed");
606 int reg2num(reg[1] - '0');
607 if (reg2num < 0 || reg2num > 6) {
608 throw Error("invalid register " + reg2);
614 case Script::TYPE_ADDRESS:
615 ReadScriptAddress(**i, text + cursor);
616 cursor += sizeof(void *);
618 case Script::TYPE_INTEGER:
619 ReadScriptInteger(**i, text + cursor);
620 cursor += sizeof(int);
622 case Script::TYPE_VECTOR:
623 ReadScriptVector(**i, text + cursor);
624 cursor += sizeof(Vector<int>);
628 } else if (cmd == "add") {
629 Script::Code &code(CreateScriptCode(Script::COMMAND_ADD, text + cursor));
630 cursor += sizeof(Script::Code);
633 const string ®((*i)->RegisterName());
636 code.type = Script::TYPE_INTEGER;
639 code.type = Script::TYPE_VECTOR;
642 throw Error("invalid register " + reg);
644 int regnum(reg[1] - '0');
645 if (regnum < 0 || regnum > 6) {
646 throw Error("invalid register " + reg);
651 if ((*i)->GetType() == ScriptToken::REGISTER) {
652 string reg2((*i)->RegisterName());
653 if (reg[0] != reg2[0]) {
654 throw Error("mixed-type commands not allowed");
656 int reg2num(reg[1] - '0');
657 if (reg2num < 0 || reg2num > 6) {
658 throw Error("invalid register name " + reg2);
664 case Script::TYPE_INTEGER:
665 ReadScriptInteger(**i, text + cursor);
666 cursor += sizeof(int);
668 case Script::TYPE_VECTOR:
669 ReadScriptVector(**i, text + cursor);
670 cursor += sizeof(Vector<int>);
674 } else if (cmd == "mod") {
675 Script::Code &code(CreateScriptCode(Script::COMMAND_MOD, text + cursor));
676 cursor += sizeof(Script::Code);
679 const string ®((*i)->RegisterName());
681 throw Error("invalid register " + reg);
683 code.type = Script::TYPE_INTEGER;
684 int regnum(reg[1] - '0');
685 if (regnum < 0 || regnum > 6) {
686 throw Error("invalid register " + reg);
691 if ((*i)->GetType() == ScriptToken::REGISTER) {
692 string reg2((*i)->RegisterName());
693 if (reg[0] != reg2[0]) {
694 throw Error("mixed-type commands not allowed");
696 int reg2num(reg[1] - '0');
697 if (reg2num < 0 || reg2num > 6) {
698 throw Error("invalid register name " + reg2);
703 ReadScriptInteger(**i, text + cursor);
704 cursor += sizeof(int);
706 } else if (cmd == "rand") {
707 Script::Code &code(CreateScriptCode(Script::COMMAND_RAND, text + cursor));
708 cursor += sizeof(Script::Code);
711 const string ®((*i)->RegisterName());
713 throw Error("invalid register " + reg);
715 code.type = Script::TYPE_INTEGER;
716 int regnum(reg[1] - '0');
717 if (regnum < 0 || regnum > 6) {
718 throw Error("invalid register " + reg);
721 } else if (cmd == "cmp") {
722 Script::Code &code(CreateScriptCode(Script::COMMAND_CMP, text + cursor));
723 cursor += sizeof(Script::Code);
726 const string ®((*i)->RegisterName());
728 throw Error("invalid register " + reg);
730 code.type = Script::TYPE_INTEGER;
731 int regnum(reg[1] - '0');
732 if (regnum < 0 || regnum > 6) {
733 throw Error("invalid register " + reg);
738 if ((*i)->GetType() == ScriptToken::REGISTER) {
739 string reg2((*i)->RegisterName());
740 if (reg[0] != reg2[0]) {
741 throw Error("mixed-type commands not allowed");
743 int reg2num(reg[1] - '0');
744 if (reg2num < 0 || reg2num > 6) {
745 throw Error("invalid register name " + reg2);
750 ReadScriptInteger(**i, text + cursor);
751 cursor += sizeof(int);
753 } else if (cmd == "jmp") {
754 Script::Code &code(CreateScriptCode(Script::COMMAND_JMP, text + cursor));
755 cursor += sizeof(Script::Code);
758 if (!labels.count((*i)->Identifier())) {
759 throw Error("use of undefined label " + (*i)->Identifier());
761 *reinterpret_cast<int *>(text + cursor) = labels[(*i)->Identifier()];
762 cursor += sizeof(int);
763 } else if (cmd == "jeq") {
764 Script::Code &code(CreateScriptCode(Script::COMMAND_JEQ, text + cursor));
765 cursor += sizeof(Script::Code);
768 if (!labels.count((*i)->Identifier())) {
769 throw Error("use of undefined label " + (*i)->Identifier());
771 *reinterpret_cast<int *>(text + cursor) = labels[(*i)->Identifier()];
772 cursor += sizeof(int);
773 } else if (cmd == "jne") {
774 Script::Code &code(CreateScriptCode(Script::COMMAND_JNE, text + cursor));
775 cursor += sizeof(Script::Code);
778 if (!labels.count((*i)->Identifier())) {
779 throw Error("use of undefined label " + (*i)->Identifier());
781 *reinterpret_cast<int *>(text + cursor) = labels[(*i)->Identifier()];
782 cursor += sizeof(int);
783 } else if (cmd == "jl") {
784 Script::Code &code(CreateScriptCode(Script::COMMAND_JL, text + cursor));
785 cursor += sizeof(Script::Code);
788 if (!labels.count((*i)->Identifier())) {
789 throw Error("use of undefined label " + (*i)->Identifier());
791 *reinterpret_cast<int *>(text + cursor) = labels[(*i)->Identifier()];
792 cursor += sizeof(int);
793 } else if (cmd == "jle") {
794 Script::Code &code(CreateScriptCode(Script::COMMAND_JLE, text + cursor));
795 cursor += sizeof(Script::Code);
798 if (!labels.count((*i)->Identifier())) {
799 throw Error("use of undefined label " + (*i)->Identifier());
801 *reinterpret_cast<int *>(text + cursor) = labels[(*i)->Identifier()];
802 cursor += sizeof(int);
803 } else if (cmd == "jg") {
804 Script::Code &code(CreateScriptCode(Script::COMMAND_JG, text + cursor));
805 cursor += sizeof(Script::Code);
808 if (!labels.count((*i)->Identifier())) {
809 throw Error("use of undefined label " + (*i)->Identifier());
811 *reinterpret_cast<int *>(text + cursor) = labels[(*i)->Identifier()];
812 cursor += sizeof(int);
813 } else if (cmd == "jge") {
814 Script::Code &code(CreateScriptCode(Script::COMMAND_JGE, text + cursor));
815 cursor += sizeof(Script::Code);
818 if (!labels.count((*i)->Identifier())) {
819 throw Error("use of undefined label " + (*i)->Identifier());
821 *reinterpret_cast<int *>(text + cursor) = labels[(*i)->Identifier()];
822 cursor += sizeof(int);
823 } else if (cmd == "sysc") {
824 Script::Code &code(CreateScriptCode(Script::COMMAND_SYSC, text + cursor));
825 cursor += sizeof(Script::Code);
828 throw Error("unknown command " + cmd);
833 script->textlen = size;
836 char *Interpreter::ReadScript(const vector<ScriptToken *> &s) {
837 char *mem(alloc.Alloc(sizeof(Script)));
839 Script *script(reinterpret_cast<Script *>(mem));
840 ReadScript(s, script);
844 Script::Code &Interpreter::CreateScriptCode(Script::Command c, char *dest) {
845 Script::Code &code(*reinterpret_cast<Script::Code *>(dest));
848 code.type = Script::TYPE_NONE;
854 void Interpreter::ReadScriptAddress(const ScriptToken &t, char *dest) {
855 if (t.GetType() != ScriptToken::IDENTIFIER) {
856 throw Error("expected identifier for address");
858 if (source.IsDefined(t.Identifier())) {
859 const ParsedDefinition &def(GetDefinition(t.Identifier()));
860 void *addr(GetObject(def.type, t.Identifier()));
861 *reinterpret_cast<void **>(dest) = addr;
863 throw Error("postponing values in scripts not implemented");
867 void Interpreter::ReadScriptInteger(const ScriptToken &t, char *dest) {
868 if (t.GetType() == ScriptToken::IDENTIFIER) {
869 if (source.IsDefined(t.Identifier())) {
870 void *num(GetObject(TypeDescription::GetTypeId("Number"), t.Identifier()));
871 *reinterpret_cast<int *>(dest) = *reinterpret_cast<int *>(num);
873 throw Error("postponing values in scripts not implemented");
875 } else if (t.GetType() == ScriptToken::LITERAL) {
876 *reinterpret_cast<int *>(dest) = t.GetLiteral()->GetNumber();
878 throw Error("expected identifier or integer literal");
882 void Interpreter::ReadScriptVector(const ScriptToken &t, char *dest) {
883 if (t.GetType() == ScriptToken::IDENTIFIER) {
884 if (source.IsDefined(t.Identifier())) {
885 void *vec(GetObject(TypeDescription::GetTypeId("Vector"), t.Identifier()));
886 *reinterpret_cast<Vector<int> *>(dest) = *reinterpret_cast<Vector<int> *>(vec);
888 throw Error("postponing values in scripts not implemented");
890 } else if (t.GetType() == ScriptToken::LITERAL) {
891 *reinterpret_cast<Vector<int> *>(dest) = Vector<int>(t.GetLiteral()->GetX(), t.GetLiteral()->GetY());
893 throw Error("expected identifier or vector literal");
898 SDL_Surface *Interpreter::GetImage(const string &path) {
899 std::map<string, SDL_Surface *>::const_iterator result(imageCache.find(path));
900 if (result != imageCache.end()) {
901 return result->second;
903 SDL_Surface *image(IMG_Load(path.c_str()));
904 imageCache.insert(make_pair(path, image));
910 bool Interpreter::CanLink(const Value &v) const {
911 return v.IsLiteral() || source.IsDefined(v.GetIdentifier());
914 void Interpreter::Postpone(
917 const std::string &identifier,
921 postponedDefinitions.push_back(
922 PostponedDefinition(object, dest, identifier, type, inlined, aggregate));
926 void Interpreter::CreateTypeDescriptions() {
928 TypeDescription &td(TypeDescription::Create(BOOLEAN_ID, "Boolean"));
929 td.SetDescription("Logical value which can be either true or false.");
930 td.SetSize(sizeof(bool));
933 TypeDescription &td(TypeDescription::Create(COLOR_ID, "Color"));
935 "A color in RGB format with an optional alpha channel.\n"
936 "Components range from 0 to 255.\n"
937 "Alpha defaults to 255 if omitted.");
938 td.SetSize(sizeof(Color));
941 TypeDescription &td(TypeDescription::Create(IMAGE_ID, "Image"));
942 td.SetDescription("Path to a PNG file with image data.");
943 td.SetSize(sizeof(SDL_Surface));
946 TypeDescription &td(TypeDescription::Create(NUMBER_ID, "Number"));
947 td.SetDescription("A signed integer.");
948 td.SetSize(sizeof(int));
951 TypeDescription &td(TypeDescription::Create(PATH_ID, "Path"));
952 td.SetDescription("A path in the filesystem which is interpreted relative to the source file's location.");
954 td.AddSupertype(STRING_ID, 0);
957 TypeDescription &td(TypeDescription::Create(SCRIPT_ID, "Script"));
958 td.SetDescription("Collection of commands that define a behaviour.");
959 td.SetSize(sizeof(Script));
962 TypeDescription &td(TypeDescription::Create(STRING_ID, "String"));
963 td.SetDescription("Some characters.");
967 TypeDescription &td(TypeDescription::Create(VECTOR_ID, "Vector"));
968 td.SetDescription("A pair of numbers usually describing a 2D translation or offset.");
969 td.SetSize(sizeof(Vector<int>));