Tue, 11 Feb 2014 03:29:03 +0200
- implemented arrays, don't quite work 100% yet
- restructured Expression::ParseSymbol, local exception no longer needed
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), |
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
|
50 | mCurrentMode (ETopLevelMode), |
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 | { | |
69 | if (mCurrentMode != ETopLevelMode) | |
70 | Error ("%1-statements may only be defined at top level!", GetTokenString()); | |
71 | } | |
72 | ||
73 | // ============================================================================ | |
74 | // | |
75 | void BotscriptParser::CheckNotToplevel() | |
76 | { | |
77 | if (mCurrentMode == ETopLevelMode) | |
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 | |
96 | if (TokenIs (tkElse) && mCanElse == false) | |
97 | Error ("else without preceding if"); | |
98 | ||
99 | if (TokenIs (tkElse) == false) | |
100 | mCanElse = false; | |
101 | ||
102 | switch (mLexer->GetToken()->type) | |
103 | { | |
104 | case tkState: | |
105 | ParseStateBlock(); | |
106 | break; | |
107 | ||
108 | case tkEvent: | |
109 | ParseEventBlock(); | |
110 | break; | |
111 | ||
112 | case tkMainloop: | |
113 | ParseMainloop(); | |
114 | break; | |
115 | ||
116 | case tkOnenter: | |
117 | case tkOnexit: | |
118 | ParseOnEnterExit(); | |
119 | break; | |
120 | ||
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
|
121 | case tkVar: |
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 | ||
125 | case tkGoto: | |
126 | ParseGoto(); | |
127 | break; | |
128 | ||
129 | case tkIf: | |
130 | ParseIf(); | |
131 | break; | |
132 | ||
133 | case tkElse: | |
134 | ParseElse(); | |
135 | break; | |
136 | ||
137 | case tkWhile: | |
138 | ParseWhileBlock(); | |
139 | break; | |
140 | ||
141 | case tkFor: | |
142 | ParseForBlock(); | |
143 | break; | |
144 | ||
145 | case tkDo: | |
146 | ParseDoBlock(); | |
147 | break; | |
148 | ||
149 | case tkSwitch: | |
150 | ParseSwitchBlock(); | |
151 | break; | |
152 | ||
153 | case tkCase: | |
154 | ParseSwitchCase(); | |
155 | break; | |
156 | ||
157 | case tkDefault: | |
158 | ParseSwitchDefault(); | |
159 | break; | |
160 | ||
161 | case tkBreak: | |
162 | ParseBreak(); | |
163 | break; | |
164 | ||
165 | case tkContinue: | |
166 | ParseContinue(); | |
167 | break; | |
168 | ||
169 | case tkBraceEnd: | |
170 | ParseBlockEnd(); | |
171 | break; | |
172 | ||
173 | case tkEventdef: | |
174 | ParseEventdef(); | |
175 | break; | |
176 | ||
177 | case tkFuncdef: | |
178 | ParseFuncdef(); | |
179 | break; | |
180 | ||
107
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
181 | case tkSemicolon: |
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 | ||
189 | if (TokenIs (tkSymbol) && | |
190 | mLexer->PeekNext (&next) && | |
191 | next.type == tkColon) | |
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)); | |
203 | mLexer->MustGetNext (tkSemicolon); | |
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); | |
218 | mLexer->MustGetNext (tkSemicolon); | |
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 | |
226 | if (mCurrentMode != ETopLevelMode) | |
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(); | |
259 | mLexer->MustGetNext (tkString); | |
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 | |
272 | mLexer->MustGetNext (tkColon); | |
273 | ||
274 | // write the previous state's onenter and | |
275 | // mainloop buffers to file now | |
276 | if (mCurrentState.IsEmpty() == false) | |
277 | writeMemberBuffers(); | |
278 | ||
279 | buffer()->WriteDWord (dhStateName); | |
280 | buffer()->WriteString (statename); | |
281 | buffer()->WriteDWord (dhStateIndex); | |
282 | buffer()->WriteDWord (mNumStates); | |
283 | ||
284 | mNumStates++; | |
285 | mCurrentState = statename; | |
286 | mGotMainLoop = false; | |
287 | } | |
288 | ||
289 | // ============================================================================ | |
290 | // | |
291 | void BotscriptParser::ParseEventBlock() | |
292 | { | |
293 | CheckToplevel(); | |
294 | mLexer->MustGetNext (tkString); | |
295 | ||
296 | EventDefinition* e = FindEventByName (GetTokenString()); | |
297 | ||
298 | if (e == null) | |
299 | Error ("bad event, got `%1`\n", GetTokenString()); | |
300 | ||
301 | mLexer->MustGetNext (tkBraceStart); | |
302 | mCurrentMode = EEventMode; | |
303 | buffer()->WriteDWord (dhEvent); | |
304 | buffer()->WriteDWord (e->number); | |
305 | mNumEvents++; | |
306 | } | |
307 | ||
308 | // ============================================================================ | |
309 | // | |
310 | void BotscriptParser::ParseMainloop() | |
311 | { | |
312 | CheckToplevel(); | |
313 | mLexer->MustGetNext (tkBraceStart); | |
314 | ||
315 | mCurrentMode = EMainLoopMode; | |
316 | mMainLoopBuffer->WriteDWord (dhMainLoop); | |
317 | } | |
318 | ||
319 | // ============================================================================ | |
320 | // | |
321 | void BotscriptParser::ParseOnEnterExit() | |
322 | { | |
323 | CheckToplevel(); | |
324 | bool onenter = (TokenIs (tkOnenter)); | |
325 | mLexer->MustGetNext (tkBraceStart); | |
326 | ||
327 | mCurrentMode = onenter ? EOnenterMode : EOnexitMode; | |
328 | buffer()->WriteDWord (onenter ? dhOnEnter : dhOnExit); | |
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; |
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
|
338 | const bool isconst = mLexer->GetNext (tkConst); |
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
|
339 | mLexer->MustGetAnyOf ({tkInt, tkStr, tkVoid}); |
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 | |
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
|
341 | EType vartype = (TokenIs (tkInt)) ? EIntType : |
88 | 342 | (TokenIs (tkStr)) ? EStringType : |
343 | EBoolType; | |
344 | ||
102
28f89ca1a236
- whoopsie, cannot allow garbage variable names there
Teemu Piippo <crimsondusk64@gmail.com>
parents:
101
diff
changeset
|
345 | mLexer->MustGetNext (tkSymbol); |
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 | |
107
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
348 | if (mLexer->GetNext (tkBracketStart)) |
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
349 | { |
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
350 | mLexer->MustGetNext (tkBracketEnd); |
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 | { |
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
|
370 | var->writelevel = Variable::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 | { |
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
|
374 | mLexer->MustGetNext (tkAssign); |
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 | { |
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
|
381 | var->writelevel = Variable::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. |
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
|
394 | if (var->writelevel != Variable::WRITE_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
|
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); |
88 | 412 | mLexer->MustGetNext (tkSemicolon); |
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 | |
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
|
423 | mLexer->MustGetNext (tkSymbol); |
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. | |
439 | buffer()->WriteDWord (dhGoto); | |
440 | buffer()->AddReference (mark); | |
441 | mLexer->MustGetNext (tkSemicolon); | |
442 | } | |
443 | ||
444 | // ============================================================================ | |
445 | // | |
446 | void BotscriptParser::ParseIf() | |
447 | { | |
448 | CheckNotToplevel(); | |
449 | PushScope(); | |
450 | ||
451 | // Condition | |
452 | mLexer->MustGetNext (tkParenStart); | |
453 | ||
454 | // Read the expression and write it. | |
455 | DataBuffer* c = ParseExpression (EIntType); | |
456 | buffer()->MergeAndDestroy (c); | |
457 | ||
458 | mLexer->MustGetNext (tkParenEnd); | |
459 | mLexer->MustGetNext (tkBraceStart); | |
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 | ||
465 | // Use dhIfNotGoto - if the expression is not true, we goto the mark | |
466 | // we just defined - and this mark will be at the end of the scope block. | |
467 | buffer()->WriteDWord (dhIfNotGoto); | |
468 | buffer()->AddReference (mark); | |
469 | ||
470 | // Store it | |
471 | SCOPE (0).mark1 = mark; | |
472 | SCOPE (0).type = eIfScope; | |
473 | } | |
474 | ||
475 | // ============================================================================ | |
476 | // | |
477 | void BotscriptParser::ParseElse() | |
478 | { | |
479 | CheckNotToplevel(); | |
480 | mLexer->MustGetNext (tkBraceStart); | |
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
481 | PushScope (eNoReset); |
88 | 482 | |
483 | if (SCOPE (0).type != eIfScope) | |
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 | |
491 | buffer()->WriteDWord (dhGoto); | |
492 | buffer()->AddReference (SCOPE (0).mark2); | |
493 | ||
494 | // Move the ifnot mark here and set type to else | |
495 | buffer()->AdjustMark (SCOPE (0).mark1); | |
496 | SCOPE (0).type = eElseScope; | |
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 | |
514 | mLexer->MustGetNext (tkParenStart); | |
515 | DataBuffer* expr = ParseExpression (EIntType); | |
516 | mLexer->MustGetNext (tkParenEnd); | |
517 | mLexer->MustGetNext (tkBraceStart); | |
518 | ||
519 | // write condition | |
520 | buffer()->MergeAndDestroy (expr); | |
521 | ||
522 | // Instruction to go to the end if it fails | |
523 | buffer()->WriteDWord (dhIfNotGoto); | |
524 | buffer()->AddReference (mark2); | |
525 | ||
526 | // Store the needed stuff | |
527 | SCOPE (0).mark1 = mark1; | |
528 | SCOPE (0).mark2 = mark2; | |
529 | SCOPE (0).type = eWhileScope; | |
530 | } | |
531 | ||
532 | // ============================================================================ | |
533 | // | |
534 | void BotscriptParser::ParseForBlock() | |
535 | { | |
536 | CheckNotToplevel(); | |
537 | PushScope(); | |
538 | ||
539 | // Initializer | |
540 | mLexer->MustGetNext (tkParenStart); | |
541 | DataBuffer* init = ParseStatement(); | |
542 | ||
543 | if (init == null) | |
544 | Error ("bad statement for initializer of for"); | |
545 | ||
546 | mLexer->MustGetNext (tkSemicolon); | |
547 | ||
548 | // Condition | |
549 | DataBuffer* cond = ParseExpression (EIntType); | |
550 | ||
551 | if (cond == null) | |
552 | Error ("bad statement for condition of for"); | |
553 | ||
554 | mLexer->MustGetNext (tkSemicolon); | |
555 | ||
556 | // Incrementor | |
557 | DataBuffer* incr = ParseStatement(); | |
558 | ||
559 | if (incr == null) | |
560 | Error ("bad statement for incrementor of for"); | |
561 | ||
562 | mLexer->MustGetNext (tkParenEnd); | |
563 | mLexer->MustGetNext (tkBraceStart); | |
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); | |
574 | buffer()->WriteDWord (dhIfNotGoto); | |
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; | |
581 | SCOPE (0).type = eForScope; | |
582 | } | |
583 | ||
584 | // ============================================================================ | |
585 | // | |
586 | void BotscriptParser::ParseDoBlock() | |
587 | { | |
588 | CheckNotToplevel(); | |
589 | PushScope(); | |
590 | mLexer->MustGetNext (tkBraceStart); | |
591 | SCOPE (0).mark1 = buffer()->AddMark (""); | |
592 | SCOPE (0).type = eDoScope; | |
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(); | |
614 | mLexer->MustGetNext (tkParenStart); | |
615 | buffer()->MergeAndDestroy (ParseExpression (EIntType)); | |
616 | mLexer->MustGetNext (tkParenEnd); | |
617 | mLexer->MustGetNext (tkBraceStart); | |
618 | SCOPE (0).type = eSwitchScope; | |
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 | |
628 | if (SCOPE (0).type != eSwitchScope) | |
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. |
88 | 633 | mLexer->MustGetNext (tkNumber); |
634 | int num = mLexer->GetToken()->text.ToLong(); | |
635 | mLexer->MustGetNext (tkColon); | |
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; | |
652 | buffer()->WriteDWord (dhCaseGoto); | |
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 | { | |
662 | if (SCOPE (0).type != eSwitchScope) | |
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 | ||
668 | mLexer->MustGetNext (tkColon); | |
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 | |
675 | // to pop it with dhDrop manually if we end up in | |
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; |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
679 | buf->WriteDWord (dhDrop); |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
680 | buf->WriteDWord (dhGoto); |
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 | ||
691 | buffer()->WriteDWord (dhGoto); | |
692 | ||
693 | // switch and if use mark1 for the closing point, | |
694 | // for and while use mark2. | |
695 | switch (SCOPE (0).type) | |
696 | { | |
697 | case eIfScope: | |
698 | case eSwitchScope: | |
699 | { | |
700 | buffer()->AddReference (SCOPE (0).mark1); | |
701 | } break; | |
702 | ||
703 | case eForScope: | |
704 | case eWhileScope: | |
705 | { | |
706 | buffer()->AddReference (SCOPE (0).mark2); | |
707 | } break; | |
708 | ||
709 | default: | |
710 | { | |
711 | Error ("unexpected `break`"); | |
712 | } break; | |
713 | } | |
714 | ||
715 | mLexer->MustGetNext (tkSemicolon); | |
716 | } | |
717 | ||
718 | // ============================================================================ | |
719 | // | |
720 | void BotscriptParser::ParseContinue() | |
721 | { | |
722 | mLexer->MustGetNext (tkSemicolon); | |
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 | { | |
732 | case eForScope: | |
733 | case eWhileScope: | |
734 | case eDoScope: | |
735 | { | |
736 | buffer()->WriteDWord (dhGoto); | |
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 | { | |
761 | case eIfScope: | |
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 | |
771 | case eElseScope: | |
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 | |
779 | case eForScope: | |
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 | } |
88 | 783 | case eWhileScope: |
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 |
88 | 785 | buffer()->WriteDWord (dhGoto); |
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 | |
793 | case eDoScope: | |
794 | { | |
795 | mLexer->MustGetNext (tkWhile); | |
796 | mLexer->MustGetNext (tkParenStart); | |
797 | DataBuffer* expr = ParseExpression (EIntType); | |
798 | mLexer->MustGetNext (tkParenEnd); | |
799 | mLexer->MustGetNext (tkSemicolon); | |
800 | ||
801 | // If the condition runs true, go back to the start. | |
802 | buffer()->MergeAndDestroy (expr); | |
803 | buffer()->WriteDWord (dhIfGoto); | |
804 | buffer()->AddReference (SCOPE (0).mark1); | |
805 | break; | |
806 | } | |
807 | ||
808 | case eSwitchScope: | |
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 | { | |
824 | buffer()->WriteDWord (dhDrop); | |
825 | buffer()->WriteDWord (dhGoto); | |
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 | ||
842 | case eUnknownScope: | |
843 | break; | |
844 | } | |
845 | ||
846 | // Descend down the stack | |
847 | mScopeCursor--; | |
848 | return; | |
849 | } | |
850 | ||
851 | int dataheader = (mCurrentMode == EEventMode) ? dhEndEvent : | |
852 | (mCurrentMode == EMainLoopMode) ? dhEndMainLoop : | |
853 | (mCurrentMode == EOnenterMode) ? dhEndOnEnter : | |
854 | (mCurrentMode == EOnexitMode) ? dhEndOnExit : -1; | |
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); | |
863 | mCurrentMode = ETopLevelMode; | |
864 | mLexer->GetNext (tkSemicolon); | |
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 | ||
893 | mLexer->MustGetNext (tkColon); | |
894 | } | |
895 | ||
896 | // ============================================================================= | |
897 | // | |
898 | void BotscriptParser::ParseEventdef() | |
899 | { | |
900 | EventDefinition* e = new EventDefinition; | |
901 | ||
902 | mLexer->MustGetNext (tkNumber); | |
903 | e->number = GetTokenString().ToLong(); | |
904 | mLexer->MustGetNext (tkColon); | |
905 | mLexer->MustGetNext (tkSymbol); | |
906 | e->name = mLexer->GetToken()->text; | |
907 | mLexer->MustGetNext (tkParenStart); | |
908 | mLexer->MustGetNext (tkParenEnd); | |
909 | mLexer->MustGetNext (tkSemicolon); | |
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 |
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
98
diff
changeset
|
921 | mLexer->MustGetAnyOf ({tkInt, tkVoid, tkBool, tkStr}); |
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 |
926 | mLexer->MustGetNext (tkNumber); | |
927 | comm->number = mLexer->GetToken()->text.ToLong(); | |
928 | mLexer->MustGetNext (tkColon); | |
929 | ||
930 | // Name | |
931 | mLexer->MustGetNext (tkSymbol); | |
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 |
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
98
diff
changeset
|
935 | mLexer->MustGetNext (tkParenStart); |
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
98
diff
changeset
|
936 | comm->minargs = 0; |
88 | 937 | |
99
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
98
diff
changeset
|
938 | while (mLexer->PeekNextType (tkParenEnd) == false) |
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) |
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
98
diff
changeset
|
941 | mLexer->MustGetNext (tkComma); |
88 | 942 | |
943 | CommandArgument arg; | |
944 | mLexer->MustGetAnyOf ({tkInt, tkBool, tkStr}); | |
99
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
98
diff
changeset
|
945 | EType type = GetTypeByName (mLexer->GetToken()->text); // TODO |
88 | 946 | assert (type != -1 && type != EVoidType); |
947 | arg.type = type; | |
948 | ||
949 | mLexer->MustGetNext (tkSymbol); | |
950 | arg.name = mLexer->GetToken()->text; | |
951 | ||
952 | // If this is an optional parameter, we need the default value. | |
99
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
98
diff
changeset
|
953 | if (comm->minargs < comm->args.Size() || mLexer->PeekNextType (tkAssign)) |
88 | 954 | { |
955 | mLexer->MustGetNext (tkAssign); | |
956 | ||
957 | switch (type) | |
958 | { | |
959 | case EIntType: | |
960 | case EBoolType: | |
961 | mLexer->MustGetNext (tkNumber); | |
962 | break; | |
963 | ||
964 | case EStringType: | |
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 | |
967 | case EUnknownType: | |
968 | case EVoidType: | |
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 | ||
99
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
98
diff
changeset
|
980 | mLexer->MustGetNext (tkParenEnd); |
88 | 981 | mLexer->MustGetNext (tkSemicolon); |
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 | ||
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
|
991 | if (mCurrentMode == ETopLevelMode && comm->returnvalue == EVoidType) |
88 | 992 | Error ("command call at top level"); |
993 | ||
994 | mLexer->MustGetNext (tkParenStart); | |
103
48472c0678cc
- removed tkAny as the default value of Lexer::MustGetNext to prevent problems like the one last commit fixed
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
995 | mLexer->MustGetNext (tkAny); |
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 | { |
1001 | if (TokenIs (tkParenEnd)) | |
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)); |
103
48472c0678cc
- removed tkAny as the default value of Lexer::MustGetNext to prevent problems like the one last commit fixed
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
1015 | mLexer->MustGetNext (tkAny); |
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 | { |
1019 | mLexer->TokenMustBe (tkComma); | |
103
48472c0678cc
- removed tkAny as the default value of Lexer::MustGetNext to prevent problems like the one last commit fixed
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
1020 | mLexer->MustGetNext (tkAny); |
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. | |
1025 | if (TokenIs (tkParenEnd)) | |
1026 | { | |
1027 | curarg++; | |
1028 | break; | |
1029 | } | |
1030 | else | |
1031 | { | |
1032 | mLexer->TokenMustBe (tkComma); | |
103
48472c0678cc
- removed tkAny as the default value of Lexer::MustGetNext to prevent problems like the one last commit fixed
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
1033 | mLexer->MustGetNext (tkAny); |
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 | { |
1043 | r->WriteDWord (dhPushNumber); | |
1044 | r->WriteDWord (comm->args[curarg].defvalue); | |
1045 | curarg++; | |
1046 | } | |
1047 | ||
1048 | r->WriteDWord (dhCommand); | |
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 | { | |
1059 | mLexer->TokenMustBe (tkNumber); | |
1060 | String floatstring = GetTokenString(); | |
1061 | Lexer::Token tok; | |
1062 | ||
1063 | // Go after the decimal point | |
1064 | if (mLexer->PeekNext (&tok) && tok.type == tkDot) | |
1065 | { | |
1066 | mLexer->Skip(); | |
1067 | mLexer->MustGetNext (tkNumber); | |
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 | // |
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
|
1079 | EAssignmentOperator BotscriptParser::ParseAssignmentOperator() |
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
1080 | { |
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
|
1081 | const List<EToken> 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
|
1082 | { |
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
|
1083 | tkAssign, |
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
|
1084 | tkAddAssign, |
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
|
1085 | tkSubAssign, |
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
|
1086 | tkMultiplyAssign, |
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
|
1087 | tkDivideAssign, |
98
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1088 | tkModulusAssign, |
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1089 | tkDoublePlus, |
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1090 | tkDoubleMinus, |
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 | { |
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
|
1097 | case tkAssign: return EAssign; |
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
1098 | case tkAddAssign: return EAssignAdd; |
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
1099 | case tkSubAssign: return EAssignSub; |
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
1100 | case tkMultiplyAssign: return EAssignMul; |
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
1101 | case tkDivideAssign: return EAssignDiv; |
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
1102 | case tkModulusAssign: return EAssignMod; |
98
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1103 | case tkDoublePlus: return EAssignIncrement; |
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1104 | case tkDoubleMinus: return EAssignDecrement; |
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); |
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
|
1109 | return (EAssignmentOperator) 0; |
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 | { |
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
1116 | EAssignmentOperator op; |
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
1117 | EDataHeader local; |
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
1118 | EDataHeader global; |
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
1119 | EDataHeader array; |
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 | { |
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
1124 | { EAssign, dhAssignLocalVar, dhAssignGlobalVar, dhAssignGlobalArray }, |
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
1125 | { EAssignAdd, dhAddLocalVar, dhAddGlobalVar, dhAddGlobalArray }, |
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
1126 | { EAssignSub, dhSubtractLocalVar, dhSubtractGlobalVar, dhSubtractGlobalArray }, |
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
1127 | { EAssignMul, dhMultiplyLocalVar, dhMultiplyGlobalVar, dhMultiplyGlobalArray }, |
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
1128 | { EAssignDiv, dhDivideLocalVar, dhDivideGlobalVar, dhDivideGlobalArray }, |
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
1129 | { EAssignMod, dhModLocalVar, dhModGlobalVar, dhModGlobalArray }, |
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
1130 | { EAssignIncrement, dhIncreaseLocalVar, dhIncreaseGlobalVar, dhIncreaseGlobalArray }, |
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
1131 | { EAssignDecrement, dhDecreaseLocalVar, dhDecreaseGlobalVar, dhDecreaseGlobalArray }, |
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 | |
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
|
1134 | EDataHeader BotscriptParser::GetAssigmentDataHeader (EAssignmentOperator 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); |
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
|
1151 | return (EDataHeader) 0; |
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 | |
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
|
1165 | if (var->writelevel != Variable::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 | { |
107
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
1170 | mLexer->MustGetNext (tkBracketStart); |
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
1171 | Expression expr (this, mLexer, EIntType); |
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(); |
55c2bcd8ed5c
- implemented arrays, don't quite work 100% yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
1174 | mLexer->MustGetNext (tkBracketEnd); |
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 |
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
|
1178 | EAssignmentOperator oper = ParseAssignmentOperator(); |
88 | 1179 | |
1180 | if (mCurrentMode == ETopLevelMode) | |
1181 | Error ("can't alter variables at top level"); | |
1182 | ||
1183 | // Parse the right operand | |
98
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1184 | if (oper != EAssignIncrement && oper != EAssignDecrement) |
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 | |
98
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1197 | retbuf->WriteDWord (var->IsGlobal() ? dhPushGlobalVar : dhPushLocalVar); |
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); |
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1200 | retbuf->WriteDWord ((oper == OPER_ASSIGNLEFTSHIFT) ? dhLeftShift : dhRightShift); |
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1201 | retbuf->WriteDWord (var->IsGlobal() ? dhAssignGlobalVar : dhAssignLocalVar); |
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 | |
98
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1205 | EDataHeader dh = GetAssigmentDataHeader (oper, var); |
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; |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1221 | reset = eResetScope; |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1222 | } |
88 | 1223 | |
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1224 | if (reset == eResetScope) |
88 | 1225 | { |
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1226 | ScopeInfo* info = &SCOPE (0); |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1227 | info->type = eUnknownScope; |
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 | // | |
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
|
1248 | DataBuffer* BotscriptParser::ParseExpression (EType 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. |
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
|
1267 | if (mLexer->GetNext (tkDollarSign)) |
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 | { |
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
|
1269 | mLexer->MustGetNext (tkSymbol); |
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 | // | |
1308 | bool BotscriptParser::TokenIs (EToken a) | |
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 | ||
1335 | if (mCurrentMode == EMainLoopMode) | |
1336 | return mMainLoopBuffer; | |
1337 | ||
1338 | if (mCurrentMode == EOnenterMode) | |
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 | { | |
1351 | mMainLoopBuffer->WriteDWord (dhMainLoop); | |
1352 | mMainLoopBuffer->WriteDWord (dhEndMainLoop); | |
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 | |
1380 | mMainBuffer->WriteDWord (dhStringList); | |
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; |
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
1456 | } |