47 #include "objwriter.h" |
47 #include "objwriter.h" |
48 #include "scriptreader.h" |
48 #include "scriptreader.h" |
49 #include "events.h" |
49 #include "events.h" |
50 #include "commands.h" |
50 #include "commands.h" |
51 #include "stringtable.h" |
51 #include "stringtable.h" |
|
52 #include "variables.h" |
52 |
53 |
53 #define MUST_TOPLEVEL if (g_CurMode != MODE_TOPLEVEL) \ |
54 #define MUST_TOPLEVEL if (g_CurMode != MODE_TOPLEVEL) \ |
54 ParserError ("%ss may only be defined at top level!", token.chars()); |
55 ParserError ("%ss may only be defined at top level!", token.chars()); |
55 |
56 |
56 int g_NumStates = 0; |
57 int g_NumStates = 0; |
134 bool onenter = !token.compare ("onenter"); |
135 bool onenter = !token.compare ("onenter"); |
135 |
136 |
136 MustNext ("{"); |
137 MustNext ("{"); |
137 g_CurMode = onenter ? MODE_ONENTER : MODE_ONEXIT; |
138 g_CurMode = onenter ? MODE_ONENTER : MODE_ONEXIT; |
138 w->Write (onenter ? DH_ONENTER : DH_ONEXIT); |
139 w->Write (onenter ? DH_ONENTER : DH_ONEXIT); |
|
140 } else if (!token.compare ("int") || !token.compare ("bool")) { |
|
141 // For now, only globals are supported |
|
142 if (g_CurMode != MODE_TOPLEVEL || g_CurState.len()) |
|
143 ParserError ("variables must only be global for now"); |
|
144 |
|
145 // Variable definition |
|
146 int type = !token.compare ("int") ? RETURNVAL_INT: RETURNVAL_BOOLEAN; |
|
147 |
|
148 MustNext (); |
|
149 str varname = token; |
|
150 ScriptVar* var = DeclareGlobalVariable (this, varname, type); |
|
151 |
|
152 if (!var) |
|
153 ParserError ("declaring %s variable %s failed", |
|
154 g_CurState.len() ? "state" : "global", varname.chars()); |
|
155 |
|
156 // Assign it, if desired |
|
157 if (!PeekNext().compare ("=")) { |
|
158 MustNext ("="); |
|
159 MustValue (type); |
|
160 |
|
161 var->value = token; |
|
162 } |
|
163 |
|
164 MustNext (";"); |
139 } else if (!token.compare ("}")) { |
165 } else if (!token.compare ("}")) { |
140 // Closing brace |
166 // Closing brace |
141 int dataheader = (g_CurMode == MODE_EVENT) ? DH_ENDEVENT : |
167 int dataheader = (g_CurMode == MODE_EVENT) ? DH_ENDEVENT : |
142 (g_CurMode == MODE_MAINLOOP) ? DH_ENDMAINLOOP : |
168 (g_CurMode == MODE_MAINLOOP) ? DH_ENDMAINLOOP : |
143 (g_CurMode == MODE_ONENTER) ? DH_ENDONENTER : |
169 (g_CurMode == MODE_ONENTER) ? DH_ENDONENTER : |
180 w->Write<long> (DH_STRINGLIST); |
206 w->Write<long> (DH_STRINGLIST); |
181 w->Write<long> (stringcount); |
207 w->Write<long> (stringcount); |
182 for (unsigned int a = 0; a < stringcount; a++) |
208 for (unsigned int a = 0; a < stringcount; a++) |
183 w->WriteString (g_StringTable[a]); |
209 w->WriteString (g_StringTable[a]); |
184 } |
210 } |
185 printf ("%u string%s written\n", stringcount, (stringcount != 1) ? "s" : ""); |
211 |
|
212 printf ("%u string%s written\n", stringcount, PLURAL (stringcount)); |
186 } |
213 } |
187 |
214 |
188 void ScriptReader::ParseCommand (CommandDef* comm, ObjWriter* w) { |
215 void ScriptReader::ParseCommand (CommandDef* comm, ObjWriter* w) { |
189 // If this was defined at top-level, we stop right at square one! |
216 // If this was defined at top-level, we stop right at square one! |
190 if (g_CurMode == MODE_TOPLEVEL) |
217 if (g_CurMode == MODE_TOPLEVEL) |