Tue, 04 Feb 2014 22:00:43 +0200
- worked away some limitations
88 | 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 too? | |
39 | #define MAX_CASE 64 | |
40 | ||
41 | // TODO: get rid of this | |
42 | #define MAX_MARKS 512 | |
43 | ||
44 | class DataBuffer; | |
45 | class Lexer; | |
46 | class ScriptVariable; | |
47 | ||
48 | // ============================================================================ | |
49 | // | |
50 | struct UndefinedLabel | |
51 | { | |
52 | String name; | |
53 | ByteMark* target; | |
54 | }; | |
55 | ||
56 | // ============================================================================ | |
57 | // Mark types | |
58 | // | |
59 | enum eMarkType | |
60 | { | |
61 | eLabelMark, | |
62 | eIfMark, | |
63 | eInternalMark, // internal structures | |
64 | }; | |
65 | ||
66 | // ============================================================================ | |
67 | // Scope types | |
68 | // | |
69 | enum EScopeType | |
70 | { | |
71 | eUnknownScope, | |
72 | eIfScope, | |
73 | eWhileScope, | |
74 | eForScope, | |
75 | eDoScope, | |
76 | eSwitchScope, | |
77 | eElseScope, | |
78 | }; | |
79 | ||
92
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
80 | enum EAssignmentOperator |
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
81 | { |
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
82 | EAssign, |
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
83 | EAssignAdd, |
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
84 | EAssignSub, |
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
85 | EAssignMul, |
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
86 | EAssignDiv, |
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
87 | EAssignMod, |
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
88 | }; |
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
89 | |
88 | 90 | // ============================================================================ |
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
91 | // |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
92 | struct CaseInfo |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
93 | { |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
94 | ByteMark* mark; |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
95 | int number; |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
96 | DataBuffer* data; |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
97 | }; |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
98 | |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
99 | // ============================================================================ |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
100 | // |
88 | 101 | // Meta-data about scopes |
102 | // | |
103 | struct ScopeInfo | |
104 | { | |
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
105 | ByteMark* mark1; |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
106 | ByteMark* mark2; |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
107 | EScopeType type; |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
108 | DataBuffer* buffer1; |
88 | 109 | |
110 | // switch-related stuff | |
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
111 | List<CaseInfo>::Iterator casecursor; |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
112 | List<CaseInfo> cases; |
88 | 113 | }; |
114 | ||
115 | // ============================================================================ | |
116 | // | |
117 | struct ConstantInfo | |
118 | { | |
119 | String name; | |
120 | EType type; | |
121 | String val; | |
122 | }; | |
123 | ||
124 | // ============================================================================ | |
125 | // | |
126 | class BotscriptParser | |
127 | { | |
128 | PROPERTY (public, bool, ReadOnly, BOOL_OPS, STOCK_WRITE) | |
129 | ||
130 | public: | |
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
131 | enum EReset |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
132 | { |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
133 | eNoReset, |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
134 | eResetScope, |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
135 | }; |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
136 | |
88 | 137 | BotscriptParser(); |
138 | ~BotscriptParser(); | |
92
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
139 | ConstantInfo* FindConstant (const String& tok); |
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
140 | void ParseBotscript (String fileName); |
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
141 | DataBuffer* ParseCommand (CommandInfo* comm); |
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
142 | DataBuffer* ParseAssignment (ScriptVariable* var); |
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
143 | EAssignmentOperator ParseAssignmentOperator (); |
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
144 | String ParseFloat(); |
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
145 | void PushScope (EReset reset = eResetScope); |
92
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
146 | DataBuffer* ParseStatement(); |
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
147 | void AddSwitchCase (DataBuffer* b); |
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
148 | void CheckToplevel(); |
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
149 | void CheckNotToplevel(); |
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
150 | bool TokenIs (EToken a); |
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
151 | String GetTokenString(); |
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
152 | String DescribePosition() const; |
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
153 | void WriteToFile (String outfile); |
88 | 154 | |
155 | inline int GetNumEvents() const | |
156 | { | |
157 | return mNumEvents; | |
158 | } | |
159 | ||
160 | inline int GetNumStates() const | |
161 | { | |
162 | return mNumStates; | |
163 | } | |
164 | ||
165 | private: | |
166 | // The main buffer - the contents of this is what we | |
167 | // write to file after parsing is complete | |
168 | DataBuffer* mMainBuffer; | |
169 | ||
170 | // onenter buffer - the contents of the onenter{} block | |
171 | // is buffered here and is merged further at the end of state | |
172 | DataBuffer* mOnEnterBuffer; | |
173 | ||
174 | // Mainloop buffer - the contents of the mainloop{} block | |
175 | // is buffered here and is merged further at the end of state | |
176 | DataBuffer* mMainLoopBuffer; | |
177 | ||
178 | // Switch buffer - switch case data is recorded to this | |
179 | // buffer initially, instead of into main buffer. | |
180 | DataBuffer* mSwitchBuffer; | |
181 | ||
182 | Lexer* mLexer; | |
183 | int mNumStates; | |
184 | int mNumEvents; | |
185 | EParserMode mCurrentMode; | |
186 | String mCurrentState; | |
187 | bool mStateSpawnDefined; | |
188 | bool mGotMainLoop; | |
189 | int mScopeCursor; | |
190 | DataBuffer* mIfExpression; | |
191 | bool mCanElse; | |
192 | List<UndefinedLabel> mUndefinedLabels; | |
193 | List<ConstantInfo> mConstants; | |
194 | ||
195 | // How many bytes have we written to file? | |
196 | int mNumWrittenBytes; | |
197 | ||
198 | // Scope data | |
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
199 | List<ScopeInfo> mScopeStack; |
88 | 200 | |
201 | DataBuffer* buffer(); | |
202 | void ParseStateBlock(); | |
203 | void ParseEventBlock(); | |
204 | void ParseMainloop(); | |
205 | void ParseOnEnterExit(); | |
206 | void ParseVariableDeclaration(); | |
207 | void ParseGoto(); | |
208 | void ParseIf(); | |
209 | void ParseElse(); | |
210 | void ParseWhileBlock(); | |
211 | void ParseForBlock(); | |
212 | void ParseDoBlock(); | |
213 | void ParseSwitchBlock(); | |
214 | void ParseSwitchCase(); | |
215 | void ParseSwitchDefault(); | |
216 | void ParseBreak(); | |
217 | void ParseContinue(); | |
218 | void ParseBlockEnd(); | |
219 | void ParseConst(); | |
220 | void ParseLabel(); | |
221 | void ParseEventdef(); | |
222 | void ParseFuncdef(); | |
223 | void writeMemberBuffers(); | |
224 | void WriteStringTable(); | |
92
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
225 | DataBuffer* ParseExpression (EType reqtype, bool fromhere = false); |
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
226 | EDataHeader GetAssigmentDataHeader (EAssignmentOperator op, ScriptVariable* var); |
88 | 227 | }; |
228 | ||
229 | #endif // BOTC_PARSER_H |