]> git.localhorst.tv Git - l2e.git/blob - src/loader/Interpreter.h
5f66c0f954e55243f018eb9065783359801e206b
[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(int type, int id, std::ptrdiff_t offset, const char *identifier, int linkedType, bool inlined)
55                 : type(type), id(id), offset(offset), identifier(identifier), linkedType(linkedType), inlined(inlined) { }
56                 int type;
57                 int id;
58                 std::ptrdiff_t offset;
59                 const char *identifier;
60                 int linkedType;
61                 bool inlined;
62         };
63
64         const std::set<std::string> &ExportedIdentifiers() const { return source.Exports(); }
65         const ParsedDefinition &GetDefinition(const std::string &identifier);
66         const std::map<std::string, SDL_Surface *> &Images() const { return imageCache; }
67         const std::vector<PostponedDefinition> &PostponedDefinitions() const { return postponedDefinitions; }
68         const std::map<int, std::vector<void *> > &Values() const { return values; }
69
70 private:
71         void ReadDefinition(const Definition &);
72         void ReadLiteral(const Definition &);
73         void ReadObject(const Definition &);
74
75         void ReadLiteral(int typeId, int id, char *dest, const Literal &);
76         void *GetObject(int typeId, const Value &value);
77         void ReadObject(int typeId, int id, char *dest, const PropertyList &);
78         void ReadScript(const std::vector<ScriptToken *> &, common::Script *);
79         char *ReadScript(const std::vector<ScriptToken *> &);
80         common::Script::Code &CreateScriptCode(common::Script::Command c, char *dest);
81         void ReadScriptAddress(const ScriptToken &t, char *dest);
82         void ReadScriptInteger(const ScriptToken &t, char *dest);
83         void ReadScriptVector(const ScriptToken &t, char *dest);
84
85         SDL_Surface *GetImage(const std::string &);
86
87         bool CanLink(const Value &) const;
88         void Postpone(int type, int id, std::ptrdiff_t offset, const std::string &identifier, int linkedType, bool inlined);
89
90 private:
91         const ParsedSource &source;
92
93         PagedAllocator alloc;
94
95         std::map<std::string, ParsedDefinition> parsedDefinitions;
96         std::vector<PostponedDefinition> postponedDefinitions;
97         std::map<std::string, SDL_Surface *> imageCache;
98         std::map<int, std::vector<void *> > values;
99
100 };
101
102 }
103
104 #endif /* LOADER_INTERPRETER_H_ */