4 * Created on: Aug 26, 2012
8 #ifndef LOADER_INTERPRETER_H_
9 #define LOADER_INTERPRETER_H_
12 #include "PagedAllocator.h"
13 #include "ParsedSource.h"
14 #include "TypeDescription.h"
15 #include "../battle/fwd.h"
16 #include "../common/fwd.h"
17 #include "../common/Script.h"
18 #include "../geometry/Vector.h"
19 #include "../graphics/Color.h"
20 #include "../graphics/ComplexAnimation.h"
21 #include "../graphics/fwd.h"
35 static const int BOOLEAN_ID = 1;
36 static const int COLOR_ID = 2;
37 static const int IMAGE_ID = 3;
38 static const int NUMBER_ID = 4;
39 static const int PATH_ID = 5;
40 static const int SCRIPT_ID = 6;
41 static const int STRING_ID = 7;
42 static const int VECTOR_ID = 8;
44 class Error: public std::runtime_error {
46 Error(const std::string &msg) : std::runtime_error("interpreter error: " + msg) { }
50 explicit Interpreter(const ParsedSource &source) : source(source), alloc(4096) { }
53 Interpreter(const Interpreter &);
54 Interpreter &operator =(const Interpreter &);
58 void *GetObject(int typeId, const std::string &name);
60 static void CreateTypeDescriptions();
61 struct ParsedDefinition {
62 ParsedDefinition(const Definition *dfn, int type, int id)
63 : dfn(dfn), type(type), id(id) { }
64 const Definition *dfn;
68 struct PostponedDefinition {
69 PostponedDefinition(int type, int id, std::ptrdiff_t offset, const char *identifier, int linkedType, bool inlined)
70 : type(type), id(id), offset(offset), identifier(identifier), linkedType(linkedType), inlined(inlined) { }
73 std::ptrdiff_t offset;
74 const char *identifier;
79 const std::set<std::string> &ExportedIdentifiers() const { return source.Exports(); }
80 const ParsedDefinition &GetDefinition(const std::string &identifier);
81 const std::map<std::string, SDL_Surface *> &Images() const { return imageCache; }
82 const std::vector<PostponedDefinition> &PostponedDefinitions() const { return postponedDefinitions; }
83 const std::map<int, std::vector<void *> > &Values() const { return values; }
86 void ReadDefinition(const Definition &);
87 void ReadLiteral(const Definition &);
88 void ReadObject(const Definition &);
90 void ReadLiteral(int typeId, int id, char *dest, const Literal &);
91 void *GetObject(int typeId, const Value &value);
92 void ReadObject(int typeId, int id, char *dest, const PropertyList &);
93 void ReadScript(const std::vector<ScriptToken *> &, common::Script *);
94 char *ReadScript(const std::vector<ScriptToken *> &);
95 common::Script::Code &CreateScriptCode(common::Script::Command c, char *dest);
96 void ReadScriptAddress(const ScriptToken &t, char *dest);
97 void ReadScriptInteger(const ScriptToken &t, char *dest);
98 void ReadScriptVector(const ScriptToken &t, char *dest);
100 SDL_Surface *GetImage(const std::string &);
102 bool CanLink(const Value &) const;
103 void Postpone(int type, int id, std::ptrdiff_t offset, const std::string &identifier, int linkedType, bool inlined);
106 const ParsedSource &source;
108 PagedAllocator alloc;
110 std::map<std::string, ParsedDefinition> parsedDefinitions;
111 std::vector<PostponedDefinition> postponedDefinitions;
112 std::map<std::string, SDL_Surface *> imageCache;
113 std::map<int, std::vector<void *> > values;
119 #endif /* LOADER_INTERPRETER_H_ */