]> git.localhorst.tv Git - l2e.git/blob - src/loader/Interpreter.h
commented Interpreter class
[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
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
68         /// object IDs.
69         struct ParsedDefinition {
70                 ParsedDefinition(const Definition *dfn, int type, int id)
71                 : dfn(dfn), type(type), id(id) { }
72                 const Definition *dfn;
73                 int type;
74                 int id;
75         };
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 {
82                 PostponedDefinition(
83                                 char *dest,
84                                 const char *identifier,
85                                 int type,
86                                 bool inlined,
87                                 bool aggregate)
88                 : dest(dest)
89                 , identifier(identifier)
90                 , type(type)
91                 , inlined(inlined)
92                 , aggregate(aggregate) { }
93                 char *dest;
94                 const char *identifier;
95                 int type;
96                 bool inlined;
97                 bool aggregate;
98         };
99
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();
104         }
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 {
111                 return imageCache;
112         }
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;
117         }
118         /// A map of object id to object.
119         const std::map<int, std::vector<void *> > &Values() const {
120                 return values;
121         }
122
123 private:
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 &);
130
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
134         /// destination.
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
138         /// unique ID.
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);
162
163         /// Get a surface from given image path.
164         /// Load it if neccessary.
165         SDL_Surface *GetImage(const std::string &);
166
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.
171         void Postpone(
172                         char *dest,
173                         const std::string &identifier,
174                         int type,
175                         bool inlined = true,
176                         bool aggregate = false);
177
178 private:
179         const ParsedSource &source;
180
181         PagedAllocator alloc;
182
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;
187
188 };
189
190 }
191
192 #endif