]> git.localhorst.tv Git - l2e.git/blob - src/loader/Interpreter.h
9e90bcc4e98fd51d9f7b05594308b689a6df3d01
[l2e.git] / src / loader / Interpreter.h
1 /*
2  * Interpreter.h
3  *
4  *  Created on: Aug 26, 2012
5  *      Author: holy
6  */
7
8 #ifndef LOADER_INTERPRETER_H_
9 #define LOADER_INTERPRETER_H_
10
11 #include "fwd.h"
12 #include "ParsedSource.h"
13 #include "TypeDescription.h"
14 #include "../battle/fwd.h"
15 #include "../common/fwd.h"
16 #include "../geometry/Vector.h"
17 #include "../graphics/Color.h"
18 #include "../graphics/ComplexAnimation.h"
19 #include "../graphics/fwd.h"
20
21 #include <map>
22 #include <set>
23 #include <stdexcept>
24 #include <string>
25 #include <vector>
26 #include <SDL.h>
27
28 namespace loader {
29
30 class Interpreter {
31
32 public:
33         class Error: public std::runtime_error {
34         public:
35                 Error(const std::string &msg) : std::runtime_error("interpreter error: " + msg) { }
36         };
37
38 public:
39         explicit Interpreter(const ParsedSource &source) : source(source) { }
40         ~Interpreter();
41 private:
42         Interpreter(const Interpreter &);
43         Interpreter &operator =(const Interpreter &);
44
45 public:
46         void ReadSource();
47         void *GetObject(int typeId, const std::string &name);
48
49         static void CreateTypeDescriptions();
50         struct ParsedDefinition {
51                 ParsedDefinition(const Definition *dfn, int type, int id)
52                 : dfn(dfn), type(type), id(id) { }
53                 const Definition *dfn;
54                 int type;
55                 int id;
56         };
57         struct PostponedDefinition {
58                 PostponedDefinition(int type, int id, std::ptrdiff_t offset, const char *identifier, int linkedType, bool inlined)
59                 : type(type), id(id), offset(offset), identifier(identifier), linkedType(linkedType), inlined(inlined) { }
60                 int type;
61                 int id;
62                 std::ptrdiff_t offset;
63                 const char *identifier;
64                 int linkedType;
65                 bool inlined;
66         };
67
68         const std::set<std::string> &ExportedIdentifiers() const { return source.Exports(); }
69         const ParsedDefinition &GetDefinition(const std::string &identifier) const;
70         const std::map<std::string, SDL_Surface *> &Images() const { return imageCache; }
71         const std::vector<PostponedDefinition> &PostponedDefinitions() const { return postponedDefinitions; }
72         const std::map<int, std::vector<void *> > &Values() const { return values; }
73
74 private:
75         void ReadDefinition(const Definition &);
76         void ReadLiteral(const Definition &);
77         void ReadObject(const Definition &);
78
79         void ReadLiteral(int typeId, int id, char *dest, const Literal &);
80         void *GetObject(int typeId, const Value &value);
81         void ReadObject(int typeId, int id, char *dest, const PropertyList &);
82
83         SDL_Surface *GetImage(const std::string &);
84
85         bool CanLink(const Value &) const;
86         void Postpone(int type, int id, std::ptrdiff_t offset, const std::string &identifier, int linkedType, bool inlined);
87
88 private:
89         const ParsedSource &source;
90
91         std::map<std::string, ParsedDefinition> parsedDefinitions;
92         std::vector<PostponedDefinition> postponedDefinitions;
93         std::map<std::string, SDL_Surface *> imageCache;
94         std::map<int, std::vector<void *> > values;
95
96 };
97
98 }
99
100 #endif /* LOADER_INTERPRETER_H_ */