-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpuzzle.y
628 lines (506 loc) · 18 KB
/
puzzle.y
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
%{
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include "puzzleData.h"
#include "lex.yy.c"
PuzzleData pd;
int yyerror(const char *str) {
fprintf(stderr, "line: %d | %s\n", yylineno, str);
exit(1);
}
int aliasLegendId(char * name) {
for (int i = 0; i < pd.aliasLegendCount; i++) {
if (strcasecmp(pd.aliasLegend[i].key, name) == 0) {
return i;
}
}
fprintf(stderr, "err: '%s' which does not exist in alias legend\n", name);
for (int i = 0; i < pd.aliasLegendCount; i++) {
fprintf(stderr, "wasn't key: '%s'\n", pd.aliasLegend[i].key);
}
return -1;
}
int glyphLegendId(char glyph) {
for (int i = 0; i < pd.glyphLegendCount; i++) {
if (toupper(pd.glyphLegend[i].key) == toupper(glyph)) {
return i;
}
}
fprintf(stderr, "err: '%c' which does not exist in glyph legend\n", glyph);
return -1;
}
void addObjectsToGlyphLegend(char * name) {
int legId = aliasLegendId(name);
int count = pd.aliasLegend[legId].objectCount;
for (int i = 0; i < count; i++) {
pd.glyphLegend[pd.glyphLegendCount].objects[pd.glyphLegend[pd.glyphLegendCount].objectCount] = pd.aliasLegend[legId].objects[i];
incGlyphLegendObject(pd.glyphLegendCount);
}
}
int objectId(char * name) {
for (int i = 0; i < pd.objectCount; i++) {
if (strcasecmp(pd.objects[i].name, name) == 0) {
return i;
}
}
fprintf(stderr, "objectId not found for '%s'\n", name);
return -1;
}
int spriteIndex = 0;
int legendIsAlias = 0;
int rowWidth = 0;
%}
%union {
char * identifier;
char tile;
float decimal;
int enumValue;
int number;
};
%define parse.error verbose
%token TITLE AUTHOR HOMEPAGE COLOR_PALETTE AGAIN_INTERVAL BACKGROUND_COLOR
%token FLICKSCREEN KEY_REPEAT_INTERVAL NOACTION NOREPEAT_ACTION NOUNDO NORESTART
%token REALTIME_INTERVAL REQUIRE_PLAYER_MOVEMENT
%token SCANLINE TEXT_COLOR THROTTLE_MOVEMENT ZOOMSCREEN
%token DEBUG VERBOSE_LOGGING RUN_RULES_ON_LEVEL_START
%token END_OF_FILE 0 "end of file"
%token MODE_HEADER EQUALS END_LAYER END_OF_RULE MESSAGE LEGEND_AND LEGEND_OR END_OF_OBJECT_LINE
%token <identifier> ID OBJID COLOR LEGEND_ID LEGEND_VALUE END_LEGEND_LINE LAYER_NAME RULE_POSTFIX
%token <decimal> DECIMAL
%token <number> DIGIT
%token DIMENSION_SEP
%token MOVE_RIGHT MOVE_UP MOVE_LEFT MOVE_DOWN
%token OPEN_SQUARE CLOSE_SQUARE VERTICAL_PIPE ARROW
%token LEVEL_EOL LEVEL_EMPTY_LINE
%token <tile> GLYPH LEGEND_GLYPH
%token <enumValue> LOGIC_WORD LOGIC_ON DIRECTION EXECUTION_TIME
%%
puzzlescript: preamble
modechange
object_definitions
modechange
legend_lines
modechange
sounds
modechange
collision_lines
modechange
rules
modechange
winconditions
modechange
levels;
preamble: preamble preamble_option | %empty;
preamble_option: title
| author
| homepage
| color_palette
| again_interval
| background_color
| flickscreen
| key_repeat_interval
| realtime_interval
| noaction
| norepeat_action
| noundo
| norestart
| scanline
| text_color
| throttle_movement
| zoomscreen
| debug
| verbose_logging
| run_rules_on_level_start
| require_player_movement
;
title: TITLE ID { free(pd.title); pd.title = $2; }
author: AUTHOR ID { free(pd.author); pd.author = $2; }
homepage: HOMEPAGE ID { free(pd.homepage); pd.homepage = $2; }
require_player_movement: REQUIRE_PLAYER_MOVEMENT { pd.requirePlayerMovement = 1; }
;
color_palette: COLOR_PALETTE ID { pd.colorPalette = colorPaletteId($2); free($2); }
;
again_interval: AGAIN_INTERVAL DECIMAL { pd.setAgainInterval = 1; pd.againInterval = 0.1f; }
;
background_color: BACKGROUND_COLOR ID { free(pd.backgroundColor); pd.backgroundColor = $2; }
;
key_repeat_interval: KEY_REPEAT_INTERVAL DECIMAL { pd.keyRepeatInterval = 0.1f; }
;
realtime_interval: REALTIME_INTERVAL DECIMAL { pd.setRealtimeInterval = 1; pd.realTimeInterval = $2; }
;
noaction: NOACTION { pd.noAction = 1; }
;
norepeat_action: NOREPEAT_ACTION { pd.noRepeatAction = 1; }
;
noundo: NOUNDO { pd.noUndo = 1; }
;
norestart: NORESTART { pd.noRestart = 1; }
;
scanline: SCANLINE { pd.scanLine = 1; }
;
text_color: TEXT_COLOR ID { free(pd.textColor); pd.textColor = $2; }
;
throttle_movement: THROTTLE_MOVEMENT ID { free($2); pd.throttleMovement = 1; }
;
zoomscreen: ZOOMSCREEN DIGIT DIGIT {
pd.doesZoomScreen = 1;
pd.zoomScreenX = $2;
pd.zoomScreenY = $3;
}
;
flickscreen: FLICKSCREEN DIGIT DIGIT {
pd.doesFlickScreen = 1;
pd.flickScreenX = $2;
pd.flickScreenY = $2;
};
debug: DEBUG { pd.debug = 1; }
;
verbose_logging: VERBOSE_LOGGING { pd.verboseLogging = 1; }
;
run_rules_on_level_start: RUN_RULES_ON_LEVEL_START { pd.runRulesOnLevelStart = 1; }
modechange: MODE_HEADER
some_object_eol: some_object_eol END_OF_OBJECT_LINE | END_OF_OBJECT_LINE;
any_object_eol: some_object_eol | %empty;
object_definitions: object_definitions object_definition | object_definition;
object_definition: any_object_eol object_name some_object_eol colors some_object_eol sprite some_object_eol {
incObject();
}
| any_object_eol object_name some_object_eol colors some_object_eol {
for (int j = 0; j < 25; j++) {
pd.objects[pd.objectCount].sprite[j] = '0';
}
incObject();
}
object_name: OBJID OBJID {
pd.objects[pd.objectCount].name = strdup($1);
pd.aliasLegend[pd.aliasLegendCount].key = $1;
pd.aliasLegend[pd.aliasLegendCount].objects[0] = pd.objectCount;
incAliasLegendObject(pd.aliasLegendCount);
incAliasLegend();
// single char key
// They only reference AliasLegend names that have the actual object ID
pd.glyphLegend[pd.glyphLegendCount].key = $2[0];
free($2);
pd.glyphLegend[pd.glyphLegendCount].objects[0] = pd.objectCount;
incGlyphLegendObject(pd.glyphLegendCount);
incGlyphLegend();
if (strlen($1) == 1) {
pd.glyphLegend[pd.glyphLegendCount].key = tolower($1[0]);
pd.glyphLegend[pd.glyphLegendCount].objects[0] = pd.objectCount;
incGlyphLegendObject(pd.glyphLegendCount);
incGlyphLegend();
}
}
| OBJID {
pd.objects[pd.objectCount].name = strdup($1);
pd.aliasLegend[pd.aliasLegendCount].key = $1;
pd.aliasLegend[pd.aliasLegendCount].objects[0] = pd.objectCount;
incAliasLegendObject(pd.aliasLegendCount);
incAliasLegend();
if (strlen($1) == 1) {
pd.glyphLegend[pd.glyphLegendCount].key = tolower($1[0]);
pd.glyphLegend[pd.glyphLegendCount].objects[0] = pd.objectCount;
incGlyphLegendObject(pd.glyphLegendCount);
incGlyphLegend();
}
}
colors: colors color | color;
color: COLOR {
pd.objects[pd.objectCount].colors[pd.objects[pd.objectCount].colorCount] = $1;
pd.objects[pd.objectCount].colorCount++;
}
sprite: sprite_row some_object_eol sprite_row some_object_eol sprite_row some_object_eol sprite_row some_object_eol sprite_row {
spriteIndex = 0;
}
sprite_row: sprite_tile sprite_tile sprite_tile sprite_tile sprite_tile
sprite_tile: GLYPH {
pd.objects[pd.objectCount].sprite[spriteIndex] = $1;
spriteIndex++;
}
legend_lines: some_legend_lines | %empty;
some_legend_lines: some_legend_lines legend_line | legend_line;
legend_line: legend_id EQUALS legend_values end_legend_line {
incAliasLegend();
pd.aliasLegend[pd.aliasLegendCount].objectRelation = LEGEND_RELATION_UNKNOWN;
legendIsAlias = 0;
}
| legend_glyph EQUALS legend_values end_legend_line {
incGlyphLegend();
pd.glyphLegend[pd.glyphLegendCount].objectRelation = LEGEND_RELATION_UNKNOWN;
legendIsAlias = 0;
}
legend_id: LEGEND_ID {
legendIsAlias = 1;
pd.aliasLegend[pd.aliasLegendCount].key = $1;
}
legend_glyph: LEGEND_GLYPH {
legendIsAlias = 0;
pd.glyphLegend[pd.glyphLegendCount].key = $1;
}
end_legend_line: END_LEGEND_LINE end_legend_line | END_LEGEND_LINE;
legend_values: legend_value legend_joiner legend_values
| legend_value
legend_value: LEGEND_VALUE {
if (legendIsAlias == 1) {
addObjectsToAliasLegend($1);
} else {
addObjectsToGlyphLegend($1);
}
free($1);
}
legend_joiner: LEGEND_AND {
if (legendIsAlias == 1) {
pd.aliasLegend[pd.aliasLegendCount].objectRelation = LEGEND_RELATION_AND;
} else {
pd.glyphLegend[pd.glyphLegendCount].objectRelation = LEGEND_RELATION_AND;
}
}
| LEGEND_OR {
if (legendIsAlias == 1) {
pd.aliasLegend[pd.aliasLegendCount].objectRelation = LEGEND_RELATION_OR;
} else {
pd.glyphLegend[pd.glyphLegendCount].objectRelation = LEGEND_RELATION_OR;
}
}
sounds: %empty; // Nothing for now
collision_lines: collision_line collision_lines
| collision_line
collision_line: layer_objects END_LAYER { incLayer(); }
layer_objects: layer_object layer_objects
| layer_object
layer_object: LAYER_NAME {
addObjectsToLayer($1);
free($1);
}
rules: rules rule_line
| rule_line
;
rule_line: any_eor rule some_eor {
pd.rules[pd.ruleCount].lineNo = yylineno;
incRule();
}
some_eor: some_eor END_OF_RULE | END_OF_RULE;
any_eor: some_eor | %empty;
rule: rule_prefix match_states arrow result_states rule_postfixs
| rule_prefix match_states arrow result_states
| match_states arrow result_states rule_postfixs {
pd.rules[pd.ruleCount].executionTime = NORMAL;
pd.rules[pd.ruleCount].directionConstraint = NONE;
}
| match_states arrow result_states {
pd.rules[pd.ruleCount].executionTime = NORMAL;
pd.rules[pd.ruleCount].directionConstraint = NONE;
}
| match_states arrow rule_postfixs
{
pd.rules[pd.ruleCount].executionTime = NORMAL;
pd.rules[pd.ruleCount].directionConstraint = NONE;
}
| rule_prefix match_states arrow rule_postfixs
arrow: ARROW
rule_prefix: EXECUTION_TIME {
pd.rules[pd.ruleCount].executionTime = $1;
pd.rules[pd.ruleCount].directionConstraint = NONE;
}
| DIRECTION {
pd.rules[pd.ruleCount].executionTime = NORMAL;
pd.rules[pd.ruleCount].directionConstraint = $1;
}
| EXECUTION_TIME DIRECTION {
pd.rules[pd.ruleCount].executionTime = $1;
pd.rules[pd.ruleCount].directionConstraint = $2;
}
rule_postfixs: rule_postfixs rule_postfix | rule_postfix
rule_postfix: OBJID {
if (strcasecmp("sfx0", $1) == 0) {
pd.rules[pd.ruleCount].commands[pd.rules[pd.ruleCount].commandCount] = SFX0;
} else if (strcasecmp("sfx1", $1) == 0) {
pd.rules[pd.ruleCount].commands[pd.rules[pd.ruleCount].commandCount] = SFX1;
} else if (strcasecmp("sfx2", $1) == 0) {
pd.rules[pd.ruleCount].commands[pd.rules[pd.ruleCount].commandCount] = SFX2;
} else if (strcasecmp("sfx3", $1) == 0) {
pd.rules[pd.ruleCount].commands[pd.rules[pd.ruleCount].commandCount] = SFX3;
} else if (strcasecmp("sfx4", $1) == 0) {
pd.rules[pd.ruleCount].commands[pd.rules[pd.ruleCount].commandCount] = SFX4;
} else if (strcasecmp("sfx5", $1) == 0) {
pd.rules[pd.ruleCount].commands[pd.rules[pd.ruleCount].commandCount] = SFX5;
} else if (strcasecmp("sfx6", $1) == 0) {
pd.rules[pd.ruleCount].commands[pd.rules[pd.ruleCount].commandCount] = SFX6;
} else if (strcasecmp("sfx7", $1) == 0) {
pd.rules[pd.ruleCount].commands[pd.rules[pd.ruleCount].commandCount] = SFX7;
} else if (strcasecmp("sfx8", $1) == 0) {
pd.rules[pd.ruleCount].commands[pd.rules[pd.ruleCount].commandCount] = SFX8;
} else if (strcasecmp("sfx9", $1) == 0) {
pd.rules[pd.ruleCount].commands[pd.rules[pd.ruleCount].commandCount] = SFX9;
} else if (strcasecmp("again", $1) == 0) {
pd.rules[pd.ruleCount].commands[pd.rules[pd.ruleCount].commandCount] = AGAIN;
} else if (strcasecmp("cancel", $1) == 0) {
pd.rules[pd.ruleCount].commands[pd.rules[pd.ruleCount].commandCount] = CANCEL;
} else if (strcasecmp("checkpoint", $1) == 0) {
pd.rules[pd.ruleCount].commands[pd.rules[pd.ruleCount].commandCount] = CHECKPOINT;
} else if (strcasecmp("restart", $1) == 0) {
pd.rules[pd.ruleCount].commands[pd.rules[pd.ruleCount].commandCount] = CMD_RESTART;
} else if (strcasecmp("win", $1) == 0) {
pd.rules[pd.ruleCount].commands[pd.rules[pd.ruleCount].commandCount] = WIN;
}
pd.rules[pd.ruleCount].commandCount++;
free($1);
}
| MESSAGE ID {
fprintf(stderr, "this should show a message '%s'\n", $2);
free($2);
}
match_states: match_states match_state | match_state;
match_state: OPEN_SQUARE match_state_internals CLOSE_SQUARE {
incRuleMatchState(&pd.rules[pd.ruleCount]);
}
match_state_internals: match_state_internals VERTICAL_PIPE match_state_part
| match_state_part
match_state_part: match_on_square {
Rule * r = &pd.rules[pd.ruleCount];
Pattern * rs = &r->matchPatterns[r->matchPatternCount];
incRulePart(rs);
if (rs->partCount > 1) {
r->hasMultipleParts = 1;
}
}
| %empty {
Rule * r = &pd.rules[pd.ruleCount];
Pattern * rs = &r->matchPatterns[r->matchPatternCount];
RulePart * rsp = &rs->parts[rs->partCount];
rsp->identity[rsp->identityCount].direction = UNSPECIFIED;
rsp->identity[rsp->identityCount].legendId = aliasLegendId("_EMPTY_");
incRuleIdent(rsp);
incRulePart(rs);
}
match_on_square: match_on_square match_object_part | match_object_part;
match_object_part: OBJID {
Rule * r = &pd.rules[pd.ruleCount];
Pattern * rs = &r->matchPatterns[r->matchPatternCount];
RulePart * rsp = &rs->parts[rs->partCount];
RuleIdentity * rid = &rsp->identity[rsp->identityCount];
if (strcasecmp("...", $1) == 0 ) {
r->hasSpread = 1;
rsp->isSpread = 1;
}
rid->direction = UNSPECIFIED;
rid->legendId = aliasLegendId($1);
free($1);
incRuleIdent(rsp);
}
| DIRECTION OBJID {
Rule * r = &pd.rules[pd.ruleCount];
Pattern * rs = &r->matchPatterns[r->matchPatternCount];
RulePart * rsp = &rs->parts[rs->partCount];
RuleIdentity * rid = &rsp->identity[rsp->identityCount];
if ($1 == PARALLEL ||
$1 == PERPENDICULAR) {
r->hasCompoundDirection = 1;
r->hasRelativeDirection = 1;
} else if ($1 == MOVING ||
$1 == REL_UP ||
$1 == REL_DOWN ||
$1 == REL_LEFT ||
$1 == REL_RIGHT ||
$1 == HORIZONTAL ||
$1 == VERTICAL
) {
r->hasRelativeDirection = 1;
}
rid->direction = $1;
rid->legendId = aliasLegendId($2);
free($2);
incRuleIdent(rsp);
}
result_states: result_states result_state | result_state;
result_state: OPEN_SQUARE result_state_internals CLOSE_SQUARE { incRuleResultState(&pd.rules[pd.ruleCount]); }
result_state_internals: result_state_internals VERTICAL_PIPE result_state_part
| result_state_part
result_state_part: result_on_square {
Rule * r = &pd.rules[pd.ruleCount];
Pattern * rs = &r->resultPatterns[r->resultPatternCount];
incRulePart(rs);
}
| %empty {
Rule * r = &pd.rules[pd.ruleCount];
Pattern * rs = &r->resultPatterns[r->resultPatternCount];
RulePart * rsp = &rs->parts[rs->partCount];
rsp->identity[rsp->identityCount].direction = UNSPECIFIED;
rsp->identity[rsp->identityCount].legendId = aliasLegendId("_EMPTY_");
incRuleIdent(rsp);
incRulePart(rs);
}
result_on_square: result_on_square result_object_part | result_object_part;
result_object_part: OBJID {
Rule * r = &pd.rules[pd.ruleCount];
Pattern * rs = &r->resultPatterns[r->resultPatternCount];
RulePart * rsp = &rs->parts[rs->partCount];
RuleIdentity * rid = &rsp->identity[rsp->identityCount];
rid->direction = UNSPECIFIED;
rid->legendId = aliasLegendId($1);
free($1);
incRuleIdent(rsp);
}
| DIRECTION OBJID {
Rule * r = &pd.rules[pd.ruleCount];
Pattern * rs = &r->resultPatterns[r->resultPatternCount];
RulePart * rsp = &rs->parts[rs->partCount];
RuleIdentity * rid = &rsp->identity[rsp->identityCount];
if ($1 == PARALLEL ||
$1 == PERPENDICULAR) {
r->hasCompoundDirection = 1;
} else if ($1 == MOVING ||
$1 == REL_UP ||
$1 == REL_DOWN ||
$1 == REL_LEFT ||
$1 == REL_RIGHT) {
r->hasRelativeDirection = 1;
}
rid->direction = $1;
rid->legendId = aliasLegendId($2);
free($2);
incRuleIdent(rsp);
}
winconditions: winconditions wincondition | %empty;
wincondition: LOGIC_WORD OBJID {
pd.winConditions[pd.winConditionCount].hasOnQualifier = 0;
pd.winConditions[pd.winConditionCount].winQualifier = $1;
pd.winConditions[pd.winConditionCount].winIdentifier = aliasLegendId($2);
free($2);
incWinCondition();
}
| LOGIC_WORD OBJID LOGIC_ON OBJID {
pd.winConditions[pd.winConditionCount].hasOnQualifier = 1;
pd.winConditions[pd.winConditionCount].winQualifier = $1;
pd.winConditions[pd.winConditionCount].winIdentifier = aliasLegendId($2);
free($2);
pd.winConditions[pd.winConditionCount].onIndentifier = aliasLegendId($4);
free($4);
incWinCondition();
}
some_level_eol: some_level_eol LEVEL_EOL | LEVEL_EOL;
any_level_eol: some_level_eol | %empty;
levels: any_level_eol the_levels;
the_levels: the_levels level | level;
level: a_level some_level_eol { incLevel(); }
| a_level END_OF_FILE { incLevel(); }
a_level: message_level
| rows
message_level: MESSAGE ID {
pd.levels[pd.levelCount].levelType = MESSAGE_TEXT;
pd.levels[pd.levelCount].message = $2;
}
rows: rows row | row { pd.levels[pd.levelCount].levelType = SQUARES; }
row: tiles LEVEL_EOL {
pd.levels[pd.levelCount].height++;
pd.levels[pd.levelCount].width = rowWidth;
rowWidth = 0;
}
tiles: tile tiles | tile
tile: GLYPH {
rowWidth++;
addTile(pd.levelCount, $1);
}
%%