src/scriptreader.h

changeset 72
03e4d9db3fd9
parent 71
11f23fabf8a6
child 73
1ee9b312dc18
equal deleted inserted replaced
71:11f23fabf8a6 72:03e4d9db3fd9
1 /* 1 #ifndef BOTC_SCRIPTREADER_H
2 * botc source code 2 #define BOTC_SCRIPTREADER_H
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 3
44 #include <stdio.h> 4 #include <stdio.h>
45 #include "str.h" 5 #include "main.h"
46 #include "commands.h" 6 #include "commands.h"
47 #include "objwriter.h" 7 #include "objwriter.h"
48 8
49 #define MAX_FILESTACK 8 9 #define MAX_FILESTACK 8
50 #define MAX_SCOPE 32 10 #define MAX_SCOPE 32
127 DataBuffer* recordbuffer; 87 DataBuffer* recordbuffer;
128 }; 88 };
129 89
130 // ============================================================================ 90 // ============================================================================
131 typedef struct { 91 typedef struct {
132 str name; 92 string name;
133 type_e type; 93 type_e type;
134 str val; 94 string val;
135 } constinfo_t; 95 } constinfo_t;
136 96
137 // ============================================================================ 97 // ============================================================================
138 // The script reader reads the script, parses it and tells the object writer 98 // The script reader reads the script, parses it and tells the object writer
139 // the bytecode it needs to write to file. 99 // the bytecode it needs to write to file.
140 class ScriptReader { 100 class ScriptReader {
141 public: 101 public:
142 // ==================================================================== 102 // ====================================================================
143 // MEMBERS 103 // MEMBERS
144 FILE* fp[MAX_FILESTACK]; 104 FILE* fp[MAX_FILESTACK];
145 char* filepath[MAX_FILESTACK]; 105 string filepath[MAX_FILESTACK];
146 int fc; 106 int fc;
147 107
148 unsigned int pos[MAX_FILESTACK]; 108 unsigned int pos[MAX_FILESTACK];
149 unsigned int curline[MAX_FILESTACK]; 109 unsigned int curline[MAX_FILESTACK];
150 unsigned int curchar[MAX_FILESTACK]; 110 unsigned int curchar[MAX_FILESTACK];
151 ScopeInfo scopestack[MAX_SCOPE]; 111 ScopeInfo scopestack[MAX_SCOPE];
152 long savedpos[MAX_FILESTACK]; // filepointer cursor position 112 long savedpos[MAX_FILESTACK]; // filepointer cursor position
153 str token; 113 string token;
154 int commentmode; 114 int commentmode;
155 long prevpos; 115 long prevpos;
156 str prevtoken; 116 string prevtoken;
157 117
158 // ==================================================================== 118 // ====================================================================
159 // METHODS 119 // METHODS
160 // scriptreader.cxx: 120 // scriptreader.cxx:
161 ScriptReader (str path); 121 ScriptReader (string path);
162 ~ScriptReader (); 122 ~ScriptReader ();
163 void OpenFile (str path); 123 void OpenFile (string path);
164 void CloseFile (unsigned int u = MAX_FILESTACK); 124 void CloseFile (unsigned int u = MAX_FILESTACK);
165 char ReadChar (); 125 char ReadChar ();
166 char PeekChar (int offset = 0); 126 char PeekChar (int offset = 0);
167 bool Next (bool peek = false); 127 bool Next (bool peek = false);
168 void Prev (); 128 void Prev ();
169 str PeekNext (int offset = 0); 129 string PeekNext (int offset = 0);
170 void Seek (unsigned int n, int origin); 130 void Seek (unsigned int n, int origin);
171 void MustNext (const char* c = ""); 131 void MustNext (const char* c = "");
172 void MustThis (const char* c); 132 void MustThis (const char* c);
173 void MustString (bool gotquote = false); 133 void MustString (bool gotquote = false);
174 void MustNumber (bool fromthis = false); 134 void MustNumber (bool fromthis = false);
183 DataBuffer* ParseCommand (CommandDef* comm); 143 DataBuffer* ParseCommand (CommandDef* comm);
184 DataBuffer* ParseExpression (type_e reqtype); 144 DataBuffer* ParseExpression (type_e reqtype);
185 DataBuffer* ParseAssignment (ScriptVar* var); 145 DataBuffer* ParseAssignment (ScriptVar* var);
186 int ParseOperator (bool peek = false); 146 int ParseOperator (bool peek = false);
187 DataBuffer* ParseExprValue (type_e reqtype); 147 DataBuffer* ParseExprValue (type_e reqtype);
188 str ParseFloat (); 148 string ParseFloat ();
189 void PushScope (); 149 void PushScope ();
190 150
191 // preprocessor.cxx: 151 // preprocessor.cxx:
192 void PreprocessDirectives (); 152 void PreprocessDirectives ();
193 void PreprocessMacros (); 153 void PreprocessMacros ();
195 void AddSwitchCase (ObjWriter* w, DataBuffer* b); 155 void AddSwitchCase (ObjWriter* w, DataBuffer* b);
196 156
197 private: 157 private:
198 bool atnewline; 158 bool atnewline;
199 char c; 159 char c;
200 void ParserMessage (const char* header, char* message); 160 void ParserMessage (const char* header, string message);
201 161
202 bool DoDirectivePreprocessing (); 162 bool DoDirectivePreprocessing ();
203 char PPReadChar (); 163 char PPReadChar ();
204 void PPMustChar (char c); 164 void PPMustChar (char c);
205 str PPReadWord (char &term); 165 string PPReadWord (char &term);
206 }; 166 };
207 167
208 constinfo_t* FindConstant (str token); 168 constinfo_t* FindConstant (string token);
209 extern bool g_Neurosphere; 169 extern bool g_Neurosphere;
210 170
211 #endif // __SCRIPTREADER_H__ 171 #endif // BOTC_SCRIPTREADER_H

mercurial