Wed, 12 Feb 2014 06:50:13 +0200
- extended refactor to EToken (now TokenType)
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 | #include "Parser.h" | |
30 | #include "Events.h" | |
31 | #include "Commands.h" | |
32 | #include "StringTable.h" | |
33 | #include "Containers.h" | |
34 | #include "Lexer.h" | |
35 | #include "DataBuffer.h" | |
91
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
36 | #include "Expression.h" |
88 | 37 | |
38 | #define SCOPE(n) (mScopeStack[mScopeCursor - n]) | |
39 | ||
40 | // ============================================================================ | |
41 | // | |
42 | BotscriptParser::BotscriptParser() : | |
43 | mReadOnly (false), | |
105
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
44 | mMainBuffer (new DataBuffer), |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
45 | mOnEnterBuffer (new DataBuffer), |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
46 | mMainLoopBuffer (new DataBuffer), |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
47 | mLexer (new Lexer), |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
48 | mNumStates (0), |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
49 | mNumEvents (0), |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
50 | mCurrentMode (PARSERMODE_TopLevel), |
105
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
51 | mStateSpawnDefined (false), |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
52 | mGotMainLoop (false), |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
53 | mScopeCursor (-1), |
106
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
54 | mCanElse (false), |
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
55 | mHighestGlobalVarIndex (0), |
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
56 | mHighestStateVarIndex (0) {} |
88 | 57 | |
58 | // ============================================================================ | |
59 | // | |
60 | BotscriptParser::~BotscriptParser() | |
61 | { | |
62 | delete mLexer; | |
63 | } | |
64 | ||
65 | // ============================================================================ | |
66 | // | |
67 | void BotscriptParser::CheckToplevel() | |
68 | { | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
69 | if (mCurrentMode != PARSERMODE_TopLevel) |
88 | 70 | Error ("%1-statements may only be defined at top level!", GetTokenString()); |
71 | } | |
72 | ||
73 | // ============================================================================ | |
74 | // | |
75 | void BotscriptParser::CheckNotToplevel() | |
76 | { | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
77 | if (mCurrentMode == PARSERMODE_TopLevel) |
88 | 78 | Error ("%1-statements must not be defined at top level!", GetTokenString()); |
79 | } | |
80 | ||
81 | // ============================================================================ | |
106
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
82 | // |
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
83 | // Main compiler code. Begins read of the script file, checks the syntax of it |
88 | 84 | // and writes the data to the object file via Objwriter - which also takes care |
85 | // of necessary buffering so stuff is written in the correct order. | |
106
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
86 | // |
88 | 87 | void BotscriptParser::ParseBotscript (String fileName) |
88 | { | |
89 | // Lex and preprocess the file | |
90 | mLexer->ProcessFile (fileName); | |
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
91 | PushScope(); |
88 | 92 | |
93 | while (mLexer->GetNext()) | |
94 | { | |
95 | // Check if else is potentically valid | |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
96 | if (TokenIs (TK_Else) && mCanElse == false) |
88 | 97 | Error ("else without preceding if"); |
98 | ||
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
99 | if (TokenIs (TK_Else) == false) |
88 | 100 | mCanElse = false; |
101 | ||
102 | switch (mLexer->GetToken()->type) | |
103 | { | |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
104 | case TK_State: |
88 | 105 | ParseStateBlock(); |
106 | break; | |
107 | ||
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
108 | case TK_Event: |
88 | 109 | ParseEventBlock(); |
110 | break; | |
111 | ||
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
112 | case TK_Mainloop: |
88 | 113 | ParseMainloop(); |
114 | break; | |
115 | ||
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
116 | case TK_Onenter: |
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
117 | case TK_Onexit: |
88 | 118 | ParseOnEnterExit(); |
119 | break; | |
120 | ||
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
121 | case TK_Var: |
101
9ffae10ef76f
- variables: merged const and mutable variables into one system, added constexpr variable support. still no locals
Teemu Piippo <crimsondusk64@gmail.com>
parents:
100
diff
changeset
|
122 | ParseVar(); |
88 | 123 | break; |
124 | ||
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
125 | case TK_Goto: |
88 | 126 | ParseGoto(); |
127 | break; | |
128 | ||
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
129 | case TK_If: |
88 | 130 | ParseIf(); |
131 | break; | |
132 | ||
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
133 | case TK_Else: |
88 | 134 | ParseElse(); |
135 | break; | |
136 | ||
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
137 | case TK_While: |
88 | 138 | ParseWhileBlock(); |
139 | break; | |
140 | ||
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
141 | case TK_For: |
88 | 142 | ParseForBlock(); |
143 | break; | |
144 | ||
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
145 | case TK_Do: |
88 | 146 | ParseDoBlock(); |
147 | break; | |
148 | ||
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
149 | case TK_Switch: |
88 | 150 | ParseSwitchBlock(); |
151 | break; | |
152 | ||
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
153 | case TK_Case: |
88 | 154 | ParseSwitchCase(); |
155 | break; | |
156 | ||
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
157 | case TK_Default: |
88 | 158 | ParseSwitchDefault(); |
159 | break; | |
160 | ||
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
161 | case TK_Break: |
88 | 162 | ParseBreak(); |
163 | break; | |
164 | ||
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
165 | case TK_Continue: |
88 | 166 | ParseContinue(); |
167 | break; | |
168 | ||
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
169 | case TK_BraceEnd: |
88 | 170 | ParseBlockEnd(); |
171 | break; | |
172 | ||
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
173 | case TK_Eventdef: |
88 | 174 | ParseEventdef(); |
175 | break; | |
176 | ||
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
177 | case TK_Funcdef: |
88 | 178 | ParseFuncdef(); |
179 | break; | |
180 | ||
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
181 | case TK_Semicolon: |
107
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
182 | break; |
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
183 | |
88 | 184 | default: |
185 | { | |
186 | // Check for labels | |
187 | Lexer::Token next; | |
188 | ||
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
189 | if (TokenIs (TK_Symbol) && |
88 | 190 | mLexer->PeekNext (&next) && |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
191 | next.type ==TK_Colon) |
88 | 192 | { |
193 | ParseLabel(); | |
194 | break; | |
195 | } | |
196 | ||
197 | // Check if it's a command | |
198 | CommandInfo* comm = FindCommandByName (GetTokenString()); | |
199 | ||
200 | if (comm) | |
201 | { | |
202 | buffer()->MergeAndDestroy (ParseCommand (comm)); | |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
203 | mLexer->MustGetNext (TK_Semicolon); |
88 | 204 | continue; |
205 | } | |
206 | ||
207 | // If nothing else, parse it as a statement | |
104
62da929f7814
- minor changes: don't allow any token for labels, run ParseStatement from the next token
Teemu Piippo <crimsondusk64@gmail.com>
parents:
103
diff
changeset
|
208 | mLexer->Skip (-1); |
88 | 209 | DataBuffer* b = ParseStatement(); |
210 | ||
211 | if (b == false) | |
107
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
212 | { |
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
213 | mLexer->GetNext(); |
88 | 214 | Error ("unknown token `%1`", GetTokenString()); |
107
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
215 | } |
88 | 216 | |
217 | buffer()->MergeAndDestroy (b); | |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
218 | mLexer->MustGetNext (TK_Semicolon); |
107
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
219 | break; |
88 | 220 | } |
221 | } | |
222 | } | |
223 | ||
224 | // =============================================================================== | |
225 | // Script file ended. Do some last checks and write the last things to main buffer | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
226 | if (mCurrentMode != PARSERMODE_TopLevel) |
88 | 227 | Error ("script did not end at top level; a `}` is missing somewhere"); |
228 | ||
229 | if (IsReadOnly() == false) | |
230 | { | |
231 | // stateSpawn must be defined! | |
232 | if (mStateSpawnDefined == false) | |
233 | Error ("script must have a state named `stateSpawn`!"); | |
234 | ||
235 | // Ensure no goto target is left undefined | |
236 | if (mUndefinedLabels.IsEmpty() == false) | |
237 | { | |
238 | StringList names; | |
239 | ||
240 | for (UndefinedLabel& undf : mUndefinedLabels) | |
241 | names << undf.name; | |
242 | ||
243 | Error ("labels `%1` are referenced via `goto` but are not defined\n", names); | |
244 | } | |
245 | ||
246 | // Dump the last state's onenter and mainloop | |
247 | writeMemberBuffers(); | |
248 | ||
249 | // String table | |
250 | WriteStringTable(); | |
251 | } | |
252 | } | |
253 | ||
254 | // ============================================================================ | |
255 | // | |
256 | void BotscriptParser::ParseStateBlock() | |
257 | { | |
258 | CheckToplevel(); | |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
259 | mLexer->MustGetNext (TK_String); |
88 | 260 | String statename = GetTokenString(); |
261 | ||
262 | // State name must be a word. | |
263 | if (statename.FirstIndexOf (" ") != -1) | |
264 | Error ("state name must be a single word, got `%1`", statename); | |
265 | ||
266 | // stateSpawn is special - it *must* be defined. If we | |
267 | // encountered it, then mark down that we have it. | |
268 | if (-statename == "statespawn") | |
269 | mStateSpawnDefined = true; | |
270 | ||
271 | // Must end in a colon | |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
272 | mLexer->MustGetNext (TK_Colon); |
88 | 273 | |
274 | // write the previous state's onenter and | |
275 | // mainloop buffers to file now | |
276 | if (mCurrentState.IsEmpty() == false) | |
277 | writeMemberBuffers(); | |
278 | ||
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
279 | buffer()->WriteDWord (DH_StateName); |
88 | 280 | buffer()->WriteString (statename); |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
281 | buffer()->WriteDWord (DH_StateIndex); |
88 | 282 | buffer()->WriteDWord (mNumStates); |
283 | ||
284 | mNumStates++; | |
285 | mCurrentState = statename; | |
286 | mGotMainLoop = false; | |
287 | } | |
288 | ||
289 | // ============================================================================ | |
290 | // | |
291 | void BotscriptParser::ParseEventBlock() | |
292 | { | |
293 | CheckToplevel(); | |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
294 | mLexer->MustGetNext (TK_String); |
88 | 295 | |
296 | EventDefinition* e = FindEventByName (GetTokenString()); | |
297 | ||
298 | if (e == null) | |
299 | Error ("bad event, got `%1`\n", GetTokenString()); | |
300 | ||
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
301 | mLexer->MustGetNext (TK_BraceStart); |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
302 | mCurrentMode = PARSERMODE_Event; |
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
303 | buffer()->WriteDWord (DH_Event); |
88 | 304 | buffer()->WriteDWord (e->number); |
305 | mNumEvents++; | |
306 | } | |
307 | ||
308 | // ============================================================================ | |
309 | // | |
310 | void BotscriptParser::ParseMainloop() | |
311 | { | |
312 | CheckToplevel(); | |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
313 | mLexer->MustGetNext (TK_BraceStart); |
88 | 314 | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
315 | mCurrentMode = PARSERMODE_MainLoop; |
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
316 | mMainLoopBuffer->WriteDWord (DH_MainLoop); |
88 | 317 | } |
318 | ||
319 | // ============================================================================ | |
320 | // | |
321 | void BotscriptParser::ParseOnEnterExit() | |
322 | { | |
323 | CheckToplevel(); | |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
324 | bool onenter = (TokenIs (TK_Onenter)); |
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
325 | mLexer->MustGetNext (TK_BraceStart); |
88 | 326 | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
327 | mCurrentMode = onenter ? PARSERMODE_Onenter : PARSERMODE_Onexit; |
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
328 | buffer()->WriteDWord (onenter ? DH_OnEnter : DH_OnExit); |
88 | 329 | } |
330 | ||
331 | // ============================================================================ | |
332 | // | |
101
9ffae10ef76f
- variables: merged const and mutable variables into one system, added constexpr variable support. still no locals
Teemu Piippo <crimsondusk64@gmail.com>
parents:
100
diff
changeset
|
333 | void BotscriptParser::ParseVar() |
88 | 334 | { |
105
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
335 | Variable* var = new Variable; |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
336 | var->origin = mLexer->DescribeCurrentPosition(); |
107
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
337 | var->isarray = false; |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
338 | const bool isconst = mLexer->GetNext (TK_Const); |
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
339 | mLexer->MustGetAnyOf ({TK_Int,TK_Str,TK_Void}); |
101
9ffae10ef76f
- variables: merged const and mutable variables into one system, added constexpr variable support. still no locals
Teemu Piippo <crimsondusk64@gmail.com>
parents:
100
diff
changeset
|
340 | |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
341 | DataType vartype = (TokenIs (TK_Int)) ? TYPE_Int : |
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
342 | (TokenIs (TK_Str)) ? TYPE_String : |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
343 | TYPE_Bool; |
88 | 344 | |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
345 | mLexer->MustGetNext (TK_Symbol); |
105
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
346 | String name = GetTokenString(); |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
347 | |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
348 | if (mLexer->GetNext (TK_BracketStart)) |
107
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
349 | { |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
350 | mLexer->MustGetNext (TK_BracketEnd); |
107
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
351 | var->isarray = true; |
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
352 | |
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
353 | if (isconst) |
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
354 | Error ("arrays cannot be const"); |
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
355 | } |
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
356 | |
105
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
357 | for (Variable* var : SCOPE(0).globalVariables + SCOPE(0).localVariables) |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
358 | { |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
359 | if (var->name == name) |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
360 | Error ("Variable $%1 is already declared on this scope; declared at %2", |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
361 | var->name, var->origin); |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
362 | } |
88 | 363 | |
105
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
364 | var->name = name; |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
365 | var->statename = ""; |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
366 | var->type = vartype; |
101
9ffae10ef76f
- variables: merged const and mutable variables into one system, added constexpr variable support. still no locals
Teemu Piippo <crimsondusk64@gmail.com>
parents:
100
diff
changeset
|
367 | |
9ffae10ef76f
- variables: merged const and mutable variables into one system, added constexpr variable support. still no locals
Teemu Piippo <crimsondusk64@gmail.com>
parents:
100
diff
changeset
|
368 | if (isconst == false) |
9ffae10ef76f
- variables: merged const and mutable variables into one system, added constexpr variable support. still no locals
Teemu Piippo <crimsondusk64@gmail.com>
parents:
100
diff
changeset
|
369 | { |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
370 | var->writelevel = WRITE_Mutable; |
101
9ffae10ef76f
- variables: merged const and mutable variables into one system, added constexpr variable support. still no locals
Teemu Piippo <crimsondusk64@gmail.com>
parents:
100
diff
changeset
|
371 | } |
9ffae10ef76f
- variables: merged const and mutable variables into one system, added constexpr variable support. still no locals
Teemu Piippo <crimsondusk64@gmail.com>
parents:
100
diff
changeset
|
372 | else |
9ffae10ef76f
- variables: merged const and mutable variables into one system, added constexpr variable support. still no locals
Teemu Piippo <crimsondusk64@gmail.com>
parents:
100
diff
changeset
|
373 | { |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
374 | mLexer->MustGetNext (TK_Assign); |
105
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
375 | Expression expr (this, mLexer, vartype); |
101
9ffae10ef76f
- variables: merged const and mutable variables into one system, added constexpr variable support. still no locals
Teemu Piippo <crimsondusk64@gmail.com>
parents:
100
diff
changeset
|
376 | |
105
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
377 | // If the expression was constexpr, we know its value and thus |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
378 | // can store it in the variable. |
101
9ffae10ef76f
- variables: merged const and mutable variables into one system, added constexpr variable support. still no locals
Teemu Piippo <crimsondusk64@gmail.com>
parents:
100
diff
changeset
|
379 | if (expr.GetResult()->IsConstexpr()) |
9ffae10ef76f
- variables: merged const and mutable variables into one system, added constexpr variable support. still no locals
Teemu Piippo <crimsondusk64@gmail.com>
parents:
100
diff
changeset
|
380 | { |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
381 | var->writelevel = WRITE_Constexpr; |
101
9ffae10ef76f
- variables: merged const and mutable variables into one system, added constexpr variable support. still no locals
Teemu Piippo <crimsondusk64@gmail.com>
parents:
100
diff
changeset
|
382 | var->value = expr.GetResult()->GetValue(); |
9ffae10ef76f
- variables: merged const and mutable variables into one system, added constexpr variable support. still no locals
Teemu Piippo <crimsondusk64@gmail.com>
parents:
100
diff
changeset
|
383 | } |
9ffae10ef76f
- variables: merged const and mutable variables into one system, added constexpr variable support. still no locals
Teemu Piippo <crimsondusk64@gmail.com>
parents:
100
diff
changeset
|
384 | else |
9ffae10ef76f
- variables: merged const and mutable variables into one system, added constexpr variable support. still no locals
Teemu Piippo <crimsondusk64@gmail.com>
parents:
100
diff
changeset
|
385 | { |
9ffae10ef76f
- variables: merged const and mutable variables into one system, added constexpr variable support. still no locals
Teemu Piippo <crimsondusk64@gmail.com>
parents:
100
diff
changeset
|
386 | // TODO: might need a VM-wise oninit for this... |
107
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
387 | Error ("const variables must be constexpr"); |
101
9ffae10ef76f
- variables: merged const and mutable variables into one system, added constexpr variable support. still no locals
Teemu Piippo <crimsondusk64@gmail.com>
parents:
100
diff
changeset
|
388 | } |
9ffae10ef76f
- variables: merged const and mutable variables into one system, added constexpr variable support. still no locals
Teemu Piippo <crimsondusk64@gmail.com>
parents:
100
diff
changeset
|
389 | } |
9ffae10ef76f
- variables: merged const and mutable variables into one system, added constexpr variable support. still no locals
Teemu Piippo <crimsondusk64@gmail.com>
parents:
100
diff
changeset
|
390 | |
105
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
391 | // Assign an index for the variable if it is not constexpr. Constexpr |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
392 | // variables can simply be substituted out for their value when used |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
393 | // so they need no index. |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
394 | if (var->writelevel != WRITE_Constexpr) |
105
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
395 | { |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
396 | bool isglobal = IsInGlobalState(); |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
397 | var->index = isglobal ? SCOPE(0).globalVarIndexBase++ : SCOPE(0).localVarIndexBase++; |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
398 | |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
399 | if ((isglobal == true && var->index >= gMaxGlobalVars) || |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
400 | (isglobal == false && var->index >= gMaxStateVars)) |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
401 | { |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
402 | Error ("too many %1 variables", isglobal ? "global" : "state-local"); |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
403 | } |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
404 | } |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
405 | |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
406 | if (IsInGlobalState()) |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
407 | SCOPE(0).globalVariables << var; |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
408 | else |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
409 | SCOPE(0).localVariables << var; |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
410 | |
106
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
411 | SuggestHighestVarIndex (IsInGlobalState(), var->index); |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
412 | mLexer->MustGetNext (TK_Semicolon); |
105
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
413 | Print ("Declared %3 variable #%1 $%2\n", var->index, var->name, IsInGlobalState() ? "global" : "state-local"); |
88 | 414 | } |
415 | ||
416 | // ============================================================================ | |
417 | // | |
418 | void BotscriptParser::ParseGoto() | |
419 | { | |
420 | CheckNotToplevel(); | |
421 | ||
422 | // Get the name of the label | |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
423 | mLexer->MustGetNext (TK_Symbol); |
88 | 424 | |
425 | // Find the mark this goto statement points to | |
426 | String target = GetTokenString(); | |
427 | ByteMark* mark = buffer()->FindMarkByName (target); | |
428 | ||
429 | // If not set, define it | |
430 | if (mark == null) | |
431 | { | |
432 | UndefinedLabel undf; | |
433 | undf.name = target; | |
434 | undf.target = buffer()->AddMark (target); | |
435 | mUndefinedLabels << undf; | |
436 | } | |
437 | ||
438 | // Add a reference to the mark. | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
439 | buffer()->WriteDWord (DH_Goto); |
88 | 440 | buffer()->AddReference (mark); |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
441 | mLexer->MustGetNext (TK_Semicolon); |
88 | 442 | } |
443 | ||
444 | // ============================================================================ | |
445 | // | |
446 | void BotscriptParser::ParseIf() | |
447 | { | |
448 | CheckNotToplevel(); | |
449 | PushScope(); | |
450 | ||
451 | // Condition | |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
452 | mLexer->MustGetNext (TK_ParenStart); |
88 | 453 | |
454 | // Read the expression and write it. | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
455 | DataBuffer* c = ParseExpression (TYPE_Int); |
88 | 456 | buffer()->MergeAndDestroy (c); |
457 | ||
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
458 | mLexer->MustGetNext (TK_ParenEnd); |
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
459 | mLexer->MustGetNext (TK_BraceStart); |
88 | 460 | |
461 | // Add a mark - to here temporarily - and add a reference to it. | |
462 | // Upon a closing brace, the mark will be adjusted. | |
463 | ByteMark* mark = buffer()->AddMark (""); | |
464 | ||
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
465 | // Use DH_IfNotGoto - if the expression is not true, we goto the mark |
88 | 466 | // we just defined - and this mark will be at the end of the scope block. |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
467 | buffer()->WriteDWord (DH_IfNotGoto); |
88 | 468 | buffer()->AddReference (mark); |
469 | ||
470 | // Store it | |
471 | SCOPE (0).mark1 = mark; | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
472 | SCOPE (0).type = SCOPE_If; |
88 | 473 | } |
474 | ||
475 | // ============================================================================ | |
476 | // | |
477 | void BotscriptParser::ParseElse() | |
478 | { | |
479 | CheckNotToplevel(); | |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
480 | mLexer->MustGetNext (TK_BraceStart); |
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
481 | PushScope (eNoReset); |
88 | 482 | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
483 | if (SCOPE (0).type != SCOPE_If) |
88 | 484 | Error ("else without preceding if"); |
485 | ||
486 | // write down to jump to the end of the else statement | |
487 | // Otherwise we have fall-throughs | |
488 | SCOPE (0).mark2 = buffer()->AddMark (""); | |
489 | ||
490 | // Instruction to jump to the end after if block is complete | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
491 | buffer()->WriteDWord (DH_Goto); |
88 | 492 | buffer()->AddReference (SCOPE (0).mark2); |
493 | ||
494 | // Move the ifnot mark here and set type to else | |
495 | buffer()->AdjustMark (SCOPE (0).mark1); | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
496 | SCOPE (0).type = SCOPE_Else; |
88 | 497 | } |
498 | ||
499 | // ============================================================================ | |
500 | // | |
501 | void BotscriptParser::ParseWhileBlock() | |
502 | { | |
503 | CheckNotToplevel(); | |
504 | PushScope(); | |
505 | ||
506 | // While loops need two marks - one at the start of the loop and one at the | |
507 | // end. The condition is checked at the very start of the loop, if it fails, | |
508 | // we use goto to skip to the end of the loop. At the end, we loop back to | |
509 | // the beginning with a go-to statement. | |
510 | ByteMark* mark1 = buffer()->AddMark (""); // start | |
511 | ByteMark* mark2 = buffer()->AddMark (""); // end | |
512 | ||
513 | // Condition | |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
514 | mLexer->MustGetNext (TK_ParenStart); |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
515 | DataBuffer* expr = ParseExpression (TYPE_Int); |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
516 | mLexer->MustGetNext (TK_ParenEnd); |
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
517 | mLexer->MustGetNext (TK_BraceStart); |
88 | 518 | |
519 | // write condition | |
520 | buffer()->MergeAndDestroy (expr); | |
521 | ||
522 | // Instruction to go to the end if it fails | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
523 | buffer()->WriteDWord (DH_IfNotGoto); |
88 | 524 | buffer()->AddReference (mark2); |
525 | ||
526 | // Store the needed stuff | |
527 | SCOPE (0).mark1 = mark1; | |
528 | SCOPE (0).mark2 = mark2; | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
529 | SCOPE (0).type = SCOPE_While; |
88 | 530 | } |
531 | ||
532 | // ============================================================================ | |
533 | // | |
534 | void BotscriptParser::ParseForBlock() | |
535 | { | |
536 | CheckNotToplevel(); | |
537 | PushScope(); | |
538 | ||
539 | // Initializer | |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
540 | mLexer->MustGetNext (TK_ParenStart); |
88 | 541 | DataBuffer* init = ParseStatement(); |
542 | ||
543 | if (init == null) | |
544 | Error ("bad statement for initializer of for"); | |
545 | ||
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
546 | mLexer->MustGetNext (TK_Semicolon); |
88 | 547 | |
548 | // Condition | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
549 | DataBuffer* cond = ParseExpression (TYPE_Int); |
88 | 550 | |
551 | if (cond == null) | |
552 | Error ("bad statement for condition of for"); | |
553 | ||
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
554 | mLexer->MustGetNext (TK_Semicolon); |
88 | 555 | |
556 | // Incrementor | |
557 | DataBuffer* incr = ParseStatement(); | |
558 | ||
559 | if (incr == null) | |
560 | Error ("bad statement for incrementor of for"); | |
561 | ||
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
562 | mLexer->MustGetNext (TK_ParenEnd); |
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
563 | mLexer->MustGetNext (TK_BraceStart); |
88 | 564 | |
565 | // First, write out the initializer | |
566 | buffer()->MergeAndDestroy (init); | |
567 | ||
568 | // Init two marks | |
569 | ByteMark* mark1 = buffer()->AddMark (""); | |
570 | ByteMark* mark2 = buffer()->AddMark (""); | |
571 | ||
572 | // Add the condition | |
573 | buffer()->MergeAndDestroy (cond); | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
574 | buffer()->WriteDWord (DH_IfNotGoto); |
88 | 575 | buffer()->AddReference (mark2); |
576 | ||
577 | // Store the marks and incrementor | |
578 | SCOPE (0).mark1 = mark1; | |
579 | SCOPE (0).mark2 = mark2; | |
580 | SCOPE (0).buffer1 = incr; | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
581 | SCOPE (0).type = SCOPE_For; |
88 | 582 | } |
583 | ||
584 | // ============================================================================ | |
585 | // | |
586 | void BotscriptParser::ParseDoBlock() | |
587 | { | |
588 | CheckNotToplevel(); | |
589 | PushScope(); | |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
590 | mLexer->MustGetNext (TK_BraceStart); |
88 | 591 | SCOPE (0).mark1 = buffer()->AddMark (""); |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
592 | SCOPE (0).type = SCOPE_Do; |
88 | 593 | } |
594 | ||
595 | // ============================================================================ | |
596 | // | |
597 | void BotscriptParser::ParseSwitchBlock() | |
598 | { | |
599 | // This gets a bit tricky. switch is structured in the | |
600 | // bytecode followingly: | |
601 | // | |
602 | // (expression) | |
603 | // case a: goto casemark1 | |
604 | // case b: goto casemark2 | |
605 | // case c: goto casemark3 | |
606 | // goto mark1 // jump to end if no matches | |
607 | // casemark1: ... | |
608 | // casemark2: ... | |
609 | // casemark3: ... | |
610 | // mark1: // end mark | |
611 | ||
612 | CheckNotToplevel(); | |
613 | PushScope(); | |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
614 | mLexer->MustGetNext (TK_ParenStart); |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
615 | buffer()->MergeAndDestroy (ParseExpression (TYPE_Int)); |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
616 | mLexer->MustGetNext (TK_ParenEnd); |
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
617 | mLexer->MustGetNext (TK_BraceStart); |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
618 | SCOPE (0).type = SCOPE_Switch; |
88 | 619 | SCOPE (0).mark1 = buffer()->AddMark (""); // end mark |
620 | SCOPE (0).buffer1 = null; // default header | |
621 | } | |
622 | ||
623 | // ============================================================================ | |
624 | // | |
625 | void BotscriptParser::ParseSwitchCase() | |
626 | { | |
627 | // case is only allowed inside switch | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
628 | if (SCOPE (0).type != SCOPE_Switch) |
88 | 629 | Error ("case label outside switch"); |
630 | ||
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
631 | // Get a literal value for the case block. Zandronum does not support |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
632 | // expressions here. |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
633 | mLexer->MustGetNext (TK_Number); |
88 | 634 | int num = mLexer->GetToken()->text.ToLong(); |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
635 | mLexer->MustGetNext (TK_Colon); |
88 | 636 | |
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
637 | for (const CaseInfo& info : SCOPE(0).cases) |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
638 | if (info.number == num) |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
639 | Error ("multiple case %1 labels in one switch", num); |
88 | 640 | |
641 | // Write down the expression and case-go-to. This builds | |
642 | // the case tree. The closing event will write the actual | |
643 | // blocks and move the marks appropriately. | |
644 | // | |
645 | // AddSwitchCase will add the reference to the mark | |
646 | // for the case block that this heralds, and takes care | |
647 | // of buffering setup and stuff like that. | |
648 | // | |
649 | // We null the switch buffer for the case-go-to statement as | |
650 | // we want it all under the switch, not into the case-buffers. | |
651 | mSwitchBuffer = null; | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
652 | buffer()->WriteDWord (DH_CaseGoto); |
88 | 653 | buffer()->WriteDWord (num); |
654 | AddSwitchCase (null); | |
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
655 | SCOPE (0).casecursor->number = num; |
88 | 656 | } |
657 | ||
658 | // ============================================================================ | |
659 | // | |
660 | void BotscriptParser::ParseSwitchDefault() | |
661 | { | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
662 | if (SCOPE (0).type != SCOPE_Switch) |
88 | 663 | Error ("default label outside switch"); |
664 | ||
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
665 | if (SCOPE (0).buffer1 != null) |
88 | 666 | Error ("multiple default labels in one switch"); |
667 | ||
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
668 | mLexer->MustGetNext (TK_Colon); |
88 | 669 | |
670 | // The default header is buffered into buffer1, since | |
671 | // it has to be the last of the case headers | |
672 | // | |
673 | // Since the expression is pushed into the switch | |
674 | // and is only popped when case succeeds, we have | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
675 | // to pop it with DH_Drop manually if we end up in |
88 | 676 | // a default. |
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
677 | DataBuffer* buf = new DataBuffer; |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
678 | SCOPE (0).buffer1 = buf; |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
679 | buf->WriteDWord (DH_Drop); |
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
680 | buf->WriteDWord (DH_Goto); |
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
681 | AddSwitchCase (buf); |
88 | 682 | } |
683 | ||
684 | // ============================================================================ | |
685 | // | |
686 | void BotscriptParser::ParseBreak() | |
687 | { | |
688 | if (mScopeCursor == 0) | |
689 | Error ("unexpected `break`"); | |
690 | ||
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
691 | buffer()->WriteDWord (DH_Goto); |
88 | 692 | |
693 | // switch and if use mark1 for the closing point, | |
694 | // for and while use mark2. | |
695 | switch (SCOPE (0).type) | |
696 | { | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
697 | case SCOPE_If: |
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
698 | case SCOPE_Switch: |
88 | 699 | { |
700 | buffer()->AddReference (SCOPE (0).mark1); | |
701 | } break; | |
702 | ||
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
703 | case SCOPE_For: |
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
704 | case SCOPE_While: |
88 | 705 | { |
706 | buffer()->AddReference (SCOPE (0).mark2); | |
707 | } break; | |
708 | ||
709 | default: | |
710 | { | |
711 | Error ("unexpected `break`"); | |
712 | } break; | |
713 | } | |
714 | ||
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
715 | mLexer->MustGetNext (TK_Semicolon); |
88 | 716 | } |
717 | ||
718 | // ============================================================================ | |
719 | // | |
720 | void BotscriptParser::ParseContinue() | |
721 | { | |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
722 | mLexer->MustGetNext (TK_Semicolon); |
88 | 723 | |
724 | int curs; | |
725 | bool found = false; | |
726 | ||
727 | // Fall through the scope until we find a loop block | |
728 | for (curs = mScopeCursor; curs > 0 && !found; curs--) | |
729 | { | |
730 | switch (mScopeStack[curs].type) | |
731 | { | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
732 | case SCOPE_For: |
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
733 | case SCOPE_While: |
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
734 | case SCOPE_Do: |
88 | 735 | { |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
736 | buffer()->WriteDWord (DH_Goto); |
88 | 737 | buffer()->AddReference (mScopeStack[curs].mark1); |
738 | found = true; | |
739 | } break; | |
740 | ||
741 | default: | |
742 | break; | |
743 | } | |
744 | } | |
745 | ||
746 | // No loop blocks | |
747 | if (found == false) | |
748 | Error ("`continue`-statement not inside a loop"); | |
749 | } | |
750 | ||
751 | // ============================================================================ | |
752 | // | |
753 | void BotscriptParser::ParseBlockEnd() | |
754 | { | |
755 | // Closing brace | |
756 | // If we're in the block stack, we're descending down from it now | |
757 | if (mScopeCursor > 0) | |
758 | { | |
759 | switch (SCOPE (0).type) | |
760 | { | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
761 | case SCOPE_If: |
105
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
762 | { |
88 | 763 | // Adjust the closing mark. |
764 | buffer()->AdjustMark (SCOPE (0).mark1); | |
765 | ||
105
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
766 | // We're returning from `if`, thus `else` follow |
88 | 767 | mCanElse = true; |
768 | break; | |
105
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
769 | } |
88 | 770 | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
771 | case SCOPE_Else: |
105
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
772 | { |
88 | 773 | // else instead uses mark1 for itself (so if expression |
774 | // fails, jump to else), mark2 means end of else | |
775 | buffer()->AdjustMark (SCOPE (0).mark2); | |
776 | break; | |
105
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
777 | } |
88 | 778 | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
779 | case SCOPE_For: |
105
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
780 | { // write the incrementor at the end of the loop block |
88 | 781 | buffer()->MergeAndDestroy (SCOPE (0).buffer1); |
105
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
782 | } |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
783 | case SCOPE_While: |
105
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
784 | { // write down the instruction to go back to the start of the loop |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
785 | buffer()->WriteDWord (DH_Goto); |
88 | 786 | buffer()->AddReference (SCOPE (0).mark1); |
787 | ||
788 | // Move the closing mark here since we're at the end of the while loop | |
789 | buffer()->AdjustMark (SCOPE (0).mark2); | |
790 | break; | |
105
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
791 | } |
88 | 792 | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
793 | case SCOPE_Do: |
88 | 794 | { |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
795 | mLexer->MustGetNext (TK_While); |
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
796 | mLexer->MustGetNext (TK_ParenStart); |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
797 | DataBuffer* expr = ParseExpression (TYPE_Int); |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
798 | mLexer->MustGetNext (TK_ParenEnd); |
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
799 | mLexer->MustGetNext (TK_Semicolon); |
88 | 800 | |
801 | // If the condition runs true, go back to the start. | |
802 | buffer()->MergeAndDestroy (expr); | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
803 | buffer()->WriteDWord (DH_IfGoto); |
88 | 804 | buffer()->AddReference (SCOPE (0).mark1); |
805 | break; | |
806 | } | |
807 | ||
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
808 | case SCOPE_Switch: |
88 | 809 | { |
810 | // Switch closes. Move down to the record buffer of | |
811 | // the lower block. | |
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
812 | if (SCOPE (1).casecursor != SCOPE (1).cases.begin() - 1) |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
813 | mSwitchBuffer = SCOPE (1).casecursor->data; |
88 | 814 | else |
815 | mSwitchBuffer = null; | |
816 | ||
817 | // If there was a default in the switch, write its header down now. | |
818 | // If not, write instruction to jump to the end of switch after | |
819 | // the headers (thus won't fall-through if no case matched) | |
820 | if (SCOPE (0).buffer1) | |
821 | buffer()->MergeAndDestroy (SCOPE (0).buffer1); | |
822 | else | |
823 | { | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
824 | buffer()->WriteDWord (DH_Drop); |
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
825 | buffer()->WriteDWord (DH_Goto); |
88 | 826 | buffer()->AddReference (SCOPE (0).mark1); |
827 | } | |
828 | ||
829 | // Go through all of the buffers we | |
830 | // recorded down and write them. | |
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
831 | for (CaseInfo& info : SCOPE (0).cases) |
88 | 832 | { |
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
833 | buffer()->AdjustMark (info.mark); |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
834 | buffer()->MergeAndDestroy (info.data); |
88 | 835 | } |
836 | ||
837 | // Move the closing mark here | |
838 | buffer()->AdjustMark (SCOPE (0).mark1); | |
839 | break; | |
840 | } | |
841 | ||
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
842 | case SCOPE_Unknown: |
88 | 843 | break; |
844 | } | |
845 | ||
846 | // Descend down the stack | |
847 | mScopeCursor--; | |
848 | return; | |
849 | } | |
850 | ||
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
851 | int dataheader = (mCurrentMode == PARSERMODE_Event) ? DH_EndEvent : |
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
852 | (mCurrentMode == PARSERMODE_MainLoop) ? DH_EndMainLoop : |
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
853 | (mCurrentMode == PARSERMODE_Onenter) ? DH_EndOnEnter : |
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
854 | (mCurrentMode == PARSERMODE_Onexit) ? DH_EndOnExit : -1; |
88 | 855 | |
856 | if (dataheader == -1) | |
857 | Error ("unexpected `}`"); | |
858 | ||
859 | // Data header must be written before mode is changed because | |
860 | // onenter and mainloop go into special buffers, and we want | |
861 | // the closing data headers into said buffers too. | |
862 | buffer()->WriteDWord (dataheader); | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
863 | mCurrentMode = PARSERMODE_TopLevel; |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
864 | mLexer->GetNext (TK_Semicolon); |
88 | 865 | } |
866 | ||
867 | // ============================================================================ | |
868 | // | |
869 | void BotscriptParser::ParseLabel() | |
870 | { | |
871 | CheckNotToplevel(); | |
872 | String labelName = GetTokenString(); | |
873 | ByteMark* mark = null; | |
874 | ||
875 | // See if a mark already exists for this label | |
89
029a330a9bef
- blargh. buffers weren't initialized properly
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
876 | for (UndefinedLabel& label : mUndefinedLabels) |
88 | 877 | { |
89
029a330a9bef
- blargh. buffers weren't initialized properly
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
878 | if (label.name != labelName) |
88 | 879 | continue; |
880 | ||
89
029a330a9bef
- blargh. buffers weren't initialized properly
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
881 | mark = label.target; |
88 | 882 | buffer()->AdjustMark (mark); |
883 | ||
884 | // No longer undefined | |
89
029a330a9bef
- blargh. buffers weren't initialized properly
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
885 | mUndefinedLabels.Remove (label); |
88 | 886 | break; |
887 | } | |
888 | ||
889 | // Not found in unmarked lists, define it now | |
890 | if (mark == null) | |
891 | buffer()->AddMark (labelName); | |
892 | ||
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
893 | mLexer->MustGetNext (TK_Colon); |
88 | 894 | } |
895 | ||
896 | // ============================================================================= | |
897 | // | |
898 | void BotscriptParser::ParseEventdef() | |
899 | { | |
900 | EventDefinition* e = new EventDefinition; | |
901 | ||
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
902 | mLexer->MustGetNext (TK_Number); |
88 | 903 | e->number = GetTokenString().ToLong(); |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
904 | mLexer->MustGetNext (TK_Colon); |
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
905 | mLexer->MustGetNext (TK_Symbol); |
88 | 906 | e->name = mLexer->GetToken()->text; |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
907 | mLexer->MustGetNext (TK_ParenStart); |
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
908 | mLexer->MustGetNext (TK_ParenEnd); |
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
909 | mLexer->MustGetNext (TK_Semicolon); |
88 | 910 | AddEvent (e); |
911 | } | |
912 | ||
913 | // ============================================================================= | |
914 | // | |
915 | void BotscriptParser::ParseFuncdef() | |
916 | { | |
917 | CommandInfo* comm = new CommandInfo; | |
105
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
918 | comm->origin = mLexer->DescribeCurrentPosition(); |
88 | 919 | |
99
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
98
diff
changeset
|
920 | // Return value |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
921 | mLexer->MustGetAnyOf ({TK_Int,TK_Void,TK_Bool,TK_Str}); |
99
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
98
diff
changeset
|
922 | comm->returnvalue = GetTypeByName (mLexer->GetToken()->text); // TODO |
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
98
diff
changeset
|
923 | assert (comm->returnvalue != -1); |
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
98
diff
changeset
|
924 | |
88 | 925 | // Number |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
926 | mLexer->MustGetNext (TK_Number); |
88 | 927 | comm->number = mLexer->GetToken()->text.ToLong(); |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
928 | mLexer->MustGetNext (TK_Colon); |
88 | 929 | |
930 | // Name | |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
931 | mLexer->MustGetNext (TK_Symbol); |
88 | 932 | comm->name = mLexer->GetToken()->text; |
933 | ||
99
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
98
diff
changeset
|
934 | // Arguments |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
935 | mLexer->MustGetNext (TK_ParenStart); |
99
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
98
diff
changeset
|
936 | comm->minargs = 0; |
88 | 937 | |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
938 | while (mLexer->PeekNextType (TK_ParenEnd) == false) |
99
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
98
diff
changeset
|
939 | { |
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
98
diff
changeset
|
940 | if (comm->args.IsEmpty() == false) |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
941 | mLexer->MustGetNext (TK_Comma); |
88 | 942 | |
943 | CommandArgument arg; | |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
944 | mLexer->MustGetAnyOf ({TK_Int,TK_Bool,TK_Str}); |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
945 | DataType type = GetTypeByName (mLexer->GetToken()->text); // TODO |
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
946 | assert (type != -1 && type != TYPE_Void); |
88 | 947 | arg.type = type; |
948 | ||
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
949 | mLexer->MustGetNext (TK_Symbol); |
88 | 950 | arg.name = mLexer->GetToken()->text; |
951 | ||
952 | // If this is an optional parameter, we need the default value. | |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
953 | if (comm->minargs < comm->args.Size() || mLexer->PeekNextType (TK_Assign)) |
88 | 954 | { |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
955 | mLexer->MustGetNext (TK_Assign); |
88 | 956 | |
957 | switch (type) | |
958 | { | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
959 | case TYPE_Int: |
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
960 | case TYPE_Bool: |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
961 | mLexer->MustGetNext (TK_Number); |
88 | 962 | break; |
963 | ||
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
964 | case TYPE_String: |
99
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
98
diff
changeset
|
965 | Error ("string arguments cannot have default values"); |
88 | 966 | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
967 | case TYPE_Unknown: |
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
968 | case TYPE_Void: |
88 | 969 | break; |
970 | } | |
971 | ||
972 | arg.defvalue = mLexer->GetToken()->text.ToLong(); | |
973 | } | |
99
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
98
diff
changeset
|
974 | else |
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
98
diff
changeset
|
975 | comm->minargs++; |
88 | 976 | |
977 | comm->args << arg; | |
978 | } | |
979 | ||
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
980 | mLexer->MustGetNext (TK_ParenEnd); |
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
981 | mLexer->MustGetNext (TK_Semicolon); |
88 | 982 | AddCommandDefinition (comm); |
983 | } | |
984 | ||
985 | // ============================================================================ | |
986 | // Parses a command call | |
987 | DataBuffer* BotscriptParser::ParseCommand (CommandInfo* comm) | |
988 | { | |
989 | DataBuffer* r = new DataBuffer (64); | |
990 | ||
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
991 | if (mCurrentMode == PARSERMODE_TopLevel && comm->returnvalue == TYPE_Void) |
88 | 992 | Error ("command call at top level"); |
993 | ||
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
994 | mLexer->MustGetNext (TK_ParenStart); |
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
995 | mLexer->MustGetNext (TK_Any); |
88 | 996 | |
997 | int curarg = 0; | |
998 | ||
99
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
98
diff
changeset
|
999 | for (;;) |
88 | 1000 | { |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
1001 | if (TokenIs (TK_ParenEnd)) |
88 | 1002 | { |
99
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
98
diff
changeset
|
1003 | if (curarg < comm->minargs) |
88 | 1004 | Error ("too few arguments passed to %1\n\tusage is: %2", |
1005 | comm->name, comm->GetSignature()); | |
1006 | ||
1007 | break; | |
1008 | } | |
1009 | ||
99
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
98
diff
changeset
|
1010 | if (curarg >= comm->args.Size()) |
107
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
1011 | Error ("too many arguments (%3) passed to %1\n\tusage is: %2", |
88 | 1012 | comm->name, comm->GetSignature()); |
1013 | ||
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
|
1014 | r->MergeAndDestroy (ParseExpression (comm->args[curarg].type, true)); |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
1015 | mLexer->MustGetNext (TK_Any); |
88 | 1016 | |
99
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
98
diff
changeset
|
1017 | if (curarg < comm->minargs - 1) |
88 | 1018 | { |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
1019 | mLexer->TokenMustBe (TK_Comma); |
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
1020 | mLexer->MustGetNext (TK_Any); |
88 | 1021 | } |
99
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
98
diff
changeset
|
1022 | else if (curarg < comm->args.Size() - 1) |
88 | 1023 | { |
1024 | // Can continue, but can terminate as well. | |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
1025 | if (TokenIs (TK_ParenEnd)) |
88 | 1026 | { |
1027 | curarg++; | |
1028 | break; | |
1029 | } | |
1030 | else | |
1031 | { | |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
1032 | mLexer->TokenMustBe (TK_Comma); |
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
1033 | mLexer->MustGetNext (TK_Any); |
88 | 1034 | } |
1035 | } | |
1036 | ||
1037 | curarg++; | |
1038 | } | |
1039 | ||
1040 | // If the script skipped any optional arguments, fill in defaults. | |
99
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
98
diff
changeset
|
1041 | while (curarg < comm->args.Size()) |
88 | 1042 | { |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
1043 | r->WriteDWord (DH_PushNumber); |
88 | 1044 | r->WriteDWord (comm->args[curarg].defvalue); |
1045 | curarg++; | |
1046 | } | |
1047 | ||
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
1048 | r->WriteDWord (DH_Command); |
88 | 1049 | r->WriteDWord (comm->number); |
99
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
98
diff
changeset
|
1050 | r->WriteDWord (comm->args.Size()); |
88 | 1051 | |
1052 | return r; | |
1053 | } | |
1054 | ||
1055 | // ============================================================================ | |
1056 | // | |
1057 | String BotscriptParser::ParseFloat() | |
1058 | { | |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
1059 | mLexer->TokenMustBe (TK_Number); |
88 | 1060 | String floatstring = GetTokenString(); |
1061 | Lexer::Token tok; | |
1062 | ||
1063 | // Go after the decimal point | |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
1064 | if (mLexer->PeekNext (&tok) && tok.type ==TK_Dot) |
88 | 1065 | { |
1066 | mLexer->Skip(); | |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
1067 | mLexer->MustGetNext (TK_Number); |
88 | 1068 | floatstring += "."; |
1069 | floatstring += GetTokenString(); | |
1070 | } | |
1071 | ||
1072 | return floatstring; | |
1073 | } | |
1074 | ||
1075 | // ============================================================================ | |
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
|
1076 | // |
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
|
1077 | // Parses an assignment operator. |
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
|
1078 | // |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
1079 | AssignmentOperator BotscriptParser::ParseAssignmentOperator() |
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
|
1080 | { |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
1081 | const List<TokenType> tokens = |
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
|
1082 | { |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
1083 | TK_Assign, |
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
1084 | TK_AddAssign, |
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
1085 | TK_SubAssign, |
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
1086 | TK_MultiplyAssign, |
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
1087 | TK_DivideAssign, |
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
1088 | TK_ModulusAssign, |
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
1089 | TK_DoublePlus, |
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
1090 | TK_DoubleMinus, |
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
|
1091 | }; |
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
|
1092 | |
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
|
1093 | mLexer->MustGetAnyOf (tokens); |
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
|
1094 | |
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
|
1095 | switch (mLexer->GetTokenType()) |
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
|
1096 | { |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
1097 | case TK_Assign: return ASSIGNOP_Assign; |
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
1098 | case TK_AddAssign: return ASSIGNOP_Add; |
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
1099 | case TK_SubAssign: return ASSIGNOP_Subtract; |
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
1100 | case TK_MultiplyAssign: return ASSIGNOP_Multiply; |
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
1101 | case TK_DivideAssign: return ASSIGNOP_Divide; |
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
1102 | case TK_ModulusAssign: return ASSIGNOP_Modulus; |
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
1103 | case TK_DoublePlus: return ASSIGNOP_Increase; |
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
1104 | case TK_DoubleMinus: return ASSIGNOP_Decrease; |
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
|
1105 | default: break; |
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
|
1106 | } |
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
|
1107 | |
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
|
1108 | assert (false); |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
1109 | return (AssignmentOperator) 0; |
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
|
1110 | } |
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
|
1111 | |
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
|
1112 | // ============================================================================ |
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
|
1113 | // |
107
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
1114 | struct AssignmentDataHeaderInfo |
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
1115 | { |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
1116 | AssignmentOperator op; |
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
1117 | DataHeader local; |
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
1118 | DataHeader global; |
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
1119 | DataHeader array; |
107
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
1120 | }; |
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
1121 | |
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
1122 | const AssignmentDataHeaderInfo gAssignmentDataHeaders[] = |
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
1123 | { |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
1124 | { ASSIGNOP_Assign, DH_AssignLocalVar, DH_AssignGlobalVar, DH_AssignGlobalArray }, |
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
1125 | { ASSIGNOP_Add, DH_AddLocalVar, DH_AddGlobalVar, DH_AddGlobalArray }, |
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
1126 | { ASSIGNOP_Subtract, DH_SubtractLocalVar, DH_SubtractGlobalVar, DH_SubtractGlobalArray }, |
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
1127 | { ASSIGNOP_Multiply, DH_MultiplyLocalVar, DH_MultiplyGlobalVar, DH_MultiplyGlobalArray }, |
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
1128 | { ASSIGNOP_Divide, DH_DivideLocalVar, DH_DivideGlobalVar, DH_DivideGlobalArray }, |
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
1129 | { ASSIGNOP_Modulus, DH_ModLocalVar, DH_ModGlobalVar, DH_ModGlobalArray }, |
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
1130 | { ASSIGNOP_Increase, DH_IncreaseLocalVar, DH_IncreaseGlobalVar, DH_IncreaseGlobalArray }, |
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
1131 | { ASSIGNOP_Decrease, DH_DecreaseLocalVar, DH_DecreaseGlobalVar, DH_DecreaseGlobalArray }, |
107
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
1132 | }; |
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
1133 | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
1134 | DataHeader BotscriptParser::GetAssigmentDataHeader (AssignmentOperator op, Variable* var) |
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
|
1135 | { |
107
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
1136 | for (const auto& a : gAssignmentDataHeaders) |
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
|
1137 | { |
107
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
1138 | if (a.op != op) |
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
1139 | continue; |
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
1140 | |
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
1141 | if (var->isarray) |
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
1142 | return a.array; |
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
1143 | |
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
1144 | if (var->IsGlobal()) |
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
1145 | return a.global; |
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
1146 | |
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
1147 | return a.local; |
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
|
1148 | } |
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
|
1149 | |
107
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
1150 | Error ("WTF: couldn't find data header for operator #%1", op); |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
1151 | return (DataHeader) 0; |
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
|
1152 | } |
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
|
1153 | |
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
|
1154 | // ============================================================================ |
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
|
1155 | // |
88 | 1156 | // Parses an assignment. An assignment starts with a variable name, followed |
1157 | // by an assignment operator, followed by an expression value. Expects current | |
1158 | // token to be the name of the variable, and expects the variable to be given. | |
1159 | // | |
105
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
1160 | DataBuffer* BotscriptParser::ParseAssignment (Variable* var) |
88 | 1161 | { |
107
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
1162 | DataBuffer* retbuf = new DataBuffer; |
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
1163 | DataBuffer* arrayindex = null; |
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
1164 | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
1165 | if (var->writelevel != WRITE_Mutable) |
107
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
1166 | Error ("cannot alter read-only variable $%1", var->name); |
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
1167 | |
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
1168 | if (var->isarray) |
101
9ffae10ef76f
- variables: merged const and mutable variables into one system, added constexpr variable support. still no locals
Teemu Piippo <crimsondusk64@gmail.com>
parents:
100
diff
changeset
|
1169 | { |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
1170 | mLexer->MustGetNext (TK_BracketStart); |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
1171 | Expression expr (this, mLexer, TYPE_Int); |
107
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
1172 | expr.GetResult()->ConvertToBuffer(); |
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
1173 | arrayindex = expr.GetResult()->GetBuffer()->Clone(); |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
1174 | mLexer->MustGetNext (TK_BracketEnd); |
101
9ffae10ef76f
- variables: merged const and mutable variables into one system, added constexpr variable support. still no locals
Teemu Piippo <crimsondusk64@gmail.com>
parents:
100
diff
changeset
|
1175 | } |
9ffae10ef76f
- variables: merged const and mutable variables into one system, added constexpr variable support. still no locals
Teemu Piippo <crimsondusk64@gmail.com>
parents:
100
diff
changeset
|
1176 | |
88 | 1177 | // Get an operator |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
1178 | AssignmentOperator oper = ParseAssignmentOperator(); |
88 | 1179 | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
1180 | if (mCurrentMode == PARSERMODE_TopLevel) |
88 | 1181 | Error ("can't alter variables at top level"); |
1182 | ||
1183 | // Parse the right operand | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
1184 | if (oper != ASSIGNOP_Increase && oper != ASSIGNOP_Decrease) |
98
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1185 | { |
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1186 | DataBuffer* expr = ParseExpression (var->type); |
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1187 | retbuf->MergeAndDestroy (expr); |
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1188 | } |
88 | 1189 | |
107
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
1190 | if (var->isarray) |
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
1191 | retbuf->MergeAndDestroy (arrayindex); |
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
1192 | |
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
|
1193 | #if 0 |
88 | 1194 | // <<= and >>= do not have data headers. Solution: expand them. |
1195 | // a <<= b -> a = a << b | |
1196 | // a >>= b -> a = a >> b | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
1197 | retbuf->WriteDWord (var->IsGlobal() ? DH_PushGlobalVar : DH_PushLocalVar); |
98
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1198 | retbuf->WriteDWord (var->index); |
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1199 | retbuf->MergeAndDestroy (expr); |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
1200 | retbuf->WriteDWord ((oper == OPER_ASSIGNLEFTSHIFT) ? DH_LeftShift : DH_RightShift); |
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
1201 | retbuf->WriteDWord (var->IsGlobal() ? DH_AssignGlobalVar : DH_AssignLocalVar); |
98
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1202 | retbuf->WriteDWord (var->index); |
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
|
1203 | #endif |
88 | 1204 | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
1205 | DataHeader dh = GetAssigmentDataHeader (oper, var); |
98
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1206 | retbuf->WriteDWord (dh); |
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1207 | retbuf->WriteDWord (var->index); |
88 | 1208 | return retbuf; |
1209 | } | |
1210 | ||
1211 | // ============================================================================ | |
1212 | // | |
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1213 | void BotscriptParser::PushScope (EReset reset) |
88 | 1214 | { |
1215 | mScopeCursor++; | |
1216 | ||
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1217 | if (mScopeStack.Size() < mScopeCursor + 1) |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1218 | { |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1219 | ScopeInfo newscope; |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1220 | mScopeStack << newscope; |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
1221 | reset = SCOPE_Reset; |
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1222 | } |
88 | 1223 | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
1224 | if (reset == SCOPE_Reset) |
88 | 1225 | { |
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1226 | ScopeInfo* info = &SCOPE (0); |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
1227 | info->type = SCOPE_Unknown; |
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1228 | info->mark1 = null; |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1229 | info->mark2 = null; |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1230 | info->buffer1 = null; |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1231 | info->cases.Clear(); |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1232 | info->casecursor = info->cases.begin() - 1; |
88 | 1233 | } |
105
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
1234 | |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
1235 | // Reset variable stuff in any case |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
1236 | SCOPE(0).globalVarIndexBase = (mScopeCursor == 0) ? 0 : SCOPE(1).globalVarIndexBase; |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
1237 | SCOPE(0).localVarIndexBase = (mScopeCursor == 0) ? 0 : SCOPE(1).localVarIndexBase; |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
1238 | |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
1239 | for (Variable* var : SCOPE(0).globalVariables + SCOPE(0).localVariables) |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
1240 | delete var; |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
1241 | |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
1242 | SCOPE(0).localVariables.Clear(); |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
1243 | SCOPE(0).globalVariables.Clear(); |
88 | 1244 | } |
1245 | ||
1246 | // ============================================================================ | |
1247 | // | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
1248 | DataBuffer* BotscriptParser::ParseExpression (DataType reqtype, bool fromhere) |
91
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
1249 | { |
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
|
1250 | // hehe |
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
|
1251 | if (fromhere == true) |
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
|
1252 | mLexer->Skip (-1); |
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
|
1253 | |
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
|
1254 | Expression expr (this, mLexer, reqtype); |
91
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
1255 | expr.GetResult()->ConvertToBuffer(); |
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
|
1256 | |
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
|
1257 | // The buffer will be destroyed once the function ends so we need to |
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
|
1258 | // clone it now. |
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
|
1259 | return expr.GetResult()->GetBuffer()->Clone(); |
91
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
1260 | } |
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
1261 | |
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
1262 | // ============================================================================ |
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
1263 | // |
88 | 1264 | DataBuffer* BotscriptParser::ParseStatement() |
1265 | { | |
101
9ffae10ef76f
- variables: merged const and mutable variables into one system, added constexpr variable support. still no locals
Teemu Piippo <crimsondusk64@gmail.com>
parents:
100
diff
changeset
|
1266 | // If it's a variable, expect assignment. |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
1267 | if (mLexer->GetNext (TK_DollarSign)) |
101
9ffae10ef76f
- variables: merged const and mutable variables into one system, added constexpr variable support. still no locals
Teemu Piippo <crimsondusk64@gmail.com>
parents:
100
diff
changeset
|
1268 | { |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
1269 | mLexer->MustGetNext (TK_Symbol); |
105
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
1270 | Variable* var = FindVariable (GetTokenString()); |
88 | 1271 | |
101
9ffae10ef76f
- variables: merged const and mutable variables into one system, added constexpr variable support. still no locals
Teemu Piippo <crimsondusk64@gmail.com>
parents:
100
diff
changeset
|
1272 | if (var == null) |
9ffae10ef76f
- variables: merged const and mutable variables into one system, added constexpr variable support. still no locals
Teemu Piippo <crimsondusk64@gmail.com>
parents:
100
diff
changeset
|
1273 | Error ("unknown variable $%1", var->name); |
9ffae10ef76f
- variables: merged const and mutable variables into one system, added constexpr variable support. still no locals
Teemu Piippo <crimsondusk64@gmail.com>
parents:
100
diff
changeset
|
1274 | |
88 | 1275 | return ParseAssignment (var); |
101
9ffae10ef76f
- variables: merged const and mutable variables into one system, added constexpr variable support. still no locals
Teemu Piippo <crimsondusk64@gmail.com>
parents:
100
diff
changeset
|
1276 | } |
88 | 1277 | |
1278 | return null; | |
1279 | } | |
1280 | ||
1281 | // ============================================================================ | |
1282 | // | |
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1283 | void BotscriptParser::AddSwitchCase (DataBuffer* casebuffer) |
88 | 1284 | { |
1285 | ScopeInfo* info = &SCOPE (0); | |
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1286 | CaseInfo casedata; |
88 | 1287 | |
1288 | // Init a mark for the case buffer | |
1289 | ByteMark* casemark = buffer()->AddMark (""); | |
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1290 | casedata.mark = casemark; |
88 | 1291 | |
1292 | // Add a reference to the mark. "case" and "default" both | |
1293 | // add the necessary bytecode before the reference. | |
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1294 | if (casebuffer != null) |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1295 | casebuffer->AddReference (casemark); |
88 | 1296 | else |
1297 | buffer()->AddReference (casemark); | |
1298 | ||
1299 | // Init a buffer for the case block and tell the object | |
1300 | // writer to record all written data to it. | |
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1301 | casedata.data = mSwitchBuffer = new DataBuffer; |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1302 | SCOPE(0).cases << casedata; |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1303 | info->casecursor++; |
88 | 1304 | } |
1305 | ||
1306 | // ============================================================================ | |
1307 | // | |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
1308 | bool BotscriptParser::TokenIs (TokenType a) |
88 | 1309 | { |
1310 | return (mLexer->GetTokenType() == a); | |
1311 | } | |
1312 | ||
1313 | // ============================================================================ | |
1314 | // | |
1315 | String BotscriptParser::GetTokenString() | |
1316 | { | |
1317 | return mLexer->GetToken()->text; | |
1318 | } | |
1319 | ||
1320 | // ============================================================================ | |
1321 | // | |
1322 | String BotscriptParser::DescribePosition() const | |
1323 | { | |
1324 | Lexer::Token* tok = mLexer->GetToken(); | |
1325 | return tok->file + ":" + String (tok->line) + ":" + String (tok->column); | |
1326 | } | |
1327 | ||
1328 | // ============================================================================ | |
1329 | // | |
1330 | DataBuffer* BotscriptParser::buffer() | |
1331 | { | |
1332 | if (mSwitchBuffer != null) | |
1333 | return mSwitchBuffer; | |
1334 | ||
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
1335 | if (mCurrentMode == PARSERMODE_MainLoop) |
88 | 1336 | return mMainLoopBuffer; |
1337 | ||
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
1338 | if (mCurrentMode == PARSERMODE_Onenter) |
88 | 1339 | return mOnEnterBuffer; |
1340 | ||
1341 | return mMainBuffer; | |
1342 | } | |
1343 | ||
1344 | // ============================================================================ | |
1345 | // | |
1346 | void BotscriptParser::writeMemberBuffers() | |
1347 | { | |
1348 | // If there was no mainloop defined, write a dummy one now. | |
1349 | if (mGotMainLoop == false) | |
1350 | { | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
1351 | mMainLoopBuffer->WriteDWord (DH_MainLoop); |
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
1352 | mMainLoopBuffer->WriteDWord (DH_EndMainLoop); |
88 | 1353 | } |
1354 | ||
1355 | // Write the onenter and mainloop buffers, in that order in particular. | |
1356 | for (DataBuffer** bufp : List<DataBuffer**> ({&mOnEnterBuffer, &mMainLoopBuffer})) | |
1357 | { | |
1358 | buffer()->MergeAndDestroy (*bufp); | |
1359 | ||
1360 | // Clear the buffer afterwards for potential next state | |
1361 | *bufp = new DataBuffer; | |
1362 | } | |
1363 | ||
1364 | // Next state definitely has no mainloop yet | |
1365 | mGotMainLoop = false; | |
1366 | } | |
1367 | ||
1368 | // ============================================================================ | |
1369 | // | |
1370 | // Write string table | |
1371 | // | |
1372 | void BotscriptParser::WriteStringTable() | |
1373 | { | |
1374 | int stringcount = CountStringsInTable(); | |
1375 | ||
1376 | if (stringcount == 0) | |
1377 | return; | |
1378 | ||
1379 | // Write header | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
1380 | mMainBuffer->WriteDWord (DH_StringList); |
88 | 1381 | mMainBuffer->WriteDWord (stringcount); |
1382 | ||
1383 | // Write all strings | |
1384 | for (int i = 0; i < stringcount; i++) | |
1385 | mMainBuffer->WriteString (GetStringTable()[i]); | |
1386 | } | |
1387 | ||
1388 | // ============================================================================ | |
1389 | // | |
1390 | // Write the compiled bytecode to a file | |
1391 | // | |
1392 | void BotscriptParser::WriteToFile (String outfile) | |
1393 | { | |
106
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
1394 | FILE* fp = fopen (outfile, "wb"); |
88 | 1395 | |
1396 | if (fp == null) | |
1397 | Error ("couldn't open %1 for writing: %2", outfile, strerror (errno)); | |
1398 | ||
1399 | // First, resolve references | |
1400 | for (MarkReference* ref : mMainBuffer->GetReferences()) | |
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1401 | for (int i = 0; i < 4; ++i) |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1402 | mMainBuffer->GetBuffer()[ref->pos + i] = (ref->target->pos >> (8 * i)) & 0xFF; |
88 | 1403 | |
1404 | // Then, dump the main buffer to the file | |
1405 | fwrite (mMainBuffer->GetBuffer(), 1, mMainBuffer->GetWrittenSize(), fp); | |
1406 | Print ("-- %1 byte%s1 written to %2\n", mMainBuffer->GetWrittenSize(), outfile); | |
1407 | fclose (fp); | |
105
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
1408 | } |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
1409 | |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
1410 | // ============================================================================ |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
1411 | // |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
1412 | // Attempt to find the variable by the given name. Looks from current scope |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
1413 | // downwards. |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
1414 | // |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
1415 | Variable* BotscriptParser::FindVariable (const String& name) |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
1416 | { |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
1417 | for (int i = mScopeCursor; i >= 0; --i) |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
1418 | { |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
1419 | for (Variable* var : mScopeStack[i].globalVariables + mScopeStack[i].localVariables) |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
1420 | { |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
1421 | if (var->name == name) |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
1422 | return var; |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
1423 | } |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
1424 | } |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
1425 | |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
1426 | return null; |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
1427 | } |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
1428 | |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
1429 | // ============================================================================ |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
1430 | // |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
1431 | // Is the parser currently in global state (i.e. not in any specific state)? |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
1432 | // |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
1433 | bool BotscriptParser::IsInGlobalState() const |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
1434 | { |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
1435 | return mCurrentState.IsEmpty(); |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
104
diff
changeset
|
1436 | } |
106
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
1437 | |
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
1438 | // ============================================================================ |
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
1439 | // |
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
1440 | void BotscriptParser::SuggestHighestVarIndex (bool global, int index) |
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
1441 | { |
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
1442 | if (global) |
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
1443 | mHighestGlobalVarIndex = max (mHighestGlobalVarIndex, index); |
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
1444 | else |
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
1445 | mHighestStateVarIndex = max (mHighestStateVarIndex, index); |
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
1446 | } |
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
1447 | |
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
1448 | // ============================================================================ |
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
1449 | // |
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
1450 | int BotscriptParser::GetHighestVarIndex (bool global) |
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
1451 | { |
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
1452 | if (global) |
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
1453 | return mHighestGlobalVarIndex; |
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
1454 | |
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
1455 | return mHighestStateVarIndex; |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
1456 | } |