]> git.localhorst.tv Git - l2e.git/blob - src/loader/Interpreter.h
5a8e3ab8aa424ed4db977e0c471c475cea05b8af
[l2e.git] / src / loader / Interpreter.h
1 #ifndef LOADER_INTERPRETER_H_
2 #define LOADER_INTERPRETER_H_
3
4 #include "PagedAllocator.h"
5 #include "ParsedSource.h"
6 #include "../common/Script.h"
7
8 #include <map>
9 #include <set>
10 #include <stdexcept>
11 #include <string>
12 #include <vector>
13 #include <SDL.h>
14
15 namespace loader {
16
17 class Interpreter {
18
19 public:
20         static const int BOOLEAN_ID = 1;
21         static const int COLOR_ID = 2;
22         static const int IMAGE_ID = 3;
23         static const int NUMBER_ID = 4;
24         static const int PATH_ID = 5;
25         static const int SCRIPT_ID = 6;
26         static const int STRING_ID = 7;
27         static const int VECTOR_ID = 8;
28
29         class Error: public std::runtime_error {
30         public:
31                 Error(const std::string &msg) : std::runtime_error("interpreter error: " + msg) { }
32         };
33
34 public:
35         explicit Interpreter(const ParsedSource &source) : source(source), alloc(4096) { }
36         ~Interpreter();
37 private:
38         Interpreter(const Interpreter &);
39         Interpreter &operator =(const Interpreter &);
40
41 public:
42         void ReadSource();
43         void *GetObject(int typeId, const std::string &name);
44
45         static void CreateTypeDescriptions();
46         struct ParsedDefinition {
47                 ParsedDefinition(const Definition *dfn, int type, int id)
48                 : dfn(dfn), type(type), id(id) { }
49                 const Definition *dfn;
50                 int type;
51                 int id;
52         };
53         struct PostponedDefinition {
54                 PostponedDefinition(
55                                 char *dest,
56                                 const char *identifier,
57                                 int type,
58                                 bool inlined,
59                                 bool aggregate)
60                 : dest(dest)
61                 , identifier(identifier)
62                 , type(type)
63                 , inlined(inlined)
64                 , aggregate(aggregate) { }
65                 char *dest;
66                 const char *identifier;
67                 int type;
68                 bool inlined;
69                 bool aggregate;
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(
97                         char *dest,
98                         const std::string &identifier,
99                         int type,
100                         bool inlined = true,
101                         bool aggregate = false);
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