Sun, 09 Feb 2014 22:43:58 +0200
- improved error handling a tad
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 | ||
181 | default: | |
182 | { | |
183 | // Check for labels | |
184 | Lexer::Token next; | |
185 | ||
186 | if (TokenIs (tkSymbol) && | |
187 | mLexer->PeekNext (&next) && | |
188 | next.type == tkColon) | |
189 | { | |
190 | ParseLabel(); | |
191 | break; | |
192 | } | |
193 | ||
194 | // Check if it's a command | |
195 | CommandInfo* comm = FindCommandByName (GetTokenString()); | |
196 | ||
197 | if (comm) | |
198 | { | |
199 | buffer()->MergeAndDestroy (ParseCommand (comm)); | |
200 | mLexer->MustGetNext (tkSemicolon); | |
201 | continue; | |
202 | } | |
203 | ||
204 | // 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
|
205 | mLexer->Skip (-1); |
88 | 206 | DataBuffer* b = ParseStatement(); |
207 | ||
208 | if (b == false) | |
209 | Error ("unknown token `%1`", GetTokenString()); | |
210 | ||
211 | buffer()->MergeAndDestroy (b); | |
212 | mLexer->MustGetNext (tkSemicolon); | |
213 | } | |
214 | break; | |
215 | } | |
216 | } | |
217 | ||
218 | // =============================================================================== | |
219 | // Script file ended. Do some last checks and write the last things to main buffer | |
220 | if (mCurrentMode != ETopLevelMode) | |
221 | Error ("script did not end at top level; a `}` is missing somewhere"); | |
222 | ||
223 | if (IsReadOnly() == false) | |
224 | { | |
225 | // stateSpawn must be defined! | |
226 | if (mStateSpawnDefined == false) | |
227 | Error ("script must have a state named `stateSpawn`!"); | |
228 | ||
229 | // Ensure no goto target is left undefined | |
230 | if (mUndefinedLabels.IsEmpty() == false) | |
231 | { | |
232 | StringList names; | |
233 | ||
234 | for (UndefinedLabel& undf : mUndefinedLabels) | |
235 | names << undf.name; | |
236 | ||
237 | Error ("labels `%1` are referenced via `goto` but are not defined\n", names); | |
238 | } | |
239 | ||
240 | // Dump the last state's onenter and mainloop | |
241 | writeMemberBuffers(); | |
242 | ||
243 | // String table | |
244 | WriteStringTable(); | |
245 | } | |
246 | } | |
247 | ||
248 | // ============================================================================ | |
249 | // | |
250 | void BotscriptParser::ParseStateBlock() | |
251 | { | |
252 | CheckToplevel(); | |
253 | mLexer->MustGetNext (tkString); | |
254 | String statename = GetTokenString(); | |
255 | ||
256 | // State name must be a word. | |
257 | if (statename.FirstIndexOf (" ") != -1) | |
258 | Error ("state name must be a single word, got `%1`", statename); | |
259 | ||
260 | // stateSpawn is special - it *must* be defined. If we | |
261 | // encountered it, then mark down that we have it. | |
262 | if (-statename == "statespawn") | |
263 | mStateSpawnDefined = true; | |
264 | ||
265 | // Must end in a colon | |
266 | mLexer->MustGetNext (tkColon); | |
267 | ||
268 | // write the previous state's onenter and | |
269 | // mainloop buffers to file now | |
270 | if (mCurrentState.IsEmpty() == false) | |
271 | writeMemberBuffers(); | |
272 | ||
273 | buffer()->WriteDWord (dhStateName); | |
274 | buffer()->WriteString (statename); | |
275 | buffer()->WriteDWord (dhStateIndex); | |
276 | buffer()->WriteDWord (mNumStates); | |
277 | ||
278 | mNumStates++; | |
279 | mCurrentState = statename; | |
280 | mGotMainLoop = false; | |
281 | } | |
282 | ||
283 | // ============================================================================ | |
284 | // | |
285 | void BotscriptParser::ParseEventBlock() | |
286 | { | |
287 | CheckToplevel(); | |
288 | mLexer->MustGetNext (tkString); | |
289 | ||
290 | EventDefinition* e = FindEventByName (GetTokenString()); | |
291 | ||
292 | if (e == null) | |
293 | Error ("bad event, got `%1`\n", GetTokenString()); | |
294 | ||
295 | mLexer->MustGetNext (tkBraceStart); | |
296 | mCurrentMode = EEventMode; | |
297 | buffer()->WriteDWord (dhEvent); | |
298 | buffer()->WriteDWord (e->number); | |
299 | mNumEvents++; | |
300 | } | |
301 | ||
302 | // ============================================================================ | |
303 | // | |
304 | void BotscriptParser::ParseMainloop() | |
305 | { | |
306 | CheckToplevel(); | |
307 | mLexer->MustGetNext (tkBraceStart); | |
308 | ||
309 | mCurrentMode = EMainLoopMode; | |
310 | mMainLoopBuffer->WriteDWord (dhMainLoop); | |
311 | } | |
312 | ||
313 | // ============================================================================ | |
314 | // | |
315 | void BotscriptParser::ParseOnEnterExit() | |
316 | { | |
317 | CheckToplevel(); | |
318 | bool onenter = (TokenIs (tkOnenter)); | |
319 | mLexer->MustGetNext (tkBraceStart); | |
320 | ||
321 | mCurrentMode = onenter ? EOnenterMode : EOnexitMode; | |
322 | buffer()->WriteDWord (onenter ? dhOnEnter : dhOnExit); | |
323 | } | |
324 | ||
325 | // ============================================================================ | |
326 | // | |
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
|
327 | void BotscriptParser::ParseVar() |
88 | 328 | { |
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
|
329 | 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
|
330 | var->origin = mLexer->DescribeCurrentPosition(); |
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
|
331 | 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
|
332 | 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
|
333 | |
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
|
334 | EType vartype = (TokenIs (tkInt)) ? EIntType : |
88 | 335 | (TokenIs (tkStr)) ? EStringType : |
336 | EBoolType; | |
337 | ||
102
28f89ca1a236
- whoopsie, cannot allow garbage variable names there
Teemu Piippo <crimsondusk64@gmail.com>
parents:
101
diff
changeset
|
338 | 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
|
339 | 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
|
340 | |
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 | 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
|
342 | { |
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
|
343 | 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
|
344 | 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
|
345 | 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
|
346 | } |
88 | 347 | |
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
|
348 | 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
|
349 | 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
|
350 | 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
|
351 | |
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
|
352 | 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
|
353 | { |
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
|
354 | 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
|
355 | } |
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
|
356 | 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
|
357 | { |
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
|
358 | 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
|
359 | 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
|
360 | |
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
|
361 | // 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
|
362 | // 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
|
363 | 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
|
364 | { |
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
|
365 | 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
|
366 | 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
|
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 | 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
|
369 | { |
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
|
370 | // TODO: might need a VM-wise oninit for this... |
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 | Error ("const variables must be constexpr for now"); |
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 | } |
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 | |
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 | // 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
|
376 | // 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
|
377 | // 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
|
378 | 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
|
379 | { |
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
|
380 | 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
|
381 | 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
|
382 | |
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
|
383 | 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
|
384 | (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
|
385 | { |
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
|
386 | 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
|
387 | } |
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
|
388 | } |
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
|
389 | |
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
|
390 | 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
|
391 | 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
|
392 | 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
|
393 | 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
|
394 | |
106
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
395 | SuggestHighestVarIndex (IsInGlobalState(), var->index); |
88 | 396 | 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
|
397 | Print ("Declared %3 variable #%1 $%2\n", var->index, var->name, IsInGlobalState() ? "global" : "state-local"); |
88 | 398 | } |
399 | ||
400 | // ============================================================================ | |
401 | // | |
402 | void BotscriptParser::ParseGoto() | |
403 | { | |
404 | CheckNotToplevel(); | |
405 | ||
406 | // 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
|
407 | mLexer->MustGetNext (tkSymbol); |
88 | 408 | |
409 | // Find the mark this goto statement points to | |
410 | String target = GetTokenString(); | |
411 | ByteMark* mark = buffer()->FindMarkByName (target); | |
412 | ||
413 | // If not set, define it | |
414 | if (mark == null) | |
415 | { | |
416 | UndefinedLabel undf; | |
417 | undf.name = target; | |
418 | undf.target = buffer()->AddMark (target); | |
419 | mUndefinedLabels << undf; | |
420 | } | |
421 | ||
422 | // Add a reference to the mark. | |
423 | buffer()->WriteDWord (dhGoto); | |
424 | buffer()->AddReference (mark); | |
425 | mLexer->MustGetNext (tkSemicolon); | |
426 | } | |
427 | ||
428 | // ============================================================================ | |
429 | // | |
430 | void BotscriptParser::ParseIf() | |
431 | { | |
432 | CheckNotToplevel(); | |
433 | PushScope(); | |
434 | ||
435 | // Condition | |
436 | mLexer->MustGetNext (tkParenStart); | |
437 | ||
438 | // Read the expression and write it. | |
439 | DataBuffer* c = ParseExpression (EIntType); | |
440 | buffer()->MergeAndDestroy (c); | |
441 | ||
442 | mLexer->MustGetNext (tkParenEnd); | |
443 | mLexer->MustGetNext (tkBraceStart); | |
444 | ||
445 | // Add a mark - to here temporarily - and add a reference to it. | |
446 | // Upon a closing brace, the mark will be adjusted. | |
447 | ByteMark* mark = buffer()->AddMark (""); | |
448 | ||
449 | // Use dhIfNotGoto - if the expression is not true, we goto the mark | |
450 | // we just defined - and this mark will be at the end of the scope block. | |
451 | buffer()->WriteDWord (dhIfNotGoto); | |
452 | buffer()->AddReference (mark); | |
453 | ||
454 | // Store it | |
455 | SCOPE (0).mark1 = mark; | |
456 | SCOPE (0).type = eIfScope; | |
457 | } | |
458 | ||
459 | // ============================================================================ | |
460 | // | |
461 | void BotscriptParser::ParseElse() | |
462 | { | |
463 | CheckNotToplevel(); | |
464 | mLexer->MustGetNext (tkBraceStart); | |
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
465 | PushScope (eNoReset); |
88 | 466 | |
467 | if (SCOPE (0).type != eIfScope) | |
468 | Error ("else without preceding if"); | |
469 | ||
470 | // write down to jump to the end of the else statement | |
471 | // Otherwise we have fall-throughs | |
472 | SCOPE (0).mark2 = buffer()->AddMark (""); | |
473 | ||
474 | // Instruction to jump to the end after if block is complete | |
475 | buffer()->WriteDWord (dhGoto); | |
476 | buffer()->AddReference (SCOPE (0).mark2); | |
477 | ||
478 | // Move the ifnot mark here and set type to else | |
479 | buffer()->AdjustMark (SCOPE (0).mark1); | |
480 | SCOPE (0).type = eElseScope; | |
481 | } | |
482 | ||
483 | // ============================================================================ | |
484 | // | |
485 | void BotscriptParser::ParseWhileBlock() | |
486 | { | |
487 | CheckNotToplevel(); | |
488 | PushScope(); | |
489 | ||
490 | // While loops need two marks - one at the start of the loop and one at the | |
491 | // end. The condition is checked at the very start of the loop, if it fails, | |
492 | // we use goto to skip to the end of the loop. At the end, we loop back to | |
493 | // the beginning with a go-to statement. | |
494 | ByteMark* mark1 = buffer()->AddMark (""); // start | |
495 | ByteMark* mark2 = buffer()->AddMark (""); // end | |
496 | ||
497 | // Condition | |
498 | mLexer->MustGetNext (tkParenStart); | |
499 | DataBuffer* expr = ParseExpression (EIntType); | |
500 | mLexer->MustGetNext (tkParenEnd); | |
501 | mLexer->MustGetNext (tkBraceStart); | |
502 | ||
503 | // write condition | |
504 | buffer()->MergeAndDestroy (expr); | |
505 | ||
506 | // Instruction to go to the end if it fails | |
507 | buffer()->WriteDWord (dhIfNotGoto); | |
508 | buffer()->AddReference (mark2); | |
509 | ||
510 | // Store the needed stuff | |
511 | SCOPE (0).mark1 = mark1; | |
512 | SCOPE (0).mark2 = mark2; | |
513 | SCOPE (0).type = eWhileScope; | |
514 | } | |
515 | ||
516 | // ============================================================================ | |
517 | // | |
518 | void BotscriptParser::ParseForBlock() | |
519 | { | |
520 | CheckNotToplevel(); | |
521 | PushScope(); | |
522 | ||
523 | // Initializer | |
524 | mLexer->MustGetNext (tkParenStart); | |
525 | DataBuffer* init = ParseStatement(); | |
526 | ||
527 | if (init == null) | |
528 | Error ("bad statement for initializer of for"); | |
529 | ||
530 | mLexer->MustGetNext (tkSemicolon); | |
531 | ||
532 | // Condition | |
533 | DataBuffer* cond = ParseExpression (EIntType); | |
534 | ||
535 | if (cond == null) | |
536 | Error ("bad statement for condition of for"); | |
537 | ||
538 | mLexer->MustGetNext (tkSemicolon); | |
539 | ||
540 | // Incrementor | |
541 | DataBuffer* incr = ParseStatement(); | |
542 | ||
543 | if (incr == null) | |
544 | Error ("bad statement for incrementor of for"); | |
545 | ||
546 | mLexer->MustGetNext (tkParenEnd); | |
547 | mLexer->MustGetNext (tkBraceStart); | |
548 | ||
549 | // First, write out the initializer | |
550 | buffer()->MergeAndDestroy (init); | |
551 | ||
552 | // Init two marks | |
553 | ByteMark* mark1 = buffer()->AddMark (""); | |
554 | ByteMark* mark2 = buffer()->AddMark (""); | |
555 | ||
556 | // Add the condition | |
557 | buffer()->MergeAndDestroy (cond); | |
558 | buffer()->WriteDWord (dhIfNotGoto); | |
559 | buffer()->AddReference (mark2); | |
560 | ||
561 | // Store the marks and incrementor | |
562 | SCOPE (0).mark1 = mark1; | |
563 | SCOPE (0).mark2 = mark2; | |
564 | SCOPE (0).buffer1 = incr; | |
565 | SCOPE (0).type = eForScope; | |
566 | } | |
567 | ||
568 | // ============================================================================ | |
569 | // | |
570 | void BotscriptParser::ParseDoBlock() | |
571 | { | |
572 | CheckNotToplevel(); | |
573 | PushScope(); | |
574 | mLexer->MustGetNext (tkBraceStart); | |
575 | SCOPE (0).mark1 = buffer()->AddMark (""); | |
576 | SCOPE (0).type = eDoScope; | |
577 | } | |
578 | ||
579 | // ============================================================================ | |
580 | // | |
581 | void BotscriptParser::ParseSwitchBlock() | |
582 | { | |
583 | // This gets a bit tricky. switch is structured in the | |
584 | // bytecode followingly: | |
585 | // | |
586 | // (expression) | |
587 | // case a: goto casemark1 | |
588 | // case b: goto casemark2 | |
589 | // case c: goto casemark3 | |
590 | // goto mark1 // jump to end if no matches | |
591 | // casemark1: ... | |
592 | // casemark2: ... | |
593 | // casemark3: ... | |
594 | // mark1: // end mark | |
595 | ||
596 | CheckNotToplevel(); | |
597 | PushScope(); | |
598 | mLexer->MustGetNext (tkParenStart); | |
599 | buffer()->MergeAndDestroy (ParseExpression (EIntType)); | |
600 | mLexer->MustGetNext (tkParenEnd); | |
601 | mLexer->MustGetNext (tkBraceStart); | |
602 | SCOPE (0).type = eSwitchScope; | |
603 | SCOPE (0).mark1 = buffer()->AddMark (""); // end mark | |
604 | SCOPE (0).buffer1 = null; // default header | |
605 | } | |
606 | ||
607 | // ============================================================================ | |
608 | // | |
609 | void BotscriptParser::ParseSwitchCase() | |
610 | { | |
611 | // case is only allowed inside switch | |
612 | if (SCOPE (0).type != eSwitchScope) | |
613 | Error ("case label outside switch"); | |
614 | ||
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
615 | // 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
|
616 | // expressions here. |
88 | 617 | mLexer->MustGetNext (tkNumber); |
618 | int num = mLexer->GetToken()->text.ToLong(); | |
619 | mLexer->MustGetNext (tkColon); | |
620 | ||
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
621 | for (const CaseInfo& info : SCOPE(0).cases) |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
622 | if (info.number == num) |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
623 | Error ("multiple case %1 labels in one switch", num); |
88 | 624 | |
625 | // Write down the expression and case-go-to. This builds | |
626 | // the case tree. The closing event will write the actual | |
627 | // blocks and move the marks appropriately. | |
628 | // | |
629 | // AddSwitchCase will add the reference to the mark | |
630 | // for the case block that this heralds, and takes care | |
631 | // of buffering setup and stuff like that. | |
632 | // | |
633 | // We null the switch buffer for the case-go-to statement as | |
634 | // we want it all under the switch, not into the case-buffers. | |
635 | mSwitchBuffer = null; | |
636 | buffer()->WriteDWord (dhCaseGoto); | |
637 | buffer()->WriteDWord (num); | |
638 | AddSwitchCase (null); | |
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
639 | SCOPE (0).casecursor->number = num; |
88 | 640 | } |
641 | ||
642 | // ============================================================================ | |
643 | // | |
644 | void BotscriptParser::ParseSwitchDefault() | |
645 | { | |
646 | if (SCOPE (0).type != eSwitchScope) | |
647 | Error ("default label outside switch"); | |
648 | ||
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
649 | if (SCOPE (0).buffer1 != null) |
88 | 650 | Error ("multiple default labels in one switch"); |
651 | ||
652 | mLexer->MustGetNext (tkColon); | |
653 | ||
654 | // The default header is buffered into buffer1, since | |
655 | // it has to be the last of the case headers | |
656 | // | |
657 | // Since the expression is pushed into the switch | |
658 | // and is only popped when case succeeds, we have | |
659 | // to pop it with dhDrop manually if we end up in | |
660 | // a default. | |
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
661 | DataBuffer* buf = new DataBuffer; |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
662 | SCOPE (0).buffer1 = buf; |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
663 | buf->WriteDWord (dhDrop); |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
664 | buf->WriteDWord (dhGoto); |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
665 | AddSwitchCase (buf); |
88 | 666 | } |
667 | ||
668 | // ============================================================================ | |
669 | // | |
670 | void BotscriptParser::ParseBreak() | |
671 | { | |
672 | if (mScopeCursor == 0) | |
673 | Error ("unexpected `break`"); | |
674 | ||
675 | buffer()->WriteDWord (dhGoto); | |
676 | ||
677 | // switch and if use mark1 for the closing point, | |
678 | // for and while use mark2. | |
679 | switch (SCOPE (0).type) | |
680 | { | |
681 | case eIfScope: | |
682 | case eSwitchScope: | |
683 | { | |
684 | buffer()->AddReference (SCOPE (0).mark1); | |
685 | } break; | |
686 | ||
687 | case eForScope: | |
688 | case eWhileScope: | |
689 | { | |
690 | buffer()->AddReference (SCOPE (0).mark2); | |
691 | } break; | |
692 | ||
693 | default: | |
694 | { | |
695 | Error ("unexpected `break`"); | |
696 | } break; | |
697 | } | |
698 | ||
699 | mLexer->MustGetNext (tkSemicolon); | |
700 | } | |
701 | ||
702 | // ============================================================================ | |
703 | // | |
704 | void BotscriptParser::ParseContinue() | |
705 | { | |
706 | mLexer->MustGetNext (tkSemicolon); | |
707 | ||
708 | int curs; | |
709 | bool found = false; | |
710 | ||
711 | // Fall through the scope until we find a loop block | |
712 | for (curs = mScopeCursor; curs > 0 && !found; curs--) | |
713 | { | |
714 | switch (mScopeStack[curs].type) | |
715 | { | |
716 | case eForScope: | |
717 | case eWhileScope: | |
718 | case eDoScope: | |
719 | { | |
720 | buffer()->WriteDWord (dhGoto); | |
721 | buffer()->AddReference (mScopeStack[curs].mark1); | |
722 | found = true; | |
723 | } break; | |
724 | ||
725 | default: | |
726 | break; | |
727 | } | |
728 | } | |
729 | ||
730 | // No loop blocks | |
731 | if (found == false) | |
732 | Error ("`continue`-statement not inside a loop"); | |
733 | } | |
734 | ||
735 | // ============================================================================ | |
736 | // | |
737 | void BotscriptParser::ParseBlockEnd() | |
738 | { | |
739 | // Closing brace | |
740 | // If we're in the block stack, we're descending down from it now | |
741 | if (mScopeCursor > 0) | |
742 | { | |
743 | switch (SCOPE (0).type) | |
744 | { | |
745 | 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
|
746 | { |
88 | 747 | // Adjust the closing mark. |
748 | buffer()->AdjustMark (SCOPE (0).mark1); | |
749 | ||
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
|
750 | // We're returning from `if`, thus `else` follow |
88 | 751 | mCanElse = true; |
752 | 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
|
753 | } |
88 | 754 | |
755 | 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
|
756 | { |
88 | 757 | // else instead uses mark1 for itself (so if expression |
758 | // fails, jump to else), mark2 means end of else | |
759 | buffer()->AdjustMark (SCOPE (0).mark2); | |
760 | 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
|
761 | } |
88 | 762 | |
763 | 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
|
764 | { // write the incrementor at the end of the loop block |
88 | 765 | 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
|
766 | } |
88 | 767 | 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
|
768 | { // write down the instruction to go back to the start of the loop |
88 | 769 | buffer()->WriteDWord (dhGoto); |
770 | buffer()->AddReference (SCOPE (0).mark1); | |
771 | ||
772 | // Move the closing mark here since we're at the end of the while loop | |
773 | buffer()->AdjustMark (SCOPE (0).mark2); | |
774 | 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
|
775 | } |
88 | 776 | |
777 | case eDoScope: | |
778 | { | |
779 | mLexer->MustGetNext (tkWhile); | |
780 | mLexer->MustGetNext (tkParenStart); | |
781 | DataBuffer* expr = ParseExpression (EIntType); | |
782 | mLexer->MustGetNext (tkParenEnd); | |
783 | mLexer->MustGetNext (tkSemicolon); | |
784 | ||
785 | // If the condition runs true, go back to the start. | |
786 | buffer()->MergeAndDestroy (expr); | |
787 | buffer()->WriteDWord (dhIfGoto); | |
788 | buffer()->AddReference (SCOPE (0).mark1); | |
789 | break; | |
790 | } | |
791 | ||
792 | case eSwitchScope: | |
793 | { | |
794 | // Switch closes. Move down to the record buffer of | |
795 | // the lower block. | |
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
796 | if (SCOPE (1).casecursor != SCOPE (1).cases.begin() - 1) |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
797 | mSwitchBuffer = SCOPE (1).casecursor->data; |
88 | 798 | else |
799 | mSwitchBuffer = null; | |
800 | ||
801 | // If there was a default in the switch, write its header down now. | |
802 | // If not, write instruction to jump to the end of switch after | |
803 | // the headers (thus won't fall-through if no case matched) | |
804 | if (SCOPE (0).buffer1) | |
805 | buffer()->MergeAndDestroy (SCOPE (0).buffer1); | |
806 | else | |
807 | { | |
808 | buffer()->WriteDWord (dhDrop); | |
809 | buffer()->WriteDWord (dhGoto); | |
810 | buffer()->AddReference (SCOPE (0).mark1); | |
811 | } | |
812 | ||
813 | // Go through all of the buffers we | |
814 | // recorded down and write them. | |
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
815 | for (CaseInfo& info : SCOPE (0).cases) |
88 | 816 | { |
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
817 | buffer()->AdjustMark (info.mark); |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
818 | buffer()->MergeAndDestroy (info.data); |
88 | 819 | } |
820 | ||
821 | // Move the closing mark here | |
822 | buffer()->AdjustMark (SCOPE (0).mark1); | |
823 | break; | |
824 | } | |
825 | ||
826 | case eUnknownScope: | |
827 | break; | |
828 | } | |
829 | ||
830 | // Descend down the stack | |
831 | mScopeCursor--; | |
832 | return; | |
833 | } | |
834 | ||
835 | int dataheader = (mCurrentMode == EEventMode) ? dhEndEvent : | |
836 | (mCurrentMode == EMainLoopMode) ? dhEndMainLoop : | |
837 | (mCurrentMode == EOnenterMode) ? dhEndOnEnter : | |
838 | (mCurrentMode == EOnexitMode) ? dhEndOnExit : -1; | |
839 | ||
840 | if (dataheader == -1) | |
841 | Error ("unexpected `}`"); | |
842 | ||
843 | // Data header must be written before mode is changed because | |
844 | // onenter and mainloop go into special buffers, and we want | |
845 | // the closing data headers into said buffers too. | |
846 | buffer()->WriteDWord (dataheader); | |
847 | mCurrentMode = ETopLevelMode; | |
848 | mLexer->GetNext (tkSemicolon); | |
849 | } | |
850 | ||
851 | // ============================================================================ | |
852 | // | |
853 | void BotscriptParser::ParseLabel() | |
854 | { | |
855 | CheckNotToplevel(); | |
856 | String labelName = GetTokenString(); | |
857 | ByteMark* mark = null; | |
858 | ||
859 | // 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
|
860 | for (UndefinedLabel& label : mUndefinedLabels) |
88 | 861 | { |
89
029a330a9bef
- blargh. buffers weren't initialized properly
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
862 | if (label.name != labelName) |
88 | 863 | continue; |
864 | ||
89
029a330a9bef
- blargh. buffers weren't initialized properly
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
865 | mark = label.target; |
88 | 866 | buffer()->AdjustMark (mark); |
867 | ||
868 | // No longer undefined | |
89
029a330a9bef
- blargh. buffers weren't initialized properly
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
869 | mUndefinedLabels.Remove (label); |
88 | 870 | break; |
871 | } | |
872 | ||
873 | // Not found in unmarked lists, define it now | |
874 | if (mark == null) | |
875 | buffer()->AddMark (labelName); | |
876 | ||
877 | mLexer->MustGetNext (tkColon); | |
878 | } | |
879 | ||
880 | // ============================================================================= | |
881 | // | |
882 | void BotscriptParser::ParseEventdef() | |
883 | { | |
884 | EventDefinition* e = new EventDefinition; | |
885 | ||
886 | mLexer->MustGetNext (tkNumber); | |
887 | e->number = GetTokenString().ToLong(); | |
888 | mLexer->MustGetNext (tkColon); | |
889 | mLexer->MustGetNext (tkSymbol); | |
890 | e->name = mLexer->GetToken()->text; | |
891 | mLexer->MustGetNext (tkParenStart); | |
892 | mLexer->MustGetNext (tkParenEnd); | |
893 | mLexer->MustGetNext (tkSemicolon); | |
894 | AddEvent (e); | |
895 | } | |
896 | ||
897 | // ============================================================================= | |
898 | // | |
899 | void BotscriptParser::ParseFuncdef() | |
900 | { | |
901 | 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
|
902 | comm->origin = mLexer->DescribeCurrentPosition(); |
88 | 903 | |
99
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
98
diff
changeset
|
904 | // Return value |
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
98
diff
changeset
|
905 | mLexer->MustGetAnyOf ({tkInt, tkVoid, tkBool, tkStr}); |
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
98
diff
changeset
|
906 | 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
|
907 | assert (comm->returnvalue != -1); |
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
98
diff
changeset
|
908 | |
88 | 909 | // Number |
910 | mLexer->MustGetNext (tkNumber); | |
911 | comm->number = mLexer->GetToken()->text.ToLong(); | |
912 | mLexer->MustGetNext (tkColon); | |
913 | ||
914 | // Name | |
915 | mLexer->MustGetNext (tkSymbol); | |
916 | comm->name = mLexer->GetToken()->text; | |
917 | ||
99
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
98
diff
changeset
|
918 | // Arguments |
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
98
diff
changeset
|
919 | mLexer->MustGetNext (tkParenStart); |
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
98
diff
changeset
|
920 | comm->minargs = 0; |
88 | 921 | |
99
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
98
diff
changeset
|
922 | while (mLexer->PeekNextType (tkParenEnd) == false) |
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
98
diff
changeset
|
923 | { |
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
98
diff
changeset
|
924 | if (comm->args.IsEmpty() == false) |
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
98
diff
changeset
|
925 | mLexer->MustGetNext (tkComma); |
88 | 926 | |
927 | CommandArgument arg; | |
928 | mLexer->MustGetAnyOf ({tkInt, tkBool, tkStr}); | |
99
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
98
diff
changeset
|
929 | EType type = GetTypeByName (mLexer->GetToken()->text); // TODO |
88 | 930 | assert (type != -1 && type != EVoidType); |
931 | arg.type = type; | |
932 | ||
933 | mLexer->MustGetNext (tkSymbol); | |
934 | arg.name = mLexer->GetToken()->text; | |
935 | ||
936 | // 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
|
937 | if (comm->minargs < comm->args.Size() || mLexer->PeekNextType (tkAssign)) |
88 | 938 | { |
939 | mLexer->MustGetNext (tkAssign); | |
940 | ||
941 | switch (type) | |
942 | { | |
943 | case EIntType: | |
944 | case EBoolType: | |
945 | mLexer->MustGetNext (tkNumber); | |
946 | break; | |
947 | ||
948 | case EStringType: | |
99
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
98
diff
changeset
|
949 | Error ("string arguments cannot have default values"); |
88 | 950 | |
951 | case EUnknownType: | |
952 | case EVoidType: | |
953 | break; | |
954 | } | |
955 | ||
956 | arg.defvalue = mLexer->GetToken()->text.ToLong(); | |
957 | } | |
99
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
98
diff
changeset
|
958 | else |
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
98
diff
changeset
|
959 | comm->minargs++; |
88 | 960 | |
961 | comm->args << arg; | |
962 | } | |
963 | ||
99
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
98
diff
changeset
|
964 | mLexer->MustGetNext (tkParenEnd); |
88 | 965 | mLexer->MustGetNext (tkSemicolon); |
966 | AddCommandDefinition (comm); | |
967 | } | |
968 | ||
969 | // ============================================================================ | |
970 | // Parses a command call | |
971 | DataBuffer* BotscriptParser::ParseCommand (CommandInfo* comm) | |
972 | { | |
973 | DataBuffer* r = new DataBuffer (64); | |
974 | ||
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
|
975 | if (mCurrentMode == ETopLevelMode && comm->returnvalue == EVoidType) |
88 | 976 | Error ("command call at top level"); |
977 | ||
978 | 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
|
979 | mLexer->MustGetNext (tkAny); |
88 | 980 | |
981 | int curarg = 0; | |
982 | ||
99
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
98
diff
changeset
|
983 | for (;;) |
88 | 984 | { |
985 | if (TokenIs (tkParenEnd)) | |
986 | { | |
99
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
98
diff
changeset
|
987 | if (curarg < comm->minargs) |
88 | 988 | Error ("too few arguments passed to %1\n\tusage is: %2", |
989 | comm->name, comm->GetSignature()); | |
990 | ||
991 | break; | |
992 | curarg++; | |
993 | } | |
994 | ||
99
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
98
diff
changeset
|
995 | if (curarg >= comm->args.Size()) |
88 | 996 | Error ("too many arguments passed to %1\n\tusage is: %2", |
997 | comm->name, comm->GetSignature()); | |
998 | ||
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
|
999 | 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
|
1000 | mLexer->MustGetNext (tkAny); |
88 | 1001 | |
99
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
98
diff
changeset
|
1002 | if (curarg < comm->minargs - 1) |
88 | 1003 | { |
1004 | 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
|
1005 | mLexer->MustGetNext (tkAny); |
88 | 1006 | } |
99
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
98
diff
changeset
|
1007 | else if (curarg < comm->args.Size() - 1) |
88 | 1008 | { |
1009 | // Can continue, but can terminate as well. | |
1010 | if (TokenIs (tkParenEnd)) | |
1011 | { | |
1012 | curarg++; | |
1013 | break; | |
1014 | } | |
1015 | else | |
1016 | { | |
1017 | 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
|
1018 | mLexer->MustGetNext (tkAny); |
88 | 1019 | } |
1020 | } | |
1021 | ||
1022 | curarg++; | |
1023 | } | |
1024 | ||
1025 | // 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
|
1026 | while (curarg < comm->args.Size()) |
88 | 1027 | { |
1028 | r->WriteDWord (dhPushNumber); | |
1029 | r->WriteDWord (comm->args[curarg].defvalue); | |
1030 | curarg++; | |
1031 | } | |
1032 | ||
1033 | r->WriteDWord (dhCommand); | |
1034 | r->WriteDWord (comm->number); | |
99
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
98
diff
changeset
|
1035 | r->WriteDWord (comm->args.Size()); |
88 | 1036 | |
1037 | return r; | |
1038 | } | |
1039 | ||
1040 | // ============================================================================ | |
1041 | // | |
1042 | String BotscriptParser::ParseFloat() | |
1043 | { | |
1044 | mLexer->TokenMustBe (tkNumber); | |
1045 | String floatstring = GetTokenString(); | |
1046 | Lexer::Token tok; | |
1047 | ||
1048 | // Go after the decimal point | |
1049 | if (mLexer->PeekNext (&tok) && tok.type == tkDot) | |
1050 | { | |
1051 | mLexer->Skip(); | |
1052 | mLexer->MustGetNext (tkNumber); | |
1053 | floatstring += "."; | |
1054 | floatstring += GetTokenString(); | |
1055 | } | |
1056 | ||
1057 | return floatstring; | |
1058 | } | |
1059 | ||
1060 | // ============================================================================ | |
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
|
1061 | // |
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
|
1062 | // 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
|
1063 | // |
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
|
1064 | 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
|
1065 | { |
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
|
1066 | 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
|
1067 | { |
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
|
1068 | 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
|
1069 | 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
|
1070 | 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
|
1071 | 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
|
1072 | tkDivideAssign, |
98
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1073 | tkModulusAssign, |
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1074 | tkDoublePlus, |
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1075 | 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
|
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 | |
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 | 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
|
1079 | |
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 | 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
|
1081 | { |
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 | 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
|
1083 | 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
|
1084 | 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
|
1085 | 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
|
1086 | 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
|
1087 | case tkModulusAssign: return EAssignMod; |
98
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1088 | case tkDoublePlus: return EAssignIncrement; |
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1089 | 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
|
1090 | 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
|
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 | 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
|
1094 | 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
|
1095 | } |
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 | // ============================================================================ |
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 | // |
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
|
1099 | 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
|
1100 | { |
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 | if (var->IsGlobal()) |
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 | { |
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
|
1103 | switch (op) |
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
|
1104 | { |
98
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1105 | case EAssign: return dhAssignGlobalVar; |
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1106 | case EAssignAdd: return dhAddGlobalVar; |
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1107 | case EAssignSub: return dhSubtractGlobalVar; |
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1108 | case EAssignMul: return dhMultiplyGlobalVar; |
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1109 | case EAssignDiv: return dhDivideGlobalVar; |
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1110 | case EAssignMod: return dhModGlobalVar; |
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1111 | case EAssignIncrement: return dhIncreaseGlobalVar; |
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1112 | case EAssignDecrement: return dhDecreaseGlobalVar; |
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
|
1113 | } |
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
|
1114 | } |
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
|
1115 | |
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
|
1116 | switch (op) |
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
|
1117 | { |
98
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1118 | case EAssign: return dhAssignLocalVar; |
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1119 | case EAssignAdd: return dhAddLocalVar; |
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1120 | case EAssignSub: return dhSubtractLocalVar; |
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1121 | case EAssignMul: return dhMultiplyLocalVar; |
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1122 | case EAssignDiv: return dhDivideLocalVar; |
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1123 | case EAssignMod: return dhModLocalVar; |
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1124 | case EAssignIncrement: return dhIncreaseLocalVar; |
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1125 | case EAssignDecrement: return dhDecreaseLocalVar; |
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
|
1126 | } |
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
|
1127 | |
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
|
1128 | 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
|
1129 | 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
|
1130 | } |
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
|
1131 | |
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
|
1132 | // ============================================================================ |
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
|
1133 | // |
88 | 1134 | // Parses an assignment. An assignment starts with a variable name, followed |
1135 | // by an assignment operator, followed by an expression value. Expects current | |
1136 | // token to be the name of the variable, and expects the variable to be given. | |
1137 | // | |
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
|
1138 | DataBuffer* BotscriptParser::ParseAssignment (Variable* var) |
88 | 1139 | { |
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
|
1140 | if (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
|
1141 | { |
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
|
1142 | Error ("cannot alter read-only 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
|
1143 | } |
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
|
1144 | |
88 | 1145 | // 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
|
1146 | EAssignmentOperator oper = ParseAssignmentOperator(); |
98
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1147 | DataBuffer* retbuf = new DataBuffer; |
88 | 1148 | |
1149 | if (mCurrentMode == ETopLevelMode) | |
1150 | Error ("can't alter variables at top level"); | |
1151 | ||
1152 | // Parse the right operand | |
98
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1153 | if (oper != EAssignIncrement && oper != EAssignDecrement) |
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1154 | { |
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1155 | DataBuffer* expr = ParseExpression (var->type); |
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1156 | retbuf->MergeAndDestroy (expr); |
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1157 | } |
88 | 1158 | |
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
|
1159 | #if 0 |
88 | 1160 | // <<= and >>= do not have data headers. Solution: expand them. |
1161 | // a <<= b -> a = a << b | |
1162 | // a >>= b -> a = a >> b | |
98
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1163 | retbuf->WriteDWord (var->IsGlobal() ? dhPushGlobalVar : dhPushLocalVar); |
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1164 | retbuf->WriteDWord (var->index); |
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1165 | retbuf->MergeAndDestroy (expr); |
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1166 | retbuf->WriteDWord ((oper == OPER_ASSIGNLEFTSHIFT) ? dhLeftShift : dhRightShift); |
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1167 | retbuf->WriteDWord (var->IsGlobal() ? dhAssignGlobalVar : dhAssignLocalVar); |
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1168 | 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
|
1169 | #endif |
88 | 1170 | |
98
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1171 | EDataHeader dh = GetAssigmentDataHeader (oper, var); |
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1172 | retbuf->WriteDWord (dh); |
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
1173 | retbuf->WriteDWord (var->index); |
88 | 1174 | return retbuf; |
1175 | } | |
1176 | ||
1177 | // ============================================================================ | |
1178 | // | |
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1179 | void BotscriptParser::PushScope (EReset reset) |
88 | 1180 | { |
1181 | mScopeCursor++; | |
1182 | ||
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1183 | if (mScopeStack.Size() < mScopeCursor + 1) |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1184 | { |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1185 | ScopeInfo newscope; |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1186 | mScopeStack << newscope; |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1187 | reset = eResetScope; |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1188 | } |
88 | 1189 | |
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1190 | if (reset == eResetScope) |
88 | 1191 | { |
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1192 | ScopeInfo* info = &SCOPE (0); |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1193 | info->type = eUnknownScope; |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1194 | info->mark1 = null; |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1195 | info->mark2 = null; |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1196 | info->buffer1 = null; |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1197 | info->cases.Clear(); |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1198 | info->casecursor = info->cases.begin() - 1; |
88 | 1199 | } |
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
|
1200 | |
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
|
1201 | // 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
|
1202 | 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
|
1203 | 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
|
1204 | |
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
|
1205 | 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
|
1206 | 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
|
1207 | |
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
|
1208 | 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
|
1209 | SCOPE(0).globalVariables.Clear(); |
88 | 1210 | } |
1211 | ||
1212 | // ============================================================================ | |
1213 | // | |
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
|
1214 | 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
|
1215 | { |
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
|
1216 | // 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
|
1217 | 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
|
1218 | 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
|
1219 | |
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
|
1220 | Expression expr (this, mLexer, reqtype); |
91
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
1221 | 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
|
1222 | |
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
|
1223 | // 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
|
1224 | // 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
|
1225 | return expr.GetResult()->GetBuffer()->Clone(); |
91
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
1226 | } |
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
1227 | |
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
1228 | // ============================================================================ |
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
1229 | // |
88 | 1230 | DataBuffer* BotscriptParser::ParseStatement() |
1231 | { | |
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
|
1232 | // 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
|
1233 | 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
|
1234 | { |
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
|
1235 | 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
|
1236 | Variable* var = FindVariable (GetTokenString()); |
88 | 1237 | |
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
|
1238 | 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
|
1239 | 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
|
1240 | |
88 | 1241 | 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
|
1242 | } |
88 | 1243 | |
1244 | return null; | |
1245 | } | |
1246 | ||
1247 | // ============================================================================ | |
1248 | // | |
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1249 | void BotscriptParser::AddSwitchCase (DataBuffer* casebuffer) |
88 | 1250 | { |
1251 | ScopeInfo* info = &SCOPE (0); | |
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1252 | CaseInfo casedata; |
88 | 1253 | |
1254 | // Init a mark for the case buffer | |
1255 | ByteMark* casemark = buffer()->AddMark (""); | |
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1256 | casedata.mark = casemark; |
88 | 1257 | |
1258 | // Add a reference to the mark. "case" and "default" both | |
1259 | // add the necessary bytecode before the reference. | |
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1260 | if (casebuffer != null) |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1261 | casebuffer->AddReference (casemark); |
88 | 1262 | else |
1263 | buffer()->AddReference (casemark); | |
1264 | ||
1265 | // Init a buffer for the case block and tell the object | |
1266 | // writer to record all written data to it. | |
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1267 | casedata.data = mSwitchBuffer = new DataBuffer; |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1268 | SCOPE(0).cases << casedata; |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1269 | info->casecursor++; |
88 | 1270 | } |
1271 | ||
1272 | // ============================================================================ | |
1273 | // | |
1274 | bool BotscriptParser::TokenIs (EToken a) | |
1275 | { | |
1276 | return (mLexer->GetTokenType() == a); | |
1277 | } | |
1278 | ||
1279 | // ============================================================================ | |
1280 | // | |
1281 | String BotscriptParser::GetTokenString() | |
1282 | { | |
1283 | return mLexer->GetToken()->text; | |
1284 | } | |
1285 | ||
1286 | // ============================================================================ | |
1287 | // | |
1288 | String BotscriptParser::DescribePosition() const | |
1289 | { | |
1290 | Lexer::Token* tok = mLexer->GetToken(); | |
1291 | return tok->file + ":" + String (tok->line) + ":" + String (tok->column); | |
1292 | } | |
1293 | ||
1294 | // ============================================================================ | |
1295 | // | |
1296 | DataBuffer* BotscriptParser::buffer() | |
1297 | { | |
1298 | if (mSwitchBuffer != null) | |
1299 | return mSwitchBuffer; | |
1300 | ||
1301 | if (mCurrentMode == EMainLoopMode) | |
1302 | return mMainLoopBuffer; | |
1303 | ||
1304 | if (mCurrentMode == EOnenterMode) | |
1305 | return mOnEnterBuffer; | |
1306 | ||
1307 | return mMainBuffer; | |
1308 | } | |
1309 | ||
1310 | // ============================================================================ | |
1311 | // | |
1312 | void BotscriptParser::writeMemberBuffers() | |
1313 | { | |
1314 | // If there was no mainloop defined, write a dummy one now. | |
1315 | if (mGotMainLoop == false) | |
1316 | { | |
1317 | mMainLoopBuffer->WriteDWord (dhMainLoop); | |
1318 | mMainLoopBuffer->WriteDWord (dhEndMainLoop); | |
1319 | } | |
1320 | ||
1321 | // Write the onenter and mainloop buffers, in that order in particular. | |
1322 | for (DataBuffer** bufp : List<DataBuffer**> ({&mOnEnterBuffer, &mMainLoopBuffer})) | |
1323 | { | |
1324 | buffer()->MergeAndDestroy (*bufp); | |
1325 | ||
1326 | // Clear the buffer afterwards for potential next state | |
1327 | *bufp = new DataBuffer; | |
1328 | } | |
1329 | ||
1330 | // Next state definitely has no mainloop yet | |
1331 | mGotMainLoop = false; | |
1332 | } | |
1333 | ||
1334 | // ============================================================================ | |
1335 | // | |
1336 | // Write string table | |
1337 | // | |
1338 | void BotscriptParser::WriteStringTable() | |
1339 | { | |
1340 | int stringcount = CountStringsInTable(); | |
1341 | ||
1342 | if (stringcount == 0) | |
1343 | return; | |
1344 | ||
1345 | // Write header | |
1346 | mMainBuffer->WriteDWord (dhStringList); | |
1347 | mMainBuffer->WriteDWord (stringcount); | |
1348 | ||
1349 | // Write all strings | |
1350 | for (int i = 0; i < stringcount; i++) | |
1351 | mMainBuffer->WriteString (GetStringTable()[i]); | |
1352 | } | |
1353 | ||
1354 | // ============================================================================ | |
1355 | // | |
1356 | // Write the compiled bytecode to a file | |
1357 | // | |
1358 | void BotscriptParser::WriteToFile (String outfile) | |
1359 | { | |
106
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
1360 | FILE* fp = fopen (outfile, "wb"); |
88 | 1361 | |
1362 | if (fp == null) | |
1363 | Error ("couldn't open %1 for writing: %2", outfile, strerror (errno)); | |
1364 | ||
1365 | // First, resolve references | |
1366 | for (MarkReference* ref : mMainBuffer->GetReferences()) | |
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1367 | for (int i = 0; i < 4; ++i) |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
1368 | mMainBuffer->GetBuffer()[ref->pos + i] = (ref->target->pos >> (8 * i)) & 0xFF; |
88 | 1369 | |
1370 | // Then, dump the main buffer to the file | |
1371 | fwrite (mMainBuffer->GetBuffer(), 1, mMainBuffer->GetWrittenSize(), fp); | |
1372 | Print ("-- %1 byte%s1 written to %2\n", mMainBuffer->GetWrittenSize(), outfile); | |
1373 | 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
|
1374 | } |
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
|
1375 | |
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
|
1376 | // ============================================================================ |
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
|
1377 | // |
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
|
1378 | // 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
|
1379 | // 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
|
1380 | // |
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
|
1381 | 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
|
1382 | { |
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
|
1383 | 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
|
1384 | { |
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
|
1385 | 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
|
1386 | { |
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
|
1387 | 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
|
1388 | 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
|
1389 | } |
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
|
1390 | } |
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
|
1391 | |
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
|
1392 | 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
|
1393 | } |
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
|
1394 | |
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
|
1395 | // ============================================================================ |
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
|
1396 | // |
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
|
1397 | // 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
|
1398 | // |
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
|
1399 | 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
|
1400 | { |
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
|
1401 | 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
|
1402 | } |
106
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
1403 | |
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
1404 | // ============================================================================ |
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
1405 | // |
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
1406 | void BotscriptParser::SuggestHighestVarIndex (bool global, int index) |
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
1407 | { |
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
1408 | if (global) |
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
1409 | mHighestGlobalVarIndex = max (mHighestGlobalVarIndex, index); |
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
1410 | else |
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
1411 | mHighestStateVarIndex = max (mHighestStateVarIndex, index); |
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
1412 | } |
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
1413 | |
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
1414 | // ============================================================================ |
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
1415 | // |
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
1416 | int BotscriptParser::GetHighestVarIndex (bool global) |
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
1417 | { |
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
1418 | if (global) |
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
1419 | return mHighestGlobalVarIndex; |
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
1420 | |
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
1421 | return mHighestStateVarIndex; |
9174be9ac686
- improved error handling a tad
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
1422 | } |