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