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