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