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