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