1 #ifndef LOADER_INTERPRETER_H_
2 #define LOADER_INTERPRETER_H_
5 #include "PagedAllocator.h"
6 #include "ParsedSource.h"
7 #include "TypeDescription.h"
8 #include "../battle/fwd.h"
9 #include "../common/fwd.h"
10 #include "../common/Script.h"
11 #include "../geometry/Vector.h"
12 #include "../graphics/Color.h"
13 #include "../graphics/ComplexAnimation.h"
14 #include "../graphics/fwd.h"
28 static const int BOOLEAN_ID = 1;
29 static const int COLOR_ID = 2;
30 static const int IMAGE_ID = 3;
31 static const int NUMBER_ID = 4;
32 static const int PATH_ID = 5;
33 static const int SCRIPT_ID = 6;
34 static const int STRING_ID = 7;
35 static const int VECTOR_ID = 8;
37 class Error: public std::runtime_error {
39 Error(const std::string &msg) : std::runtime_error("interpreter error: " + msg) { }
43 explicit Interpreter(const ParsedSource &source) : source(source), alloc(4096) { }
46 Interpreter(const Interpreter &);
47 Interpreter &operator =(const Interpreter &);
51 void *GetObject(int typeId, const std::string &name);
53 static void CreateTypeDescriptions();
54 struct ParsedDefinition {
55 ParsedDefinition(const Definition *dfn, int type, int id)
56 : dfn(dfn), type(type), id(id) { }
57 const Definition *dfn;
61 struct PostponedDefinition {
62 PostponedDefinition(int type, int id, std::ptrdiff_t offset, const char *identifier, int linkedType, bool inlined)
63 : type(type), id(id), offset(offset), identifier(identifier), linkedType(linkedType), inlined(inlined) { }
66 std::ptrdiff_t offset;
67 const char *identifier;
72 const std::set<std::string> &ExportedIdentifiers() const { return source.Exports(); }
73 const ParsedDefinition &GetDefinition(const std::string &identifier);
74 const std::map<std::string, SDL_Surface *> &Images() const { return imageCache; }
75 const std::vector<PostponedDefinition> &PostponedDefinitions() const { return postponedDefinitions; }
76 const std::map<int, std::vector<void *> > &Values() const { return values; }
79 void ReadDefinition(const Definition &);
80 void ReadLiteral(const Definition &);
81 void ReadObject(const Definition &);
83 void ReadLiteral(int typeId, int id, char *dest, const Literal &);
84 void *GetObject(int typeId, const Value &value);
85 void ReadObject(int typeId, int id, char *dest, const PropertyList &);
86 void ReadScript(const std::vector<ScriptToken *> &, common::Script *);
87 char *ReadScript(const std::vector<ScriptToken *> &);
88 common::Script::Code &CreateScriptCode(common::Script::Command c, char *dest);
89 void ReadScriptAddress(const ScriptToken &t, char *dest);
90 void ReadScriptInteger(const ScriptToken &t, char *dest);
91 void ReadScriptVector(const ScriptToken &t, char *dest);
93 SDL_Surface *GetImage(const std::string &);
95 bool CanLink(const Value &) const;
96 void Postpone(int type, int id, std::ptrdiff_t offset, const std::string &identifier, int linkedType, bool inlined);
99 const ParsedSource &source;
101 PagedAllocator alloc;
103 std::map<std::string, ParsedDefinition> parsedDefinitions;
104 std::vector<PostponedDefinition> postponedDefinitions;
105 std::map<std::string, SDL_Surface *> imageCache;
106 std::map<int, std::vector<void *> > values;
112 #endif /* LOADER_INTERPRETER_H_ */