]> git.localhorst.tv Git - l2e.git/blob - src/loader/Interpreter.h
7eccab344bdc105cd50ddda63f65c80bb6c72690
[l2e.git] / src / loader / Interpreter.h
1 #ifndef LOADER_INTERPRETER_H_
2 #define LOADER_INTERPRETER_H_
3
4 #include "PagedAllocator.h"
5 #include "ParsedSource.h"
6 #include "../common/Script.h"
7
8 #include <map>
9 #include <set>
10 #include <stdexcept>
11 #include <string>
12 #include <vector>
13 #include <SDL.h>
14
15 namespace loader {
16
17 class Interpreter {
18
19 public:
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;
28
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
32         /// object.
33         class Error: public std::runtime_error {
34         public:
35                 Error(const std::string &msg)
36                 : std::runtime_error("interpreter error: " + msg) { }
37         };
38
39 public:
40         /// Create an interpreter that rads from given parsed
41         /// source.
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) { }
46         ~Interpreter();
47 private:
48         Interpreter(const Interpreter &);
49         Interpreter &operator =(const Interpreter &);
50
51 public:
52         /// Interpret all exported definitions of the underlying
53         /// source file.
54         void ReadSource();
55         /// Get an object of given type that is defined by given
56         /// name.
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
60         /// was unsuccessful.
61         void *GetObject(int typeId, const std::string &name);
62         const void *GetObject(int typeId, const std::string &name) const;
63
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
69         /// object IDs.
70         struct ParsedDefinition {
71                 ParsedDefinition(const Definition *dfn, int type, int id)
72                 : dfn(dfn), type(type), id(id) { }
73                 const Definition *dfn;
74                 int type;
75                 int id;
76         };
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 {
83                 PostponedDefinition(
84                                 char *object,
85                                 char *dest,
86                                 const std::string &identifier,
87                                 int type,
88                                 bool inlined,
89                                 bool aggregate)
90                 : object(object)
91                 , dest(dest)
92                 , identifier(identifier)
93                 , type(type)
94                 , inlined(inlined)
95                 , aggregate(aggregate) { }
96                 char *object;
97                 char *dest;
98                 std::string identifier;
99                 int type;
100                 bool inlined;
101                 bool aggregate;
102         };
103
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();
108         }
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 {
116                 return imageCache;
117         }
118         /// Get all definitions that were postponed because they
119         /// were not in the parsed source.
120         const std::vector<PostponedDefinition> &PostponedDefinitions() const {
121                 return postponedDefinitions;
122         }
123         /// A map of object id to object.
124         const std::map<int, std::vector<void *> > &Values() const {
125                 return values;
126         }
127         struct Array {
128                 void *data;
129                 unsigned int typeId;
130                 unsigned int size;
131                 bool ref;
132         };
133         const std::vector<Array> &Arrays() const {
134                 return arrays;
135         }
136
137 private:
138         /// Interpret given definition.
139         void ReadDefinition(const Definition &);
140         /// Interpret given definition as a literal.
141         void ReadLiteral(const Definition &);
142         /// Interpret given definition as a complex object.
143         void ReadObject(const Definition &);
144
145         /// Interpret given literal as an object of given type.
146         /// The resulting object shall be accessible by given ID
147         /// (whiich is already reserved) and be written to given
148         /// destination.
149         void ReadLiteral(int typeId, int id, char *dest, const Literal &);
150         /// Get an object of given type from given value.
151         /// The object will be registere as new and be given a
152         /// unique ID.
153         /// Any nested objects will be created likewise.
154         void *GetObject(int typeId, const Value &value);
155         /// Read an object of given type and properties into dest.
156         /// The object should alrady have an assigned identifier.
157         /// Pass -1 as ID if the object is anonymous.
158         /// Note that anonymous objects must not contain
159         /// references as they are excluded from linking.
160         void ReadObject(int typeId, int id, char *dest, const PropertyList &);
161         /// The function that does the actual compiling when you
162         /// call ReadScript(const vector<ScriptToken *> &).
163         void ReadScript(const std::vector<ScriptToken *> &, common::Script *);
164         /// Compile a tokenized script.
165         /// Storage for the script will be allocated as neccessary.
166         char *ReadScript(const std::vector<ScriptToken *> &);
167         /// Reinterpret dest as a script code and return a
168         /// reference to that.
169         common::Script::Code &CreateScriptCode(common::Script::Command c, char *dest);
170         /// Write address to dest.
171         void ReadScriptAddress(const ScriptToken &t, char *dest);
172         /// Write integer to est.
173         void ReadScriptInteger(const ScriptToken &t, char *dest);
174         /// Write vector to dest.
175         void ReadScriptVector(const ScriptToken &t, char *dest);
176
177         /// Get a surface from given image path.
178         /// Load it if neccessary.
179         SDL_Surface *GetImage(const std::string &);
180
181         /// Check if a value can be linked right now or if linkage
182         /// must be deferred (with Postpone()).
183         bool CanLink(const Value &) const;
184         /// Defer linking of given object reference.
185         void Postpone(
186                         char *object,
187                         char *dest,
188                         const std::string &identifier,
189                         int type,
190                         bool inlined = true,
191                         bool aggregate = false);
192
193 private:
194         const ParsedSource &source;
195
196         PagedAllocator alloc;
197
198         std::map<std::string, ParsedDefinition> parsedDefinitions;
199         std::vector<PostponedDefinition> postponedDefinitions;
200         std::map<std::string, SDL_Surface *> imageCache;
201         std::map<int, std::vector<void *> > values;
202         std::vector<Array> arrays;
203
204 };
205
206 }
207
208 #endif