]> git.localhorst.tv Git - l2e.git/blob - src/loader/Interpreter.h
new object file format in compiler
[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 size;
130                 bool ref;
131         };
132         const std::vector<Array> &Arrays() const {
133                 return arrays;
134         }
135
136 private:
137         /// Interpret given definition.
138         void ReadDefinition(const Definition &);
139         /// Interpret given definition as a literal.
140         void ReadLiteral(const Definition &);
141         /// Interpret given definition as a complex object.
142         void ReadObject(const Definition &);
143
144         /// Interpret given literal as an object of given type.
145         /// The resulting object shall be accessible by given ID
146         /// (whiich is already reserved) and be written to given
147         /// destination.
148         void ReadLiteral(int typeId, int id, char *dest, const Literal &);
149         /// Get an object of given type from given value.
150         /// The object will be registere as new and be given a
151         /// unique ID.
152         /// Any nested objects will be created likewise.
153         void *GetObject(int typeId, const Value &value);
154         /// Read an object of given type and properties into dest.
155         /// The object should alrady have an assigned identifier.
156         /// Pass -1 as ID if the object is anonymous.
157         /// Note that anonymous objects must not contain
158         /// references as they are excluded from linking.
159         void ReadObject(int typeId, int id, char *dest, const PropertyList &);
160         /// The function that does the actual compiling when you
161         /// call ReadScript(const vector<ScriptToken *> &).
162         void ReadScript(const std::vector<ScriptToken *> &, common::Script *);
163         /// Compile a tokenized script.
164         /// Storage for the script will be allocated as neccessary.
165         char *ReadScript(const std::vector<ScriptToken *> &);
166         /// Reinterpret dest as a script code and return a
167         /// reference to that.
168         common::Script::Code &CreateScriptCode(common::Script::Command c, char *dest);
169         /// Write address to dest.
170         void ReadScriptAddress(const ScriptToken &t, char *dest);
171         /// Write integer to est.
172         void ReadScriptInteger(const ScriptToken &t, char *dest);
173         /// Write vector to dest.
174         void ReadScriptVector(const ScriptToken &t, char *dest);
175
176         /// Get a surface from given image path.
177         /// Load it if neccessary.
178         SDL_Surface *GetImage(const std::string &);
179
180         /// Check if a value can be linked right now or if linkage
181         /// must be deferred (with Postpone()).
182         bool CanLink(const Value &) const;
183         /// Defer linking of given object reference.
184         void Postpone(
185                         char *object,
186                         char *dest,
187                         const std::string &identifier,
188                         int type,
189                         bool inlined = true,
190                         bool aggregate = false);
191
192 private:
193         const ParsedSource &source;
194
195         PagedAllocator alloc;
196
197         std::map<std::string, ParsedDefinition> parsedDefinitions;
198         std::vector<PostponedDefinition> postponedDefinitions;
199         std::map<std::string, SDL_Surface *> imageCache;
200         std::map<int, std::vector<void *> > values;
201         std::vector<Array> arrays;
202
203 };
204
205 }
206
207 #endif