|
1 /* |
|
2 Copyright 2012-2014 Santeri Piippo |
|
3 All rights reserved. |
|
4 |
|
5 Redistribution and use in source and binary forms, with or without |
|
6 modification, are permitted provided that the following conditions |
|
7 are met: |
|
8 |
|
9 1. Redistributions of source code must retain the above copyright |
|
10 notice, this list of conditions and the following disclaimer. |
|
11 2. Redistributions in binary form must reproduce the above copyright |
|
12 notice, this list of conditions and the following disclaimer in the |
|
13 documentation and/or other materials provided with the distribution. |
|
14 3. The name of the author may not be used to endorse or promote products |
|
15 derived from this software without specific prior written permission. |
|
16 |
|
17 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
|
18 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
|
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
|
20 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
|
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
|
22 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
|
26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
27 */ |
|
28 |
|
29 #ifndef BOTC_PARSER_H |
|
30 #define BOTC_PARSER_H |
|
31 |
|
32 #include <stdio.h> |
|
33 #include "Main.h" |
|
34 #include "Commands.h" |
|
35 #include "LexerScanner.h" |
|
36 #include "Tokens.h" |
|
37 |
|
38 // TODO: get rid of this |
|
39 #define MAX_SCOPE 32 |
|
40 |
|
41 // TODO: get rid of this too? |
|
42 #define MAX_CASE 64 |
|
43 |
|
44 // TODO: get rid of this |
|
45 #define MAX_MARKS 512 |
|
46 |
|
47 class DataBuffer; |
|
48 class Lexer; |
|
49 class ScriptVariable; |
|
50 |
|
51 // ============================================================================ |
|
52 // |
|
53 struct UndefinedLabel |
|
54 { |
|
55 String name; |
|
56 ByteMark* target; |
|
57 }; |
|
58 |
|
59 // ============================================================================ |
|
60 // Operators |
|
61 // |
|
62 enum EOperator |
|
63 { |
|
64 OPER_ADD, |
|
65 OPER_SUBTRACT, |
|
66 OPER_MULTIPLY, |
|
67 OPER_DIVIDE, |
|
68 OPER_MODULUS, |
|
69 OPER_ASSIGN, |
|
70 OPER_ASSIGNADD, |
|
71 OPER_ASSIGNSUB, |
|
72 OPER_ASSIGNMUL, |
|
73 OPER_ASSIGNDIV, |
|
74 OPER_ASSIGNMOD, // -- 10 |
|
75 OPER_EQUALS, |
|
76 OPER_NOTEQUALS, |
|
77 OPER_LESSTHAN, |
|
78 OPER_GREATERTHAN, |
|
79 OPER_LESSTHANEQUALS, |
|
80 OPER_GREATERTHANEQUALS, |
|
81 OPER_LEFTSHIFT, |
|
82 OPER_RIGHTSHIFT, |
|
83 OPER_ASSIGNLEFTSHIFT, |
|
84 OPER_ASSIGNRIGHTSHIFT, // -- 20 |
|
85 OPER_OR, |
|
86 OPER_AND, |
|
87 OPER_BITWISEOR, |
|
88 OPER_BITWISEAND, |
|
89 OPER_BITWISEEOR, |
|
90 OPER_TERNARY, |
|
91 OPER_STRLEN, |
|
92 }; |
|
93 |
|
94 // ============================================================================ |
|
95 // |
|
96 struct operatorInfo |
|
97 { |
|
98 EOperator opercode; |
|
99 EDataHeader dataheader; |
|
100 EToken token; |
|
101 }; |
|
102 |
|
103 // ============================================================================ |
|
104 // Mark types |
|
105 // |
|
106 enum eMarkType |
|
107 { |
|
108 eLabelMark, |
|
109 eIfMark, |
|
110 eInternalMark, // internal structures |
|
111 }; |
|
112 |
|
113 // ============================================================================ |
|
114 // Scope types |
|
115 // |
|
116 enum EScopeType |
|
117 { |
|
118 eUnknownScope, |
|
119 eIfScope, |
|
120 eWhileScope, |
|
121 eForScope, |
|
122 eDoScope, |
|
123 eSwitchScope, |
|
124 eElseScope, |
|
125 }; |
|
126 |
|
127 // ============================================================================ |
|
128 // Meta-data about scopes |
|
129 // |
|
130 struct ScopeInfo |
|
131 { |
|
132 ByteMark* mark1; |
|
133 ByteMark* mark2; |
|
134 EScopeType type; |
|
135 DataBuffer* buffer1; |
|
136 |
|
137 // switch-related stuff |
|
138 // Which case are we at? |
|
139 int casecursor; |
|
140 |
|
141 // Marks to case-blocks |
|
142 ByteMark* casemarks[MAX_CASE]; |
|
143 |
|
144 // Numbers of the case labels |
|
145 int casenumbers[MAX_CASE]; |
|
146 |
|
147 // actual case blocks |
|
148 DataBuffer* casebuffers[MAX_CASE]; |
|
149 |
|
150 // What is the current buffer of the block? |
|
151 DataBuffer* recordbuffer; |
|
152 }; |
|
153 |
|
154 // ============================================================================ |
|
155 // |
|
156 struct ConstantInfo |
|
157 { |
|
158 String name; |
|
159 EType type; |
|
160 String val; |
|
161 }; |
|
162 |
|
163 // ============================================================================ |
|
164 // |
|
165 class BotscriptParser |
|
166 { |
|
167 PROPERTY (public, bool, ReadOnly, BOOL_OPS, STOCK_WRITE) |
|
168 |
|
169 public: |
|
170 // ==================================================================== |
|
171 // METHODS |
|
172 BotscriptParser(); |
|
173 ~BotscriptParser(); |
|
174 void ParseBotscript (String fileName); |
|
175 DataBuffer* ParseCommand (CommandInfo* comm); |
|
176 DataBuffer* ParseExpression (EType reqtype); |
|
177 DataBuffer* ParseAssignment (ScriptVariable* var); |
|
178 int ParseOperator (bool peek = false); |
|
179 DataBuffer* ParseExprValue (EType reqtype); |
|
180 String ParseFloat(); |
|
181 void PushScope(); |
|
182 DataBuffer* ParseStatement(); |
|
183 void AddSwitchCase (DataBuffer* b); |
|
184 void CheckToplevel(); |
|
185 void CheckNotToplevel(); |
|
186 bool TokenIs (EToken a); |
|
187 String GetTokenString(); |
|
188 String DescribePosition() const; |
|
189 void WriteToFile (String outfile); |
|
190 |
|
191 inline int GetNumEvents() const |
|
192 { |
|
193 return mNumEvents; |
|
194 } |
|
195 |
|
196 inline int GetNumStates() const |
|
197 { |
|
198 return mNumStates; |
|
199 } |
|
200 |
|
201 private: |
|
202 // The main buffer - the contents of this is what we |
|
203 // write to file after parsing is complete |
|
204 DataBuffer* mMainBuffer; |
|
205 |
|
206 // onenter buffer - the contents of the onenter{} block |
|
207 // is buffered here and is merged further at the end of state |
|
208 DataBuffer* mOnEnterBuffer; |
|
209 |
|
210 // Mainloop buffer - the contents of the mainloop{} block |
|
211 // is buffered here and is merged further at the end of state |
|
212 DataBuffer* mMainLoopBuffer; |
|
213 |
|
214 // Switch buffer - switch case data is recorded to this |
|
215 // buffer initially, instead of into main buffer. |
|
216 DataBuffer* mSwitchBuffer; |
|
217 |
|
218 Lexer* mLexer; |
|
219 int mNumStates; |
|
220 int mNumEvents; |
|
221 EParserMode mCurrentMode; |
|
222 String mCurrentState; |
|
223 bool mStateSpawnDefined; |
|
224 bool mGotMainLoop; |
|
225 int mScopeCursor; |
|
226 DataBuffer* mIfExpression; |
|
227 bool mCanElse; |
|
228 List<UndefinedLabel> mUndefinedLabels; |
|
229 List<ConstantInfo> mConstants; |
|
230 |
|
231 // How many bytes have we written to file? |
|
232 int mNumWrittenBytes; |
|
233 |
|
234 // Scope data |
|
235 // TODO: make a List |
|
236 ScopeInfo mScopeStack[MAX_SCOPE]; |
|
237 |
|
238 DataBuffer* buffer(); |
|
239 ConstantInfo* FindConstant (const String& tok); |
|
240 void ParseStateBlock(); |
|
241 void ParseEventBlock(); |
|
242 void ParseMainloop(); |
|
243 void ParseOnEnterExit(); |
|
244 void ParseVariableDeclaration(); |
|
245 void ParseGoto(); |
|
246 void ParseIf(); |
|
247 void ParseElse(); |
|
248 void ParseWhileBlock(); |
|
249 void ParseForBlock(); |
|
250 void ParseDoBlock(); |
|
251 void ParseSwitchBlock(); |
|
252 void ParseSwitchCase(); |
|
253 void ParseSwitchDefault(); |
|
254 void ParseBreak(); |
|
255 void ParseContinue(); |
|
256 void ParseBlockEnd(); |
|
257 void ParseConst(); |
|
258 void ParseLabel(); |
|
259 void ParseEventdef(); |
|
260 void ParseFuncdef(); |
|
261 void writeMemberBuffers(); |
|
262 void WriteStringTable(); |
|
263 }; |
|
264 |
|
265 #endif // BOTC_PARSER_H |