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