Tue, 04 Feb 2014 02:28:33 +0200
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
| 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 | DataBuffer* c = ParseExpression (EIntType); | |
| 397 | buffer()->MergeAndDestroy (c); | |
| 398 | ||
| 399 | mLexer->MustGetNext (tkParenEnd); | |
| 400 | mLexer->MustGetNext (tkBraceStart); | |
| 401 | ||
| 402 | // Add a mark - to here temporarily - and add a reference to it. | |
| 403 | // Upon a closing brace, the mark will be adjusted. | |
| 404 | ByteMark* mark = buffer()->AddMark (""); | |
| 405 | ||
| 406 | // Use dhIfNotGoto - if the expression is not true, we goto the mark | |
| 407 | // we just defined - and this mark will be at the end of the scope block. | |
| 408 | buffer()->WriteDWord (dhIfNotGoto); | |
| 409 | buffer()->AddReference (mark); | |
| 410 | ||
| 411 | // Store it | |
| 412 | SCOPE (0).mark1 = mark; | |
| 413 | SCOPE (0).type = eIfScope; | |
| 414 | } | |
| 415 | ||
| 416 | // ============================================================================ | |
| 417 | // | |
| 418 | void BotscriptParser::ParseElse() | |
| 419 | { | |
| 420 | CheckNotToplevel(); | |
| 421 | mLexer->MustGetNext (tkBraceStart); | |
| 422 | ||
| 423 | // Don't use PushScope as it resets the scope | |
| 424 | mScopeCursor++; | |
| 425 | ||
| 426 | if (mScopeCursor >= MAX_SCOPE) | |
| 427 | Error ("too deep scope"); | |
| 428 | ||
| 429 | if (SCOPE (0).type != eIfScope) | |
| 430 | Error ("else without preceding if"); | |
| 431 | ||
| 432 | // write down to jump to the end of the else statement | |
| 433 | // Otherwise we have fall-throughs | |
| 434 | SCOPE (0).mark2 = buffer()->AddMark (""); | |
| 435 | ||
| 436 | // Instruction to jump to the end after if block is complete | |
| 437 | buffer()->WriteDWord (dhGoto); | |
| 438 | buffer()->AddReference (SCOPE (0).mark2); | |
| 439 | ||
| 440 | // Move the ifnot mark here and set type to else | |
| 441 | buffer()->AdjustMark (SCOPE (0).mark1); | |
| 442 | SCOPE (0).type = eElseScope; | |
| 443 | } | |
| 444 | ||
| 445 | // ============================================================================ | |
| 446 | // | |
| 447 | void BotscriptParser::ParseWhileBlock() | |
| 448 | { | |
| 449 | CheckNotToplevel(); | |
| 450 | PushScope(); | |
| 451 | ||
| 452 | // While loops need two marks - one at the start of the loop and one at the | |
| 453 | // end. The condition is checked at the very start of the loop, if it fails, | |
| 454 | // we use goto to skip to the end of the loop. At the end, we loop back to | |
| 455 | // the beginning with a go-to statement. | |
| 456 | ByteMark* mark1 = buffer()->AddMark (""); // start | |
| 457 | ByteMark* mark2 = buffer()->AddMark (""); // end | |
| 458 | ||
| 459 | // Condition | |
| 460 | mLexer->MustGetNext (tkParenStart); | |
| 461 | DataBuffer* expr = ParseExpression (EIntType); | |
| 462 | mLexer->MustGetNext (tkParenEnd); | |
| 463 | mLexer->MustGetNext (tkBraceStart); | |
| 464 | ||
| 465 | // write condition | |
| 466 | buffer()->MergeAndDestroy (expr); | |
| 467 | ||
| 468 | // Instruction to go to the end if it fails | |
| 469 | buffer()->WriteDWord (dhIfNotGoto); | |
| 470 | buffer()->AddReference (mark2); | |
| 471 | ||
| 472 | // Store the needed stuff | |
| 473 | SCOPE (0).mark1 = mark1; | |
| 474 | SCOPE (0).mark2 = mark2; | |
| 475 | SCOPE (0).type = eWhileScope; | |
| 476 | } | |
| 477 | ||
| 478 | // ============================================================================ | |
| 479 | // | |
| 480 | void BotscriptParser::ParseForBlock() | |
| 481 | { | |
| 482 | CheckNotToplevel(); | |
| 483 | PushScope(); | |
| 484 | ||
| 485 | // Initializer | |
| 486 | mLexer->MustGetNext (tkParenStart); | |
| 487 | mLexer->MustGetNext(); | |
| 488 | DataBuffer* init = ParseStatement(); | |
| 489 | ||
| 490 | if (init == null) | |
| 491 | Error ("bad statement for initializer of for"); | |
| 492 | ||
| 493 | mLexer->MustGetNext (tkSemicolon); | |
| 494 | ||
| 495 | // Condition | |
| 496 | DataBuffer* cond = ParseExpression (EIntType); | |
| 497 | ||
| 498 | if (cond == null) | |
| 499 | Error ("bad statement for condition of for"); | |
| 500 | ||
| 501 | mLexer->MustGetNext (tkSemicolon); | |
| 502 | ||
| 503 | // Incrementor | |
| 504 | mLexer->MustGetNext(); | |
| 505 | DataBuffer* incr = ParseStatement(); | |
| 506 | ||
| 507 | if (incr == null) | |
| 508 | Error ("bad statement for incrementor of for"); | |
| 509 | ||
| 510 | mLexer->MustGetNext (tkParenEnd); | |
| 511 | mLexer->MustGetNext (tkBraceStart); | |
| 512 | ||
| 513 | // First, write out the initializer | |
| 514 | buffer()->MergeAndDestroy (init); | |
| 515 | ||
| 516 | // Init two marks | |
| 517 | ByteMark* mark1 = buffer()->AddMark (""); | |
| 518 | ByteMark* mark2 = buffer()->AddMark (""); | |
| 519 | ||
| 520 | // Add the condition | |
| 521 | buffer()->MergeAndDestroy (cond); | |
| 522 | buffer()->WriteDWord (dhIfNotGoto); | |
| 523 | buffer()->AddReference (mark2); | |
| 524 | ||
| 525 | // Store the marks and incrementor | |
| 526 | SCOPE (0).mark1 = mark1; | |
| 527 | SCOPE (0).mark2 = mark2; | |
| 528 | SCOPE (0).buffer1 = incr; | |
| 529 | SCOPE (0).type = eForScope; | |
| 530 | } | |
| 531 | ||
| 532 | // ============================================================================ | |
| 533 | // | |
| 534 | void BotscriptParser::ParseDoBlock() | |
| 535 | { | |
| 536 | CheckNotToplevel(); | |
| 537 | PushScope(); | |
| 538 | mLexer->MustGetNext (tkBraceStart); | |
| 539 | SCOPE (0).mark1 = buffer()->AddMark (""); | |
| 540 | SCOPE (0).type = eDoScope; | |
| 541 | } | |
| 542 | ||
| 543 | // ============================================================================ | |
| 544 | // | |
| 545 | void BotscriptParser::ParseSwitchBlock() | |
| 546 | { | |
| 547 | // This gets a bit tricky. switch is structured in the | |
| 548 | // bytecode followingly: | |
| 549 | // | |
| 550 | // (expression) | |
| 551 | // case a: goto casemark1 | |
| 552 | // case b: goto casemark2 | |
| 553 | // case c: goto casemark3 | |
| 554 | // goto mark1 // jump to end if no matches | |
| 555 | // casemark1: ... | |
| 556 | // casemark2: ... | |
| 557 | // casemark3: ... | |
| 558 | // mark1: // end mark | |
| 559 | ||
| 560 | CheckNotToplevel(); | |
| 561 | PushScope(); | |
| 562 | mLexer->MustGetNext (tkParenStart); | |
| 563 | buffer()->MergeAndDestroy (ParseExpression (EIntType)); | |
| 564 | mLexer->MustGetNext (tkParenEnd); | |
| 565 | mLexer->MustGetNext (tkBraceStart); | |
| 566 | SCOPE (0).type = eSwitchScope; | |
| 567 | SCOPE (0).mark1 = buffer()->AddMark (""); // end mark | |
| 568 | SCOPE (0).buffer1 = null; // default header | |
| 569 | } | |
| 570 | ||
| 571 | // ============================================================================ | |
| 572 | // | |
| 573 | void BotscriptParser::ParseSwitchCase() | |
| 574 | { | |
| 575 | // case is only allowed inside switch | |
| 576 | if (SCOPE (0).type != eSwitchScope) | |
| 577 | Error ("case label outside switch"); | |
| 578 | ||
| 579 | // Get the literal (Zandronum does not support expressions here) | |
| 580 | mLexer->MustGetNext (tkNumber); | |
| 581 | int num = mLexer->GetToken()->text.ToLong(); | |
| 582 | mLexer->MustGetNext (tkColon); | |
| 583 | ||
| 584 | for (int i = 0; i < MAX_CASE; i++) | |
| 585 | if (SCOPE (0).casenumbers[i] == num) | |
| 586 | Error ("multiple case %d labels in one switch", num); | |
| 587 | ||
| 588 | // Write down the expression and case-go-to. This builds | |
| 589 | // the case tree. The closing event will write the actual | |
| 590 | // blocks and move the marks appropriately. | |
| 591 | // | |
| 592 | // AddSwitchCase will add the reference to the mark | |
| 593 | // for the case block that this heralds, and takes care | |
| 594 | // of buffering setup and stuff like that. | |
| 595 | // | |
| 596 | // We null the switch buffer for the case-go-to statement as | |
| 597 | // we want it all under the switch, not into the case-buffers. | |
| 598 | mSwitchBuffer = null; | |
| 599 | buffer()->WriteDWord (dhCaseGoto); | |
| 600 | buffer()->WriteDWord (num); | |
| 601 | AddSwitchCase (null); | |
| 602 | SCOPE (0).casenumbers[SCOPE (0).casecursor] = num; | |
| 603 | } | |
| 604 | ||
| 605 | // ============================================================================ | |
| 606 | // | |
| 607 | void BotscriptParser::ParseSwitchDefault() | |
| 608 | { | |
| 609 | if (SCOPE (0).type != eSwitchScope) | |
| 610 | Error ("default label outside switch"); | |
| 611 | ||
| 612 | if (SCOPE (0).buffer1) | |
| 613 | Error ("multiple default labels in one switch"); | |
| 614 | ||
| 615 | mLexer->MustGetNext (tkColon); | |
| 616 | ||
| 617 | // The default header is buffered into buffer1, since | |
| 618 | // it has to be the last of the case headers | |
| 619 | // | |
| 620 | // Since the expression is pushed into the switch | |
| 621 | // and is only popped when case succeeds, we have | |
| 622 | // to pop it with dhDrop manually if we end up in | |
| 623 | // a default. | |
| 624 | DataBuffer* b = new DataBuffer; | |
| 625 | SCOPE (0).buffer1 = b; | |
| 626 | b->WriteDWord (dhDrop); | |
| 627 | b->WriteDWord (dhGoto); | |
| 628 | AddSwitchCase (b); | |
| 629 | } | |
| 630 | ||
| 631 | // ============================================================================ | |
| 632 | // | |
| 633 | void BotscriptParser::ParseBreak() | |
| 634 | { | |
| 635 | if (mScopeCursor == 0) | |
| 636 | Error ("unexpected `break`"); | |
| 637 | ||
| 638 | buffer()->WriteDWord (dhGoto); | |
| 639 | ||
| 640 | // switch and if use mark1 for the closing point, | |
| 641 | // for and while use mark2. | |
| 642 | switch (SCOPE (0).type) | |
| 643 | { | |
| 644 | case eIfScope: | |
| 645 | case eSwitchScope: | |
| 646 | { | |
| 647 | buffer()->AddReference (SCOPE (0).mark1); | |
| 648 | } break; | |
| 649 | ||
| 650 | case eForScope: | |
| 651 | case eWhileScope: | |
| 652 | { | |
| 653 | buffer()->AddReference (SCOPE (0).mark2); | |
| 654 | } break; | |
| 655 | ||
| 656 | default: | |
| 657 | { | |
| 658 | Error ("unexpected `break`"); | |
| 659 | } break; | |
| 660 | } | |
| 661 | ||
| 662 | mLexer->MustGetNext (tkSemicolon); | |
| 663 | } | |
| 664 | ||
| 665 | // ============================================================================ | |
| 666 | // | |
| 667 | void BotscriptParser::ParseContinue() | |
| 668 | { | |
| 669 | mLexer->MustGetNext (tkSemicolon); | |
| 670 | ||
| 671 | int curs; | |
| 672 | bool found = false; | |
| 673 | ||
| 674 | // Fall through the scope until we find a loop block | |
| 675 | for (curs = mScopeCursor; curs > 0 && !found; curs--) | |
| 676 | { | |
| 677 | switch (mScopeStack[curs].type) | |
| 678 | { | |
| 679 | case eForScope: | |
| 680 | case eWhileScope: | |
| 681 | case eDoScope: | |
| 682 | { | |
| 683 | buffer()->WriteDWord (dhGoto); | |
| 684 | buffer()->AddReference (mScopeStack[curs].mark1); | |
| 685 | found = true; | |
| 686 | } break; | |
| 687 | ||
| 688 | default: | |
| 689 | break; | |
| 690 | } | |
| 691 | } | |
| 692 | ||
| 693 | // No loop blocks | |
| 694 | if (found == false) | |
| 695 | Error ("`continue`-statement not inside a loop"); | |
| 696 | } | |
| 697 | ||
| 698 | // ============================================================================ | |
| 699 | // | |
| 700 | void BotscriptParser::ParseBlockEnd() | |
| 701 | { | |
| 702 | // Closing brace | |
| 703 | // If we're in the block stack, we're descending down from it now | |
| 704 | if (mScopeCursor > 0) | |
| 705 | { | |
| 706 | switch (SCOPE (0).type) | |
| 707 | { | |
| 708 | case eIfScope: | |
| 709 | // Adjust the closing mark. | |
| 710 | buffer()->AdjustMark (SCOPE (0).mark1); | |
| 711 | ||
| 712 | // We're returning from if, thus else can be next | |
| 713 | mCanElse = true; | |
| 714 | break; | |
| 715 | ||
| 716 | case eElseScope: | |
| 717 | // else instead uses mark1 for itself (so if expression | |
| 718 | // fails, jump to else), mark2 means end of else | |
| 719 | buffer()->AdjustMark (SCOPE (0).mark2); | |
| 720 | break; | |
| 721 | ||
| 722 | case eForScope: | |
| 723 | // write the incrementor at the end of the loop block | |
| 724 | buffer()->MergeAndDestroy (SCOPE (0).buffer1); | |
| 725 | case eWhileScope: | |
| 726 | // write down the instruction to go back to the start of the loop | |
| 727 | buffer()->WriteDWord (dhGoto); | |
| 728 | buffer()->AddReference (SCOPE (0).mark1); | |
| 729 | ||
| 730 | // Move the closing mark here since we're at the end of the while loop | |
| 731 | buffer()->AdjustMark (SCOPE (0).mark2); | |
| 732 | break; | |
| 733 | ||
| 734 | case eDoScope: | |
| 735 | { | |
| 736 | mLexer->MustGetNext (tkWhile); | |
| 737 | mLexer->MustGetNext (tkParenStart); | |
| 738 | DataBuffer* expr = ParseExpression (EIntType); | |
| 739 | mLexer->MustGetNext (tkParenEnd); | |
| 740 | mLexer->MustGetNext (tkSemicolon); | |
| 741 | ||
| 742 | // If the condition runs true, go back to the start. | |
| 743 | buffer()->MergeAndDestroy (expr); | |
| 744 | buffer()->WriteDWord (dhIfGoto); | |
| 745 | buffer()->AddReference (SCOPE (0).mark1); | |
| 746 | break; | |
| 747 | } | |
| 748 | ||
| 749 | case eSwitchScope: | |
| 750 | { | |
| 751 | // Switch closes. Move down to the record buffer of | |
| 752 | // the lower block. | |
| 753 | if (SCOPE (1).casecursor != -1) | |
| 754 | mSwitchBuffer = SCOPE (1).casebuffers[SCOPE (1).casecursor]; | |
| 755 | else | |
| 756 | mSwitchBuffer = null; | |
| 757 | ||
| 758 | // If there was a default in the switch, write its header down now. | |
| 759 | // If not, write instruction to jump to the end of switch after | |
| 760 | // the headers (thus won't fall-through if no case matched) | |
| 761 | if (SCOPE (0).buffer1) | |
| 762 | buffer()->MergeAndDestroy (SCOPE (0).buffer1); | |
| 763 | else | |
| 764 | { | |
| 765 | buffer()->WriteDWord (dhDrop); | |
| 766 | buffer()->WriteDWord (dhGoto); | |
| 767 | buffer()->AddReference (SCOPE (0).mark1); | |
| 768 | } | |
| 769 | ||
| 770 | // Go through all of the buffers we | |
| 771 | // recorded down and write them. | |
| 772 | for (int u = 0; u < MAX_CASE; u++) | |
| 773 | { | |
| 774 | if (SCOPE (0).casebuffers[u] == null) | |
| 775 | continue; | |
| 776 | ||
| 777 | buffer()->AdjustMark (SCOPE (0).casemarks[u]); | |
| 778 | buffer()->MergeAndDestroy (SCOPE (0).casebuffers[u]); | |
| 779 | } | |
| 780 | ||
| 781 | // Move the closing mark here | |
| 782 | buffer()->AdjustMark (SCOPE (0).mark1); | |
| 783 | break; | |
| 784 | } | |
| 785 | ||
| 786 | case eUnknownScope: | |
| 787 | break; | |
| 788 | } | |
| 789 | ||
| 790 | // Descend down the stack | |
| 791 | mScopeCursor--; | |
| 792 | return; | |
| 793 | } | |
| 794 | ||
| 795 | int dataheader = (mCurrentMode == EEventMode) ? dhEndEvent : | |
| 796 | (mCurrentMode == EMainLoopMode) ? dhEndMainLoop : | |
| 797 | (mCurrentMode == EOnenterMode) ? dhEndOnEnter : | |
| 798 | (mCurrentMode == EOnexitMode) ? dhEndOnExit : -1; | |
| 799 | ||
| 800 | if (dataheader == -1) | |
| 801 | Error ("unexpected `}`"); | |
| 802 | ||
| 803 | // Data header must be written before mode is changed because | |
| 804 | // onenter and mainloop go into special buffers, and we want | |
| 805 | // the closing data headers into said buffers too. | |
| 806 | buffer()->WriteDWord (dataheader); | |
| 807 | mCurrentMode = ETopLevelMode; | |
| 808 | mLexer->GetNext (tkSemicolon); | |
| 809 | } | |
| 810 | ||
| 811 | // ============================================================================ | |
| 812 | // | |
| 813 | void BotscriptParser::ParseConst() | |
| 814 | { | |
| 815 | ConstantInfo info; | |
| 816 | ||
| 817 | // Get the type | |
| 818 | mLexer->MustGetNext(); | |
| 819 | String typestring = GetTokenString(); | |
| 820 | info.type = GetTypeByName (typestring); | |
| 821 | ||
| 822 | if (info.type == EUnknownType || info.type == EVoidType) | |
| 823 | Error ("unknown type `%1` for constant", typestring); | |
| 824 | ||
| 825 | mLexer->MustGetNext(); | |
| 826 | info.name = GetTokenString(); | |
| 827 | ||
| 828 | mLexer->MustGetNext (tkAssign); | |
| 829 | ||
| 830 | switch (info.type) | |
| 831 | { | |
| 832 | case EBoolType: | |
| 833 | case EIntType: | |
| 834 | { | |
| 835 | mLexer->MustGetNext (tkNumber); | |
| 836 | } break; | |
| 837 | ||
| 838 | case EStringType: | |
| 839 | { | |
| 840 | mLexer->MustGetNext (tkString); | |
| 841 | } break; | |
| 842 | ||
| 843 | case EUnknownType: | |
| 844 | case EVoidType: | |
| 845 | break; | |
| 846 | } | |
| 847 | ||
| 848 | info.val = mLexer->GetToken()->text; | |
| 849 | mConstants << info; | |
| 850 | ||
| 851 | mLexer->MustGetNext (tkSemicolon); | |
| 852 | } | |
| 853 | ||
| 854 | // ============================================================================ | |
| 855 | // | |
| 856 | void BotscriptParser::ParseLabel() | |
| 857 | { | |
| 858 | CheckNotToplevel(); | |
| 859 | String labelName = GetTokenString(); | |
| 860 | ByteMark* mark = null; | |
| 861 | ||
| 862 | // want no conflicts.. | |
| 863 | if (FindCommandByName (labelName)) | |
| 864 | Error ("label name `%1` conflicts with command name\n", labelName); | |
| 865 | ||
| 866 | if (FindGlobalVariable (labelName)) | |
| 867 | Error ("label name `%1` conflicts with variable\n", labelName); | |
| 868 | ||
| 869 | // 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
|
870 | for (UndefinedLabel& label : mUndefinedLabels) |
| 88 | 871 | { |
|
89
029a330a9bef
- blargh. buffers weren't initialized properly
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
872 | if (label.name != labelName) |
| 88 | 873 | continue; |
| 874 | ||
|
89
029a330a9bef
- blargh. buffers weren't initialized properly
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
875 | mark = label.target; |
| 88 | 876 | buffer()->AdjustMark (mark); |
| 877 | ||
| 878 | // No longer undefined | |
|
89
029a330a9bef
- blargh. buffers weren't initialized properly
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
879 | mUndefinedLabels.Remove (label); |
| 88 | 880 | break; |
| 881 | } | |
| 882 | ||
| 883 | // Not found in unmarked lists, define it now | |
| 884 | if (mark == null) | |
| 885 | buffer()->AddMark (labelName); | |
| 886 | ||
| 887 | mLexer->MustGetNext (tkColon); | |
| 888 | } | |
| 889 | ||
| 890 | // ============================================================================= | |
| 891 | // | |
| 892 | void BotscriptParser::ParseEventdef() | |
| 893 | { | |
| 894 | EventDefinition* e = new EventDefinition; | |
| 895 | ||
| 896 | mLexer->MustGetNext (tkNumber); | |
| 897 | e->number = GetTokenString().ToLong(); | |
| 898 | mLexer->MustGetNext (tkColon); | |
| 899 | mLexer->MustGetNext (tkSymbol); | |
| 900 | e->name = mLexer->GetToken()->text; | |
| 901 | mLexer->MustGetNext (tkParenStart); | |
| 902 | mLexer->MustGetNext (tkParenEnd); | |
| 903 | mLexer->MustGetNext (tkSemicolon); | |
| 904 | AddEvent (e); | |
| 905 | } | |
| 906 | ||
| 907 | // ============================================================================= | |
| 908 | // | |
| 909 | void BotscriptParser::ParseFuncdef() | |
| 910 | { | |
| 911 | CommandInfo* comm = new CommandInfo; | |
| 912 | ||
| 913 | // Number | |
| 914 | mLexer->MustGetNext (tkNumber); | |
| 915 | comm->number = mLexer->GetToken()->text.ToLong(); | |
| 916 | mLexer->MustGetNext (tkColon); | |
| 917 | ||
| 918 | // Name | |
| 919 | mLexer->MustGetNext (tkSymbol); | |
| 920 | comm->name = mLexer->GetToken()->text; | |
| 921 | mLexer->MustGetNext (tkColon); | |
| 922 | ||
| 923 | // Return value | |
| 924 | mLexer->MustGetAnyOf ({tkInt, tkVoid, tkBool, tkStr}); | |
| 925 | comm->returnvalue = GetTypeByName (mLexer->GetToken()->text); // TODO | |
| 926 | assert (comm->returnvalue != -1); | |
| 927 | mLexer->MustGetNext (tkColon); | |
| 928 | ||
| 929 | // Num args | |
| 930 | mLexer->MustGetNext (tkNumber); | |
| 931 | comm->numargs = mLexer->GetToken()->text.ToLong(); | |
| 932 | mLexer->MustGetNext (tkColon); | |
| 933 | ||
| 934 | // Max args | |
| 935 | mLexer->MustGetNext (tkNumber); | |
| 936 | comm->maxargs = mLexer->GetToken()->text.ToLong(); | |
| 937 | ||
| 938 | // Argument types | |
| 939 | int curarg = 0; | |
| 940 | ||
| 941 | while (curarg < comm->maxargs) | |
| 942 | { | |
| 943 | CommandArgument arg; | |
| 944 | mLexer->MustGetNext (tkColon); | |
| 945 | mLexer->MustGetAnyOf ({tkInt, tkBool, tkStr}); | |
| 946 | EType type = GetTypeByName (mLexer->GetToken()->text); | |
| 947 | assert (type != -1 && type != EVoidType); | |
| 948 | arg.type = type; | |
| 949 | ||
| 950 | mLexer->MustGetNext (tkParenStart); | |
| 951 | mLexer->MustGetNext (tkSymbol); | |
| 952 | arg.name = mLexer->GetToken()->text; | |
| 953 | ||
| 954 | // If this is an optional parameter, we need the default value. | |
| 955 | if (curarg >= comm->numargs) | |
| 956 | { | |
| 957 | mLexer->MustGetNext (tkAssign); | |
| 958 | ||
| 959 | switch (type) | |
| 960 | { | |
| 961 | case EIntType: | |
| 962 | case EBoolType: | |
| 963 | mLexer->MustGetNext (tkNumber); | |
| 964 | break; | |
| 965 | ||
| 966 | case EStringType: | |
| 967 | mLexer->MustGetNext (tkString); | |
| 968 | break; | |
| 969 | ||
| 970 | case EUnknownType: | |
| 971 | case EVoidType: | |
| 972 | break; | |
| 973 | } | |
| 974 | ||
| 975 | arg.defvalue = mLexer->GetToken()->text.ToLong(); | |
| 976 | } | |
| 977 | ||
| 978 | mLexer->MustGetNext (tkParenEnd); | |
| 979 | comm->args << arg; | |
| 980 | curarg++; | |
| 981 | } | |
| 982 | ||
| 983 | mLexer->MustGetNext (tkSemicolon); | |
| 984 | AddCommandDefinition (comm); | |
| 985 | } | |
| 986 | ||
| 987 | // ============================================================================ | |
| 988 | // Parses a command call | |
| 989 | DataBuffer* BotscriptParser::ParseCommand (CommandInfo* comm) | |
| 990 | { | |
| 991 | DataBuffer* r = new DataBuffer (64); | |
| 992 | ||
| 993 | if (mCurrentMode == ETopLevelMode) | |
| 994 | Error ("command call at top level"); | |
| 995 | ||
| 996 | mLexer->MustGetNext (tkParenStart); | |
| 997 | mLexer->MustGetNext(); | |
| 998 | ||
| 999 | int curarg = 0; | |
| 1000 | ||
| 1001 | while (1) | |
| 1002 | { | |
| 1003 | if (TokenIs (tkParenEnd)) | |
| 1004 | { | |
| 1005 | if (curarg < comm->numargs) | |
| 1006 | Error ("too few arguments passed to %1\n\tusage is: %2", | |
| 1007 | comm->name, comm->GetSignature()); | |
| 1008 | ||
| 1009 | break; | |
| 1010 | curarg++; | |
| 1011 | } | |
| 1012 | ||
| 1013 | if (curarg >= comm->maxargs) | |
| 1014 | Error ("too many arguments passed to %1\n\tusage is: %2", | |
| 1015 | comm->name, comm->GetSignature()); | |
| 1016 | ||
|
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
|
1017 | r->MergeAndDestroy (ParseExpression (comm->args[curarg].type, true)); |
| 88 | 1018 | mLexer->MustGetNext(); |
| 1019 | ||
| 1020 | if (curarg < comm->numargs - 1) | |
| 1021 | { | |
| 1022 | mLexer->TokenMustBe (tkComma); | |
| 1023 | mLexer->MustGetNext(); | |
| 1024 | } | |
| 1025 | else if (curarg < comm->maxargs - 1) | |
| 1026 | { | |
| 1027 | // Can continue, but can terminate as well. | |
| 1028 | if (TokenIs (tkParenEnd)) | |
| 1029 | { | |
| 1030 | curarg++; | |
| 1031 | break; | |
| 1032 | } | |
| 1033 | else | |
| 1034 | { | |
| 1035 | mLexer->TokenMustBe (tkComma); | |
| 1036 | mLexer->MustGetNext(); | |
| 1037 | } | |
| 1038 | } | |
| 1039 | ||
| 1040 | curarg++; | |
| 1041 | } | |
| 1042 | ||
| 1043 | // If the script skipped any optional arguments, fill in defaults. | |
| 1044 | while (curarg < comm->maxargs) | |
| 1045 | { | |
| 1046 | r->WriteDWord (dhPushNumber); | |
| 1047 | r->WriteDWord (comm->args[curarg].defvalue); | |
| 1048 | curarg++; | |
| 1049 | } | |
| 1050 | ||
| 1051 | r->WriteDWord (dhCommand); | |
| 1052 | r->WriteDWord (comm->number); | |
| 1053 | r->WriteDWord (comm->maxargs); | |
| 1054 | ||
| 1055 | return r; | |
| 1056 | } | |
| 1057 | ||
| 1058 | // ============================================================================ | |
| 1059 | // | |
| 1060 | String BotscriptParser::ParseFloat() | |
| 1061 | { | |
| 1062 | mLexer->TokenMustBe (tkNumber); | |
| 1063 | String floatstring = GetTokenString(); | |
| 1064 | Lexer::Token tok; | |
| 1065 | ||
| 1066 | // Go after the decimal point | |
| 1067 | if (mLexer->PeekNext (&tok) && tok.type == tkDot) | |
| 1068 | { | |
| 1069 | mLexer->Skip(); | |
| 1070 | mLexer->MustGetNext (tkNumber); | |
| 1071 | floatstring += "."; | |
| 1072 | floatstring += GetTokenString(); | |
| 1073 | } | |
| 1074 | ||
| 1075 | return floatstring; | |
| 1076 | } | |
| 1077 | ||
| 1078 | // ============================================================================ | |
|
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
|
1079 | // |
|
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
1080 | // 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
|
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 | 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
|
1083 | { |
|
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
1084 | 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
|
1085 | { |
|
3a00d396bce2
- now 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 | 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
|
1087 | 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
|
1088 | 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
|
1089 | 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
|
1090 | 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
|
1091 | 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
|
1092 | }; |
|
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
1093 | |
|
3a00d396bce2
- now 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 | 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
|
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 | 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
|
1097 | { |
|
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
1098 | 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
|
1099 | 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
|
1100 | 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
|
1101 | 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
|
1102 | 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
|
1103 | 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
|
1104 | 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
|
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 | |
|
3a00d396bce2
- now 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 | 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
|
1108 | 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
|
1109 | } |
|
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
1110 | |
|
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
1111 | // ============================================================================ |
|
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
1112 | // |
|
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
1113 | 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
|
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 | 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
|
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 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
|
1120 | 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
|
1121 | 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
|
1122 | 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
|
1123 | 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
|
1124 | 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
|
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 | |
|
3a00d396bce2
- now 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 | 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
|
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 | 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
|
1131 | 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
|
1132 | 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
|
1133 | 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
|
1134 | 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
|
1135 | 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
|
1136 | } |
|
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
1137 | |
|
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
1138 | 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
|
1139 | 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
|
1140 | } |
|
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
1141 | |
|
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
1142 | // ============================================================================ |
|
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
1143 | // |
| 88 | 1144 | // Parses an assignment. An assignment starts with a variable name, followed |
| 1145 | // by an assignment operator, followed by an expression value. Expects current | |
| 1146 | // token to be the name of the variable, and expects the variable to be given. | |
| 1147 | // | |
| 1148 | DataBuffer* BotscriptParser::ParseAssignment (ScriptVariable* var) | |
| 1149 | { | |
| 1150 | // 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
|
1151 | EAssignmentOperator oper = ParseAssignmentOperator(); |
| 88 | 1152 | |
| 1153 | if (mCurrentMode == ETopLevelMode) | |
| 1154 | Error ("can't alter variables at top level"); | |
| 1155 | ||
| 1156 | // Parse the right operand | |
| 1157 | DataBuffer* retbuf = new DataBuffer; | |
| 1158 | DataBuffer* expr = ParseExpression (var->type); | |
| 1159 | ||
|
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
|
1160 | #if 0 |
| 88 | 1161 | // <<= and >>= do not have data headers. Solution: expand them. |
| 1162 | // a <<= b -> a = a << b | |
| 1163 | // a >>= b -> a = a >> b | |
| 1164 | if (oper == OPER_ASSIGNLEFTSHIFT || oper == OPER_ASSIGNRIGHTSHIFT) | |
| 1165 | { | |
| 1166 | retbuf->WriteDWord (var->IsGlobal() ? dhPushGlobalVar : dhPushLocalVar); | |
| 1167 | retbuf->WriteDWord (var->index); | |
| 1168 | retbuf->MergeAndDestroy (expr); | |
| 1169 | retbuf->WriteDWord ((oper == OPER_ASSIGNLEFTSHIFT) ? dhLeftShift : dhRightShift); | |
| 1170 | retbuf->WriteDWord (var->IsGlobal() ? dhAssignGlobalVar : dhAssignLocalVar); | |
| 1171 | retbuf->WriteDWord (var->index); | |
| 1172 | } | |
| 1173 | else | |
| 1174 | { | |
|
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
|
1175 | #endif |
| 88 | 1176 | 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
|
1177 | EDataHeader dh = GetAssigmentDataHeader (oper, var); |
| 88 | 1178 | retbuf->WriteDWord (dh); |
| 1179 | 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
|
1180 | #if 0 |
| 88 | 1181 | } |
|
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
|
1182 | #endif |
| 88 | 1183 | |
| 1184 | return retbuf; | |
| 1185 | } | |
| 1186 | ||
| 1187 | // ============================================================================ | |
| 1188 | // | |
| 1189 | void BotscriptParser::PushScope() | |
| 1190 | { | |
| 1191 | mScopeCursor++; | |
| 1192 | ||
| 1193 | if (mScopeCursor >= MAX_SCOPE) | |
| 1194 | Error ("too deep scope"); | |
| 1195 | ||
| 1196 | ScopeInfo* info = &SCOPE (0); | |
| 1197 | info->type = eUnknownScope; | |
| 1198 | info->mark1 = null; | |
| 1199 | info->mark2 = null; | |
| 1200 | info->buffer1 = null; | |
| 1201 | info->casecursor = -1; | |
| 1202 | ||
| 1203 | for (int i = 0; i < MAX_CASE; i++) | |
| 1204 | { | |
| 1205 | info->casemarks[i] = null; | |
| 1206 | info->casebuffers[i] = null; | |
| 1207 | info->casenumbers[i] = -1; | |
| 1208 | } | |
| 1209 | } | |
| 1210 | ||
| 1211 | // ============================================================================ | |
| 1212 | // | |
|
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
|
1213 | 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
|
1214 | { |
|
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
|
1215 | // 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
|
1216 | 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
|
1217 | 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
|
1218 | |
|
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
1219 | Expression expr (this, mLexer, reqtype); |
|
91
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
1220 | 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
|
1221 | |
|
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
1222 | // 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
|
1223 | // 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
|
1224 | return expr.GetResult()->GetBuffer()->Clone(); |
|
91
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
1225 | } |
|
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
1226 | |
|
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
1227 | // ============================================================================ |
|
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
1228 | // |
| 88 | 1229 | DataBuffer* BotscriptParser::ParseStatement() |
| 1230 | { | |
| 1231 | if (FindConstant (GetTokenString())) // There should not be constants here. | |
| 1232 | Error ("invalid use for constant\n"); | |
| 1233 | ||
| 1234 | // If it's a variable, expect assignment. | |
| 1235 | if (ScriptVariable* var = FindGlobalVariable (GetTokenString())) | |
| 1236 | return ParseAssignment (var); | |
| 1237 | ||
| 1238 | return null; | |
| 1239 | } | |
| 1240 | ||
| 1241 | // ============================================================================ | |
| 1242 | // | |
| 1243 | void BotscriptParser::AddSwitchCase (DataBuffer* b) | |
| 1244 | { | |
| 1245 | ScopeInfo* info = &SCOPE (0); | |
| 1246 | ||
| 1247 | info->casecursor++; | |
| 1248 | ||
| 1249 | if (info->casecursor >= MAX_CASE) | |
| 1250 | Error ("too many cases in one switch"); | |
| 1251 | ||
| 1252 | // Init a mark for the case buffer | |
| 1253 | ByteMark* casemark = buffer()->AddMark (""); | |
| 1254 | info->casemarks[info->casecursor] = casemark; | |
| 1255 | ||
| 1256 | // Add a reference to the mark. "case" and "default" both | |
| 1257 | // add the necessary bytecode before the reference. | |
| 1258 | if (b) | |
| 1259 | b->AddReference (casemark); | |
| 1260 | else | |
| 1261 | buffer()->AddReference (casemark); | |
| 1262 | ||
| 1263 | // Init a buffer for the case block and tell the object | |
| 1264 | // writer to record all written data to it. | |
| 1265 | info->casebuffers[info->casecursor] = mSwitchBuffer = new DataBuffer; | |
| 1266 | } | |
| 1267 | ||
| 1268 | // ============================================================================ | |
| 1269 | // | |
| 1270 | ConstantInfo* BotscriptParser::FindConstant (const String& tok) | |
| 1271 | { | |
| 1272 | for (int i = 0; i < mConstants.Size(); i++) | |
| 1273 | if (mConstants[i].name == tok) | |
| 1274 | return &mConstants[i]; | |
| 1275 | ||
| 1276 | return null; | |
| 1277 | } | |
| 1278 | ||
| 1279 | // ============================================================================ | |
| 1280 | // | |
| 1281 | bool BotscriptParser::TokenIs (EToken a) | |
| 1282 | { | |
| 1283 | return (mLexer->GetTokenType() == a); | |
| 1284 | } | |
| 1285 | ||
| 1286 | // ============================================================================ | |
| 1287 | // | |
| 1288 | String BotscriptParser::GetTokenString() | |
| 1289 | { | |
| 1290 | return mLexer->GetToken()->text; | |
| 1291 | } | |
| 1292 | ||
| 1293 | // ============================================================================ | |
| 1294 | // | |
| 1295 | String BotscriptParser::DescribePosition() const | |
| 1296 | { | |
| 1297 | Lexer::Token* tok = mLexer->GetToken(); | |
| 1298 | return tok->file + ":" + String (tok->line) + ":" + String (tok->column); | |
| 1299 | } | |
| 1300 | ||
| 1301 | // ============================================================================ | |
| 1302 | // | |
| 1303 | DataBuffer* BotscriptParser::buffer() | |
| 1304 | { | |
| 1305 | if (mSwitchBuffer != null) | |
| 1306 | return mSwitchBuffer; | |
| 1307 | ||
| 1308 | if (mCurrentMode == EMainLoopMode) | |
| 1309 | return mMainLoopBuffer; | |
| 1310 | ||
| 1311 | if (mCurrentMode == EOnenterMode) | |
| 1312 | return mOnEnterBuffer; | |
| 1313 | ||
| 1314 | return mMainBuffer; | |
| 1315 | } | |
| 1316 | ||
| 1317 | // ============================================================================ | |
| 1318 | // | |
| 1319 | void BotscriptParser::writeMemberBuffers() | |
| 1320 | { | |
| 1321 | // If there was no mainloop defined, write a dummy one now. | |
| 1322 | if (mGotMainLoop == false) | |
| 1323 | { | |
| 1324 | mMainLoopBuffer->WriteDWord (dhMainLoop); | |
| 1325 | mMainLoopBuffer->WriteDWord (dhEndMainLoop); | |
| 1326 | } | |
| 1327 | ||
| 1328 | // Write the onenter and mainloop buffers, in that order in particular. | |
| 1329 | for (DataBuffer** bufp : List<DataBuffer**> ({&mOnEnterBuffer, &mMainLoopBuffer})) | |
| 1330 | { | |
| 1331 | buffer()->MergeAndDestroy (*bufp); | |
| 1332 | ||
| 1333 | // Clear the buffer afterwards for potential next state | |
| 1334 | *bufp = new DataBuffer; | |
| 1335 | } | |
| 1336 | ||
| 1337 | // Next state definitely has no mainloop yet | |
| 1338 | mGotMainLoop = false; | |
| 1339 | } | |
| 1340 | ||
| 1341 | // ============================================================================ | |
| 1342 | // | |
| 1343 | // Write string table | |
| 1344 | // | |
| 1345 | void BotscriptParser::WriteStringTable() | |
| 1346 | { | |
| 1347 | int stringcount = CountStringsInTable(); | |
| 1348 | ||
| 1349 | if (stringcount == 0) | |
| 1350 | return; | |
| 1351 | ||
| 1352 | // Write header | |
| 1353 | mMainBuffer->WriteDWord (dhStringList); | |
| 1354 | mMainBuffer->WriteDWord (stringcount); | |
| 1355 | ||
| 1356 | // Write all strings | |
| 1357 | for (int i = 0; i < stringcount; i++) | |
| 1358 | mMainBuffer->WriteString (GetStringTable()[i]); | |
| 1359 | } | |
| 1360 | ||
| 1361 | // ============================================================================ | |
| 1362 | // | |
| 1363 | // Write the compiled bytecode to a file | |
| 1364 | // | |
| 1365 | void BotscriptParser::WriteToFile (String outfile) | |
| 1366 | { | |
| 1367 | FILE* fp = fopen (outfile, "w"); | |
| 1368 | ||
| 1369 | if (fp == null) | |
| 1370 | Error ("couldn't open %1 for writing: %2", outfile, strerror (errno)); | |
| 1371 | ||
| 1372 | // First, resolve references | |
| 1373 | for (MarkReference* ref : mMainBuffer->GetReferences()) | |
| 1374 | { | |
| 1375 | // Substitute the placeholder with the mark position | |
| 1376 | for (int v = 0; v < 4; v++) | |
| 1377 | mMainBuffer->GetBuffer()[ref->pos + v] = ((ref->target->pos) << (8 * v)) & 0xFF; | |
| 1378 | ||
| 1379 | Print ("reference at %1 resolved to mark at %2\n", ref->pos, ref->target->pos); | |
| 1380 | } | |
| 1381 | ||
| 1382 | // Then, dump the main buffer to the file | |
| 1383 | fwrite (mMainBuffer->GetBuffer(), 1, mMainBuffer->GetWrittenSize(), fp); | |
| 1384 | Print ("-- %1 byte%s1 written to %2\n", mMainBuffer->GetWrittenSize(), outfile); | |
| 1385 | fclose (fp); | |
| 1386 | } |