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);
72 void *Interpreter::GetObject(int typeId, const std::string &name) {
73 std::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 const string &typeName(dfn.GetLiteral()->IsArray() ? "Array" : dfn.GetLiteral()->GetTypeName());
111 int typeId(TypeDescription::GetTypeId(typeName));
112 int id(values[typeId].size());
113 const TypeDescription &td(TypeDescription::Get(typeId));
115 (dfn.GetLiteral()->GetType() == Literal::PATH
116 || dfn.GetLiteral()->GetType() == Literal::STRING)
117 ? dfn.GetLiteral()->GetString().size() : td.Size());
118 char *object(alloc.Alloc(size));
119 values[typeId].push_back(object);
120 parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, typeId, id)));
121 if (dfn.GetLiteral()->GetType() == Literal::OBJECT) {
122 ReadObject(typeId, id, object, *dfn.GetLiteral()->GetProperties());
124 ReadLiteral(typeId, id, object, *dfn.GetLiteral());
128 void Interpreter::ReadLiteral(int typeId, int id, char *object, const Literal &literal) {
129 switch (literal.GetType()) {
130 case Literal::ARRAY_VALUES:
131 case Literal::ARRAY_PROPS:
132 case Literal::ARRAY_IDENTS:
133 throw Error("named arrays are not supported, sorry");
135 case Literal::BOOLEAN:
136 new (object) bool(literal.GetBoolean());
139 new (object) Color(literal.GetRed(), literal.GetGreen(), literal.GetBlue(), literal.GetAlpha());
141 case Literal::NUMBER:
142 new (object) int(literal.GetNumber());
145 std::memcpy(object, literal.GetString().c_str(), literal.GetString().size());
146 object[literal.GetString().size()] = '\0';
148 case Literal::SCRIPT:
150 ReadScript(literal.GetScript(), reinterpret_cast<Script *>(object));
152 case Literal::STRING:
153 std::memcpy(object, literal.GetString().c_str(), literal.GetString().size());
154 object[literal.GetString().size()] = '\0';
156 case Literal::VECTOR:
157 new (object) Vector<int>(literal.GetX(), literal.GetY());
159 case Literal::OBJECT:
160 throw Error("illogical branch: read literal object as non-object literal");
165 void *Interpreter::GetObject(int typeId, const Value &v) {
167 if (v.GetLiteral().IsObject()) {
168 int typeId(TypeDescription::GetTypeId(v.GetLiteral().GetTypeName()));
169 const TypeDescription &td(TypeDescription::Get(typeId));
170 char *object(alloc.Alloc(td.Size()));
171 td.Construct(object);
172 int id(values[typeId].size());
173 values[typeId].push_back(object);
174 ReadObject(typeId, id, object, *v.GetLiteral().GetProperties());
177 int typeId(0), id(0);
178 switch (v.GetLiteral().GetType()) {
179 case Literal::ARRAY_VALUES:
180 case Literal::ARRAY_PROPS:
181 case Literal::ARRAY_IDENTS:
182 throw Error("cannot copy 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 char *script(ReadScript(v.GetLiteral().GetScript()));
225 id = values[typeId].size();
226 values[typeId].push_back(script);
229 case Literal::STRING:
231 typeId = TypeDescription::GetTypeId("String");
232 id = values[typeId].size();
233 char *str(alloc.Alloc(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(alloc.Alloc(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(alloc.Alloc(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(alloc.Alloc(td.Size()));
273 td.Construct(object);
274 values[typeId].push_back(object);
275 parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, typeId, id)));
276 ReadObject(typeId, id, object, *dfn.GetProperties());
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 int arraySize(i->second->GetLiteral().ArraySize());
289 size_t memberSize = fd.IsReferenced() ? sizeof(char *) : fieldType.Size();
290 char *aggregate = alloc.Alloc(arraySize * memberSize);
291 char *iter = aggregate;
292 if (i->second->GetLiteral().GetType() == Literal::ARRAY_PROPS) {
293 const vector<PropertyList *> &list(i->second->GetLiteral().GetPropertyLists());
294 for (vector<PropertyList *>::const_iterator j(list.begin()), end(list.end());
295 j != end; ++j, iter += memberSize) {
297 if (fd.IsReferenced()) {
298 member = alloc.Alloc(fieldType.Size());
299 *reinterpret_cast<char **>(iter) = member;
303 fieldType.Construct(member);
304 ReadObject(fieldType.TypeId(), -1, member, **j);
306 } else if (i->second->GetLiteral().GetType() == Literal::ARRAY_VALUES) {
307 const vector<Value *> &list(i->second->GetLiteral().GetValues());
308 for (vector<Value *>::const_iterator j(list.begin()), end(list.end());
309 j != end; ++j, iter += memberSize) {
311 if (fd.IsReferenced()) {
312 member = alloc.Alloc(fieldType.Size());
313 *reinterpret_cast<char **>(iter) = member;
317 fieldType.Construct(member);
318 ReadLiteral(fieldType.TypeId(), -1, member, (*j)->GetLiteral());
321 if (!fd.IsReferenced()) {
322 // TODO: implement inline identifier arrays
323 throw std::runtime_error("inline identifier arrays not implemented (yet)");
325 const vector<string> &list(i->second->GetLiteral().GetIdentifiers());
326 for (vector<string>::const_iterator j(list.begin()), end(list.end());
327 j != end; ++j, iter += memberSize) {
328 if (source.IsDefined(*j)) {
329 *reinterpret_cast<void **>(iter)
330 = GetObject(fd.TypeId(), *j);
332 Postpone(iter, *j, fd.TypeId(), false);
336 std::memcpy(dest, &aggregate, sizeof(char *));
337 dest += sizeof(char *);
338 std::memcpy(dest, &arraySize, sizeof(int));
339 } else if (i->second->IsLiteral() && !fd.IsReferenced()) {
341 if (i->second->GetLiteral().IsObject()) {
342 ReadObject(fd.TypeId(), -1, dest, *i->second->GetLiteral().GetProperties());
344 ReadLiteral(fd.TypeId(), -1, dest, i->second->GetLiteral());
347 char *src(reinterpret_cast<char *>(GetObject(fd.TypeId(), *i->second)));
348 if (fd.TypeId() == TypeDescription::GetTypeId("Image")) {
349 src = reinterpret_cast<char *>(GetImage(src));
351 if (fd.IsReferenced()) {
352 std::memcpy(dest, &src, sizeof(char *));
354 std::memcpy(dest, src, fieldType.Size());
358 Postpone(object, i->second->GetIdentifier(), fd.TypeId(), !fd.IsReferenced(), fd.IsAggregate());
365 void Interpreter::ReadScript(const std::vector<ScriptToken *> &s, Script *script) {
366 std::map<string, int> labels;
368 for (vector<ScriptToken *>::const_iterator i(s.begin()), end(s.end()); i != end; ++i) {
369 if ((*i)->GetType() == ScriptToken::LABEL) {
370 if (labels.count((*i)->Label())) {
371 throw Error("duplicate label " + (*i)->Label());
373 labels[(*i)->Label()] = size;
375 } else if ((*i)->GetType() != ScriptToken::COMMAND) {
376 throw Error("unexpected script token");
378 size += sizeof(Script::Code);
379 const string &cmd((*i)->CommandName());
383 throw Error("unexpected script end after move");
385 const string ®((*i)->RegisterName());
386 if (reg.size() != 2) {
387 throw Error("invalid register name " + reg);
391 throw Error("unexpected script end after move");
393 if ((*i)->GetType() != ScriptToken::REGISTER) {
396 size += sizeof(void *);
402 size += sizeof(Vector<int>);
405 throw Error("unknown register " + reg);
408 } else if (cmd == "add") {
411 throw Error("unexpected script end after add");
413 const string ®((*i)->RegisterName());
414 if (reg.size() != 2) {
415 throw Error("invalid register name " + reg);
419 throw Error("unexpected script end after add");
421 if ((*i)->GetType() != ScriptToken::REGISTER) {
427 size += sizeof(Vector<int>);
430 throw Error("expected register after add " + reg);
433 } else if (cmd == "mod") {
436 throw Error("unexpected script end after mod");
440 throw Error("unexpected script end after mod");
442 if ((*i)->GetType() != ScriptToken::REGISTER) {
445 } else if (cmd == "rand") {
448 throw Error("unexpected script end after rand");
450 } else if (cmd == "cmp") {
453 throw Error("unexpected script end after cmp");
455 if ((*i)->GetType() != ScriptToken::REGISTER) {
460 throw Error("unexpected script end after cmp");
462 if ((*i)->GetType() != ScriptToken::REGISTER) {
465 } else if (cmd == "jmp") {
469 throw Error("unexpected script end after cmp");
471 } else if (cmd == "jeq") {
475 throw Error("unexpected script end after cmp");
477 } else if (cmd == "jne") {
481 throw Error("unexpected script end after cmp");
483 } else if (cmd == "jl") {
487 throw Error("unexpected script end after cmp");
489 } else if (cmd == "jle") {
493 throw Error("unexpected script end after cmp");
495 } else if (cmd == "jg") {
499 throw Error("unexpected script end after cmp");
501 } else if (cmd == "jge") {
505 throw Error("unexpected script end after cmp");
507 } else if (cmd == "sysc") {
510 throw Error("unknown command " + cmd);
514 char *text(alloc.Alloc(size));
516 for (vector<ScriptToken *>::const_iterator i(s.begin()), end(s.end()); i != end; ++i) {
517 if ((*i)->GetType() == ScriptToken::LABEL) {
520 if ((*i)->GetType() != ScriptToken::COMMAND) {
521 throw Error("unexpected script token");
523 const string &cmd((*i)->CommandName());
525 Script::Code &code(CreateScriptCode(Script::COMMAND_MOVE, text + cursor));
526 cursor += sizeof(Script::Code);
529 const string ®((*i)->RegisterName());
532 code.type = Script::TYPE_ADDRESS;
535 code.type = Script::TYPE_INTEGER;
538 code.type = Script::TYPE_VECTOR;
541 throw Error("invalid register " + reg);
543 int regnum(reg[1] - '0');
544 if (regnum < 0 || regnum > 6) {
545 throw Error("invalid register " + reg);
550 if ((*i)->GetType() == ScriptToken::REGISTER) {
551 string reg2((*i)->RegisterName());
552 if (reg[0] != reg2[0]) {
553 throw Error("mixed-type commands not allowed");
555 int reg2num(reg[1] - '0');
556 if (reg2num < 0 || reg2num > 6) {
557 throw Error("invalid register " + reg2);
563 case Script::TYPE_ADDRESS:
564 ReadScriptAddress(**i, text + cursor);
565 cursor += sizeof(void *);
567 case Script::TYPE_INTEGER:
568 ReadScriptInteger(**i, text + cursor);
569 cursor += sizeof(int);
571 case Script::TYPE_VECTOR:
572 ReadScriptVector(**i, text + cursor);
573 cursor += sizeof(Vector<int>);
577 } else if (cmd == "add") {
578 Script::Code &code(CreateScriptCode(Script::COMMAND_ADD, text + cursor));
579 cursor += sizeof(Script::Code);
582 const string ®((*i)->RegisterName());
585 code.type = Script::TYPE_INTEGER;
588 code.type = Script::TYPE_VECTOR;
591 throw Error("invalid register " + reg);
593 int regnum(reg[1] - '0');
594 if (regnum < 0 || regnum > 6) {
595 throw Error("invalid register " + reg);
600 if ((*i)->GetType() == ScriptToken::REGISTER) {
601 string reg2((*i)->RegisterName());
602 if (reg[0] != reg2[0]) {
603 throw Error("mixed-type commands not allowed");
605 int reg2num(reg[1] - '0');
606 if (reg2num < 0 || reg2num > 6) {
607 throw Error("invalid register name " + reg2);
613 case Script::TYPE_INTEGER:
614 ReadScriptInteger(**i, text + cursor);
615 cursor += sizeof(int);
617 case Script::TYPE_VECTOR:
618 ReadScriptVector(**i, text + cursor);
619 cursor += sizeof(Vector<int>);
623 } else if (cmd == "mod") {
624 Script::Code &code(CreateScriptCode(Script::COMMAND_MOD, text + cursor));
625 cursor += sizeof(Script::Code);
628 const string ®((*i)->RegisterName());
630 throw Error("invalid register " + reg);
632 code.type = Script::TYPE_INTEGER;
633 int regnum(reg[1] - '0');
634 if (regnum < 0 || regnum > 6) {
635 throw Error("invalid register " + reg);
640 if ((*i)->GetType() == ScriptToken::REGISTER) {
641 string reg2((*i)->RegisterName());
642 if (reg[0] != reg2[0]) {
643 throw Error("mixed-type commands not allowed");
645 int reg2num(reg[1] - '0');
646 if (reg2num < 0 || reg2num > 6) {
647 throw Error("invalid register name " + reg2);
652 ReadScriptInteger(**i, text + cursor);
653 cursor += sizeof(int);
655 } else if (cmd == "rand") {
656 Script::Code &code(CreateScriptCode(Script::COMMAND_RAND, text + cursor));
657 cursor += sizeof(Script::Code);
660 const string ®((*i)->RegisterName());
662 throw Error("invalid register " + reg);
664 code.type = Script::TYPE_INTEGER;
665 int regnum(reg[1] - '0');
666 if (regnum < 0 || regnum > 6) {
667 throw Error("invalid register " + reg);
670 } else if (cmd == "cmp") {
671 Script::Code &code(CreateScriptCode(Script::COMMAND_CMP, text + cursor));
672 cursor += sizeof(Script::Code);
675 const string ®((*i)->RegisterName());
677 throw Error("invalid register " + reg);
679 code.type = Script::TYPE_INTEGER;
680 int regnum(reg[1] - '0');
681 if (regnum < 0 || regnum > 6) {
682 throw Error("invalid register " + reg);
687 if ((*i)->GetType() == ScriptToken::REGISTER) {
688 string reg2((*i)->RegisterName());
689 if (reg[0] != reg2[0]) {
690 throw Error("mixed-type commands not allowed");
692 int reg2num(reg[1] - '0');
693 if (reg2num < 0 || reg2num > 6) {
694 throw Error("invalid register name " + reg2);
699 ReadScriptInteger(**i, text + cursor);
700 cursor += sizeof(int);
702 } else if (cmd == "jmp") {
703 Script::Code &code(CreateScriptCode(Script::COMMAND_JMP, text + cursor));
704 cursor += sizeof(Script::Code);
707 if (!labels.count((*i)->Identifier())) {
708 throw Error("use of undefined label " + (*i)->Identifier());
710 *reinterpret_cast<int *>(text + cursor) = labels[(*i)->Identifier()];
711 cursor += sizeof(int);
712 } else if (cmd == "jeq") {
713 Script::Code &code(CreateScriptCode(Script::COMMAND_JEQ, text + cursor));
714 cursor += sizeof(Script::Code);
717 if (!labels.count((*i)->Identifier())) {
718 throw Error("use of undefined label " + (*i)->Identifier());
720 *reinterpret_cast<int *>(text + cursor) = labels[(*i)->Identifier()];
721 cursor += sizeof(int);
722 } else if (cmd == "jne") {
723 Script::Code &code(CreateScriptCode(Script::COMMAND_JNE, text + cursor));
724 cursor += sizeof(Script::Code);
727 if (!labels.count((*i)->Identifier())) {
728 throw Error("use of undefined label " + (*i)->Identifier());
730 *reinterpret_cast<int *>(text + cursor) = labels[(*i)->Identifier()];
731 cursor += sizeof(int);
732 } else if (cmd == "jl") {
733 Script::Code &code(CreateScriptCode(Script::COMMAND_JL, text + cursor));
734 cursor += sizeof(Script::Code);
737 if (!labels.count((*i)->Identifier())) {
738 throw Error("use of undefined label " + (*i)->Identifier());
740 *reinterpret_cast<int *>(text + cursor) = labels[(*i)->Identifier()];
741 cursor += sizeof(int);
742 } else if (cmd == "jle") {
743 Script::Code &code(CreateScriptCode(Script::COMMAND_JLE, text + cursor));
744 cursor += sizeof(Script::Code);
747 if (!labels.count((*i)->Identifier())) {
748 throw Error("use of undefined label " + (*i)->Identifier());
750 *reinterpret_cast<int *>(text + cursor) = labels[(*i)->Identifier()];
751 cursor += sizeof(int);
752 } else if (cmd == "jg") {
753 Script::Code &code(CreateScriptCode(Script::COMMAND_JG, text + cursor));
754 cursor += sizeof(Script::Code);
757 if (!labels.count((*i)->Identifier())) {
758 throw Error("use of undefined label " + (*i)->Identifier());
760 *reinterpret_cast<int *>(text + cursor) = labels[(*i)->Identifier()];
761 cursor += sizeof(int);
762 } else if (cmd == "jge") {
763 Script::Code &code(CreateScriptCode(Script::COMMAND_JGE, text + cursor));
764 cursor += sizeof(Script::Code);
767 if (!labels.count((*i)->Identifier())) {
768 throw Error("use of undefined label " + (*i)->Identifier());
770 *reinterpret_cast<int *>(text + cursor) = labels[(*i)->Identifier()];
771 cursor += sizeof(int);
772 } else if (cmd == "sysc") {
773 Script::Code &code(CreateScriptCode(Script::COMMAND_SYSC, text + cursor));
774 cursor += sizeof(Script::Code);
777 throw Error("unknown command " + cmd);
782 script->textlen = size;
785 char *Interpreter::ReadScript(const vector<ScriptToken *> &s) {
786 char *mem(alloc.Alloc(sizeof(Script)));
788 Script *script(reinterpret_cast<Script *>(mem));
789 ReadScript(s, script);
793 Script::Code &Interpreter::CreateScriptCode(Script::Command c, char *dest) {
794 Script::Code &code(*reinterpret_cast<Script::Code *>(dest));
797 code.type = Script::TYPE_NONE;
803 void Interpreter::ReadScriptAddress(const ScriptToken &t, char *dest) {
804 if (t.GetType() != ScriptToken::IDENTIFIER) {
805 throw Error("expected identifier for address");
807 if (source.IsDefined(t.Identifier())) {
808 const ParsedDefinition &def(GetDefinition(t.Identifier()));
809 void *addr(GetObject(def.type, t.Identifier()));
810 *reinterpret_cast<void **>(dest) = addr;
812 throw Error("postponing values in scripts not implemented");
816 void Interpreter::ReadScriptInteger(const ScriptToken &t, char *dest) {
817 if (t.GetType() == ScriptToken::IDENTIFIER) {
818 if (source.IsDefined(t.Identifier())) {
819 void *num(GetObject(TypeDescription::GetTypeId("Number"), t.Identifier()));
820 *reinterpret_cast<int *>(dest) = *reinterpret_cast<int *>(num);
822 throw Error("postponing values in scripts not implemented");
824 } else if (t.GetType() == ScriptToken::LITERAL) {
825 *reinterpret_cast<int *>(dest) = t.GetLiteral()->GetNumber();
827 throw Error("expected identifier or integer literal");
831 void Interpreter::ReadScriptVector(const ScriptToken &t, char *dest) {
832 if (t.GetType() == ScriptToken::IDENTIFIER) {
833 if (source.IsDefined(t.Identifier())) {
834 void *vec(GetObject(TypeDescription::GetTypeId("Vector"), t.Identifier()));
835 *reinterpret_cast<Vector<int> *>(dest) = *reinterpret_cast<Vector<int> *>(vec);
837 throw Error("postponing values in scripts not implemented");
839 } else if (t.GetType() == ScriptToken::LITERAL) {
840 *reinterpret_cast<Vector<int> *>(dest) = Vector<int>(t.GetLiteral()->GetX(), t.GetLiteral()->GetY());
842 throw Error("expected identifier or vector literal");
847 SDL_Surface *Interpreter::GetImage(const string &path) {
848 std::map<string, SDL_Surface *>::const_iterator result(imageCache.find(path));
849 if (result != imageCache.end()) {
850 return result->second;
852 SDL_Surface *image(IMG_Load(path.c_str()));
853 imageCache.insert(make_pair(path, image));
859 bool Interpreter::CanLink(const Value &v) const {
860 return v.IsLiteral() || source.IsDefined(v.GetIdentifier());
863 void Interpreter::Postpone(
865 const std::string &identifier,
869 char *str(alloc.Alloc(identifier.size() + 1));
870 std::memcpy(str, identifier.c_str(), identifier.size());
871 str[identifier.size()] = '\0';
872 postponedDefinitions.push_back(
873 PostponedDefinition(dest, str, type, inlined, aggregate));
877 void Interpreter::CreateTypeDescriptions() {
879 TypeDescription &td(TypeDescription::Create(BOOLEAN_ID, "Boolean"));
880 td.SetDescription("Logical value which can be either true or false.");
881 td.SetSize(sizeof(bool));
884 TypeDescription &td(TypeDescription::Create(COLOR_ID, "Color"));
886 "A color in RGB format with an optional alpha channel.\n"
887 "Components range from 0 to 255.\n"
888 "Alpha defaults to 255 if omitted.");
889 td.SetSize(sizeof(Color));
892 TypeDescription &td(TypeDescription::Create(IMAGE_ID, "Image"));
893 td.SetDescription("Path to a PNG file with image data.");
894 td.SetSize(sizeof(SDL_Surface));
897 TypeDescription &td(TypeDescription::Create(NUMBER_ID, "Number"));
898 td.SetDescription("A signed integer.");
899 td.SetSize(sizeof(int));
902 TypeDescription &td(TypeDescription::Create(PATH_ID, "Path"));
903 td.SetDescription("A path in the filesystem which is interpreted relative to the source file's location.");
905 td.AddSupertype(STRING_ID, 0);
908 TypeDescription &td(TypeDescription::Create(SCRIPT_ID, "Script"));
909 td.SetDescription("Collection of commands that define a behaviour.");
910 td.SetSize(sizeof(Script));
913 TypeDescription &td(TypeDescription::Create(STRING_ID, "String"));
914 td.SetDescription("Some characters.");
918 TypeDescription &td(TypeDescription::Create(VECTOR_ID, "Vector"));
919 td.SetDescription("A pair of numbers usually describing a 2D translation or offset.");
920 td.SetSize(sizeof(Vector<int>));