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 /// Instances of this class are thrown if an
30 /// interpretation error is encounteres such as a
31 /// reference to an undeclared or inlining of an undefined
33 class Error: public std::runtime_error {
35 Error(const std::string &msg)
36 : std::runtime_error("interpreter error: " + msg) { }
40 /// Create an interpreter that rads from given parsed
42 /// After creation you should call ReadSource() before
43 /// querying e.g. with GetObject().
44 explicit Interpreter(const ParsedSource &source)
45 : source(source), alloc(4096) { }
48 Interpreter(const Interpreter &);
49 Interpreter &operator =(const Interpreter &);
52 /// Interpret all exported definitions of the underlying
55 /// Get an object of given type that is defined by given
57 /// If the object identified by name is of another type,
58 /// a cast is performed for compatible types.
59 /// Error is thrown if the object is undefined or a cast
61 void *GetObject(int typeId, const std::string &name);
62 const void *GetObject(int typeId, const std::string &name) const;
64 /// Register the type descriptions of builtin types.
65 /// This should be called before any interpretation (like
66 /// calling ReadSource()) is performed.
67 static void CreateTypeDescriptions();
68 /// Represents a parsed definition with resulting type and
70 struct ParsedDefinition {
71 ParsedDefinition(const Definition *dfn, int type, int id)
72 : dfn(dfn), type(type), id(id) { }
73 const Definition *dfn;
77 /// Represents a postponed object where the type and
78 /// target location are already known, but the definition
79 /// was not accessible at interpretation time.
80 /// It could become available later or be dynamically
81 /// linked by its identifier.
82 struct PostponedDefinition {
86 const std::string &identifier,
92 , identifier(identifier)
95 , aggregate(aggregate) { }
98 std::string identifier;
104 /// Get all the identifiers that are marked for export
105 /// by name in the source.
106 const std::set<std::string> &ExportedIdentifiers() const {
107 return source.Exports();
109 /// Get the object definition for given identifier.
110 const ParsedDefinition &GetDefinition(const std::string &identifier);
111 const ParsedDefinition &GetDefinition(const std::string &identifier) const;
112 /// Get all images reference by he source.
113 /// The returned map is indexed by filenames relative to
114 /// the source file and contains ready-to-blit surfaces.
115 const std::map<std::string, SDL_Surface *> &Images() const {
118 const std::string &FindImage(SDL_Surface *) const;
119 /// Get all definitions that were postponed because they
120 /// were not in the parsed source.
121 const std::vector<PostponedDefinition> &PostponedDefinitions() const {
122 return postponedDefinitions;
124 /// A map of object id to object.
125 const std::map<int, std::vector<void *> > &Values() const {
134 const std::vector<Array> &Arrays() const {
139 /// Interpret given definition.
140 void ReadDefinition(const Definition &);
141 /// Interpret given definition as a literal.
142 void ReadLiteral(const Definition &);
143 /// Interpret given definition as a complex object.
144 void ReadObject(const Definition &);
146 /// Interpret given literal as an object of given type.
147 /// The resulting object shall be accessible by given ID
148 /// (whiich is already reserved) and be written to given
150 void ReadLiteral(int typeId, int id, char *dest, const Literal &);
151 /// Get an object of given type from given value.
152 /// The object will be registere as new and be given a
154 /// Any nested objects will be created likewise.
155 void *GetObject(int typeId, const Value &value);
156 /// Read an object of given type and properties into dest.
157 /// The object should alrady have an assigned identifier.
158 /// Pass -1 as ID if the object is anonymous.
159 /// Note that anonymous objects must not contain
160 /// references as they are excluded from linking.
161 void ReadObject(int typeId, int id, char *dest, const PropertyList &);
162 /// The function that does the actual compiling when you
163 /// call ReadScript(const vector<ScriptToken *> &).
164 void ReadScript(const std::vector<ScriptToken *> &, common::Script *);
165 /// Compile a tokenized script.
166 /// Storage for the script will be allocated as neccessary.
167 char *ReadScript(const std::vector<ScriptToken *> &);
168 /// Reinterpret dest as a script code and return a
169 /// reference to that.
170 common::Script::Code &CreateScriptCode(common::Script::Command c, char *dest);
171 /// Write address to dest.
172 void ReadScriptAddress(const ScriptToken &t, char *dest);
173 /// Write integer to est.
174 void ReadScriptInteger(const ScriptToken &t, char *dest);
175 /// Write vector to dest.
176 void ReadScriptVector(const ScriptToken &t, char *dest);
178 /// Get a surface from given image path.
179 /// Load it if neccessary.
180 SDL_Surface *GetImage(const std::string &);
182 /// Check if a value can be linked right now or if linkage
183 /// must be deferred (with Postpone()).
184 bool CanLink(const Value &) const;
185 /// Defer linking of given object reference.
189 const std::string &identifier,
192 bool aggregate = false);
195 const ParsedSource &source;
197 PagedAllocator alloc;
199 std::map<std::string, ParsedDefinition> parsedDefinitions;
200 std::vector<PostponedDefinition> postponedDefinitions;
201 std::map<std::string, SDL_Surface *> imageCache;
202 std::map<int, std::vector<void *> > values;
203 std::vector<Array> arrays;