summaryrefslogtreecommitdiff
path: root/src/puzzles
diff options
context:
space:
mode:
Diffstat (limited to 'src/puzzles')
-rw-r--r--src/puzzles/parser.c55
1 files changed, 44 insertions, 11 deletions
diff --git a/src/puzzles/parser.c b/src/puzzles/parser.c
index 0f539b8..c88442b 100644
--- a/src/puzzles/parser.c
+++ b/src/puzzles/parser.c
@@ -61,7 +61,7 @@ typedef enum {
TYPE_LIST = 4,
} V1_IO_TYPE;
-void ReadPuzzleIO_V1(FILE *file, PuzzleIO *io) {
+bool ReadPuzzleIO_V1(FILE *file, PuzzleIO *io) {
uint8_t type = read_u8(file);
switch (type) {
@@ -73,6 +73,7 @@ void ReadPuzzleIO_V1(FILE *file, PuzzleIO *io) {
case TYPE_BOOL:
{
bool *outp = malloc(sizeof(bool));
+ if (outp == NULL) return false;
*outp = read_u8(file) != 0;
io->type = WREN_TYPE_BOOL;
io->value = outp;
@@ -82,6 +83,7 @@ void ReadPuzzleIO_V1(FILE *file, PuzzleIO *io) {
case TYPE_NUM:
{
double *outp = malloc(sizeof(double));
+ if (outp == NULL) return false;
*outp = read_double(file);
io->type = WREN_TYPE_NUM;
io->value = outp;
@@ -93,28 +95,56 @@ void ReadPuzzleIO_V1(FILE *file, PuzzleIO *io) {
io->value = read_string(file);
break;
- case TYPE_LIST:
- TraceLog(LOG_ERROR, "The arrays are not yet supported");
- io->type = WREN_TYPE_NULL;
- io->value = NULL;
- break;
+ case TYPE_LIST:
+ {
+ PuzzleListIO *outp = malloc(sizeof(PuzzleListIO));
+ if (outp == NULL) return false;
+
+ outp->length = read_u32(file);
+ outp->elements = malloc(sizeof(PuzzleIO) * outp->length);
+ if (outp->elements == NULL) {
+ free(outp);
+ return false;
+ }
+
+ for (size_t i = 0; i < outp->length; i++) {
+ if (!ReadPuzzleIO_V1(file, &outp->elements[i])) {
+ free(outp);
+ return false;
+ }
+ }
+
+ io->type = WREN_TYPE_LIST;
+ io->value = outp;
+ break;
+ }
default:
- return;
+ return true;
}
+ return true;
}
-void readTestV1(FILE *file, Test *outp) {
+bool readTestV1(FILE *file, Test *outp) {
outp->RecieverName = read_string(file);
outp->MainMethodSignature = read_string(file);
outp->argc = read_u32(file);
outp->argv = malloc(sizeof(PuzzleIO) * outp->argc);
+ if (outp->argv == NULL) return false;
for (size_t i = 0; i < outp->argc; i++) {
- ReadPuzzleIO_V1(file, &outp->argv[i]);
+ if (!ReadPuzzleIO_V1(file, &outp->argv[i])) {
+ free(outp->argv);
+ return false;
+ }
+ }
+
+ if (!ReadPuzzleIO_V1(file, &outp->ExpectedResult)) {
+ free(outp->argv);
+ return false;
}
- ReadPuzzleIO_V1(file, &outp->ExpectedResult);
+ return true;
}
// Reads file with the specification from puzzle_specs_v1.md.
@@ -130,7 +160,10 @@ Puzzle *parseFileV1(FILE *file) {
outp->Tests = malloc(sizeof(Test) * outp->TestC);
for (size_t i = 0; i < outp->TestC; i++) {
- readTestV1(file, &outp->Tests[i]);
+ if (!readTestV1(file, &outp->Tests[i])) {
+ FreePuzzle(outp);
+ return NULL;
+ }
}
return outp;