]> git.localhorst.tv Git - l2e.git/blob - src/loader/Interpreter.h
8c14d1e10435bd4615f4916c8b674fddb7803c72
[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 "../common/Script.h"
18 #include "../geometry/Vector.h"
19 #include "../graphics/Color.h"
20 #include "../graphics/ComplexAnimation.h"
21 #include "../graphics/fwd.h"
22
23 #include <map>
24 #include <set>
25 #include <stdexcept>
26 #include <string>
27 #include <vector>
28 #include <SDL.h>
29
30 namespace loader {
31
32 class Interpreter {
33
34 public:
35         static const int BOOLEAN_ID = 1;
36         static const int COLOR_ID = 2;
37         static const int IMAGE_ID = 3;
38         static const int NUMBER_ID = 4;
39         static const int PATH_ID = 5;
40         static const int SCRIPT_ID = 6;
41         static const int STRING_ID = 7;
42         static const int VECTOR_ID = 8;
43
44         class Error: public std::runtime_error {
45         public:
46                 Error(const std::string &msg) : std::runtime_error("interpreter error: " + msg) { }
47         };
48
49 public:
50         explicit Interpreter(const ParsedSource &source) : source(source), alloc(4096) { }
51         ~Interpreter();
52 private:
53         Interpreter(const Interpreter &);
54         Interpreter &operator =(const Interpreter &);
55
56 public:
57         void ReadSource();
58         void *GetObject(int typeId, const std::string &name);
59
60         static void CreateTypeDescriptions();
61         struct ParsedDefinition {
62                 ParsedDefinition(const Definition *dfn, int type, int id)
63                 : dfn(dfn), type(type), id(id) { }
64                 const Definition *dfn;
65                 int type;
66                 int id;
67         };
68         struct PostponedDefinition {
69                 PostponedDefinition(int type, int id, std::ptrdiff_t offset, const char *identifier, int linkedType, bool inlined)
70                 : type(type), id(id), offset(offset), identifier(identifier), linkedType(linkedType), inlined(inlined) { }
71                 int type;
72                 int id;
73                 std::ptrdiff_t offset;
74                 const char *identifier;
75                 int linkedType;
76                 bool inlined;
77         };
78
79         const std::set<std::string> &ExportedIdentifiers() const { return source.Exports(); }
80         const ParsedDefinition &GetDefinition(const std::string &identifier);
81         const std::map<std::string, SDL_Surface *> &Images() const { return imageCache; }
82         const std::vector<PostponedDefinition> &PostponedDefinitions() const { return postponedDefinitions; }
83         const std::map<int, std::vector<void *> > &Values() const { return values; }
84
85 private:
86         void ReadDefinition(const Definition &);
87         void ReadLiteral(const Definition &);
88         void ReadObject(const Definition &);
89
90         void ReadLiteral(int typeId, int id, char *dest, const Literal &);
91         void *GetObject(int typeId, const Value &value);
92         void ReadObject(int typeId, int id, char *dest, const PropertyList &);
93         void ReadScript(const std::vector<ScriptToken *> &, common::Script *);
94         char *ReadScript(const std::vector<ScriptToken *> &);
95         common::Script::Code &CreateScriptCode(common::Script::Command c, char *dest);
96         void ReadScriptAddress(const ScriptToken &t, char *dest);
97         void ReadScriptInteger(const ScriptToken &t, char *dest);
98         void ReadScriptVector(const ScriptToken &t, char *dest);
99
100         SDL_Surface *GetImage(const std::string &);
101
102         bool CanLink(const Value &) const;
103         void Postpone(int type, int id, std::ptrdiff_t offset, const std::string &identifier, int linkedType, bool inlined);
104
105 private:
106         const ParsedSource &source;
107
108         PagedAllocator alloc;
109
110         std::map<std::string, ParsedDefinition> parsedDefinitions;
111         std::vector<PostponedDefinition> postponedDefinitions;
112         std::map<std::string, SDL_Surface *> imageCache;
113         std::map<int, std::vector<void *> > values;
114
115 };
116
117 }
118
119 #endif /* LOADER_INTERPRETER_H_ */