1 #ifndef LOADER_INTERPRETER_H_
2 #define LOADER_INTERPRETER_H_
4 #include "PagedAllocator.h"
5 #include "ParsedSource.h"
6 #include "../common/Script.h"
20 static const int BOOLEAN_ID = 1;
21 static const int COLOR_ID = 2;
22 static const int IMAGE_ID = 3;
23 static const int NUMBER_ID = 4;
24 static const int PATH_ID = 5;
25 static const int SCRIPT_ID = 6;
26 static const int STRING_ID = 7;
27 static const int VECTOR_ID = 8;
29 class Error: public std::runtime_error {
31 Error(const std::string &msg) : std::runtime_error("interpreter error: " + msg) { }
35 explicit Interpreter(const ParsedSource &source) : source(source), alloc(4096) { }
38 Interpreter(const Interpreter &);
39 Interpreter &operator =(const Interpreter &);
43 void *GetObject(int typeId, const std::string &name);
45 static void CreateTypeDescriptions();
46 struct ParsedDefinition {
47 ParsedDefinition(const Definition *dfn, int type, int id)
48 : dfn(dfn), type(type), id(id) { }
49 const Definition *dfn;
53 struct PostponedDefinition {
54 PostponedDefinition(int type, int id, std::ptrdiff_t offset, const char *identifier, int linkedType, bool inlined)
55 : type(type), id(id), offset(offset), identifier(identifier), linkedType(linkedType), inlined(inlined) { }
58 std::ptrdiff_t offset;
59 const char *identifier;
64 const std::set<std::string> &ExportedIdentifiers() const { return source.Exports(); }
65 const ParsedDefinition &GetDefinition(const std::string &identifier);
66 const std::map<std::string, SDL_Surface *> &Images() const { return imageCache; }
67 const std::vector<PostponedDefinition> &PostponedDefinitions() const { return postponedDefinitions; }
68 const std::map<int, std::vector<void *> > &Values() const { return values; }
71 void ReadDefinition(const Definition &);
72 void ReadLiteral(const Definition &);
73 void ReadObject(const Definition &);
75 void ReadLiteral(int typeId, int id, char *dest, const Literal &);
76 void *GetObject(int typeId, const Value &value);
77 void ReadObject(int typeId, int id, char *dest, const PropertyList &);
78 void ReadScript(const std::vector<ScriptToken *> &, common::Script *);
79 char *ReadScript(const std::vector<ScriptToken *> &);
80 common::Script::Code &CreateScriptCode(common::Script::Command c, char *dest);
81 void ReadScriptAddress(const ScriptToken &t, char *dest);
82 void ReadScriptInteger(const ScriptToken &t, char *dest);
83 void ReadScriptVector(const ScriptToken &t, char *dest);
85 SDL_Surface *GetImage(const std::string &);
87 bool CanLink(const Value &) const;
88 void Postpone(int type, int id, std::ptrdiff_t offset, const std::string &identifier, int linkedType, bool inlined);
91 const ParsedSource &source;
95 std::map<std::string, ParsedDefinition> parsedDefinitions;
96 std::vector<PostponedDefinition> postponedDefinitions;
97 std::map<std::string, SDL_Surface *> imageCache;
98 std::map<int, std::vector<void *> > values;