Tue, 04 Feb 2014 02:28:33 +0200
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
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 | |
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 | // Mark types | |
61 | // | |
62 | enum eMarkType | |
63 | { | |
64 | eLabelMark, | |
65 | eIfMark, | |
66 | eInternalMark, // internal structures | |
67 | }; | |
68 | ||
69 | // ============================================================================ | |
70 | // Scope types | |
71 | // | |
72 | enum EScopeType | |
73 | { | |
74 | eUnknownScope, | |
75 | eIfScope, | |
76 | eWhileScope, | |
77 | eForScope, | |
78 | eDoScope, | |
79 | eSwitchScope, | |
80 | eElseScope, | |
81 | }; | |
82 | ||
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
|
83 | 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
|
84 | { |
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 | 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
|
86 | 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
|
87 | 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
|
88 | 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
|
89 | 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
|
90 | 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
|
91 | }; |
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
|
92 | |
88 | 93 | // ============================================================================ |
94 | // Meta-data about scopes | |
95 | // | |
96 | struct ScopeInfo | |
97 | { | |
98 | ByteMark* mark1; | |
99 | ByteMark* mark2; | |
100 | EScopeType type; | |
101 | DataBuffer* buffer1; | |
102 | ||
103 | // switch-related stuff | |
104 | // Which case are we at? | |
105 | int casecursor; | |
106 | ||
107 | // Marks to case-blocks | |
108 | ByteMark* casemarks[MAX_CASE]; | |
109 | ||
110 | // Numbers of the case labels | |
111 | int casenumbers[MAX_CASE]; | |
112 | ||
113 | // actual case blocks | |
114 | DataBuffer* casebuffers[MAX_CASE]; | |
115 | ||
116 | // What is the current buffer of the block? | |
117 | DataBuffer* recordbuffer; | |
118 | }; | |
119 | ||
120 | // ============================================================================ | |
121 | // | |
122 | struct ConstantInfo | |
123 | { | |
124 | String name; | |
125 | EType type; | |
126 | String val; | |
127 | }; | |
128 | ||
129 | // ============================================================================ | |
130 | // | |
131 | class BotscriptParser | |
132 | { | |
133 | PROPERTY (public, bool, ReadOnly, BOOL_OPS, STOCK_WRITE) | |
134 | ||
135 | public: | |
136 | // ==================================================================== | |
137 | // METHODS | |
138 | BotscriptParser(); | |
139 | ~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
|
140 | 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
|
141 | 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
|
142 | 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
|
143 | 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
|
144 | 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
|
145 | String ParseFloat(); |
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 | void PushScope(); |
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 | 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
|
148 | 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
|
149 | 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
|
150 | 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
|
151 | 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
|
152 | 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
|
153 | 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
|
154 | void WriteToFile (String outfile); |
88 | 155 | |
156 | inline int GetNumEvents() const | |
157 | { | |
158 | return mNumEvents; | |
159 | } | |
160 | ||
161 | inline int GetNumStates() const | |
162 | { | |
163 | return mNumStates; | |
164 | } | |
165 | ||
166 | private: | |
167 | // The main buffer - the contents of this is what we | |
168 | // write to file after parsing is complete | |
169 | DataBuffer* mMainBuffer; | |
170 | ||
171 | // onenter buffer - the contents of the onenter{} block | |
172 | // is buffered here and is merged further at the end of state | |
173 | DataBuffer* mOnEnterBuffer; | |
174 | ||
175 | // Mainloop buffer - the contents of the mainloop{} block | |
176 | // is buffered here and is merged further at the end of state | |
177 | DataBuffer* mMainLoopBuffer; | |
178 | ||
179 | // Switch buffer - switch case data is recorded to this | |
180 | // buffer initially, instead of into main buffer. | |
181 | DataBuffer* mSwitchBuffer; | |
182 | ||
183 | Lexer* mLexer; | |
184 | int mNumStates; | |
185 | int mNumEvents; | |
186 | EParserMode mCurrentMode; | |
187 | String mCurrentState; | |
188 | bool mStateSpawnDefined; | |
189 | bool mGotMainLoop; | |
190 | int mScopeCursor; | |
191 | DataBuffer* mIfExpression; | |
192 | bool mCanElse; | |
193 | List<UndefinedLabel> mUndefinedLabels; | |
194 | List<ConstantInfo> mConstants; | |
195 | ||
196 | // How many bytes have we written to file? | |
197 | int mNumWrittenBytes; | |
198 | ||
199 | // Scope data | |
200 | // TODO: make a List | |
201 | ScopeInfo mScopeStack[MAX_SCOPE]; | |
202 | ||
203 | DataBuffer* buffer(); | |
204 | void ParseStateBlock(); | |
205 | void ParseEventBlock(); | |
206 | void ParseMainloop(); | |
207 | void ParseOnEnterExit(); | |
208 | void ParseVariableDeclaration(); | |
209 | void ParseGoto(); | |
210 | void ParseIf(); | |
211 | void ParseElse(); | |
212 | void ParseWhileBlock(); | |
213 | void ParseForBlock(); | |
214 | void ParseDoBlock(); | |
215 | void ParseSwitchBlock(); | |
216 | void ParseSwitchCase(); | |
217 | void ParseSwitchDefault(); | |
218 | void ParseBreak(); | |
219 | void ParseContinue(); | |
220 | void ParseBlockEnd(); | |
221 | void ParseConst(); | |
222 | void ParseLabel(); | |
223 | void ParseEventdef(); | |
224 | void ParseFuncdef(); | |
225 | void writeMemberBuffers(); | |
226 | 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
|
227 | 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
|
228 | EDataHeader GetAssigmentDataHeader (EAssignmentOperator op, ScriptVariable* var); |
88 | 229 | }; |
230 | ||
231 | #endif // BOTC_PARSER_H |