Sun, 09 Feb 2014 14:56:58 +0200
- variables: merged const and mutable variables into one system, added constexpr variable support. still no locals
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 | #ifndef BOTC_PARSER_H | |
30 | #define BOTC_PARSER_H | |
31 | ||
32 | #include <stdio.h> | |
33 | #include "Main.h" | |
34 | #include "Commands.h" | |
35 | #include "LexerScanner.h" | |
36 | #include "Tokens.h" | |
37 | ||
38 | // TODO: get rid of this too? | |
39 | #define MAX_CASE 64 | |
40 | ||
41 | // TODO: get rid of this | |
42 | #define MAX_MARKS 512 | |
43 | ||
44 | class DataBuffer; | |
45 | class Lexer; | |
46 | class ScriptVariable; | |
47 | ||
48 | // ============================================================================ | |
49 | // | |
50 | struct UndefinedLabel | |
51 | { | |
52 | String name; | |
53 | ByteMark* target; | |
54 | }; | |
55 | ||
56 | // ============================================================================ | |
57 | // Mark types | |
58 | // | |
59 | enum eMarkType | |
60 | { | |
61 | eLabelMark, | |
62 | eIfMark, | |
63 | eInternalMark, // internal structures | |
64 | }; | |
65 | ||
66 | // ============================================================================ | |
67 | // Scope types | |
68 | // | |
69 | enum EScopeType | |
70 | { | |
71 | eUnknownScope, | |
72 | eIfScope, | |
73 | eWhileScope, | |
74 | eForScope, | |
75 | eDoScope, | |
76 | eSwitchScope, | |
77 | eElseScope, | |
78 | }; | |
79 | ||
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
|
80 | enum EAssignmentOperator |
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
81 | { |
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
82 | EAssign, |
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
83 | EAssignAdd, |
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
84 | EAssignSub, |
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
85 | EAssignMul, |
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
86 | EAssignDiv, |
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
87 | EAssignMod, |
98
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
88 | EAssignIncrement, |
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
97
diff
changeset
|
89 | EAssignDecrement, |
92
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
90 | }; |
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
91 | |
88 | 92 | // ============================================================================ |
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
93 | // |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
94 | struct CaseInfo |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
95 | { |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
96 | ByteMark* mark; |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
97 | int number; |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
98 | DataBuffer* data; |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
99 | }; |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
100 | |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
101 | // ============================================================================ |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
102 | // |
88 | 103 | // Meta-data about scopes |
104 | // | |
105 | struct ScopeInfo | |
106 | { | |
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
107 | ByteMark* mark1; |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
108 | ByteMark* mark2; |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
109 | EScopeType type; |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
110 | DataBuffer* buffer1; |
88 | 111 | |
112 | // switch-related stuff | |
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
113 | List<CaseInfo>::Iterator casecursor; |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
114 | List<CaseInfo> cases; |
88 | 115 | }; |
116 | ||
117 | // ============================================================================ | |
118 | // | |
119 | class BotscriptParser | |
120 | { | |
121 | PROPERTY (public, bool, ReadOnly, BOOL_OPS, STOCK_WRITE) | |
122 | ||
123 | public: | |
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
124 | enum EReset |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
125 | { |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
126 | eNoReset, |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
127 | eResetScope, |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
128 | }; |
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
129 | |
88 | 130 | BotscriptParser(); |
131 | ~BotscriptParser(); | |
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
|
132 | void ParseBotscript (String fileName); |
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
133 | DataBuffer* ParseCommand (CommandInfo* comm); |
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
134 | DataBuffer* ParseAssignment (ScriptVariable* var); |
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
135 | EAssignmentOperator ParseAssignmentOperator (); |
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
136 | String ParseFloat(); |
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
137 | void PushScope (EReset reset = eResetScope); |
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
|
138 | DataBuffer* ParseStatement(); |
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
139 | void AddSwitchCase (DataBuffer* b); |
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
140 | void CheckToplevel(); |
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
141 | void CheckNotToplevel(); |
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
142 | bool TokenIs (EToken a); |
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
143 | String GetTokenString(); |
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
144 | String DescribePosition() const; |
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
145 | void WriteToFile (String outfile); |
88 | 146 | |
147 | inline int GetNumEvents() const | |
148 | { | |
149 | return mNumEvents; | |
150 | } | |
151 | ||
152 | inline int GetNumStates() const | |
153 | { | |
154 | return mNumStates; | |
155 | } | |
156 | ||
157 | private: | |
158 | // The main buffer - the contents of this is what we | |
159 | // write to file after parsing is complete | |
160 | DataBuffer* mMainBuffer; | |
161 | ||
162 | // onenter buffer - the contents of the onenter{} block | |
163 | // is buffered here and is merged further at the end of state | |
164 | DataBuffer* mOnEnterBuffer; | |
165 | ||
166 | // Mainloop buffer - the contents of the mainloop{} block | |
167 | // is buffered here and is merged further at the end of state | |
168 | DataBuffer* mMainLoopBuffer; | |
169 | ||
170 | // Switch buffer - switch case data is recorded to this | |
171 | // buffer initially, instead of into main buffer. | |
172 | DataBuffer* mSwitchBuffer; | |
173 | ||
174 | Lexer* mLexer; | |
175 | int mNumStates; | |
176 | int mNumEvents; | |
177 | EParserMode mCurrentMode; | |
178 | String mCurrentState; | |
179 | bool mStateSpawnDefined; | |
180 | bool mGotMainLoop; | |
181 | int mScopeCursor; | |
182 | DataBuffer* mIfExpression; | |
183 | bool mCanElse; | |
184 | List<UndefinedLabel> mUndefinedLabels; | |
185 | ||
186 | // How many bytes have we written to file? | |
187 | int mNumWrittenBytes; | |
188 | ||
189 | // Scope data | |
97
49e38433b9fd
- worked away some limitations
Teemu Piippo <crimsondusk64@gmail.com>
parents:
92
diff
changeset
|
190 | List<ScopeInfo> mScopeStack; |
88 | 191 | |
192 | DataBuffer* buffer(); | |
193 | void ParseStateBlock(); | |
194 | void ParseEventBlock(); | |
195 | void ParseMainloop(); | |
196 | void ParseOnEnterExit(); | |
101
9ffae10ef76f
- variables: merged const and mutable variables into one system, added constexpr variable support. still no locals
Teemu Piippo <crimsondusk64@gmail.com>
parents:
98
diff
changeset
|
197 | void ParseVar(); |
88 | 198 | void ParseGoto(); |
199 | void ParseIf(); | |
200 | void ParseElse(); | |
201 | void ParseWhileBlock(); | |
202 | void ParseForBlock(); | |
203 | void ParseDoBlock(); | |
204 | void ParseSwitchBlock(); | |
205 | void ParseSwitchCase(); | |
206 | void ParseSwitchDefault(); | |
207 | void ParseBreak(); | |
208 | void ParseContinue(); | |
209 | void ParseBlockEnd(); | |
210 | void ParseLabel(); | |
211 | void ParseEventdef(); | |
212 | void ParseFuncdef(); | |
213 | void writeMemberBuffers(); | |
214 | void WriteStringTable(); | |
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
|
215 | DataBuffer* ParseExpression (EType reqtype, bool fromhere = false); |
3a00d396bce2
- now compiles again, further work on expressions (proper parsing + assignment operator handling (albeit unrelated) + operator adjusting)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
216 | EDataHeader GetAssigmentDataHeader (EAssignmentOperator op, ScriptVariable* var); |
88 | 217 | }; |
218 | ||
219 | #endif // BOTC_PARSER_H |