scriptreader.h

changeset 66
4fc1ec88aa41
parent 65
ec04357f5bb9
child 68
588cc27e84bb
equal deleted inserted replaced
65:ec04357f5bb9 66:4fc1ec88aa41
50 #define MAX_SCOPE 32 50 #define MAX_SCOPE 32
51 #define MAX_CASE 64 51 #define MAX_CASE 64
52 52
53 class ScriptVar; 53 class ScriptVar;
54 54
55 // ============================================================================ 55 enum type_e {
56 // Meta-data about blocks
57 struct ScopeInfo {
58 unsigned int mark1;
59 unsigned int mark2;
60 unsigned int type;
61 DataBuffer* buffer1;
62
63 // switch-related stuff
64 // Which case are we at?
65 short casecursor;
66
67 // Marks to case-blocks
68 int casemarks[MAX_CASE];
69
70 // Numbers of the case labels
71 int casenumbers[MAX_CASE];
72
73 // actual case blocks
74 DataBuffer* casebuffers[MAX_CASE];
75
76 // What is the current buffer of the block?
77 DataBuffer* recordbuffer;
78 };
79
80 // ============================================================================
81 // The script reader reads the script, parses it and tells the object writer
82 // the bytecode it needs to write to file.
83 class ScriptReader {
84 public:
85 // ====================================================================
86 // MEMBERS
87 FILE* fp[MAX_FILESTACK];
88 char* filepath[MAX_FILESTACK];
89 int fc;
90
91 unsigned int pos[MAX_FILESTACK];
92 unsigned int curline[MAX_FILESTACK];
93 unsigned int curchar[MAX_FILESTACK];
94 ScopeInfo scopestack[MAX_SCOPE];
95 long savedpos[MAX_FILESTACK]; // filepointer cursor position
96 str token;
97 int commentmode;
98 long prevpos;
99 str prevtoken;
100
101 // ====================================================================
102 // METHODS
103 // scriptreader.cxx:
104 ScriptReader (str path);
105 ~ScriptReader ();
106 void OpenFile (str path);
107 void CloseFile (unsigned int u = MAX_FILESTACK);
108 char ReadChar ();
109 char PeekChar (int offset = 0);
110 bool Next (bool peek = false);
111 void Prev ();
112 str PeekNext (int offset = 0);
113 void Seek (unsigned int n, int origin);
114 void MustNext (const char* c = "");
115 void MustThis (const char* c);
116 void MustString (bool gotquote = false);
117 void MustNumber (bool fromthis = false);
118 void MustBool ();
119 bool BoolValue ();
120
121 void ParserError (const char* message, ...);
122 void ParserWarning (const char* message, ...);
123
124 // parser.cxx:
125 void ParseBotScript (ObjWriter* w);
126 DataBuffer* ParseCommand (CommandDef* comm);
127 DataBuffer* ParseExpression (int reqtype);
128 DataBuffer* ParseAssignment (ScriptVar* var);
129 int ParseOperator (bool peek = false);
130 DataBuffer* ParseExprValue (int reqtype);
131 void PushScope ();
132
133 // preprocessor.cxx:
134 void PreprocessDirectives ();
135 void PreprocessMacros ();
136 DataBuffer* ParseStatement (ObjWriter* w);
137 void AddSwitchCase (ObjWriter* w, DataBuffer* b);
138
139 private:
140 bool atnewline;
141 char c;
142 void ParserMessage (const char* header, char* message);
143
144 bool DoDirectivePreprocessing ();
145 char PPReadChar ();
146 void PPMustChar (char c);
147 str PPReadWord (char &term);
148 };
149
150 enum {
151 TYPE_VOID = 0, 56 TYPE_VOID = 0,
152 TYPE_INT, 57 TYPE_INT,
153 TYPE_STRING, 58 TYPE_STRING,
154 TYPE_FLOAT, 59 TYPE_FLOAT,
155 TYPE_BOOL 60 TYPE_BOOL
156 }; 61 };
157 62
158 // Operators 63 // Operators
159 enum { 64 enum operator_e {
160 OPER_ADD, 65 OPER_ADD,
161 OPER_SUBTRACT, 66 OPER_SUBTRACT,
162 OPER_MULTIPLY, 67 OPER_MULTIPLY,
163 OPER_DIVIDE, 68 OPER_DIVIDE,
164 OPER_MODULUS, 69 OPER_MODULUS,
185 OPER_BITWISEEOR, 90 OPER_BITWISEEOR,
186 OPER_TERNARY, 91 OPER_TERNARY,
187 }; 92 };
188 93
189 // Mark types 94 // Mark types
190 enum { 95 enum marktype_e {
191 MARKTYPE_LABEL, 96 MARKTYPE_LABEL,
192 MARKTYPE_IF, 97 MARKTYPE_IF,
193 MARKTYPE_INTERNAL, // internal structures 98 MARKTYPE_INTERNAL, // internal structures
194 }; 99 };
195 100
196 // Block types 101 // Block types
197 enum { 102 enum scopetype_e {
198 SCOPETYPE_UNSET = 0, 103 SCOPETYPE_UNKNOWN,
199 SCOPETYPE_IF, 104 SCOPETYPE_IF,
200 SCOPETYPE_WHILE, 105 SCOPETYPE_WHILE,
201 SCOPETYPE_FOR, 106 SCOPETYPE_FOR,
202 SCOPETYPE_DO, 107 SCOPETYPE_DO,
203 SCOPETYPE_SWITCH, 108 SCOPETYPE_SWITCH,
204 SCOPETYPE_ELSE, 109 SCOPETYPE_ELSE,
205 }; 110 };
206 111
112 // ============================================================================
113 // Meta-data about blocks
114 struct ScopeInfo {
115 unsigned int mark1;
116 unsigned int mark2;
117 scopetype_e type;
118 DataBuffer* buffer1;
119
120 // switch-related stuff
121 // Which case are we at?
122 short casecursor;
123
124 // Marks to case-blocks
125 int casemarks[MAX_CASE];
126
127 // Numbers of the case labels
128 int casenumbers[MAX_CASE];
129
130 // actual case blocks
131 DataBuffer* casebuffers[MAX_CASE];
132
133 // What is the current buffer of the block?
134 DataBuffer* recordbuffer;
135 };
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 (int reqtype);
185 DataBuffer* ParseAssignment (ScriptVar* var);
186 int ParseOperator (bool peek = false);
187 DataBuffer* ParseExprValue (int reqtype);
188 void PushScope ();
189
190 // preprocessor.cxx:
191 void PreprocessDirectives ();
192 void PreprocessMacros ();
193 DataBuffer* ParseStatement (ObjWriter* w);
194 void AddSwitchCase (ObjWriter* w, DataBuffer* b);
195
196 private:
197 bool atnewline;
198 char c;
199 void ParserMessage (const char* header, char* message);
200
201 bool DoDirectivePreprocessing ();
202 char PPReadChar ();
203 void PPMustChar (char c);
204 str PPReadWord (char &term);
205 };
206
207 extern bool g_Neurosphere;
208
207 #endif // __SCRIPTREADER_H__ 209 #endif // __SCRIPTREADER_H__

mercurial