|
1 /* |
|
2 * botc source code |
|
3 * Copyright (C) 2012 Santeri `Dusk` Piippo |
|
4 * All rights reserved. |
|
5 * |
|
6 * Redistribution and use in source and binary forms, with or without |
|
7 * modification, are permitted provided that the following conditions are met: |
|
8 * |
|
9 * 1. Redistributions of source code must retain the above copyright notice, |
|
10 * this list of conditions and the following disclaimer. |
|
11 * 2. Redistributions in binary form must reproduce the above copyright notice, |
|
12 * this list of conditions and the following disclaimer in the documentation |
|
13 * and/or other materials provided with the distribution. |
|
14 * 3. Neither the name of the developer nor the names of its contributors may |
|
15 * be used to endorse or promote products derived from this software without |
|
16 * specific prior written permission. |
|
17 * 4. Redistributions in any form must be accompanied by information on how to |
|
18 * obtain complete source code for the software and any accompanying |
|
19 * software that uses the software. The source code must either be included |
|
20 * in the distribution or be available for no more than the cost of |
|
21 * distribution plus a nominal fee, and must be freely redistributable |
|
22 * under reasonable conditions. For an executable file, complete source |
|
23 * code means the source code for all modules it contains. It does not |
|
24 * include source code for modules or files that typically accompany the |
|
25 * major components of the operating system on which the executable file |
|
26 * runs. |
|
27 * |
|
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
29 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
32 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
38 * POSSIBILITY OF SUCH DAMAGE. |
|
39 */ |
|
40 |
|
41 #ifndef __SCRIPTREADER_H__ |
|
42 #define __SCRIPTREADER_H__ |
|
43 |
|
44 #include <stdio.h> |
|
45 #include "str.h" |
|
46 #include "commands.h" |
|
47 #include "objwriter.h" |
|
48 |
|
49 #define MAX_FILESTACK 8 |
|
50 #define MAX_SCOPE 32 |
|
51 #define MAX_CASE 64 |
|
52 |
|
53 class ScriptVar; |
|
54 |
|
55 // Operators |
|
56 enum operator_e { |
|
57 OPER_ADD, |
|
58 OPER_SUBTRACT, |
|
59 OPER_MULTIPLY, |
|
60 OPER_DIVIDE, |
|
61 OPER_MODULUS, |
|
62 OPER_ASSIGN, |
|
63 OPER_ASSIGNADD, |
|
64 OPER_ASSIGNSUB, |
|
65 OPER_ASSIGNMUL, |
|
66 OPER_ASSIGNDIV, |
|
67 OPER_ASSIGNMOD, // -- 10 |
|
68 OPER_EQUALS, |
|
69 OPER_NOTEQUALS, |
|
70 OPER_LESSTHAN, |
|
71 OPER_GREATERTHAN, |
|
72 OPER_LESSTHANEQUALS, |
|
73 OPER_GREATERTHANEQUALS, |
|
74 OPER_LEFTSHIFT, |
|
75 OPER_RIGHTSHIFT, |
|
76 OPER_ASSIGNLEFTSHIFT, |
|
77 OPER_ASSIGNRIGHTSHIFT, // -- 20 |
|
78 OPER_OR, |
|
79 OPER_AND, |
|
80 OPER_BITWISEOR, |
|
81 OPER_BITWISEAND, |
|
82 OPER_BITWISEEOR, |
|
83 OPER_TERNARY, |
|
84 OPER_STRLEN, |
|
85 }; |
|
86 |
|
87 // Mark types |
|
88 enum marktype_e { |
|
89 MARKTYPE_LABEL, |
|
90 MARKTYPE_IF, |
|
91 MARKTYPE_INTERNAL, // internal structures |
|
92 }; |
|
93 |
|
94 // Block types |
|
95 enum scopetype_e { |
|
96 SCOPETYPE_UNKNOWN, |
|
97 SCOPETYPE_IF, |
|
98 SCOPETYPE_WHILE, |
|
99 SCOPETYPE_FOR, |
|
100 SCOPETYPE_DO, |
|
101 SCOPETYPE_SWITCH, |
|
102 SCOPETYPE_ELSE, |
|
103 }; |
|
104 |
|
105 // ============================================================================ |
|
106 // Meta-data about blocks |
|
107 struct ScopeInfo { |
|
108 unsigned int mark1; |
|
109 unsigned int mark2; |
|
110 scopetype_e type; |
|
111 DataBuffer* buffer1; |
|
112 |
|
113 // switch-related stuff |
|
114 // Which case are we at? |
|
115 short casecursor; |
|
116 |
|
117 // Marks to case-blocks |
|
118 int casemarks[MAX_CASE]; |
|
119 |
|
120 // Numbers of the case labels |
|
121 int casenumbers[MAX_CASE]; |
|
122 |
|
123 // actual case blocks |
|
124 DataBuffer* casebuffers[MAX_CASE]; |
|
125 |
|
126 // What is the current buffer of the block? |
|
127 DataBuffer* recordbuffer; |
|
128 }; |
|
129 |
|
130 // ============================================================================ |
|
131 typedef struct { |
|
132 str name; |
|
133 type_e type; |
|
134 str val; |
|
135 } constinfo_t; |
|
136 |
|
137 // ============================================================================ |
|
138 // The script reader reads the script, parses it and tells the object writer |
|
139 // the bytecode it needs to write to file. |
|
140 class ScriptReader { |
|
141 public: |
|
142 // ==================================================================== |
|
143 // MEMBERS |
|
144 FILE* fp[MAX_FILESTACK]; |
|
145 char* filepath[MAX_FILESTACK]; |
|
146 int fc; |
|
147 |
|
148 unsigned int pos[MAX_FILESTACK]; |
|
149 unsigned int curline[MAX_FILESTACK]; |
|
150 unsigned int curchar[MAX_FILESTACK]; |
|
151 ScopeInfo scopestack[MAX_SCOPE]; |
|
152 long savedpos[MAX_FILESTACK]; // filepointer cursor position |
|
153 str token; |
|
154 int commentmode; |
|
155 long prevpos; |
|
156 str prevtoken; |
|
157 |
|
158 // ==================================================================== |
|
159 // METHODS |
|
160 // scriptreader.cxx: |
|
161 ScriptReader (str path); |
|
162 ~ScriptReader (); |
|
163 void OpenFile (str path); |
|
164 void CloseFile (unsigned int u = MAX_FILESTACK); |
|
165 char ReadChar (); |
|
166 char PeekChar (int offset = 0); |
|
167 bool Next (bool peek = false); |
|
168 void Prev (); |
|
169 str PeekNext (int offset = 0); |
|
170 void Seek (unsigned int n, int origin); |
|
171 void MustNext (const char* c = ""); |
|
172 void MustThis (const char* c); |
|
173 void MustString (bool gotquote = false); |
|
174 void MustNumber (bool fromthis = false); |
|
175 void MustBool (); |
|
176 bool BoolValue (); |
|
177 |
|
178 void ParserError (const char* message, ...); |
|
179 void ParserWarning (const char* message, ...); |
|
180 |
|
181 // parser.cxx: |
|
182 void ParseBotScript (ObjWriter* w); |
|
183 DataBuffer* ParseCommand (CommandDef* comm); |
|
184 DataBuffer* ParseExpression (type_e reqtype); |
|
185 DataBuffer* ParseAssignment (ScriptVar* var); |
|
186 int ParseOperator (bool peek = false); |
|
187 DataBuffer* ParseExprValue (type_e reqtype); |
|
188 str ParseFloat (); |
|
189 void PushScope (); |
|
190 |
|
191 // preprocessor.cxx: |
|
192 void PreprocessDirectives (); |
|
193 void PreprocessMacros (); |
|
194 DataBuffer* ParseStatement (ObjWriter* w); |
|
195 void AddSwitchCase (ObjWriter* w, DataBuffer* b); |
|
196 |
|
197 private: |
|
198 bool atnewline; |
|
199 char c; |
|
200 void ParserMessage (const char* header, char* message); |
|
201 |
|
202 bool DoDirectivePreprocessing (); |
|
203 char PPReadChar (); |
|
204 void PPMustChar (char c); |
|
205 str PPReadWord (char &term); |
|
206 }; |
|
207 |
|
208 constinfo_t* FindConstant (str token); |
|
209 extern bool g_Neurosphere; |
|
210 |
|
211 #endif // __SCRIPTREADER_H__ |