Debian Codegraph @

src/tools/dlgedit/dlg_compiler.h

raw
1 /*
2 Copyright (C) 2002 Kai Sterker <[email protected]>
3 Part of the Adonthell Project <http://adonthell.nongnu.org>
4
5 Dlgedit is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 Dlgedit is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with Dlgedit. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /**
20 * @file dlg_compiler.h
21 *
22 * @author Kai Sterker
23 * @brief The dialogue compiler.
24 */
25
26 #ifndef DLG_COMPILER_H
27 #define DLG_COMPILER_H
28
29 #include "dlg_module.h"
30 #include "dlg_circle.h"
31
32 /**
33 * Python tokens
34 */
35 enum token
36 {
37 EQ = 0,
38 NEQ = 1,
39 LT = 2,
40 LEQ = 3,
41 GT = 4,
42 GEQ = 5,
43 ASSIGN = 6,
44 ACCESS = 7,
45 COLON = 8,
46 IF = 9,
47 ELIF = 10,
48 ELSE = 11,
49 PASS = 12,
50 RETURN = 13,
51 BAND = 14,
52 BOR = 15,
53 NOT = 16,
54 ADD = 17,
55 SUB = 18,
56 MUL = 19,
57 DIV = 20,
58 QUOT = 21,
59 SQUOT = 22,
60 LBRACE = 23,
61 RBRACE = 24,
62 LBRACKET= 25,
63 RBRACKET= 26,
64 COMMA = 27,
65 COMMENT = 28,
66 MOD = 29,
67 AND = 30,
68 OR = 31,
69 XOR = 32,
70 FIXED = 33,
71 VARIABLE= 34,
72 CONSTANT= 35,
73 NONE = 36
74 };
75
76 #define NUM_OPS 33
77 #define NUM_FXD 5
78
79 /**
80 * It transforms the dialogue into the Python script needed by
81 * the Dialogue Engine.
82 *
83 * The Engine itself consists of two parts:
84 * - the dialogue base class defined in the dialogue.py module. It
85 * contains executable code shared by all dialogues.
86 * - the C++ part of the Engine uses the methods the Python dialogue
87 * class provides to step through the dialogue and retrieve the
88 * respective text.
89 *
90 * The main reason for this architecture is to keep the individual
91 * dialogue scripts as small as possible. Therefore, the scripts
92 * created by the compiler contain mainly data and little or no code.
93 */
94 class DlgCompiler
95 {
96 public:
97 /**
98 * Instantiate the compiler.
99 * @param module Module to be compiled
100 */
101 DlgCompiler (DlgModule *module);
102 /**
103 * Dtor.
104 */
105 ~DlgCompiler ();
106
107 /**
108 * Compile the module passed to DlgCompiler
109 */
110 void run ();
111
112 private:
113 void writeHeader (const std::string &theClass);
114 void writeText ();
115 void writeCode ();
116 void writeLoop ();
117 void writeConditions ();
118 void writeStart ();
119 void writeDialogue ();
120 void writeFollower (DlgNode *node);
121 void writeCustomCode ();
122
123 void addStart (DlgNode *node);
124 void addCode (const std::string &cde, int index);
125 bool addCondition (DlgCircle *circle, int index);
126
127 int checkFollowers (DlgCircle *node);
128 bool checkConditions (DlgCircle* node);
129
130 std::string escapeCode (std::string code);
131 std::string splitCode (std::string code, int space = 0);
132 std::string inflateCode (std::string code);
133
134 token getKeyword (const std::string &statement);
135 token getToken (const std::string &statement);
136
137 std::ofstream file;
138 DlgModule *dialogue; // The dialogue to be compiled
139
140 DlgCircle start; // Start node of the dialogue
141 std::vector<std::string> code; // Temporary storage for all code
142 std::vector<std::string> conditions; // Temporary storage for all conditions
143 std::vector<int> loop; // nodes that are allowed to looped
144 int errors; // number of errors in dialogue
145
146 int *codeTable; // Mapping between nodes and code
147 int *conditionTable; // Mapping between nodes and conditions
148 int *operationTable; // Mapping between nodes and condition type
149
150 static std::string operators[NUM_OPS];
151 static std::string fixed[NUM_FXD];
152 };
153
154 #endif // DLG_COMPILER_H