src/scriptreader.h

changeset 73
1ee9b312dc18
parent 72
03e4d9db3fd9
equal deleted inserted replaced
72:03e4d9db3fd9 73:1ee9b312dc18
1 #ifndef BOTC_SCRIPTREADER_H
2 #define BOTC_SCRIPTREADER_H
3
4 #include <stdio.h>
5 #include "main.h"
6 #include "commands.h"
7 #include "objwriter.h"
8
9 #define MAX_FILESTACK 8
10 #define MAX_SCOPE 32
11 #define MAX_CASE 64
12
13 class ScriptVar;
14
15 // Operators
16 enum operator_e {
17 OPER_ADD,
18 OPER_SUBTRACT,
19 OPER_MULTIPLY,
20 OPER_DIVIDE,
21 OPER_MODULUS,
22 OPER_ASSIGN,
23 OPER_ASSIGNADD,
24 OPER_ASSIGNSUB,
25 OPER_ASSIGNMUL,
26 OPER_ASSIGNDIV,
27 OPER_ASSIGNMOD, // -- 10
28 OPER_EQUALS,
29 OPER_NOTEQUALS,
30 OPER_LESSTHAN,
31 OPER_GREATERTHAN,
32 OPER_LESSTHANEQUALS,
33 OPER_GREATERTHANEQUALS,
34 OPER_LEFTSHIFT,
35 OPER_RIGHTSHIFT,
36 OPER_ASSIGNLEFTSHIFT,
37 OPER_ASSIGNRIGHTSHIFT, // -- 20
38 OPER_OR,
39 OPER_AND,
40 OPER_BITWISEOR,
41 OPER_BITWISEAND,
42 OPER_BITWISEEOR,
43 OPER_TERNARY,
44 OPER_STRLEN,
45 };
46
47 // Mark types
48 enum marktype_e {
49 MARKTYPE_LABEL,
50 MARKTYPE_IF,
51 MARKTYPE_INTERNAL, // internal structures
52 };
53
54 // Block types
55 enum scopetype_e {
56 SCOPETYPE_UNKNOWN,
57 SCOPETYPE_IF,
58 SCOPETYPE_WHILE,
59 SCOPETYPE_FOR,
60 SCOPETYPE_DO,
61 SCOPETYPE_SWITCH,
62 SCOPETYPE_ELSE,
63 };
64
65 // ============================================================================
66 // Meta-data about blocks
67 struct ScopeInfo {
68 unsigned int mark1;
69 unsigned int mark2;
70 scopetype_e type;
71 DataBuffer* buffer1;
72
73 // switch-related stuff
74 // Which case are we at?
75 short casecursor;
76
77 // Marks to case-blocks
78 int casemarks[MAX_CASE];
79
80 // Numbers of the case labels
81 int casenumbers[MAX_CASE];
82
83 // actual case blocks
84 DataBuffer* casebuffers[MAX_CASE];
85
86 // What is the current buffer of the block?
87 DataBuffer* recordbuffer;
88 };
89
90 // ============================================================================
91 typedef struct {
92 string name;
93 type_e type;
94 string val;
95 } constinfo_t;
96
97 // ============================================================================
98 // The script reader reads the script, parses it and tells the object writer
99 // the bytecode it needs to write to file.
100 class ScriptReader {
101 public:
102 // ====================================================================
103 // MEMBERS
104 FILE* fp[MAX_FILESTACK];
105 string filepath[MAX_FILESTACK];
106 int fc;
107
108 unsigned int pos[MAX_FILESTACK];
109 unsigned int curline[MAX_FILESTACK];
110 unsigned int curchar[MAX_FILESTACK];
111 ScopeInfo scopestack[MAX_SCOPE];
112 long savedpos[MAX_FILESTACK]; // filepointer cursor position
113 string token;
114 int commentmode;
115 long prevpos;
116 string prevtoken;
117
118 // ====================================================================
119 // METHODS
120 // scriptreader.cxx:
121 ScriptReader (string path);
122 ~ScriptReader ();
123 void OpenFile (string path);
124 void CloseFile (unsigned int u = MAX_FILESTACK);
125 char ReadChar ();
126 char PeekChar (int offset = 0);
127 bool Next (bool peek = false);
128 void Prev ();
129 string PeekNext (int offset = 0);
130 void Seek (unsigned int n, int origin);
131 void MustNext (const char* c = "");
132 void MustThis (const char* c);
133 void MustString (bool gotquote = false);
134 void MustNumber (bool fromthis = false);
135 void MustBool ();
136 bool BoolValue ();
137
138 void ParserError (const char* message, ...);
139 void ParserWarning (const char* message, ...);
140
141 // parser.cxx:
142 void ParseBotScript (ObjWriter* w);
143 DataBuffer* ParseCommand (CommandDef* comm);
144 DataBuffer* ParseExpression (type_e reqtype);
145 DataBuffer* ParseAssignment (ScriptVar* var);
146 int ParseOperator (bool peek = false);
147 DataBuffer* ParseExprValue (type_e reqtype);
148 string ParseFloat ();
149 void PushScope ();
150
151 // preprocessor.cxx:
152 void PreprocessDirectives ();
153 void PreprocessMacros ();
154 DataBuffer* ParseStatement (ObjWriter* w);
155 void AddSwitchCase (ObjWriter* w, DataBuffer* b);
156
157 private:
158 bool atnewline;
159 char c;
160 void ParserMessage (const char* header, string message);
161
162 bool DoDirectivePreprocessing ();
163 char PPReadChar ();
164 void PPMustChar (char c);
165 string PPReadWord (char &term);
166 };
167
168 constinfo_t* FindConstant (string token);
169 extern bool g_Neurosphere;
170
171 #endif // BOTC_SCRIPTREADER_H

mercurial