119 ParserError ("unexpected `}`"); |
119 ParserError ("unexpected `}`"); |
120 } |
120 } |
121 } else { |
121 } else { |
122 // Check if it's a command. |
122 // Check if it's a command. |
123 CommandDef* comm = GetCommandByName (token); |
123 CommandDef* comm = GetCommandByName (token); |
124 if (comm) { |
124 if (comm) |
125 w->Write<long> (DH_COMMAND); |
125 ParseCommand (comm, w); |
126 w->Write<long> (comm->number); |
126 else |
127 w->Write<long> (comm->maxargs); |
|
128 MustNext ("("); |
|
129 int curarg = 0; |
|
130 while (1) { |
|
131 if (curarg >= comm->maxargs) { |
|
132 if (!PeekNext().compare (",")) |
|
133 ParserError ("got `,` while expecting command-terminating `)`, are you passing too many parameters? (max %d)", |
|
134 comm->maxargs); |
|
135 MustNext (")"); |
|
136 curarg++; |
|
137 break; |
|
138 } |
|
139 |
|
140 if (!Next ()) |
|
141 ParserError ("unexpected end-of-file, unterminated command"); |
|
142 |
|
143 // If we get a ")" now, the user probably gave too few parameters |
|
144 if (!token.compare (")")) |
|
145 ParserError ("unexpected `)`, did you pass too few parameters? (need %d)", comm->numargs); |
|
146 |
|
147 // For now, it takes in just numbers. |
|
148 // Needs to cater for string arguments too... |
|
149 if (!token.isnumber()) |
|
150 ParserError ("argument %d (`%s`) is not a number", curarg, token.chars()); |
|
151 |
|
152 int i = atoi (token.chars ()); |
|
153 w->Write<long> (i); |
|
154 |
|
155 if (curarg < comm->numargs - 1) { |
|
156 MustNext (","); |
|
157 } else if (curarg < comm->maxargs - 1) { |
|
158 // Can continue, but can terminate as well. |
|
159 if (!PeekNext ().compare (")")) { |
|
160 MustNext (")"); |
|
161 curarg++; |
|
162 break; |
|
163 } else |
|
164 MustNext (","); |
|
165 } |
|
166 |
|
167 curarg++; |
|
168 } |
|
169 MustNext (";"); |
|
170 |
|
171 // If the script skipped a few arguments, fill in defaults. |
|
172 while (curarg < comm->maxargs) { |
|
173 w->Write<long> (comm->defvals[curarg]); |
|
174 curarg++; |
|
175 } |
|
176 } else |
|
177 ParserError ("unknown keyword `%s`!", token.chars()); |
127 ParserError ("unknown keyword `%s`!", token.chars()); |
178 } |
128 } |
179 } |
129 } |
180 |
130 |
181 if (g_CurMode != MODE_TOPLEVEL) |
131 if (g_CurMode != MODE_TOPLEVEL) |
197 w->Write (BOTCMD_BEGINJUMPING); |
147 w->Write (BOTCMD_BEGINJUMPING); |
198 w->Write (0); |
148 w->Write (0); |
199 w->Write (DH_ENDEVENT); |
149 w->Write (DH_ENDEVENT); |
200 */ |
150 */ |
201 } |
151 } |
|
152 |
|
153 void ScriptReader::ParseCommand (CommandDef* comm, ObjWriter* w) { |
|
154 w->Write<long> (DH_COMMAND); |
|
155 w->Write<long> (comm->number); |
|
156 w->Write<long> (comm->maxargs); |
|
157 MustNext ("("); |
|
158 int curarg = 0; |
|
159 while (1) { |
|
160 if (curarg >= comm->maxargs) { |
|
161 if (!PeekNext().compare (",")) |
|
162 ParserError ("got `,` while expecting command-terminating `)`, are you passing too many parameters? (max %d)", |
|
163 comm->maxargs); |
|
164 MustNext (")"); |
|
165 curarg++; |
|
166 break; |
|
167 } |
|
168 |
|
169 if (!Next ()) |
|
170 ParserError ("unexpected end-of-file, unterminated command"); |
|
171 |
|
172 // If we get a ")" now, the user probably gave too few parameters |
|
173 if (!token.compare (")")) |
|
174 ParserError ("unexpected `)`, did you pass too few parameters? (need %d)", comm->numargs); |
|
175 |
|
176 // For now, it takes in just numbers. |
|
177 // Needs to cater for string arguments too... |
|
178 if (!token.isnumber()) |
|
179 ParserError ("argument %d (`%s`) is not a number", curarg, token.chars()); |
|
180 |
|
181 int i = atoi (token.chars ()); |
|
182 w->Write<long> (i); |
|
183 |
|
184 if (curarg < comm->numargs - 1) { |
|
185 MustNext (","); |
|
186 } else if (curarg < comm->maxargs - 1) { |
|
187 // Can continue, but can terminate as well. |
|
188 if (!PeekNext ().compare (")")) { |
|
189 MustNext (")"); |
|
190 curarg++; |
|
191 break; |
|
192 } else |
|
193 MustNext (","); |
|
194 } |
|
195 |
|
196 curarg++; |
|
197 } |
|
198 MustNext (";"); |
|
199 |
|
200 // If the script skipped any optional arguments, fill in defaults. |
|
201 while (curarg < comm->maxargs) { |
|
202 w->Write<long> (comm->defvals[curarg]); |
|
203 curarg++; |
|
204 } |
|
205 } |