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