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);
63 /// Register the type descriptions of builtin types.
64 /// This should be called before any interpretation (like
65 /// calling ReadSource()) is performed.
66 static void CreateTypeDescriptions();
67 /// Represents a parsed definition with resulting type and
69 struct ParsedDefinition {
70 ParsedDefinition(const Definition *dfn, int type, int id)
71 : dfn(dfn), type(type), id(id) { }
72 const Definition *dfn;
76 /// Represents a postponed object where the type and
77 /// target location are already known, but the definition
78 /// was not accessible at interpretation time.
79 /// It could become available later or be dynamically
80 /// linked by its identifier.
81 struct PostponedDefinition {
84 const char *identifier,
89 , identifier(identifier)
92 , aggregate(aggregate) { }
94 const char *identifier;
100 /// Get all the identifiers that are marked for export
101 /// by name in the source.
102 const std::set<std::string> &ExportedIdentifiers() const {
103 return source.Exports();
105 /// Get the object definition for given identifier.
106 const ParsedDefinition &GetDefinition(const std::string &identifier);
107 /// Get all images reference by he source.
108 /// The returned map is indexed by filenames relative to
109 /// the source file and contains ready-to-blit surfaces.
110 const std::map<std::string, SDL_Surface *> &Images() const {
113 /// Get all definitions that were postponed because they
114 /// were not in the parsed source.
115 const std::vector<PostponedDefinition> &PostponedDefinitions() const {
116 return postponedDefinitions;
118 /// A map of object id to object.
119 const std::map<int, std::vector<void *> > &Values() const {
124 /// Interpret given definition.
125 void ReadDefinition(const Definition &);
126 /// Interpret given definition as a literal.
127 void ReadLiteral(const Definition &);
128 /// Interpret given definition as a complex object.
129 void ReadObject(const Definition &);
131 /// Interpret given literal as an object of given type.
132 /// The resulting object shall be accessible by given ID
133 /// (whiich is already reserved) and be written to given
135 void ReadLiteral(int typeId, int id, char *dest, const Literal &);
136 /// Get an object of given type from given value.
137 /// The object will be registere as new and be given a
139 /// Any nested objects will be created likewise.
140 void *GetObject(int typeId, const Value &value);
141 /// Read an object of given type and properties into dest.
142 /// The object should alrady have an assigned identifier.
143 /// Pass -1 as ID if the object is anonymous.
144 /// Note that anonymous objects must not contain
145 /// references as they are excluded from linking.
146 void ReadObject(int typeId, int id, char *dest, const PropertyList &);
147 /// The function that does the actual compiling when you
148 /// call ReadScript(const vector<ScriptToken *> &).
149 void ReadScript(const std::vector<ScriptToken *> &, common::Script *);
150 /// Compile a tokenized script.
151 /// Storage for the script will be allocated as neccessary.
152 char *ReadScript(const std::vector<ScriptToken *> &);
153 /// Reinterpret dest as a script code and return a
154 /// reference to that.
155 common::Script::Code &CreateScriptCode(common::Script::Command c, char *dest);
156 /// Write address to dest.
157 void ReadScriptAddress(const ScriptToken &t, char *dest);
158 /// Write integer to est.
159 void ReadScriptInteger(const ScriptToken &t, char *dest);
160 /// Write vector to dest.
161 void ReadScriptVector(const ScriptToken &t, char *dest);
163 /// Get a surface from given image path.
164 /// Load it if neccessary.
165 SDL_Surface *GetImage(const std::string &);
167 /// Check if a value can be linked right now or if linkage
168 /// must be deferred (with Postpone()).
169 bool CanLink(const Value &) const;
170 /// Defer linking of given object reference.
173 const std::string &identifier,
176 bool aggregate = false);
179 const ParsedSource &source;
181 PagedAllocator alloc;
183 std::map<std::string, ParsedDefinition> parsedDefinitions;
184 std::vector<PostponedDefinition> postponedDefinitions;
185 std::map<std::string, SDL_Surface *> imageCache;
186 std::map<int, std::vector<void *> > values;