summaryrefslogtreecommitdiff
path: root/src/puzzles/parser.c
blob: c88442bb144cdaa94f1618439b421126934b4421 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <string.h>
#include "puzzle.h"
#include <raylib.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>

/// Reads a uint8_t from file.
/// Assumes that the file is on the position
uint8_t read_u8(FILE *file) {
    uint8_t outp;
    fread(&outp, 1, 1, file);
    return outp;
}

/// Reads a uint32_t (little-endian) from file.
/// Assumes that the file is on the position
uint32_t read_u32(FILE *file) {
    uint32_t outp = 0;
    for (int i = 0; i < 4; i++) {
        outp += ((uint32_t)read_u8(file)) << (i * 8);
    }
    return outp;
}

/// Reads a double from file.
/// Assumes that the file is on the position
double read_double(FILE *file) {
    double outp;
    fread(&outp, sizeof(double), 1, file);
    return outp;
}

/// Reads a null terminated string from file. 
/// Assumes that the file if on the position.
/// Allocates the string.
char *read_string(FILE *file) {
    char buf[8192];
    size_t idx = 0;
    int c;

    while (idx < sizeof(buf) - 1) {
        c = fgetc(file);
        
        if (c == 0 || c == EOF) break;
        
        buf[idx] = (char)c;
        idx += 1;
    }
    
    buf[idx] = 0x0;
    return strdup(buf);
}

typedef enum {
    TYPE_NULL = 0,
    TYPE_BOOL = 1,
    TYPE_NUM = 2,
    TYPE_STRING = 3,
    TYPE_LIST = 4,
} V1_IO_TYPE;

bool ReadPuzzleIO_V1(FILE *file, PuzzleIO *io) {
    uint8_t type = read_u8(file);

    switch (type) {
        case TYPE_NULL: // NULL
            io->type = WREN_TYPE_NULL;
            io->value = NULL;
            break;

        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;
                break;
            }

        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;
                break;
            }

        case TYPE_STRING:
            io->type = WREN_TYPE_STRING;
            io->value = read_string(file);
            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 true;
    }
    return true;
}

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++) {
         if (!ReadPuzzleIO_V1(file, &outp->argv[i])) {
             free(outp->argv);
             return false;
         }
    }

    if (!ReadPuzzleIO_V1(file, &outp->ExpectedResult)) {
        free(outp->argv);
        return false;
    }

    return true;
}

// Reads file with the specification from puzzle_specs_v1.md.
// Assumes the first byte of the file was read and the file was not rewinded.
Puzzle *parseFileV1(FILE *file) {
    Puzzle *outp = malloc(sizeof(Puzzle));
    if (outp == NULL) return NULL;

    outp->Name = read_string(file);
    outp->Description = read_string(file);
    outp->InitialCode = read_string(file);
    outp->TestC = read_u32(file);

    outp->Tests = malloc(sizeof(Test) * outp->TestC);
    for (size_t i = 0; i < outp->TestC; i++) {
         if (!readTestV1(file, &outp->Tests[i])) {
             FreePuzzle(outp);
             return NULL;
         }
    }

    return outp;
}

Puzzle *ReadPuzzleFromFile(const char *path) {
    FILE *file = fopen(path, "r");
    if (file == NULL) {
        perror("Failed to parse the puzzle file");
        return NULL;
    }

    uint8_t spec_version = read_u8(file);
    // printf("Version: %d\n", spec_version);
    Puzzle *outp = NULL;
    switch (spec_version) {
        case 1:
            outp = parseFileV1(file);
            break;
        default:
            TraceLog(LOG_ERROR, "Failed to parse the puzzle file: unknown spec version");
            break;
    }

    fclose(file);
    return outp;
}