-
Notifications
You must be signed in to change notification settings - Fork 0
/
epd.c
4482 lines (4202 loc) · 118 KB
/
epd.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
/*>>> epd.c: Extended Position Description routines */
#if defined(EPD)
/* Revised: 1996.06.23 */
/*
Copyright (C) 1996 by Steven J. Edwards (sje@mv.mv.com)
All rights reserved. This code may be freely redistibuted and used by
both research and commerical applications. No warranty exists.
*/
/*
Everything in this source file is independent of the host program, as
are the prototypes in the epd.h include file. Requests for changes
and additions should be communicated to the author via the e-mail
address given above.
*/
/*
This file was originally prepared on an Apple Macintosh using the
Metrowerks CodeWarrior 6 ANSI C compiler. Tabs are set at every
four columns. Further testing and development was performed on a
generic PC running Linux 1.3.20 and using the gcc 2.7.0 compiler.
*/
/* system includes */
# include <ctype.h>
# include <time.h>
# include "chess.h"
# include "data.h"
/* EPD definitions (host independent) */
# include "epddefs.h"
/* EPD routine prototypes (host independent) */
# include "epd.h"
/* ASCII character constants */
# define ascii_nul ((char) 0x00)
# define ascii_sp ((char) 0x20)
/* tree limit; adjust according to memory availability */
# define treeL 16384
/* tree overrun safety margin */
# define treemarginL 256
/* played moves history limit; adjust according to memory availability */
# define pmhL 512
/* data flows (input and output) */
# define flowL 2
/* character case mapping */
# define map_lower(ch) (isupper((ch)) ? tolower((ch)) : (ch))
# define map_upper(ch) (islower((ch)) ? toupper((ch)) : (ch))
/* identifier character check */
# define IdentChar(ch) (isalpha((ch)) || isdigit((ch)) || ((ch) == '_'))
/* vacancy check */
# define Vacant(sq) (EPDboard.rbv[(sq)] == cp_v0)
/* token record type (for token chain) */
typedef struct tknS {
charptrT tkn_str; /* allocated token string value */
struct tknS *tkn_prev; /* previous record */
struct tknS *tkn_next; /* next record */
} tknT, *tknptrT;
/* tree stack entry record type */
typedef struct tseS {
siT tse_count; /* entry count for this level */
mptrT tse_base; /* first move in moveset */
mptrT tse_curr; /* current move of interest in moveset */
} tseT, *tseptrT;
/* color to move strings */
/* global game chain anchors */
static gamptrT head_gamptr;
static gamptrT tail_gamptr;
/* EPD standard opcode mnemonics */
static charptrT epdsostrv[epdsoL];
/* EPD refcom operand strings */
static charptrT refcomstrv[refcomL];
/* EPD refreq operand strings */
static charptrT refreqstrv[refreqL];
/* PGN Seven Tag Roster names */
static charptrT pgnstrstrv[pgnstrL];
/* game termination indication marker strings */
static charptrT gtimstrv[gtimL];
/* player name strings */
/* character conversion vectors (colors and pieces) */
static char asccv[rcL];
static char ascpv[rpL];
/* character conversion vectors (ranks and files) */
static char ascrv[rankL];
static char ascfv[fileL];
/* promotion piece from special case move code coversion vector */
static pT cv_p_scmvv[scmvL];
/* various color and piece conversion vectors */
static cpT cv_cp_c_pv[rcL][rpL];
static cT cv_c_cpv[cpL];
static pT cv_p_cpv[cpL];
static cT inv_cv[rcL];
/* direction vectors */
static dvT dvv[dxL];
static xdvT xdvv[dxL];
/* extension board (border detection) */
static xbT xb;
/* token chain anchors */
static tknptrT head_tknptr;
static tknptrT tail_tknptr;
/* local SAN vector and its index */
static sanT lsan;
static siT lsani;
/* census vectors */
static siT count_cv[rcL];
static siT count_cpv[rcL][rpL];
/* the current board */
static rbT EPDboard;
/* the current environment stack entry */
static eseT ese;
/* the current tree stack entry */
static tseT tse;
/* the master ply index */
static siT ply;
/* the base of the move tree and its current pointer */
static mptrT treebaseptr;
static mptrT treeptr;
/* the base of the tree stack entry stack and its current pointer */
static tseptrT tsebaseptr;
static tseptrT tseptr;
/* base of the environment stack and its current pointer */
static eseptrT esebaseptr;
static eseptrT eseptr;
/* return area for board data */
static rbT ret_rb;
/* return area for move data */
static mT ret_m;
/*--> EPDFatal: emit fatal diagnostic and quit */
nonstatic void EPDFatal(charptrT s) {
fprintf(stderr, "EPD Fatal error: %s.\n", s);
exit(1);
}
/*--> EPDSwitchFault: emit switch fault diagnostic and quit */
nonstatic void EPDSwitchFault(charptrT s) {
fprintf(stderr, "Switch fault detected.\n");
EPDFatal(s);
return;
}
/*--> EPDMemoryGrab: allocate memory */
nonstatic voidptrT EPDMemoryGrab(liT n) {
voidptrT ptr;
ptr = (voidptrT) malloc((n == 0) ? 1 : n);
if (ptr == NULL)
EPDFatal("EPDMemoryGrab");
return (ptr);
}
/*--> EPDMemoryFree: deallocate memory */
nonstatic void EPDMemoryFree(voidptrT ptr) {
if (ptr != NULL)
free(ptr);
return;
}
/*--> EPDStringGrab: allocate and copy a string */
nonstatic charptrT EPDStringGrab(charptrT s) {
charptrT ptr;
ptr = (charptrT) EPDMemoryGrab(strlen(s) + 1);
strcpy(ptr, s);
return (ptr);
}
/*--> EPDStringAppendChar: append a character to a string */
nonstatic charptrT EPDStringAppendChar(charptrT s, char c) {
charptrT ptr;
liT length;
/* the first argument is deallocated */
length = strlen(s);
ptr = (charptrT) EPDMemoryGrab(length + 2);
strcpy(ptr, s);
EPDMemoryFree(s);
*(ptr + length) = c;
*(ptr + length + 1) = ascii_nul;
return (ptr);
}
/*--> EPDStringAppendStr: append a string to a string */
nonstatic charptrT EPDStringAppendStr(charptrT s0, charptrT s1) {
charptrT ptr;
liT length;
/* the first argument is deallocated */
length = strlen(s0) + strlen(s1);
ptr = (charptrT) EPDMemoryGrab(length + 1);
strcpy(ptr, s0);
strcat(ptr, s1);
EPDMemoryFree(s0);
return (ptr);
}
/*--> EPDNewGPM: allocate and initialize a new GPM record */
static gpmptrT EPDNewGPM(mptrT mptr) {
gpmptrT gpmptr;
cT c;
sqT sq;
cpT cp0, cp1;
gpmptr = (gpmptrT) EPDMemoryGrab(sizeof(gpmT));
gpmptr->gpm_m = *mptr;
gpmptr->gpm_ese.ese_actc = ese.ese_actc;
gpmptr->gpm_ese.ese_cast = ese.ese_cast;
gpmptr->gpm_ese.ese_epsq = ese.ese_epsq;
gpmptr->gpm_ese.ese_hmvc = ese.ese_hmvc;
gpmptr->gpm_ese.ese_fmvn = ese.ese_fmvn;
for (c = c_w; c <= c_b; c++)
gpmptr->gpm_ese.ese_ksqv[c] = ese.ese_ksqv[c];
for (sq = sq_a1; sq <= sq_h8; sq += 2) {
cp0 = EPDboard.rbv[sq + 0];
cp1 = EPDboard.rbv[sq + 1];
gpmptr->gpm_nbv[sq >> 1] = ((cp1 << nybbW) | cp0);
};
gpmptr->gpm_prev = gpmptr->gpm_next = NULL;
return (gpmptr);
}
/*--> EPDReleaseGPM: release a GPM record */
static
void EPDReleaseGPM(gpmptrT gpmptr) {
if (gpmptr != NULL)
EPDMemoryFree(gpmptr);
return;
}
/*--> EPDAppendGPM: append a GPM record to a game */
static
void EPDAppendGPM(gamptrT gamptr, gpmptrT gpmptr) {
if (gamptr->gam_tailgpm == NULL)
gamptr->gam_headgpm = gpmptr;
else
gamptr->gam_tailgpm->gpm_next = gpmptr;
gpmptr->gpm_prev = gamptr->gam_tailgpm;
gpmptr->gpm_next = NULL;
gamptr->gam_tailgpm = gpmptr;
return;
}
/*--> EPDUnthreadGPM: unthread a GPM record from a game */
static
void EPDUnthreadGPM(gamptrT gamptr, gpmptrT gpmptr) {
if (gpmptr->gpm_prev == NULL)
gamptr->gam_headgpm = gpmptr->gpm_next;
else
gpmptr->gpm_prev->gpm_next = gpmptr->gpm_next;
if (gpmptr->gpm_next == NULL)
gamptr->gam_tailgpm = gpmptr->gpm_prev;
else
gpmptr->gpm_next->gpm_prev = gpmptr->gpm_prev;
return;
}
/*--> EPDReleaseGPMoveChain: release the game played moves chain */
static
void EPDReleaseGPMoveChain(gamptrT gamptr) {
gpmptrT gpmptr;
while (gamptr->gam_headgpm != NULL) {
gpmptr = gamptr->gam_tailgpm;
EPDUnthreadGPM(gamptr, gpmptr);
EPDReleaseGPM(gpmptr);
};
return;
}
/*--> EPDNewGAM: allocate and initialize a new GAM record */
static gamptrT EPDNewGAM(void) {
gamptrT gamptr;
pgnstrT pgnstr;
gamptr = (gamptrT) EPDMemoryGrab(sizeof(gamT));
for (pgnstr = 0; pgnstr < pgnstrL; pgnstr++)
gamptr->gam_strv[pgnstr] = EPDStringGrab("");
gamptr->gam_gtim = gtim_u;
gamptr->gam_headgpm = gamptr->gam_tailgpm = NULL;
gamptr->gam_prev = gamptr->gam_next = NULL;
return (gamptr);
}
/*--> EPDReleaseGAM: release a GAM record */
static
void EPDReleaseGAM(gamptrT gamptr) {
pgnstrT pgnstr;
if (gamptr != NULL) {
for (pgnstr = 0; pgnstr < pgnstrL; pgnstr++)
if (gamptr->gam_strv[pgnstr] != NULL)
EPDMemoryFree(gamptr->gam_strv[pgnstr]);
EPDReleaseGPMoveChain(gamptr);
EPDMemoryFree(gamptr);
};
return;
}
/*--> EPDAppendGAM: append a GAM record to the game chain */
static
void EPDAppendGAM(gamptrT gamptr) {
if (tail_gamptr == NULL)
head_gamptr = gamptr;
else
tail_gamptr->gam_next = gamptr;
gamptr->gam_prev = tail_gamptr;
gamptr->gam_next = NULL;
tail_gamptr = gamptr;
return;
}
/*--> EPDUnthreadGAM: unthread a GAM record from the game chain */
static
void EPDUnthreadGAM(gamptrT gamptr) {
if (gamptr->gam_prev == NULL)
head_gamptr = gamptr->gam_next;
else
gamptr->gam_prev->gam_next = gamptr->gam_next;
if (gamptr->gam_next == NULL)
tail_gamptr = gamptr->gam_prev;
else
gamptr->gam_next->gam_prev = gamptr->gam_prev;
return;
}
/*--> EPDReleaseGameChain: release the game chain */
static
void EPDReleaseGameChain(void) {
gamptrT gamptr;
while (head_gamptr != NULL) {
gamptr = tail_gamptr;
EPDUnthreadGAM(gamptr);
EPDReleaseGAM(gamptr);
};
return;
}
/*--> EPDGamePlyCount: return ply count of a game */
static siT EPDGamePlyCount(gamptrT gamptr) {
siT count;
gpmptrT gpmptr;
count = 0;
gpmptr = gamptr->gam_headgpm;
while (gpmptr != NULL) {
count++;
gpmptr = gpmptr->gpm_next;
};
return (count);
}
/*--> EPDGameOpen: create/open a new game structure */
nonstatic gamptrT EPDGameOpen(void) {
gamptrT gamptr;
gamptr = EPDNewGAM();
EPDAppendGAM(gamptr);
return (gamptr);
}
/*--> EPDGameClose: close/destroy a game structure */
nonstatic void EPDGameClose(gamptrT gamptr) {
if (gamptr != NULL) {
EPDUnthreadGAM(gamptr);
EPDReleaseGAM(gamptr);
};
return;
}
/*--> EPDGameAppendMove: append a move to a game structure */
nonstatic void EPDGameAppendMove(gamptrT gamptr, mptrT mptr) {
gpmptrT gpmptr;
gpmptr = EPDNewGPM(mptr);
EPDAppendGPM(gamptr, gpmptr);
return;
}
/*--> EPDNewTKN: allocate and initialize a new TKN record */
static tknptrT EPDNewTKN(charptrT s) {
tknptrT tknptr;
tknptr = (tknptrT) EPDMemoryGrab(sizeof(tknT));
tknptr->tkn_str = EPDStringGrab(s);
tknptr->tkn_prev = tknptr->tkn_next = NULL;
return (tknptr);
}
/*--> EPDReleaseTKN: release a TKN record */
static
void EPDReleaseTKN(tknptrT tknptr) {
if (tknptr != NULL) {
if (tknptr->tkn_str != NULL)
EPDMemoryFree(tknptr->tkn_str);
EPDMemoryFree(tknptr);
};
return;
}
/*--> EPDAppendTKN: append a TKN record to the token chain */
static
void EPDAppendTKN(tknptrT tknptr) {
if (tail_tknptr == NULL)
head_tknptr = tknptr;
else
tail_tknptr->tkn_next = tknptr;
tknptr->tkn_prev = tail_tknptr;
tknptr->tkn_next = NULL;
tail_tknptr = tknptr;
return;
}
/*--> EPDUnthreadTKN: unthread a TKN record from the token chain */
static
void EPDUnthreadTKN(tknptrT tknptr) {
if (tknptr->tkn_prev == NULL)
head_tknptr = tknptr->tkn_next;
else
tknptr->tkn_prev->tkn_next = tknptr->tkn_next;
if (tknptr->tkn_next == NULL)
tail_tknptr = tknptr->tkn_prev;
else
tknptr->tkn_next->tkn_prev = tknptr->tkn_prev;
return;
}
/*--> EPDReleaseTokenChain: release the token chain */
static
void EPDReleaseTokenChain(void) {
tknptrT tknptr;
while (head_tknptr != NULL) {
tknptr = tail_tknptr;
EPDUnthreadTKN(tknptr);
EPDReleaseTKN(tknptr);
};
return;
}
/*--> EPDTokenize: create the token chain */
nonstatic void EPDTokenize(charptrT s) {
siT i;
char c;
tknptrT tknptr;
charptrT str;
/* first, release any existing chain */
EPDReleaseTokenChain();
/* scan the input until end of string */
i = 0;
c = *(s + i++);
while (c != ascii_nul) {
/* skip leading whitespace */
while ((c != ascii_nul) && isspace(c))
c = *(s + i++);
/* if not at end of string, then a token has started */
if (c != ascii_nul) {
str = EPDStringGrab("");
while ((c != ascii_nul) && !isspace(c)) {
str = EPDStringAppendChar(str, c);
c = *(s + i++);
};
tknptr = EPDNewTKN(str);
EPDAppendTKN(tknptr);
EPDMemoryFree(str);
};
};
return;
}
/*--> EPDTokenCount: count the tokens in the token chain */
nonstatic siT EPDTokenCount(void) {
siT n;
tknptrT tknptr;
n = 0;
tknptr = head_tknptr;
while (tknptr != NULL) {
n++;
tknptr = tknptr->tkn_next;
};
return (n);
}
/*--> EPDTokenFetch: fetch n-th (zero origin) token from the token chain */
nonstatic charptrT EPDTokenFetch(siT n) {
charptrT s;
siT i;
tknptrT tknptr;
i = 0;
tknptr = head_tknptr;
while ((tknptr != NULL) && (i < n)) {
i++;
tknptr = tknptr->tkn_next;
};
if (tknptr == NULL)
s = NULL;
else
s = tknptr->tkn_str;
return (s);
}
/*--> EPDCICharEqual: test for case independent character equality */
nonstatic siT EPDCICharEqual(char ch0, char ch1) {
siT flag;
if (map_lower(ch0) == map_lower(ch1))
flag = 1;
else
flag = 0;
return (flag);
}
/*--> EPDPieceFromCP: fetch piece from color-piece */
nonstatic pT EPDPieceFromCP(cpT cp) {
pT p;
p = cv_p_cpv[cp];
return (p);
}
/*--> EPDCheckPiece: test if a character is a piece letter */
nonstatic siT EPDCheckPiece(char ch) {
siT flag;
pT p;
flag = 0;
p = p_p;
while (!flag && (p <= p_k))
if (EPDCICharEqual(ch, ascpv[p]))
flag = 1;
else
p++;
return (flag);
}
/*--> EPDEvaluatePiece: evaluate a piece letter character */
nonstatic pT EPDEvaluatePiece(char ch) {
pT p;
siT flag;
flag = 0;
p = p_p;
while (!flag && (p <= p_k))
if (EPDCICharEqual(ch, ascpv[p]))
flag = 1;
else
p++;
if (!flag)
p = p_nil;
return (p);
}
/*--> EPDCheckColor: test if a character is a color letter */
nonstatic siT EPDCheckColor(char ch) {
siT flag;
cT c;
flag = 0;
c = c_w;
while (!flag && (c <= c_b))
if (EPDCICharEqual(ch, asccv[c]))
flag = 1;
else
c++;
return (flag);
}
/*--> EPDEvaluateColor: evaluate a color letter character */
nonstatic cT EPDEvaluateColor(char ch) {
cT c;
siT flag;
flag = 0;
c = c_w;
while (!flag && (c <= c_b))
if (EPDCICharEqual(ch, asccv[c]))
flag = 1;
else
c++;
if (!flag)
c = c_nil;
return (c);
}
/*--> EPDCheckRank: test if a character is a rank character */
nonstatic siT EPDCheckRank(char ch) {
siT flag;
rankT rank;
flag = 0;
rank = rank_1;
while (!flag && (rank <= rank_8))
if (EPDCICharEqual(ch, ascrv[rank]))
flag = 1;
else
rank++;
return (flag);
}
/*--> EPDEvaluateRank: evaluate a color rank character */
nonstatic rankT EPDEvaluateRank(char ch) {
rankT rank;
siT flag;
flag = 0;
rank = rank_1;
while (!flag && (rank <= rank_8))
if (EPDCICharEqual(ch, ascrv[rank]))
flag = 1;
else
rank++;
if (!flag)
rank = rank_nil;
return (rank);
}
/*--> EPDCheckFile: test if a character is a file character */
nonstatic siT EPDCheckFile(char ch) {
siT flag;
fileT file;
flag = 0;
file = file_a;
while (!flag && (file <= file_h))
if (EPDCICharEqual(ch, ascfv[file]))
flag = 1;
else
file++;
return (flag);
}
/*--> EPDEvaluateFile: evaluate a color file character */
nonstatic rankT EPDEvaluateFile(char ch) {
fileT file;
siT flag;
flag = 0;
file = file_a;
while (!flag && (file <= file_h))
if (EPDCICharEqual(ch, ascfv[file]))
flag = 1;
else
file++;
if (!flag)
file = file_nil;
return (file);
}
/*--> EPDNewEOV: allocate a new EOV record */
nonstatic eovptrT EPDNewEOV(void) {
eovptrT eovptr;
eovptr = (eovptrT) EPDMemoryGrab(sizeof(eovT));
eovptr->eov_eob = eob_nil;
eovptr->eov_str = NULL;
eovptr->eov_prev = eovptr->eov_next = NULL;
return (eovptr);
}
/*--> EPDReleaseEOV: release an EOV record */
nonstatic void EPDReleaseEOV(eovptrT eovptr) {
if (eovptr != NULL) {
if (eovptr->eov_str != NULL)
EPDMemoryFree(eovptr->eov_str);
EPDMemoryFree(eovptr);
};
return;
}
/*--> EPDAppendEOV: append an EOV record */
nonstatic void EPDAppendEOV(eopptrT eopptr, eovptrT eovptr) {
if (eopptr->eop_taileov == NULL)
eopptr->eop_headeov = eovptr;
else
eopptr->eop_taileov->eov_next = eovptr;
eovptr->eov_prev = eopptr->eop_taileov;
eovptr->eov_next = NULL;
eopptr->eop_taileov = eovptr;
return;
}
/*--> EPDUnthreadEOV: unthread an EOV record */
static
void EPDUnthreadEOV(eopptrT eopptr, eovptrT eovptr) {
if (eovptr->eov_prev == NULL)
eopptr->eop_headeov = eovptr->eov_next;
else
eovptr->eov_prev->eov_next = eovptr->eov_next;
if (eovptr->eov_next == NULL)
eopptr->eop_taileov = eovptr->eov_prev;
else
eovptr->eov_next->eov_prev = eovptr->eov_prev;
return;
}
/*--> EPDCreateEOVSym: create a new EOV record with a symbol value */
nonstatic eovptrT EPDCreateEOVSym(charptrT sym) {
eovptrT eovptr;
eovptr = EPDNewEOV();
eovptr->eov_eob = eob_symbol;
eovptr->eov_str = EPDStringGrab(sym);
return (eovptr);
}
/*--> EPDCreateEOVInt: create a new EOV record with an integer value */
nonstatic eovptrT EPDCreateEOVInt(liT lval) {
eovptrT eovptr;
char tv[tL];
sprintf(tv, "%ld", lval);
eovptr = EPDNewEOV();
eovptr->eov_eob = eob_symbol;
eovptr->eov_str = EPDStringGrab(tv);
return (eovptr);
}
/*--> EPDLocateEOV: try to locate 1st EOV record with given string value */
nonstatic eovptrT EPDLocateEOV(eopptrT eopptr, charptrT strval) {
eovptrT eovptr;
siT flag;
flag = 0;
eovptr = eopptr->eop_headeov;
while (!flag && (eovptr != NULL))
if (strcmp(strval, eovptr->eov_str) == 0)
flag = 1;
else
eovptr = eovptr->eov_next;
if (!flag)
eovptr = NULL;
return (eovptr);
}
/*--> EPDReplaceEOVStr: replace EOV string value with given string value */
nonstatic void EPDReplaceEOVStr(eovptrT eovptr, charptrT str) {
if (eovptr->eov_str != NULL) {
EPDMemoryFree(eovptr->eov_str);
eovptr->eov_str = NULL;
};
if (str != NULL)
eovptr->eov_str = EPDStringGrab(str);
return;
}
/*--> EPDNewEOP: allocate a new EOP record */
nonstatic eopptrT EPDNewEOP(void) {
eopptrT eopptr;
eopptr = (eopptrT) EPDMemoryGrab(sizeof(eopT));
eopptr->eop_opsym = NULL;
eopptr->eop_headeov = eopptr->eop_taileov = NULL;
eopptr->eop_prev = eopptr->eop_next = NULL;
return (eopptr);
}
/*--> EPDReleaseEOP: release an EOP record */
nonstatic void EPDReleaseEOP(eopptrT eopptr) {
eovptrT eovptr0, eovptr1;
if (eopptr != NULL) {
if (eopptr->eop_opsym != NULL)
EPDMemoryFree(eopptr->eop_opsym);
eovptr0 = eopptr->eop_headeov;
while (eovptr0 != NULL) {
eovptr1 = eovptr0->eov_next;
EPDUnthreadEOV(eopptr, eovptr0);
EPDReleaseEOV(eovptr0);
eovptr0 = eovptr1;
};
EPDMemoryFree(eopptr);
};
return;
}
/*--> EPDAppendEOP: append an EOP record */
nonstatic void EPDAppendEOP(epdptrT epdptr, eopptrT eopptr) {
if (epdptr->epd_taileop == NULL)
epdptr->epd_headeop = eopptr;
else
epdptr->epd_taileop->eop_next = eopptr;
eopptr->eop_prev = epdptr->epd_taileop;
eopptr->eop_next = NULL;
epdptr->epd_taileop = eopptr;
return;
}
/*--> EPDUnthreadEOP: unthread an EOP record */
static
void EPDUnthreadEOP(epdptrT epdptr, eopptrT eopptr) {
if (eopptr->eop_prev == NULL)
epdptr->epd_headeop = eopptr->eop_next;
else
eopptr->eop_prev->eop_next = eopptr->eop_next;
if (eopptr->eop_next == NULL)
epdptr->epd_taileop = eopptr->eop_prev;
else
eopptr->eop_next->eop_prev = eopptr->eop_prev;
return;
}
/*--> EPDCreateEOP: create a new EOP record with opsym */
nonstatic eopptrT EPDCreateEOP(charptrT opsym) {
eopptrT eopptr;
eopptr = EPDNewEOP();
eopptr->eop_opsym = EPDStringGrab(opsym);
return (eopptr);
}
/*--> EPDCreateEOPCode: create a new EOP record using opsym index */
nonstatic eopptrT EPDCreateEOPCode(epdsoT epdso) {
eopptrT eopptr;
eopptr = EPDCreateEOP(EPDFetchOpsym(epdso));
return (eopptr);
}
/*--> EPDLocateEOP: attempt to locate EOP record with given opsym */
nonstatic eopptrT EPDLocateEOP(epdptrT epdptr, charptrT opsym) {
eopptrT eopptr;
siT flag;
flag = 0;
eopptr = epdptr->epd_headeop;
while (!flag && (eopptr != NULL))
if (strcmp(opsym, eopptr->eop_opsym) == 0)
flag = 1;
else
eopptr = eopptr->eop_next;
if (!flag)
eopptr = NULL;
return (eopptr);
}
/*--> EPDLocateEOPCode: attempt to locate EOP record with given code */
nonstatic eopptrT EPDLocateEOPCode(epdptrT epdptr, epdsoT epdso) {
return (EPDLocateEOP(epdptr, epdsostrv[epdso]));
}
/*--> EPDDropIfLocEOPCode: try to locate/drop EOP record with given code */
nonstatic void EPDDropIfLocEOPCode(epdptrT epdptr, epdsoT epdso) {
eopptrT eopptr;
eopptr = EPDLocateEOP(epdptr, epdsostrv[epdso]);
if (eopptr != NULL) {
EPDUnthreadEOP(epdptr, eopptr);
EPDReleaseEOP(eopptr);
};
return;
}
/*--> EPDAddOpInt: add a single integer operand operation */
nonstatic void EPDAddOpInt(epdptrT epdptr, epdsoT epdso, liT val) {
eopptrT eopptr;
eovptrT eovptr;
eovptr = EPDCreateEOVInt(val);
eopptr = EPDCreateEOPCode(epdso);
EPDAppendEOV(eopptr, eovptr);
EPDDropIfLocEOPCode(epdptr, epdso);
EPDAppendEOP(epdptr, eopptr);
return;
}
/*--> EPDAddOpSym: add a single symbol operand operation */
nonstatic void EPDAddOpSym(epdptrT epdptr, epdsoT epdso, charptrT s) {
eopptrT eopptr;
eovptrT eovptr;
eovptr = EPDCreateEOVSym(s);
eopptr = EPDCreateEOPCode(epdso);
EPDAppendEOV(eopptr, eovptr);
EPDDropIfLocEOPCode(epdptr, epdso);
EPDAppendEOP(epdptr, eopptr);
return;
}
/*--> EPDNewEPD: allocate a new EPD record */
nonstatic epdptrT EPDNewEPD(void) {
epdptrT epdptr;
siT i;
epdptr = (epdptrT) EPDMemoryGrab(sizeof(epdT));
for (i = 0; i < nbL; i++)
epdptr->epd_nbv[i] = ((cp_v0 << nybbW) | cp_v0);
epdptr->epd_actc = c_v;
epdptr->epd_cast = 0;
epdptr->epd_epsq = sq_nil;
epdptr->epd_headeop = epdptr->epd_taileop = NULL;
return (epdptr);
}
/*--> EPDReleaseOperations: release EPD operation list */
nonstatic void EPDReleaseOperations(epdptrT epdptr) {
eopptrT eopptr0, eopptr1;
if (epdptr != NULL) {
eopptr0 = epdptr->epd_headeop;
while (eopptr0 != NULL) {
eopptr1 = eopptr0->eop_next;
EPDUnthreadEOP(epdptr, eopptr0);
EPDReleaseEOP(eopptr0);
eopptr0 = eopptr1;
};
epdptr->epd_headeop = NULL;
epdptr->epd_taileop = NULL;
};
return;
}
/*--> EPDReleaseEPD: release an EPD record */
nonstatic void EPDReleaseEPD(epdptrT epdptr) {
if (epdptr != NULL) {
EPDReleaseOperations(epdptr);
EPDMemoryFree(epdptr);
};
return;
}
/*--> EPDFetchOpsym: return a pointer to the indicated mnemonic */
nonstatic charptrT EPDFetchOpsym(epdsoT epdso) {
return (epdsostrv[epdso]);
}
/*--> EPDCountOperands: count operands */
static siT EPDCountOperands(eopptrT eopptr) {
siT count;
eovptrT eovptr;
count = 0;
eovptr = eopptr->eop_headeov;
while (eovptr != NULL) {
count++;
eovptr = eovptr->eov_next;
};
return (count);
}
/*--> EPDCountOperations: count operations */
static siT EPDCountOperations(epdptrT epdptr) {
siT count;
eopptrT eopptr;
count = 0;
eopptr = epdptr->epd_headeop;
while (eopptr != NULL) {
count++;
eopptr = eopptr->eop_next;
};
return (count);
}
/*--> EPDSortOperands: sort operands according to string value */
static
void EPDSortOperands(eopptrT eopptr) {
siT count;
siT pass, flag;
eovptrT ptr0, ptr1, ptr2, ptr3;
count = EPDCountOperands(eopptr);
if (count > 1) {
flag = 1;
pass = 0;
while (flag && (pass < (count - 1))) {
flag = 0;
ptr0 = eopptr->eop_headeov;
ptr1 = ptr0->eov_next;
while (ptr1 != NULL) {
if (strcmp(ptr0->eov_str, ptr1->eov_str) > 0) {
flag = 1;
ptr2 = ptr0->eov_prev;
ptr3 = ptr1->eov_next;
ptr0->eov_prev = ptr1;
ptr0->eov_next = ptr3;
ptr1->eov_prev = ptr2;
ptr1->eov_next = ptr0;