@@ -21,6 +21,9 @@ enum vm_token
2121 VM_TOKEN_STORE2 = 0x21 ,
2222 VM_TOKEN_STORE3 = 0x22 ,
2323 VM_TOKEN_STORE4 = 0x23 ,
24+ VM_TOKEN_END = 0x30 ,
25+ VM_TOKEN_STRUCTURE = 0x31 ,
26+ VM_TOKEN_FIELD=0x32 ,
2427 VM_TOKEN_LOCATION1 = 0x90 ,
2528 VM_TOKEN_LOCATION2 = 0x91 ,
2629 VM_TOKEN_LOCATION3 = 0x92 ,
@@ -142,6 +145,18 @@ void get_next_token(const std::string& line, int* pos, vm_token* token)
142145 {
143146 *token = VM_TOKEN_LOCATION4;
144147 }
148+ else if (strcmp (data, " STRUCTURE" ) == 0 )
149+ {
150+ *token = VM_TOKEN_STRUCTURE;
151+ }
152+ else if (strcmp (data, " FIELD" ) == 0 )
153+ {
154+ *token = VM_TOKEN_FIELD;
155+ }
156+ else if (strcmp (data, " END" ) == 0 )
157+ {
158+ *token = VM_TOKEN_END;
159+ }
145160 else
146161 {
147162 *token = VM_TOKEN_LITERAL;
@@ -253,6 +268,96 @@ bool read_vm_mul(const std::string& input, int* pos, int* start, vm_token* token
253268 return read_vm_binary_op (input, pos, start, token, op, VM_MUL);
254269}
255270
271+ void read_vm_structure_name (const std::string& input, int start, char * name)
272+ {
273+ char * end = name + VM_STRUCTURE_MAX_NAME;
274+ bool eatingWhitespace = true ;
275+ for (; start < input.length () && name < end - 1 ; start++)
276+ {
277+ if (input[start] == ' ' )
278+ {
279+ if (eatingWhitespace)
280+ {
281+ continue ;
282+ }
283+ else
284+ {
285+ break ;
286+ }
287+ }
288+
289+ if (input[start] == ' \n ' )
290+ {
291+ break ;
292+ }
293+
294+ if (input[start] == ' ;' )
295+ {
296+ break ;
297+ }
298+
299+ *name = input[start];
300+ name++;
301+ eatingWhitespace = false ;
302+ }
303+
304+ *name = ' \0 ' ;
305+ }
306+
307+ bool read_vm_struct (const std::string& input, int * pos, int * start, vm_token* token, vm_structure* structure)
308+ {
309+ *start = *pos;
310+ get_next_token (input, pos, token);
311+ if (*token != VM_TOKEN_LITERAL)
312+ {
313+ // error
314+ return false ;
315+ }
316+
317+ read_vm_structure_name (input, *start, structure->name );
318+
319+ structure->fieldcount = 0 ;
320+
321+ while (*token != VM_TOKEN_EOF)
322+ {
323+ *start = *pos;
324+ get_next_token (input, pos, token);
325+ if (*token == VM_TOKEN_FIELD)
326+ {
327+ *start = *pos;
328+ get_next_token (input, pos, token);
329+ if (*token == VM_TOKEN_LITERAL)
330+ {
331+ const char * ptr = input.c_str ();
332+ int size = strtol (ptr + (*start), 0 , 10 );
333+
334+ if (structure->fieldcount == VM_STRUCTURE_MAX_FIELD)
335+ {
336+ // error
337+ return false ;
338+ }
339+
340+ vm_field* field = &structure->fields [structure->fieldcount ];
341+ field->size = size;
342+ field->array_size = 0 ;
343+ structure->fieldcount ++;
344+ }
345+ }
346+ else if (*token == VM_TOKEN_END)
347+ {
348+ return true ;
349+ }
350+ else
351+ {
352+ // error
353+ return false ;
354+ }
355+ }
356+
357+ // error
358+ return false ;
359+ }
360+
256361void set_error (char * errorOutput, char * errorMsg, int maxErrorLength, const std::string& str, int pos, int end)
257362{
258363 std::stringstream stream;
@@ -269,6 +374,7 @@ void set_error(char* errorOutput, char* errorMsg, int maxErrorLength, const std:
269374bool load_file_and_execute (const char * filename, const vm_options& options, char * errorText, int errorLength)
270375{
271376 std::vector<vm_operation> operations;
377+ std::vector<vm_structure> structures;
272378
273379 std::string str;
274380 std::ifstream stream;
@@ -364,6 +470,20 @@ bool load_file_and_execute(const char* filename, const vm_options& options, char
364470 break ;
365471 }
366472 }
473+ else if (token == VM_TOKEN_STRUCTURE)
474+ {
475+ vm_structure structure;
476+ if (read_vm_struct (str, &pos, &start, &token, &structure))
477+ {
478+ structures.push_back (structure);
479+ }
480+ else
481+ {
482+ success = false ;
483+ set_error (errorText, " Error: Unexpected token parsing STRUCTURE" , errorLength, str, start, pos);
484+ break ;
485+ }
486+ }
367487 else if (token != VM_TOKEN_EOF)
368488 {
369489 success = false ;
0 commit comments