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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sqlite3.h"
#include "smallxrm.h"
#include "heromesh.h"
#define TF_COMMA 0x0001
#define TF_EQUAL 0x0002
#define TF_ABNORMAL 0x0004
#define TF_COMPAT 0x0008
#define TF_DIR 0x0010
#define TF_DROP 0x0020
#define TF_NAME 0x0080
#define TF_KEY 0x0100
#define TF_OPEN 0x0400
#define TF_CLOSE 0x0800
#define TF_INT 0x1000
#define TF_MACRO 0x4000
#define TF_EOF 0x8000
typedef struct {
const char*txt;
Uint32 num;
} Op_Names;
#include "instruc.h"
#define Tokenf(x) (tokent&(x))
Class*classes[0x4000];
const char*messages[0x4000];
int max_animation=32;
Sint32 max_volume=10000;
static FILE*classfp;
static Uint16 tokent;
static Uint32 tokenv;
static int linenum=1;
static char tokenstr[0x2000];
static int undef_class=1;
static int undef_message=0;
#define ParseError(a,...) fatal("On line %d: " a,linenum,##__VA_ARGS__)
static const unsigned char chkind[256]={
['$']=1, ['!']=1, ['\'']=1, ['#']=1, ['@']=1, ['%']=1, ['&']=1, [':']=1,
['0'...'9']=2, ['-']=2, ['+']=2,
['A'...'Z']=3, ['a'...'z']=3, ['_']=3, ['?']=3, ['.']=3, ['*']=3, ['/']=3,
};
static void read_quoted_string(void) {
int c,i,n;
int isimg=0;
for(i=0;i<0x1FFD;) {
c=fgetc(classfp);
if(c==EOF) {
|
|
|
|
|
|
|
|
|
|
|
|
>
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sqlite3.h"
#include "smallxrm.h"
#include "heromesh.h"
#define TF_COMMA 0x0001 // has a comma modifier
#define TF_EQUAL 0x0002 // has an equal sign modifier
#define TF_ABNORMAL 0x0004 // with TF_NAME, not directly an opcode
#define TF_COMPAT 0x0008 // add 1 to opcode in compatibility mode
#define TF_DIR 0x0010 // token is a direction
#define TF_DROP 0x0020 // add 0x2000 to opcode if followed by OP_DROP
#define TF_NAME 0x0080 // token is a name or opcode
#define TF_KEY 0x0100 // token is a Hero Mesh key name
#define TF_OPEN 0x0400 // token is (
#define TF_CLOSE 0x0800 // token is )
#define TF_INT 0x1000 // token is a number
#define TF_FUNCTION 0x2000 // token is a user function
#define TF_MACRO 0x4000 // token is a macro; never returned by nxttok()
#define TF_EOF 0x8000 // end of file
typedef struct {
const char*txt;
Uint32 num;
} Op_Names;
#include "instruc.h"
#define Tokenf(x) (tokent&(x))
Class*classes[0x4000];
const char*messages[0x4000];
Uint16 functions[0x4000];
int max_animation=32;
Sint32 max_volume=10000;
#define HASH_SIZE 8888
#define LOCAL_HASH_SIZE 5555
typedef struct {
Uint16 id;
char*txt;
} Hash;
static FILE*classfp;
static Uint16 tokent;
static Uint32 tokenv;
static int linenum=1;
static char tokenstr[0x2000];
static int undef_class=1;
static int undef_message=0;
static int num_globals=0;
static int num_functions=0;
static Hash*glohash;
#define ParseError(a,...) fatal("On line %d: " a,linenum,##__VA_ARGS__)
static const unsigned char chkind[256]={
['$']=1, ['!']=1, ['\'']=1, ['#']=1, ['@']=1, ['%']=1, ['&']=1, [':']=1,
['0'...'9']=2, ['-']=2, ['+']=2,
['A'...'Z']=3, ['a'...'z']=3, ['_']=3, ['?']=3, ['.']=3, ['*']=3, ['/']=3,
};
#define MIN_VERSION 0
#define MAX_VERSION 0
static void read_quoted_string(void) {
int c,i,n;
int isimg=0;
for(i=0;i<0x1FFD;) {
c=fgetc(classfp);
if(c==EOF) {
|
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
}
if(u==0x4000) ParseError("Too many message names; cannot add #%s",tokenstr);
if(u==undef_message) ++undef_message;
messages[u]=strdup(tokenstr);
if(!messages[u]) fatal("Allocation failed\n");
return u;
}
#define ReturnToken(x,y) do{ tokent=x; tokenv=y; return; }while(0)
static void nxttok(void) {
int c,i;
int fl=0;
int n=0;
int pr=0;
tokent=tokenv=0;
*tokenstr=0;
again:
c=fgetc(classfp);
while(c==' ' || c=='\t' || c=='\r' || c=='\n') {
if(c=='\n') ++linenum;
c=fgetc(classfp);
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
<
<
|
|
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
|
}
if(u==0x4000) ParseError("Too many message names; cannot add #%s",tokenstr);
if(u==undef_message) ++undef_message;
messages[u]=strdup(tokenstr);
if(!messages[u]) fatal("Allocation failed\n");
return u;
}
static Uint16 look_hash(Hash*tbl,int size,Uint16 minv,Uint16 maxv,Uint16 newv,const char*err) {
int h,i,m;
for(h=i=0;tokenstr[i];i++) h=(13*h+tokenstr[i])%size;
m=h;
for(;;) {
if(tbl[h].id>=minv && tbl[h].id<=maxv && !strcmp(tokenstr,tbl[h].txt)) return tbl[h].id;
if(!tbl[h].id) {
if(newv>maxv) ParseError("Too many %s\n",err);
tbl[h].txt=strdup(tokenstr);
if(!tbl[h].txt) ParseError("Out of string space in hash table\n");
tbl[h].id=newv;
return 0;
}
h=(h+1)%size;
if(h==m) ParseError("Hash table full\n");
}
}
#define ReturnToken(x,y) do{ tokent=x; tokenv=y; return; }while(0)
static void nxttok(void) {
int c,i,fl,n,pr;
fl=n=pr=0;
tokent=tokenv=0;
*tokenstr=0;
again:
c=fgetc(classfp);
while(c==' ' || c=='\t' || c=='\r' || c=='\n') {
if(c=='\n') ++linenum;
c=fgetc(classfp);
|
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
|
// Just ignore sounds for now
if(fl) ParseError("Invalid use of , and = in token\n");
tokent=TF_NAME;
tokenv=0x0400;
break;
case '%':
if(fl&TF_COMMA) ParseError("Invalid use of , in token\n");
tokent=TF_NAME|fl;
tokenv=OP_LOCAL;
break;
case '@':
if(fl&TF_COMMA) ParseError("Invalid use of , in token\n");
tokent=TF_NAME|fl;
break;
case '#':
if(fl) ParseError("Invalid use of , and = in token\n");
tokent=TF_NAME;
tokenv=look_message_name()+0xC000;
break;
case ':':
tokent=TF_NAME|TF_ABNORMAL|fl;
tokenv=OP_LABEL;
break;
case '&':
break;
case '\'':
if(fl) ParseError("Invalid use of , and = in token\n");
tokent=TF_NAME|TF_KEY;
for(i=8;i<256;i++) {
if(heromesh_key_names[i] && !strcmp(heromesh_key_names[i],tokenstr)) {
tokenv=i;
return;
}
}
ParseError("Invalid Hero Mesh key name: %s\n",tokenstr);
break;
}
}
void load_classes(void) {
int i;
for(i=1;i<undef_class;i++) if(classes[i] && (classes[i]->cflags&CF_NOCLASS1)) fatal("Class $%s mentioned but not defined",classes[i]->name);
}
|
|
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
|
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
|
// Just ignore sounds for now
if(fl) ParseError("Invalid use of , and = in token\n");
tokent=TF_NAME;
tokenv=0x0400;
break;
case '%':
if(fl&TF_COMMA) ParseError("Invalid use of , in token\n");
tokent=TF_NAME|TF_ABNORMAL|fl;
tokenv=OP_LOCAL;
break;
case '@':
if(fl&TF_COMMA) ParseError("Invalid use of , in token\n");
tokent=TF_NAME|fl;
tokenv=look_hash(glohash,HASH_SIZE,0x2800,0x2FFF,num_globals+0x2800,"user global variables")?:(num_globals++)+0x2800;
break;
case '#':
if(fl) ParseError("Invalid use of , and = in token\n");
tokent=TF_NAME;
tokenv=look_message_name()+0xC000;
break;
case ':':
tokent=TF_NAME|TF_ABNORMAL|fl;
tokenv=OP_LABEL;
break;
case '&':
if(fl) ParseError("Invalid use of , and = in token\n");
tokent=TF_FUNCTION|TF_ABNORMAL;
tokenv=look_hash(glohash,HASH_SIZE,0x8000,0xBFFF,num_functions,"user functions")?:(num_functions++)+0x8000;
break;
case '\'':
if(fl) ParseError("Invalid use of , and = in token\n");
tokent=TF_NAME|TF_KEY;
for(i=8;i<256;i++) {
if(heromesh_key_names[i] && !strcmp(heromesh_key_names[i],tokenstr)) {
tokenv=i;
return;
}
}
ParseError("Invalid Hero Mesh key name: %s\n",tokenstr);
break;
}
}
void load_classes(void) {
int i;
char*nam=sqlite3_mprintf("%s.class",basefilename);
if(!nam) fatal("Allocation failed\n");
classfp=fopen(nam,"r");
sqlite3_free(nam);
if(!classfp) fatal("Cannot open class file '%s': %m\n",nam);
glohash=calloc(HASH_SIZE,sizeof(Hash));
if(!glohash) fatal("Allocation failed\n");
if(main_options['L']) {
for(;;) {
nxttok();
printf("** %5d %04X %08X \"",linenum,tokent,tokenv);
for(i=0;tokenstr[i];i++) {
if(tokenstr[i]<32 || tokenstr[i]>126) printf("<%02X>",tokenstr[i]&255);
else putchar(tokenstr[i]);
}
printf("\"\n");
if(Tokenf(TF_EOF)) goto done;
}
}
done:
fclose(classfp);
for(i=0;i<HASH_SIZE;i++) free(glohash[i].txt);
free(glohash);
for(i=1;i<undef_class;i++) if(classes[i] && (classes[i]->cflags&CF_NOCLASS1)) fatal("Class $%s mentioned but not defined",classes[i]->name);
}
|