-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.c
2992 lines (2921 loc) · 100 KB
/
class.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
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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#if 0
gcc ${CFLAGS:--s -O2} -c -Wno-unused-result class.c `sdl-config --cflags`
exit
#endif
/*
This program is part of Free Hero Mesh and is public domain.
*/
#define HEROMESH_CLASS
#include "SDL.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sqlite3.h"
#include "smallxrm.h"
#include "heromesh.h"
#include "names.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
#define TF_EOF 0x8000 // end of file
typedef struct {
const char*txt;
Uint32 num;
} Op_Names;
#include "instruc.h"
#define Tokenf(x) (tokent&(x))
Value initglobals[0x800];
Class*classes[0x4000];
const char*messages[0x4000];
Uint16 functions[0x4000];
int max_animation=32;
Sint32 max_volume=10000;
Uint8 back_color=1;
Uint8 inv_back_color=9;
char**stringpool;
AnimationSlot anim_slot[8];
Uint8 keymask[256/8];
Uint16 array_size;
Uint16*orders;
Uint8 norders;
Uint16 control_class;
Uint8 has_xy_input;
Uint8 has_mbcs=0;
char*ll_head;
DisplayColumn*ll_disp;
Uint8 ll_ndisp;
DataColumn*ll_data;
Uint8 ll_ndata;
Uint8 ll_naggregate;
Uint16*ll_code;
#ifdef CONFIG_GLOBAL_HASH_SIZE
#define HASH_SIZE CONFIG_GLOBAL_HASH_SIZE
#else
#define HASH_SIZE 8888
#endif
#ifdef CONFIG_LOCAL_HASH_SIZE
#define LOCAL_HASH_SIZE CONFIG_LOCAL_HASH_SIZE
#else
#define LOCAL_HASH_SIZE 5555
#endif
typedef struct {
Uint16 id;
char*txt;
} Hash;
/*
Global hash:
1000-10FF = User flags
2800-2FFF = Variables
8000-BFFF = Functions
C000-FFFF = Macros
Local hash:
2000-27FF = Variables
8000-FFFF = Labels
*/
typedef struct InputStack {
FILE*classfp;
int linenum;
struct InputStack*next;
} InputStack;
typedef struct TokenList {
Uint16 t;
Uint32 v;
char*str;
Uint16 ref;
struct TokenList*next;
} TokenList;
typedef struct MacroStack {
TokenList*tok;
Sint16 n;
TokenList**args;
struct MacroStack*next;
} MacroStack;
typedef struct LabelStack {
Uint16 id;
Uint16 addr;
struct LabelStack*next;
} LabelStack;
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 int num_strings=0;
static char pushback=0;
static Hash*glohash;
static InputStack*inpstack;
static MacroStack*macstack;
static TokenList**macros;
static LabelStack*labelstack;
static Uint16*labelptr;
static Uint8 dense[8];
#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, ['^']=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
#define MAX_MACRO 0x3FC0
#define MAC_ADD 0xFFC0
#define MAC_SUB 0xFFC1
#define MAC_MUL 0xFFC2
#define MAC_DIV 0xFFC3
#define MAC_MOD 0xFFC4
#define MAC_BAND 0xFFC5
#define MAC_BOR 0xFFC6
#define MAC_BXOR 0xFFC7
#define MAC_BNOT 0xFFC8
#define MAC_CAT 0xFFC9
#define MAC_BIT 0xFFCA
#define MAC_MAKE 0xFFCB
#define MAC_VERSION 0xFFE0
#define MAC_DEFINE 0xFFE1
#define MAC_INCLUDE 0xFFE2
#define MAC_CALL 0xFFE3
#define MAC_APPEND 0xFFE4
#define MAC_EDIT 0xFFE5
#define MAC_UNDEFINED 0xFFFF
static TokenList*add_macro(void) {
TokenList*o=malloc(sizeof(TokenList));
if(!o) fatal("Allocation failed\n");
o->t=tokent;
o->v=tokenv;
o->str=0;
o->ref=0;
o->next=0;
if(*tokenstr) {
o->str=strdup(tokenstr);
if(!o->str) fatal("Allocation failed\n");
}
return o;
}
static void ref_macro(TokenList*o) {
while(o) {
if(++o->ref==65000) ParseError("Reference count of macro token list overflowed\n");
o=o->next;
}
}
static void free_macro(TokenList*o) {
TokenList*p;
while(o && !o->ref--) {
free(o->str);
o=(p=o)->next;
free(p);
}
}
static inline TokenList*step_macro(TokenList*o) {
TokenList*p=o->next;
if(!o->ref--) {
free(o->str);
free(o);
}
return p;
}
#ifndef CONFIG_OMIT_MBCS
static inline void valid_part_of_code(Uint8 b) {
if(b<0x21 || b>0xFD || b==0x7F) ParseError("Improper TRON code\n");
}
static Uint32 read_euc_tron_1(const Uint8*s,int*p) {
Uint32 r=0;
Uint8 h=0;
int i=*p;
int c=s[i++];
if(c==0x90 || c==0x91) h=c,c=s[i++];
while(c==0x80) r+=0x1000000,c=s[i++];
if(c<0xA1) ParseError("Improper TRON code\n");
if(h) {
if(h==0x90) c-=0x40;
r|=c<<16;
} else {
r|=(c-0x80)<<16;
}
c=s[i++];
if(c<0xA0) ParseError("Improper TRON code\n");
r|=(c<<8)|s[i++];
*p=i;
return r;
}
static void convert_euctron_to_tron8(void) {
static const Uint8 jisx[0x80]={
0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0xA1, 0xA3,0xA4,0xA5,0xA8, 0xAC,0xAD,0xAE,0xAF,
0xEE,0xEF,0xF0,0xF1, 0xF2,0xF3,0xF4,0xF5, 0xF6,0xF7,0xF8,0xF9, 0xFA,0xFB,0xFC,0xFD,
0xFE,0x87,0x00,0x88, 0x89,0x8A,0x00,0x00, 0x8B,0x00,0x00,0x00, 0x8C,0x8D,0x8E,0x8F,
0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x90,0x91,
0x92,0x93,0x94,0x95, 0x96,0x97,0x98,0x99, 0x9A,0x9B,0x9C,0x9D, 0x9E,0x9F,0xA0,0x00,
};
Uint8 s[0x2000];
int i,o,p,x,y;
Uint32 c;
strcpy(s,tokenstr);
i=o=p=0;
if(*s<=0x20) tokenstr[0]=0xFE,tokenstr[1]=p=0x21,o=2;
while(i<0x1FFF && s[i] && o<0x1FFD) {
if(s[i]==27) {
i++;
c=0;
for(;;) switch(x=s[i++]) {
case '0' ... '9': c=16*c+x-'0'; break;
case 'A' ... 'F': c=16*c+x+10-'A'; break;
case 'a' ... 'f': c=16*c+x+10-'a'; break;
case ';': goto code;
default: ParseError("Invalid \\T escape\n");
}
} else if(s[i]<=32) {
tokenstr[o++]=c=s[i++];
if(c==16 || c==31) {
if(!s[i]) ParseError("Invalid string\n");
tokenstr[o++]=s[i++];
} else if(c==14 || c==30) {
x=strcspn(s+i,"\\");
if(!s[i+x]) ParseError("Invalid string\n");
if(o+x+1>=0x1FFC) ParseError("Converted string too long\n");
memcpy(tokenstr+o,s+i,x+1);
o+=x+1;
}
} else if(s[i]=='%') {
tokenstr[o++]=0x7F;
i++; x=s[i++];
if(!x) break;
tokenstr[o++]=x;
p=0; // ensure not being shifted to the wrong code set after a substitution
} else if(s[i]<127) {
// Note that the non-official plane 0x70 is used, rather than conversion to JIS or Unicode.
// This might be changed in future to convert to JIS.
c=0x702200+s[i++];
goto code;
} else if(s[i]>=0x88 && s[i]<=0x8D) {
x=s[i++];
c=read_euc_tron_1(s,&i);
y=(c>>8)&0xFF;
switch(x) {
case 0x88: c-=0x8080; break;
case 0x89: c-=0x0080; break;
case 0x8A: c-=0x8000; break;
case 0x8B: break;
case 0x8C: c=(y<0xC0?c-0x2080:(c&~0xFFFF)+((c>>8)&0xFF)+(y<<8)-0x8040); break;
case 0x8D: c=(y<0xC0?c-0x2000:y<0xE0?(c&~0xFFFF)+((c>>8)&0xFF)+(y<<8)-0x0040:c-0x6060); break;
}
goto code;
} else if(s[i]==0x8E) {
// Half width katakana; see comments in the (s[i]<127) block
c=0x702300+s[i++];
goto code;
} else if(s[i]==0x8F) {
i++;
if(!s[i] || !s[i+1]) break;
x=s[i++];
if(!x) ParseError("Invalid TRON code\n");
c=s[i++]^((jisx[x&0x7F]?:x)<<8)^0x210080;
goto code;
} else {
c=0x210000+(s[i++]<<8);
if(s[i]<0x80) ParseError("Invalid EUC-JP character\n");
c+=s[i++]-0x8080;
code:
valid_part_of_code(c&0xFF);
valid_part_of_code((c>>8)&0xFF);
valid_part_of_code((c>>16)&0xFF);
if((c>>16)!=p) {
if(o+(c>>24)>=0x1FFB) ParseError("Converted string too long\n");
memset(tokenstr+o,0xFE,x=(c>>24)+1);
o+=x;
tokenstr[o++]=p=c>>16;
}
tokenstr[o++]=c>>8; tokenstr[o++]=c;
}
}
tokenstr[o]=0;
}
#endif // CONFIG_OMIT_MBCS
static void read_quoted_string(void) {
char y=0;
int c,i,n;
int isimg=0;
for(i=0;i<0x1FFD;) {
c=fgetc(classfp);
if(c==EOF) {
ParseError("Unterminated string literal\n");
} else if(c=='\\') {
if(isimg) {
isimg=0;
tokenstr[i++]=c;
} else switch(c=fgetc(classfp)) {
case '"': case '\\': tokenstr[i++]=c; break;
case '0' ... '7': tokenstr[i++]=c+1-'0'; break;
case 'b': tokenstr[i++]=15; break;
case 'c': tokenstr[i++]=12; break;
case 'd': tokenstr[i++]=30; isimg=1; break;
case 'i': tokenstr[i++]=14; isimg=1; break;
case 'l': tokenstr[i++]=11; break;
case 'n': tokenstr[i++]=10; break;
case 'q': tokenstr[i++]=16; break;
case 'x':
c=fgetc(classfp);
if(c>='0' && c<='9') n=c-'0';
else if(c>='A' && c<='F') n=c+10-'A';
else if(c>='a' && c<='f') n=c+10-'a';
else ParseError("Invalid string escape: \\x%c\n",c);
n<<=4;
c=fgetc(classfp);
if(c>='0' && c<='9') n|=c-'0';
else if(c>='A' && c<='F') n|=c+10-'A';
else if(c>='a' && c<='f') n|=c+10-'a';
else ParseError("Invalid string escape: \\x%X%c\n",n>>4,c);
if(n<32) tokenstr[i++]=31;
tokenstr[i++]=n?:255;
break;
#ifndef CONFIG_OMIT_MBCS
case 'T':
if(has_mbcs) {
y=1;
tokenstr[i++]=27;
break;
} // else fall through
#endif
default: ParseError("Invalid string escape: \\%c\n",c);
}
} else if(c=='"') {
if(isimg) ParseError("Unterminated \\i escape in string literal\n");
tokenstr[i]=0;
#ifndef CONFIG_OMIT_MBCS
if(y) convert_euctron_to_tron8();
#endif
return;
} else if(c!='\r') {
tokenstr[i++]=c;
if(c=='\n') ++linenum;
#ifndef CONFIG_OMIT_MBCS
if((c&0x80) && has_mbcs) y=1;
#endif
}
}
ParseError("Too long string literal\n");
}
static int name_compar(const void*a,const void*b) {
const Op_Names*x=a;
const Op_Names*y=b;
return strcmp(x->txt,y->txt);
}
static Class*initialize_class(int n,int f,const char*name) {
Class*cl;
if(classes[n]) fatal("Duplicate class number %d\n",n);
cl=classes[n]=malloc(sizeof(Class));
if(!cl) fatal("Allocation failed\n");
cl->name=strdup(name);
if(!cl->name) fatal("Allocation failed\n");
cl->edithelp=cl->gamehelp=0;
cl->codes=cl->messages=cl->images=0;
cl->height=cl->weight=cl->climb=cl->density=cl->volume=cl->strength=cl->arrivals=cl->departures=0;
cl->temperature=cl->misc4=cl->misc5=cl->misc6=cl->misc7=0;
cl->uservars=cl->hard[0]=cl->hard[1]=cl->hard[2]=cl->hard[3]=cl->nmsg=0;
cl->oflags=cl->sharp[0]=cl->sharp[1]=cl->sharp[2]=cl->sharp[3]=0;
cl->shape=cl->shovable=cl->collisionLayers=cl->nimages=cl->order=0;
cl->cflags=f;
if(undef_class<=n) undef_class=n+1;
}
Uint16 get_message_ptr(int c,int m) {
if(!classes[c]) return 0xFFFF;
if(classes[c]->nmsg<=m) return 0xFFFF;
return classes[c]->messages[m];
}
static void set_message_ptr(int c,int m,int p) {
if(m>=classes[c]->nmsg) {
classes[c]->messages=realloc(classes[c]->messages,(m+1)*sizeof(Uint16));
if(!classes[c]->messages) fatal("Allocation failed\n");
while(m>=classes[c]->nmsg) classes[c]->messages[classes[c]->nmsg++]=0xFFFF;
}
classes[c]->messages[m]=p;
}
static int look_class_name(void) {
int i;
int u=undef_class;
for(i=1;i<undef_class;i++) {
if(classes[i]) {
if(!strcmp(classes[i]->name,tokenstr)) {
if(classes[i]->cflags&CF_NOCLASS2) classes[i]->cflags|=CF_NOCLASS1;
return i;
}
} else {
u=i;
}
}
if(u==0x4000) ParseError("Too many class names; cannot add $%s\n",tokenstr);
initialize_class(u,CF_NOCLASS1,tokenstr);
return u;
}
static int look_message_name(void) {
int i;
int u=undef_message;
for(i=0;i<undef_message;i++) {
if(messages[i]) {
if(!strcmp(messages[i],tokenstr)) return i;
} else {
u=i;
}
}
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");
}
}
static Uint16 look_hash_mac(void) {
int h,i,m;
for(h=i=0;tokenstr[i];i++) h=(13*h+tokenstr[i])%HASH_SIZE;
m=h;
for(;;) {
if(glohash[h].id>=0xC000 && !strcmp(tokenstr,glohash[h].txt)) return h;
if(!glohash[h].id) {
glohash[h].txt=strdup(tokenstr);
if(!glohash[h].txt) ParseError("Out of string space in hash table\n");
glohash[h].id=0xFFFF;
return h;
}
h=(h+1)%HASH_SIZE;
if(h==m) ParseError("Hash table full\n");
}
}
static int check_improper_name(void) {
unsigned char*p=tokenstr;
while(*p) if(*p++<=32) return 1;
return 0;
}
#define ReturnToken(x,y) do{ tokent=x; tokenv=y; return; }while(0)
static void nxttok1(void) {
int c,i,fl,n,pr;
magain: if(macstack) {
TokenList*tl=0;
pr=0;
tokent=macstack->tok->t;
if(tokent&TF_EOF) ParseError("Unexpected end of file in macro expansion\n");
tokenv=macstack->tok->v;
if(tokent!=(TF_MACRO|TF_CLOSE)) *tokenstr=0;
if(macstack->tok->str) strcpy(tokenstr,macstack->tok->str);
if(main_options['M']) {
printf("M* Macro %04X %08X \"",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(tokent==TF_MACRO+TF_INT && macstack->n>=0) {
if(tokenv&~0xFF) {
tokenv-=0x100;
} else {
if(tokenv<macstack->n) tl=macstack->args[tokenv];
if(tl) ref_macro(tl);
pr=1;
}
}
macstack->tok=step_macro(macstack->tok);
if(!macstack->tok) {
MacroStack*ms=macstack->next;
for(i=0;i<macstack->n;i++) free_macro(macstack->args[i]);
free(macstack->args);
free(macstack);
macstack=ms;
if(main_options['M']) printf("M.\n");
}
if(pr) {
if(tl) {
MacroStack*ms=malloc(sizeof(MacroStack));
if(!ms) fatal("Allocation failed\n");
ms->tok=tl;
ms->n=-1;
ms->args=0;
ms->next=macstack;
macstack=ms;
}
if(main_options['M']) printf("M^ %d\n",tl?1:0);
goto magain;
}
if(tokent==TF_MACRO+TF_ABNORMAL) goto magain; // denotes a empty macro
return;
}
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);
}
if(c==EOF) ReturnToken(TF_EOF,0);
if(c==';') {
while(c!=EOF && c!='\n') c=fgetc(classfp);
++linenum;
goto again;
}
if(c=='(') ReturnToken(TF_OPEN,0);
if(c==')') ReturnToken(TF_CLOSE,0);
if(c=='"') {
tokent=TF_NAME|TF_ABNORMAL;
tokenv=OP_STRING;
read_quoted_string();
return;
}
if(c=='=') {
fl|=TF_EQUAL;
c=fgetc(classfp);
}
if(c==',') {
fl|=TF_COMMA;
c=fgetc(classfp);
}
if(c=='{') {
c=fgetc(classfp);
while(c==' ' || c=='\t' || c=='\r' || c=='\n') {
if(c=='\n') ++linenum;
c=fgetc(classfp);
}
while(c>0 && (chkind[c]&2)) {
if(n==256) {
tokenstr[256]=0;
ParseError("Identifier too long: %s\n",tokenstr);
}
tokenstr[n++]=c;
c=fgetc(classfp);
}
tokenstr[n]=0;
if(!n) fatal("Expected macro name\n");
if(c!=EOF) ungetc(c,classfp);
ReturnToken(TF_MACRO|TF_OPEN,look_hash_mac());
}
if(c=='}') ReturnToken(TF_MACRO|TF_CLOSE,0);
if(c=='|') ReturnToken(TF_MACRO,1);
if(c=='\\') {
i=0;
while((c=fgetc(classfp))=='\\') i+=0x100;
tokenv=0;
for(;c>='0' && c<='9';c=fgetc(classfp)) tokenv=10*tokenv+c-'0';
--tokenv;
if(tokenv&~255) ParseError("Macro parameter reference out of range\n");
if(c!=EOF) ungetc(c,classfp);
tokent=TF_MACRO|TF_INT;
tokenv|=i;
return;
}
if(c==EOF) ParseError("Unexpected end of file\n");
if(chkind[c]==1) {
pr=c;
c=fgetc(classfp);
if(c==EOF) ParseError("Unexpected end of file\n");
}
while(c>0 && (chkind[c]&2)) {
if(n==256) {
tokenstr[256]=0;
ParseError("Identifier too long: %s\n",tokenstr);
}
tokenstr[n++]=c;
c=fgetc(classfp);
}
tokenstr[n]=0;
if(!n) fatal("Expected identifier\n");
if(c!=EOF) ungetc(c,classfp);
switch(pr) {
case 0:
if(chkind[c=*tokenstr]==2) {
char*e;
if(c=='-' || c=='+') n=1; else n=0;
if(n && !tokenstr[1]) goto norm;
tokent=TF_INT;
if(fl) ParseError("Invalid use of , and = in token\n");
if(c=='0' && tokenstr[1]=='x') {
tokenv=strtol(tokenstr+2,&e,16);
} else if(c=='0' && tokenstr[1]=='o') {
tokenv=strtol(tokenstr+2,&e,8);
} else {
tokenv=strtol(tokenstr+n,&e,10);
}
if(e && *e) goto norm;
if(c=='-') tokenv=-tokenv;
} else {
static Op_Names key={tokenstr};
const Op_Names*op;
norm:
op=bsearch(&key,op_names,N_OP_NAMES,sizeof(Op_Names),name_compar);
if(!op) ParseError("Invalid token: %s\n",tokenstr);
tokenv=op->num&0xFFFF;
tokent=op->num>>16;
if(fl&~tokent) ParseError("Invalid use of , and = in token: %s\n",tokenstr);
if(fl&TF_COMMA) tokenv+=0x0800;
if(fl&TF_EQUAL) tokenv+=0x1000;
tokent&=fl|~(TF_COMMA|TF_EQUAL);
}
break;
case '$':
if(fl) ParseError("Invalid use of , and = in token\n");
tokent=TF_NAME;
tokenv=look_class_name()+0x4000;
break;
case '!':
if(fl) ParseError("Invalid use of , and = in token\n");
tokent=TF_NAME;
tokenv=find_user_sound(tokenstr);
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;
if(fl&TF_EQUAL) tokenv+=0x1000;
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+0x8000,"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;
case '^':
tokent=TF_NAME|TF_ABNORMAL|fl;
tokenv=OP_USERFLAG;
break;
}
}
static void define_macro(Uint16 name,Uint8 q) {
int i;
TokenList**t;
if(main_options['M']) printf("M< %04X %04X \"%s\" %d\n",name,glohash[name].id,glohash[name].txt,q);
if(glohash[name].id<0xC000) fatal("Confusion\n");
if(glohash[name].id<MAX_MACRO+0xC000) {
i=glohash[name].id-0xC000;
if(q) {
free_macro(macros[i]);
macros[i]=0;
}
} else {
q=1;
for(i=0;i<MAX_MACRO;i++) if(!macros[i]) break;
if(i==MAX_MACRO) ParseError("Too many macro definitions\n");
}
glohash[name].id=i+0xC000;
t=macros+i;
if(!q) while(*t) t=&(*t)->next;
i=1;
for(;;) {
nxttok1();
if(main_options['L'] && main_options['M']) {
int j;
printf("*: %5d %04X %08X \"",i,tokent,tokenv);
for(j=0;tokenstr[j];j++) {
if(tokenstr[j]<32 || tokenstr[j]>126) printf("<%02X>",tokenstr[j]&255);
else putchar(tokenstr[j]);
}
printf("\"\n");
}
if(tokent==TF_MACRO+TF_OPEN && ++i>65000) ParseError("Too much macro nesting\n");
if(tokent==TF_MACRO+TF_CLOSE && !--i) break;
*t=add_macro();
t=&(*t)->next;
}
if(main_options['M']) printf("M> %04X %04X %p\n",name,glohash[name].id,macros[glohash[name].id-0xC000]);
if(!macros[glohash[name].id-0xC000]) {
// The macro is empty, so add a token which denotes a empty macro.
// This token will be skipped during macro expansion.
tokent=TF_MACRO+TF_ABNORMAL;
*tokenstr=0;
macros[glohash[name].id-0xC000]=add_macro();
}
}
#ifndef CONFIG_OMIT_INCLUDE
static void begin_include_file(const char*name) {
InputStack*nxt=inpstack;
inpstack=malloc(sizeof(InputStack));
if(!inpstack) fatal("Allocation failed\n");
inpstack->classfp=classfp;
inpstack->linenum=linenum;
inpstack->next=nxt;
linenum=1;
if(main_options['S']) fatal("Cannot use {include} with level hash calculation mode\n");
if(*name=='.' || *name=='_' || *name=='-' || !*name || name[strspn(name,"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_-abcdefghijklmnopqrstuvwxyz.")])
ParseError("Improper name of include file \"%s\"\n",name);
if(main_options['z']) {
if(strlen(name)<9 || memcmp(name-strlen(name),".include",8)) ParseError("Include file name doesn't end with .include\n");
classfp=composite_slice(name,0)?:fopen(name,"r");
} else {
classfp=fopen(name,"r");
}
if(!classfp) ParseError("Cannot open include file \"%s\": %m\n",name);
}
#endif
static void begin_macro(TokenList*mac) {
MacroStack*ms=malloc(sizeof(MacroStack));
TokenList**ap=0;
int a=0;
int b=0;
int c=0;
if(StackProtection()) fatal("Stack overflow\n");
ref_macro(mac);
if(!ms) fatal("Allocation failed\n");
ms->tok=mac;
ms->n=0;
ms->args=0;
for(;;) {
nxttok1();
if(tokent&TF_EOF) ParseError("Unexpected end of file in macro argument\n");
if(main_options['M']) {
int i;
printf("M= %c%4d %04X %08X \"",b?'|':a?'^':' ',ms->n,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(tokent&TF_OPEN) {
++a;
if(tokent&TF_MACRO) ++c;
}
if(tokent&TF_CLOSE) {
--a;
if(tokent&TF_MACRO) --c;
}
if(c==-1) {
if(a!=-1 && !b) ParseError("Misnested macro argument\n");
ms->next=macstack;
macstack=ms;
return;
} else if(a==-1 && !b) {
ParseError("Misnested macro argument\n");
} else if(tokent==TF_MACRO && tokenv==1 && !a && !b) {
if(c) ParseError("Misnested macro argument\n");
b=1;
ms->args=realloc(ms->args,++ms->n*sizeof(TokenList*));
if(!ms->args) fatal("Allocation failed\n");
ap=ms->args+ms->n-1;
*ap=0;
} else {
if(!b && !(tokent&TF_CLOSE) && a==(tokent&TF_OPEN?1:0)) {
ms->args=realloc(ms->args,++ms->n*sizeof(TokenList*));
if(!ms->args) fatal("Allocation failed\n");
ap=ms->args+ms->n-1;
*ap=0;
}
*ap=add_macro();
ap=&((*ap)->next);
}
}
}
static void nxttok(void) {
if(pushback) {
pushback=0;
return;
}
if(StackProtection()) fatal("Stack overflow\n");
again:
nxttok1();
if(tokent&TF_EOF) {
if(inpstack) {
InputStack s=*inpstack;
free(inpstack);
fclose(classfp);
classfp=s.classfp;
linenum=s.linenum;
inpstack=s.next;
goto again;
}
} else if(tokent&TF_MACRO) {
Uint32 n;
char*s;
if(tokent&TF_OPEN) {
call:
if(main_options['M']) printf("M+ {%s}\n",glohash[tokenv].txt);
switch(glohash[tokenv].id) {
case 0xC000 ... MAX_MACRO+0xC000-1:
begin_macro(macros[glohash[tokenv].id-0xC000]);
goto again;
case MAC_ADD:
n=0;
for(;;) {
nxttok();
if(tokent==TF_MACRO+TF_CLOSE) break;
if(tokent!=TF_INT && tokent!=TF_NAME+TF_DIR && tokent!=TF_NAME+TF_KEY) ParseError("Number expected\n");
n+=tokenv;
}
tokent=TF_INT;
tokenv=n;
break;
case MAC_SUB:
nxttok();
if(tokent!=TF_INT && tokent!=TF_NAME+TF_DIR && tokent!=TF_NAME+TF_KEY) ParseError("Number expected\n");
n=tokenv;
nxttok();
if(tokent!=TF_INT && tokent!=TF_NAME+TF_DIR && tokent!=TF_NAME+TF_KEY) ParseError("Number expected\n");
n-=tokenv;
nxttok();
if(tokent!=TF_MACRO+TF_CLOSE) ParseError("Too many macro arguments\n");
tokent=TF_INT;
tokenv=n;
break;
case MAC_MUL:
n=1;
for(;;) {
nxttok();
if(tokent==TF_MACRO+TF_CLOSE) break;
if(tokent!=TF_INT && tokent!=TF_NAME+TF_DIR && tokent!=TF_NAME+TF_KEY) ParseError("Number expected\n");
n*=tokenv;
}
tokent=TF_INT;
tokenv=n;
break;
case MAC_DIV:
nxttok();
if(tokent!=TF_INT && tokent!=TF_NAME+TF_DIR && tokent!=TF_NAME+TF_KEY) ParseError("Number expected\n");
n=tokenv;
nxttok();
if(tokent!=TF_INT && tokent!=TF_NAME+TF_DIR && tokent!=TF_NAME+TF_KEY) ParseError("Number expected\n");
if(!tokenv) ParseError("Division by zero\n");
n/=tokenv;
nxttok();
if(tokent!=TF_MACRO+TF_CLOSE) ParseError("Too many macro arguments\n");
tokent=TF_INT;
tokenv=n;
break;
case MAC_MOD:
nxttok();
if(tokent!=TF_INT && tokent!=TF_NAME+TF_DIR && tokent!=TF_NAME+TF_KEY) ParseError("Number expected\n");
n=tokenv;
nxttok();
if(tokent!=TF_INT && tokent!=TF_NAME+TF_DIR && tokent!=TF_NAME+TF_KEY) ParseError("Number expected\n");
if(!tokenv) ParseError("Division by zero\n");
n%=tokenv;
nxttok();
if(tokent!=TF_MACRO+TF_CLOSE) ParseError("Too many macro arguments\n");
tokent=TF_INT;
tokenv=n;
break;
case MAC_BAND:
n=-1;
for(;;) {
nxttok();
if(tokent==TF_MACRO+TF_CLOSE) break;
if(tokent!=TF_INT && tokent!=TF_NAME+TF_DIR && tokent!=TF_NAME+TF_KEY) ParseError("Number expected\n");
n&=tokenv;
}
tokent=TF_INT;
tokenv=n;
break;
case MAC_BOR:
n=0;
for(;;) {
nxttok();
if(tokent==TF_MACRO+TF_CLOSE) break;
if(tokent!=TF_INT && tokent!=TF_NAME+TF_DIR && tokent!=TF_NAME+TF_KEY) ParseError("Number expected\n");
n|=tokenv;
}
tokent=TF_INT;
tokenv=n;
break;
case MAC_BXOR:
n=0;
for(;;) {
nxttok();
if(tokent==TF_MACRO+TF_CLOSE) break;
if(tokent!=TF_INT && tokent!=TF_NAME+TF_DIR && tokent!=TF_NAME+TF_KEY) ParseError("Number expected\n");
n^=tokenv;
}
tokent=TF_INT;
tokenv=n;
break;
case MAC_BNOT:
nxttok();
if(tokent!=TF_INT && tokent!=TF_NAME+TF_DIR && tokent!=TF_NAME+TF_KEY) ParseError("Number expected\n");
n=~tokenv;
nxttok();
if(tokent!=TF_MACRO+TF_CLOSE) ParseError("Too many macro arguments\n");
tokent=TF_INT;
tokenv=n;
break;
case MAC_CAT:
s=malloc(0x2000);
if(!s) fatal("Allocation failed\n");
n=0;
for(;;) {
nxttok();
if(tokent==TF_MACRO+TF_CLOSE) {
break;
} else if(tokent==TF_INT) {
sprintf(tokenstr,"%d",tokenv);