aboutsummaryrefslogtreecommitdiff
path: root/src/core/sok-level-parser.c
blob: 276ebcb3b3bfb80c716a7f30abb125e4021e3535 (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 <stdbool.h>
#include <stddef.h> // size_t
#include "sok.h"

#define UNUSED(a) ((void) (a))

#define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
#define CASE_DIGIT \
  case '0': \
  case '1': \
  case '2': \
  case '3': \
  case '4': \
  case '5': \
  case '6': \
  case '7': \
  case '8': \
  case '9':

void
sok_level_parser_init(
  sok_level_parser_t * const parser, 
  const sok_level_parser_cbs_t * const cbs,
  void * const user_data
) {
  parser->cbs = cbs;
  parser->user_data = user_data;
}

bool
sok_level_parser_parse(
  sok_level_parser_t * const parser,
  const char * const buf,
  const size_t buf_len
) {
  sok_pos_t size = { .x = 0, .y = 0 };

  for (size_t i = 0, ofs = 0, w = 0; i < buf_len; i++) {
    if (buf[i] == '|') {
      if (w > size.x) {
        size.x = w;
      }

      // reset column position, increment row count
      w = 0;
      size.y++;
    } else if (IS_DIGIT(buf[i])) {
      ofs = 10 * ofs + (buf[i] - '0');
    } else if (ofs > 0) {
      w += ofs;
      ofs = 0;
    } else {
      w++;
    }
  }

  // increment row count
  size.y++;

  // emit level size
  if (
    parser->cbs->on_size &&
    !parser->cbs->on_size(parser, size) 
  ) {
    // return failure
    return false;
  }

  sok_pos_t pos = { 0, 0 };
  for (size_t i = 0, ofs = 0; i < buf_len; i++) {
    switch (buf[i]) {
    case '|':
      // new line
      pos.x = 0;
      pos.y++;

      break;
    CASE_DIGIT
      ofs = 10 * ofs + (buf[i] - '0');
      break;
    case '-':
    case '_':
    case ' ':
      // advance
      pos.x += (ofs > 0) ? ofs : 1;
      ofs = 0;

      break;
    case '+':
      // emit goal
      if (parser->cbs->on_goal && !parser->cbs->on_goal(parser, pos)) {
        // return failure
        return false;
      }

      // emit home
      if (parser->cbs->on_home && !parser->cbs->on_home(parser, pos)) {
        // return failure
        return false;
      }

      // advance
      pos.x++;

      break;
    case '@':
      // emit home
      if (parser->cbs->on_home && !parser->cbs->on_home(parser, pos)) {
        // return failure
        return false;
      }

      // advance
      pos.x++;

      break;
    case '#':
      for (size_t j = (ofs > 0) ? ofs : 1; j; j--) {
        // emit wall
        if (parser->cbs->on_wall && !parser->cbs->on_wall(parser, pos)) {
          // return failure
          return false;
        }

        // advance
        pos.x++;
      }
      ofs = 0;

      break;
    case '*':
      for (size_t j = (ofs > 0) ? ofs : 1; j--;) {
        // emit goal
        if (parser->cbs->on_goal && !parser->cbs->on_goal(parser, pos)) {
          // return failure
          return false;
        }

        // emit box
        if (parser->cbs->on_box && !parser->cbs->on_box(parser, pos)) {
          // return failure
          return false;
        }

        // advance
        pos.x++;
      }
      ofs = 0;

      break;
    case '.':
      for (size_t j = (ofs > 0) ? ofs : 1; j--;) {
        // emit goal
        if (parser->cbs->on_goal && !parser->cbs->on_goal(parser, pos)) {
          // return failure
          return false;
        }

        // advance
        pos.x++;
      }
      ofs = 0;

      break;
    case '$':
      for (size_t j = (ofs > 0) ? ofs : 1; j--;) {
        // emit box
        if (parser->cbs->on_box && !parser->cbs->on_box(parser, pos)) {
          // return failure
          return false;
        }

        // advance
        pos.x++;
      }
      ofs = 0;

      break;
    default:
      // emit junk
      if (parser->cbs->on_junk && !parser->cbs->on_junk(parser, i, buf[i])) {
        // return failure
        return false;
      }

      // return failure
      return false;
    }
  }

  // return success
  return true;
}