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 std::memset(array.data, 0, array.size);
341 char *iter = reinterpret_cast<char *>(array.data);
342 if (i->second->GetLiteral().GetType() == Literal::ARRAY_PROPS) {
343 const vector<PropertyList *> &list(i->second->GetLiteral().GetPropertyLists());
344 for (vector<PropertyList *>::const_iterator
345 j(list.begin()), end(list.end());
346 j != end; ++j, iter += memberSize) {
348 if (fd.IsReferenced()) {
349 member = alloc.Alloc(fieldType.Size());
350 *reinterpret_cast<char **>(iter) = member;
354 fieldType.Construct(member);
355 ReadObject(fieldType.TypeId(), -1, member, **j);
357 } else if (i->second->GetLiteral().GetType() == Literal::ARRAY_VALUES) {
358 const vector<Value *> &list(i->second->GetLiteral().GetValues());
359 for (vector<Value *>::const_iterator j(list.begin()), end(list.end());
360 j != end; ++j, iter += memberSize) {
362 if (fd.IsReferenced()) {
363 member = alloc.Alloc(fieldType.Size());
364 *reinterpret_cast<char **>(iter) = member;
368 fieldType.Construct(member);
369 ReadLiteral(fieldType.TypeId(), -1, member, (*j)->GetLiteral());
372 if (!fd.IsReferenced()) {
373 // TODO: implement inline identifier arrays
374 throw std::runtime_error("inline identifier arrays not implemented (yet)");
376 const vector<string> &list(i->second->GetLiteral().GetIdentifiers());
377 for (vector<string>::const_iterator j(list.begin()), end(list.end());
378 j != end; ++j, iter += memberSize) {
379 if (source.IsDefined(*j)) {
380 *reinterpret_cast<void **>(iter)
381 = GetObject(fd.TypeId(), *j);
383 Postpone(reinterpret_cast<char *>(array.data),
384 iter, *j, fd.TypeId(), false);
388 size_t pLen = sizeof(char *);
389 std::memcpy(dest, &array.data, pLen);
390 dest += sizeof(char *);
391 std::memcpy(dest, &arraySize, sizeof(int));
392 } else if (i->second->IsLiteral() && !fd.IsReferenced()) {
394 if (i->second->GetLiteral().IsObject()) {
395 ReadObject(fd.TypeId(), -1, dest, *i->second->GetLiteral().GetProperties());
397 ReadLiteral(fd.TypeId(), -1, dest, i->second->GetLiteral());
400 char *src(reinterpret_cast<char *>(GetObject(fd.TypeId(), *i->second)));
401 if (fd.TypeId() == TypeDescription::GetTypeId("Image")) {
402 src = reinterpret_cast<char *>(GetImage(src));
404 if (fd.IsReferenced()) {
405 std::memcpy(dest, &src, sizeof(char *));
407 std::memcpy(dest, src, fieldType.Size());
411 Postpone(object, object + fd.Offset(), i->second->GetIdentifier(), fd.TypeId(), !fd.IsReferenced(), fd.IsAggregate());
418 void Interpreter::ReadScript(const std::vector<ScriptToken *> &s, Script *script) {
419 std::map<string, int> labels;
421 for (vector<ScriptToken *>::const_iterator i(s.begin()), end(s.end()); i != end; ++i) {
422 if ((*i)->GetType() == ScriptToken::LABEL) {
423 if (labels.count((*i)->Label())) {
424 throw Error("duplicate label " + (*i)->Label());
426 labels[(*i)->Label()] = size;
428 } else if ((*i)->GetType() != ScriptToken::COMMAND) {
429 throw Error("unexpected script token");
431 size += sizeof(Script::Code);
432 const string &cmd((*i)->CommandName());
436 throw Error("unexpected script end after move");
438 const string ®((*i)->RegisterName());
439 if (reg.size() != 2) {
440 throw Error("invalid register name " + reg);
444 throw Error("unexpected script end after move");
446 if ((*i)->GetType() != ScriptToken::REGISTER) {
449 size += sizeof(void *);
455 size += sizeof(Vector<int>);
458 throw Error("unknown register " + reg);
461 } else if (cmd == "add") {
464 throw Error("unexpected script end after add");
466 const string ®((*i)->RegisterName());
467 if (reg.size() != 2) {
468 throw Error("invalid register name " + reg);
472 throw Error("unexpected script end after add");
474 if ((*i)->GetType() != ScriptToken::REGISTER) {
480 size += sizeof(Vector<int>);
483 throw Error("expected register after add " + reg);
486 } else if (cmd == "mod") {
489 throw Error("unexpected script end after mod");
493 throw Error("unexpected script end after mod");
495 if ((*i)->GetType() != ScriptToken::REGISTER) {
498 } else if (cmd == "rand") {
501 throw Error("unexpected script end after rand");
503 } else if (cmd == "cmp") {
506 throw Error("unexpected script end after cmp");
508 if ((*i)->GetType() != ScriptToken::REGISTER) {
513 throw Error("unexpected script end after cmp");
515 if ((*i)->GetType() != ScriptToken::REGISTER) {
518 } else if (cmd == "jmp") {
522 throw Error("unexpected script end after cmp");
524 } else if (cmd == "jeq") {
528 throw Error("unexpected script end after cmp");
530 } else if (cmd == "jne") {
534 throw Error("unexpected script end after cmp");
536 } else if (cmd == "jl") {
540 throw Error("unexpected script end after cmp");
542 } else if (cmd == "jle") {
546 throw Error("unexpected script end after cmp");
548 } else if (cmd == "jg") {
552 throw Error("unexpected script end after cmp");
554 } else if (cmd == "jge") {
558 throw Error("unexpected script end after cmp");
560 } else if (cmd == "sysc") {
563 throw Error("unknown command " + cmd);
567 char *text(alloc.Alloc(size));
569 for (vector<ScriptToken *>::const_iterator i(s.begin()), end(s.end()); i != end; ++i) {
570 if ((*i)->GetType() == ScriptToken::LABEL) {
573 if ((*i)->GetType() != ScriptToken::COMMAND) {
574 throw Error("unexpected script token");
576 const string &cmd((*i)->CommandName());
578 Script::Code &code(CreateScriptCode(Script::COMMAND_MOVE, text + cursor));
579 cursor += sizeof(Script::Code);
582 const string ®((*i)->RegisterName());
585 code.type = Script::TYPE_ADDRESS;
588 code.type = Script::TYPE_INTEGER;
591 code.type = Script::TYPE_VECTOR;
594 throw Error("invalid register " + reg);
596 int regnum(reg[1] - '0');
597 if (regnum < 0 || regnum > 6) {
598 throw Error("invalid register " + reg);
603 if ((*i)->GetType() == ScriptToken::REGISTER) {
604 string reg2((*i)->RegisterName());
605 if (reg[0] != reg2[0]) {
606 throw Error("mixed-type commands not allowed");
608 int reg2num(reg[1] - '0');
609 if (reg2num < 0 || reg2num > 6) {
610 throw Error("invalid register " + reg2);
616 case Script::TYPE_ADDRESS:
617 ReadScriptAddress(**i, text + cursor);
618 cursor += sizeof(void *);
620 case Script::TYPE_INTEGER:
621 ReadScriptInteger(**i, text + cursor);
622 cursor += sizeof(int);
624 case Script::TYPE_VECTOR:
625 ReadScriptVector(**i, text + cursor);
626 cursor += sizeof(Vector<int>);
630 } else if (cmd == "add") {
631 Script::Code &code(CreateScriptCode(Script::COMMAND_ADD, text + cursor));
632 cursor += sizeof(Script::Code);
635 const string ®((*i)->RegisterName());
638 code.type = Script::TYPE_INTEGER;
641 code.type = Script::TYPE_VECTOR;
644 throw Error("invalid register " + reg);
646 int regnum(reg[1] - '0');
647 if (regnum < 0 || regnum > 6) {
648 throw Error("invalid register " + reg);
653 if ((*i)->GetType() == ScriptToken::REGISTER) {
654 string reg2((*i)->RegisterName());
655 if (reg[0] != reg2[0]) {
656 throw Error("mixed-type commands not allowed");
658 int reg2num(reg[1] - '0');
659 if (reg2num < 0 || reg2num > 6) {
660 throw Error("invalid register name " + reg2);
666 case Script::TYPE_INTEGER:
667 ReadScriptInteger(**i, text + cursor);
668 cursor += sizeof(int);
670 case Script::TYPE_VECTOR:
671 ReadScriptVector(**i, text + cursor);
672 cursor += sizeof(Vector<int>);
676 } else if (cmd == "mod") {
677 Script::Code &code(CreateScriptCode(Script::COMMAND_MOD, text + cursor));
678 cursor += sizeof(Script::Code);
681 const string ®((*i)->RegisterName());
683 throw Error("invalid register " + reg);
685 code.type = Script::TYPE_INTEGER;
686 int regnum(reg[1] - '0');
687 if (regnum < 0 || regnum > 6) {
688 throw Error("invalid register " + reg);
693 if ((*i)->GetType() == ScriptToken::REGISTER) {
694 string reg2((*i)->RegisterName());
695 if (reg[0] != reg2[0]) {
696 throw Error("mixed-type commands not allowed");
698 int reg2num(reg[1] - '0');
699 if (reg2num < 0 || reg2num > 6) {
700 throw Error("invalid register name " + reg2);
705 ReadScriptInteger(**i, text + cursor);
706 cursor += sizeof(int);
708 } else if (cmd == "rand") {
709 Script::Code &code(CreateScriptCode(Script::COMMAND_RAND, text + cursor));
710 cursor += sizeof(Script::Code);
713 const string ®((*i)->RegisterName());
715 throw Error("invalid register " + reg);
717 code.type = Script::TYPE_INTEGER;
718 int regnum(reg[1] - '0');
719 if (regnum < 0 || regnum > 6) {
720 throw Error("invalid register " + reg);
723 } else if (cmd == "cmp") {
724 Script::Code &code(CreateScriptCode(Script::COMMAND_CMP, text + cursor));
725 cursor += sizeof(Script::Code);
728 const string ®((*i)->RegisterName());
730 throw Error("invalid register " + reg);
732 code.type = Script::TYPE_INTEGER;
733 int regnum(reg[1] - '0');
734 if (regnum < 0 || regnum > 6) {
735 throw Error("invalid register " + reg);
740 if ((*i)->GetType() == ScriptToken::REGISTER) {
741 string reg2((*i)->RegisterName());
742 if (reg[0] != reg2[0]) {
743 throw Error("mixed-type commands not allowed");
745 int reg2num(reg[1] - '0');
746 if (reg2num < 0 || reg2num > 6) {
747 throw Error("invalid register name " + reg2);
752 ReadScriptInteger(**i, text + cursor);
753 cursor += sizeof(int);
755 } else if (cmd == "jmp") {
756 Script::Code &code(CreateScriptCode(Script::COMMAND_JMP, text + cursor));
757 cursor += sizeof(Script::Code);
760 if (!labels.count((*i)->Identifier())) {
761 throw Error("use of undefined label " + (*i)->Identifier());
763 *reinterpret_cast<int *>(text + cursor) = labels[(*i)->Identifier()];
764 cursor += sizeof(int);
765 } else if (cmd == "jeq") {
766 Script::Code &code(CreateScriptCode(Script::COMMAND_JEQ, text + cursor));
767 cursor += sizeof(Script::Code);
770 if (!labels.count((*i)->Identifier())) {
771 throw Error("use of undefined label " + (*i)->Identifier());
773 *reinterpret_cast<int *>(text + cursor) = labels[(*i)->Identifier()];
774 cursor += sizeof(int);
775 } else if (cmd == "jne") {
776 Script::Code &code(CreateScriptCode(Script::COMMAND_JNE, text + cursor));
777 cursor += sizeof(Script::Code);
780 if (!labels.count((*i)->Identifier())) {
781 throw Error("use of undefined label " + (*i)->Identifier());
783 *reinterpret_cast<int *>(text + cursor) = labels[(*i)->Identifier()];
784 cursor += sizeof(int);
785 } else if (cmd == "jl") {
786 Script::Code &code(CreateScriptCode(Script::COMMAND_JL, text + cursor));
787 cursor += sizeof(Script::Code);
790 if (!labels.count((*i)->Identifier())) {
791 throw Error("use of undefined label " + (*i)->Identifier());
793 *reinterpret_cast<int *>(text + cursor) = labels[(*i)->Identifier()];
794 cursor += sizeof(int);
795 } else if (cmd == "jle") {
796 Script::Code &code(CreateScriptCode(Script::COMMAND_JLE, text + cursor));
797 cursor += sizeof(Script::Code);
800 if (!labels.count((*i)->Identifier())) {
801 throw Error("use of undefined label " + (*i)->Identifier());
803 *reinterpret_cast<int *>(text + cursor) = labels[(*i)->Identifier()];
804 cursor += sizeof(int);
805 } else if (cmd == "jg") {
806 Script::Code &code(CreateScriptCode(Script::COMMAND_JG, text + cursor));
807 cursor += sizeof(Script::Code);
810 if (!labels.count((*i)->Identifier())) {
811 throw Error("use of undefined label " + (*i)->Identifier());
813 *reinterpret_cast<int *>(text + cursor) = labels[(*i)->Identifier()];
814 cursor += sizeof(int);
815 } else if (cmd == "jge") {
816 Script::Code &code(CreateScriptCode(Script::COMMAND_JGE, text + cursor));
817 cursor += sizeof(Script::Code);
820 if (!labels.count((*i)->Identifier())) {
821 throw Error("use of undefined label " + (*i)->Identifier());
823 *reinterpret_cast<int *>(text + cursor) = labels[(*i)->Identifier()];
824 cursor += sizeof(int);
825 } else if (cmd == "sysc") {
826 Script::Code &code(CreateScriptCode(Script::COMMAND_SYSC, text + cursor));
827 cursor += sizeof(Script::Code);
830 throw Error("unknown command " + cmd);
835 script->textlen = size;
838 char *Interpreter::ReadScript(const vector<ScriptToken *> &s) {
839 char *mem(alloc.Alloc(sizeof(Script)));
841 Script *script(reinterpret_cast<Script *>(mem));
842 ReadScript(s, script);
846 Script::Code &Interpreter::CreateScriptCode(Script::Command c, char *dest) {
847 Script::Code &code(*reinterpret_cast<Script::Code *>(dest));
850 code.type = Script::TYPE_NONE;
856 void Interpreter::ReadScriptAddress(const ScriptToken &t, char *dest) {
857 if (t.GetType() != ScriptToken::IDENTIFIER) {
858 throw Error("expected identifier for address");
860 if (source.IsDefined(t.Identifier())) {
861 const ParsedDefinition &def(GetDefinition(t.Identifier()));
862 void *addr(GetObject(def.type, t.Identifier()));
863 *reinterpret_cast<void **>(dest) = addr;
865 throw Error("postponing values in scripts not implemented");
869 void Interpreter::ReadScriptInteger(const ScriptToken &t, char *dest) {
870 if (t.GetType() == ScriptToken::IDENTIFIER) {
871 if (source.IsDefined(t.Identifier())) {
872 void *num(GetObject(TypeDescription::GetTypeId("Number"), t.Identifier()));
873 *reinterpret_cast<int *>(dest) = *reinterpret_cast<int *>(num);
875 throw Error("postponing values in scripts not implemented");
877 } else if (t.GetType() == ScriptToken::LITERAL) {
878 *reinterpret_cast<int *>(dest) = t.GetLiteral()->GetNumber();
880 throw Error("expected identifier or integer literal");
884 void Interpreter::ReadScriptVector(const ScriptToken &t, char *dest) {
885 if (t.GetType() == ScriptToken::IDENTIFIER) {
886 if (source.IsDefined(t.Identifier())) {
887 void *vec(GetObject(TypeDescription::GetTypeId("Vector"), t.Identifier()));
888 *reinterpret_cast<Vector<int> *>(dest) = *reinterpret_cast<Vector<int> *>(vec);
890 throw Error("postponing values in scripts not implemented");
892 } else if (t.GetType() == ScriptToken::LITERAL) {
893 *reinterpret_cast<Vector<int> *>(dest) = Vector<int>(t.GetLiteral()->GetX(), t.GetLiteral()->GetY());
895 throw Error("expected identifier or vector literal");
900 SDL_Surface *Interpreter::GetImage(const string &path) {
901 std::map<string, SDL_Surface *>::const_iterator result(imageCache.find(path));
902 if (result != imageCache.end()) {
903 return result->second;
905 SDL_Surface *image(IMG_Load(path.c_str()));
906 imageCache.insert(make_pair(path, image));
911 const string &Interpreter::FindImage(SDL_Surface *img) const {
912 for (std::map<string, SDL_Surface *>::const_iterator
913 i(imageCache.begin()), end(imageCache.end());
915 if (i->second == img) {
919 throw Error("unable to locate image");
923 bool Interpreter::CanLink(const Value &v) const {
924 return v.IsLiteral() || source.IsDefined(v.GetIdentifier());
927 void Interpreter::Postpone(
930 const std::string &identifier,
934 postponedDefinitions.push_back(
935 PostponedDefinition(object, dest, identifier, type, inlined, aggregate));
939 void Interpreter::CreateTypeDescriptions() {
941 TypeDescription &td(TypeDescription::Create(BOOLEAN_ID, "Boolean"));
942 td.SetDescription("Logical value which can be either true or false.");
943 td.SetSize(sizeof(bool));
946 TypeDescription &td(TypeDescription::Create(COLOR_ID, "Color"));
948 "A color in RGB format with an optional alpha channel.\n"
949 "Components range from 0 to 255.\n"
950 "Alpha defaults to 255 if omitted.");
951 td.SetSize(sizeof(Color));
954 TypeDescription &td(TypeDescription::Create(IMAGE_ID, "Image"));
955 td.SetDescription("Path to a PNG file with image data.");
956 td.SetSize(sizeof(SDL_Surface));
959 TypeDescription &td(TypeDescription::Create(NUMBER_ID, "Number"));
960 td.SetDescription("A signed integer.");
961 td.SetSize(sizeof(int));
964 TypeDescription &td(TypeDescription::Create(PATH_ID, "Path"));
965 td.SetDescription("A path in the filesystem which is interpreted relative to the source file's location.");
967 td.AddSupertype(STRING_ID, 0);
971 TypeDescription &td(TypeDescription::Create(SCRIPT_ID, "Script"));
972 td.SetDescription("Collection of commands that define a behaviour.");
973 td.SetSize(sizeof(Script));
974 td.AddField("text", FieldDescription(((char *)&s.text) - ((char *)&s), STRING_ID).SetAggregate());
977 TypeDescription &td(TypeDescription::Create(STRING_ID, "String"));
978 td.SetDescription("Some characters.");
982 TypeDescription &td(TypeDescription::Create(VECTOR_ID, "Vector"));
983 td.SetDescription("A pair of numbers usually describing a 2D translation or offset.");
984 td.SetSize(sizeof(Vector<int>));