Skip to content

Commit 39be93a

Browse files
committed
Add parsing structures
1 parent 3325da6 commit 39be93a

File tree

3 files changed

+159
-0
lines changed

3 files changed

+159
-0
lines changed

IslanderVM/Test/Test5.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
; Structures
2+
STRUCTURE MyStruct
3+
FIELD 4
4+
FIELD 4
5+
FIELD 2
6+
END
7+
8+
;STRUCTURE MyString
9+
; FIELD 4
10+
; FIELD ARRAY 4,128
11+
;END
12+
13+
;PUSH MyStruct : M1, M2, M3
14+
;PUSH MyString : M4, M5
15+
16+
;STORE1 1
17+
;ADD M4, S1
18+
19+
20+
;POP MyString
21+
;POP MyStruct
22+
23+

IslanderVM/parser.cpp

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
256361
void 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:
269374
bool 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;

IslanderVM/vm.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#pragma once
22
#include <vector>
33

4+
#define VM_STRUCTURE_MAX_NAME 128
5+
#define VM_STRUCTURE_MAX_FIELD 128
6+
47
enum vm_code
58
{
69
VM_NOP = 0x0,
@@ -26,6 +29,19 @@ struct vm_operation
2629
int arg2;
2730
};
2831

32+
struct vm_field
33+
{
34+
int size;
35+
int array_size;
36+
};
37+
38+
struct vm_structure
39+
{
40+
int fieldcount;
41+
char name[VM_STRUCTURE_MAX_NAME];
42+
vm_field fields[VM_STRUCTURE_MAX_FIELD];
43+
};
44+
2945
struct vm_options
3046
{
3147
bool x64;

0 commit comments

Comments
 (0)