/
compile.c
429 lines (329 loc) · 8.86 KB
/
compile.c
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
/* Includes */
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "chksum.h"
#include "compile.h"
#include "firestr.h"
#include "icon.h"
#include "main.h"
#include "mystring.h"
#include "myutil.h"
#include "p2m.h"
/* Defines */
#define MAX_STRING_LENGTH 38
#define MAX_CHEATS_PER_GAME 65535
#define MAX_CODES_PER_CHEAT 65535
#define NUM_DIGITS_CHEAT_CODE 16
#define NUM_BYTES_CHEAT_CODE 8
#define TOK_GAME_TITLE 1
#define TOK_CHEAT_DESC 2
#define TOK_CHEAT_CODE 4
#define TOK_ALL 7 // TOK_GAME_TITLE | TOK_CHEAT_DESC | TOK_CHEAT_CODE
/* Typedefs */
typedef struct {
char s[MAX_STRING_LENGTH + 1]; // Cheat Description
u16 ncodes;
u32 *codes; // Cheat Codes
} CHEAT;
typedef struct {
char s[MAX_STRING_LENGTH + 1]; // Game Title
u16 ncheats;
CHEAT *cheats;
} GAME;
typedef struct {
u16 ngames;
GAME *games;
u32 nallcheats;
u32 nallcodes;
} AST; // Abstract Syntax Tree
/* Global Variables */
int nline = 1;
int nerr = 0;
int nwarn = 0;
/* Function Declarations */
void checkStr(char *s)
{
int i;
if (setMaxStrLen(s, MAX_STRING_LENGTH)) {
trimStr(s);
fprintf(stderr, "WARNING(%i): String shortened to '%s'\n", nline, s);
nwarn++;
}
if (i = mkPrintStr(s, '?')) {
fprintf(stderr, "WARNING(%i): %i bad char(s) replaced: '%s'\n", nline, i, s);
nwarn++;
}
}
int isGameTitle(char *s)
{
u32 slen = strlen(s);
return ((slen >= 2) && (*s == '"') && (s[slen - 1] == '"'));
}
int isCheatCode(char *s)
{
u32 i = 0;
while (*s) {
if (isxdigit((u8)*s)) i++;
else if (!isspace((u8)*s)) return FALSE;
s++;
}
return (i == NUM_DIGITS_CHEAT_CODE);
}
int getToken(char *s)
{
if (isGameTitle(s)) return TOK_GAME_TITLE;
if (isCheatCode(s)) return TOK_CHEAT_CODE;
return TOK_CHEAT_DESC;
}
char *getGameTitle(char *s)
{
// if (!isGameTitle(s)) return NULL;
s[strlen(s) - 1] = NUL;
return ++s;
}
u32 *getCheatCode(char *s)
{
char digits[NUM_DIGITS_CHEAT_CODE];
static u32 code[2];
u32 i = 0;
// if (!isCheatCode(s)) return NULL;
while (*s) {
if (isxdigit((u8)*s)) digits[i++] = *s;
s++;
}
sscanf(digits, "%08X%08X", &code[0], &code[1]);
return code;
}
int addGameToTree(AST *tree, char *s)
{
tree->games = (GAME*) realloc(tree->games, ++tree->ngames * sizeof(GAME));
if (tree->games == NULL) return -1;
strcpy(tree->games[tree->ngames - 1].s, s);
tree->games[tree->ngames - 1].ncheats = 0;
tree->games[tree->ngames - 1].cheats = NULL;
return 0;
}
int addCheatToTree(AST *tree, char *s)
{
GAME *lastgame = &tree->games[tree->ngames - 1];
if (lastgame->ncheats == MAX_CHEATS_PER_GAME) return -1;
tree->nallcheats++;
lastgame->ncheats++;
lastgame->cheats = (CHEAT*) realloc(lastgame->cheats, lastgame->ncheats * sizeof(CHEAT));
if (lastgame->cheats == NULL) return -2;
strcpy(lastgame->cheats[lastgame->ncheats - 1].s, s);
lastgame->cheats[lastgame->ncheats - 1].ncodes = 0;
lastgame->cheats[lastgame->ncheats - 1].codes = NULL;
return 0;
}
int addCodeToTree(AST *tree, u32 *code)
{
GAME *lastgame = &tree->games[tree->ngames - 1];
CHEAT *lastcheat = &lastgame->cheats[lastgame->ncheats - 1];
if (lastcheat->ncodes == MAX_CODES_PER_CHEAT) return -1;
tree->nallcodes++;
lastcheat->ncodes++;
lastcheat->codes = (u32*) realloc(lastcheat->codes, lastcheat->ncodes * NUM_BYTES_CHEAT_CODE);
if (lastcheat->codes == NULL) return -2;
memcpy(&lastcheat->codes[2 * (lastcheat->ncodes - 1)], code, NUM_BYTES_CHEAT_CODE);
return 0;
}
int compare(const void *arg1, const void *arg2)
{
return strcoll(((GAME*)arg1)->s, ((GAME*)arg2)->s);
}
void sortTree(AST *tree)
{
qsort(tree->games, tree->ngames, sizeof(GAME), compare);
}
u8 *translateTree(AST *tree)
{
FILE *fp = NULL;
u16 i, j, k;
u8 *bin = NULL;
u32 binlen;
fp = tmpfile();
if (fp == NULL) return NULL;
put_u32(0, fp);
put_u32(0, fp);
for (i = 0; i < tree->ngames; i++) {
putFireStr(toFireStr(tree->games[i].s), fp);
put_u16(tree->games[i].ncheats, fp);
for (j = 0; j < tree->games[i].ncheats; j++) {
putFireStr(toFireStr(tree->games[i].cheats[j].s), fp);
put_u16(tree->games[i].cheats[j].ncodes, fp);
for (k = 0; k < tree->games[i].cheats[j].ncodes; k++) {
put_u32(tree->games[i].cheats[j].codes[2 * k], fp);
put_u32(tree->games[i].cheats[j].codes[(2 * k) + 1], fp);
}
}
}
put_u32(BIN_MKR_EOF, fp);
binlen = ftell(fp);
bin = (u8*) readFromFile(fp, 0, binlen);
if (bin == NULL) return NULL;
fclose(fp); // Delete temporary file
*(u32*) &bin[0] = binlen - 8;
*(u32*) &bin[4] = get_crc32(bin + 8, binlen - 8);
return bin;
}
void freeTree(AST *tree)
{
u16 i, k;
i = tree->ngames;
while (i--) {
k = tree->games[i].ncheats;
while (k--) free(tree->games[i].cheats[k].codes); // Free all codes per cheat
free(tree->games[i].cheats); // Free all cheats per game
}
free(tree->games); // Free all games
free(tree); // Deallocate tree
}
AST *frontend(char *txt, u32 txtlen)
{
char *s = NULL;
u32 *code = NULL;
u32 i = 0;
u32 slen;
int tokcur, tokexp = TOK_GAME_TITLE;
AST *tree = (AST*) malloc(sizeof(AST));
memset(tree, 0, sizeof(AST));
// Text MUST contain a new line char at the end
if (txt[txtlen - 1] != '\n') {
txt = (char*) realloc(txt, ++txtlen);
txt[txtlen - 1] = '\n';
}
printf("*** Compiler front end ***\n");
printf("Parsing...\n");
/*** Scanner ***/
while (i < txtlen) {
slen = indexOfChr(&txt[i], '\n');
if (!_isEmptyStr(&txt[i], slen)) {
s = &txt[i];
s[slen] = NUL;
/*** Screener ***/
remCmtFromStr(s);
trimStr(s);
if (strlen(s) > 0) {
/*** Parser ***/
tokcur = getToken(s);
if (!(tokexp & tokcur)) {
fprintf(stderr, "ERROR(%i): '%s' : ", nline, s);
if (tokexp & TOK_CHEAT_DESC) {
fprintf(stderr, "Cheat description expected\n");
tokexp = TOK_ALL;
} else /* if (tokexp & TOK_GAME_TITLE) */ {
fprintf(stderr, "Game title expected\n");
tokexp = TOK_GAME_TITLE | TOK_CHEAT_DESC;
}
nerr++;
} else switch (tokcur) {
case TOK_GAME_TITLE:
s = getGameTitle(s);
if (*s == NUL) {
fprintf(stderr, "ERROR(%i): Empty game title\n", nline);
nerr++;
} else {
checkStr(s);
if (!nerr && addGameToTree(tree, s) < 0) {
fprintf(stderr, "ERROR(%i): '%s' : Cannot add more games\n", nline, s);
nerr++;
}
}
tokexp = TOK_GAME_TITLE | TOK_CHEAT_DESC;
break;
case TOK_CHEAT_DESC:
checkStr(s);
if (!nerr && addCheatToTree(tree, s) < 0) {
fprintf(stderr, "ERROR(%i): '%s' : Cannot add more cheats\n", nline, s);
nerr++;
}
tokexp = TOK_ALL;
break;
case TOK_CHEAT_CODE:
code = getCheatCode(s);
if (!nerr && addCodeToTree(tree, code) < 0) {
fprintf(stderr, "ERROR(%i): %08X %08X : Cannot add more codes\n", nline, code[0], code[1]);
nerr++;
}
tokexp = TOK_ALL;
break;
}
}
}
nline++;
i += slen + 1;
}
printf("Parsing done: %i error(s), %i warning(s)\n", nerr, nwarn);
if (nerr) {
fprintf(stderr, "Error(s) occurred during parsing.\n");
freeTree(tree);
return NULL;
} else if (!tree->ngames) {
fprintf(stderr, "Nothing parsed.\n");
freeTree(tree);
return NULL;
}
printf("Stats: %u game(s), %u cheat(s), %u code(s)\n", tree->ngames, tree->nallcheats, tree->nallcodes);
return tree;
}
int createP2mFile(FILE *fp, u8 *bin, u32 binlen, u32 *p2mlen)
{
P2M_DIR *dir = newP2mDir(P2M_GAMENAME, P2M_ROOTNAME);
if (dir == NULL) return -1;
if (addP2mDirFile(dir, "icon.sys", icon_sys, sizeof(icon_sys)) < 0) return -2;
if (addP2mDirFile(dir, "fcd.ico", fcd_ico, sizeof(fcd_ico)) < 0) return -2;
if (addP2mDirFile(dir, P2M_FILENAME, bin, binlen) < 0) return -2;
fwrite(P2M_FILE_ID, sizeof(char), strlen(P2M_FILE_ID), fp);
scramble(dir);
writeP2mDir(dir, fp);
put_u32(0, fp); // No further dirs
*p2mlen = dir->dirhdr->totalsize + 12;
freeP2mDir(dir);
return 0;
}
int backend(AST *tree, char *filename)
{
FILE *fp = NULL;
u8 *bin = NULL;
u32 binlen;
u32 p2mlen;
printf("*** Compiler back end ***\n");
if (mode & MODE_SORT) {
printf("Sorting by game title... ");
sortTree(tree);
printf("Done.\n");
}
printf("Translating to BIN... ");
if ((bin = translateTree(tree)) == NULL) {
fprintf(stderr, "Failed.\n");
return -1;
}
binlen = *(u32*) bin + 8;
printf("Done (%s).\n", sizeToStr(binlen));
printf("%s file '%s'... ", mode & MODE_BIN ? "Writing to" : "Creating P2M", filename);
if ((fp = fopen(filename, "wb")) == NULL) {
fprintf(stderr, "Failed.\n");
return -2;
}
if ((mode & MODE_BIN ? writeToFile(fp, bin, 0, binlen) : createP2mFile(fp, bin, binlen, &p2mlen)) < 0) {
fprintf(stderr, "Failed.\n");
return -3;
}
free(bin);
fclose(fp);
if (mode & MODE_BIN) printf("Done.\n");
else printf("Done (%s).\n", sizeToStr(p2mlen));
return 0;
}
int compile(char *txt, u32 txtlen, char *filename)
{
int ret = 0;
AST *tree = frontend(txt, txtlen);
if (tree == NULL) return -1;
if (backend(tree, filename) < 0) ret = -2;
freeTree(tree);
return ret;
}