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