64 static UBYTE *tablebase = (UBYTE *)
"tablebase";
71 void ClearTableTree(
TABLES T)
102 int InsTableTree(
TABLES T, WORD *tp)
120 if ( q->
right == -1 ) {
135 v2 = tp; v3 = tp + T->
numind;
136 while ( *v1 == *v2 && v2 < v3 ) { v1++; v2++; }
137 if ( v2 >= v3 )
return(-p->value);
140 if ( iq >= 0 ) { ip = iq; }
150 if ( p->blnce == 0 )
return(T->
numtree);
154 else if ( *v1 < *v2 ) {
156 if ( iq >= 0 ) { ip = iq; }
166 if ( p->blnce == 0 )
return(T->
numtree);
171 MesPrint(
"Serious problems in InsTableTree!\n");
182 if ( q->
blnce == 0 )
break;
183 if ( q->
blnce == -2 ) {
184 if ( p->blnce == -1 ) {
189 if ( boomlijst[p->parent].left == iq ) boomlijst[p->parent].
left = ip;
190 else boomlijst[p->parent].
right = ip;
192 q->
blnce = p->blnce = 0;
200 if ( p->right >= 0 ) boomlijst[p->right].
parent = ip;
205 if ( boomlijst[s->
parent].left == iq )
214 else if ( q->
blnce == 2 ) {
215 if ( p->blnce == 1 ) {
220 if ( boomlijst[p->parent].left == iq ) boomlijst[p->parent].
left = ip;
221 else boomlijst[p->parent].
right = ip;
223 q->
blnce = p->blnce = 0;
231 if ( p->left >= 0 ) boomlijst[p->left].
parent = ip;
263 void RedoTableTree(
TABLES T,
int newsize)
270 tp += T->
numind+TABLEEXTENSION;
288 int FindTableTree(
TABLES T, WORD *tp,
int inc)
293 if ( q->
right == -1 )
return(-1);
295 if ( inc > 1 ) tp += inc-1;
299 v2 = tp; v3 = v1 + T->
numind;
300 while ( *v1 == *v2 && v1 < v3 ) { v1++; v2 += inc; }
307 if ( iq >= 0 ) { ip = iq; }
310 else if ( *v1 < *v2 ) {
312 if ( iq >= 0 ) { ip = iq; }
316 MesPrint(
"Serious problems in FindTableTree\n");
326 WORD DoTableExpansion(WORD *term, WORD level)
329 WORD *t, *tstop, *stopper, *termout, *m, *mm, *tp, *r;
332 AN.TeInFun = AR.TePos = 0;
333 tstop = term + *term;
334 stopper = tstop - ABS(tstop[-1]);
336 while ( t < stopper ) {
337 if ( *t != TABLEFUNCTION ) { t += t[1];
continue; }
338 if ( t[FUNHEAD] > -FUNCTION ) { t += t[1];
continue; }
339 T = functions[-t[FUNHEAD]-FUNCTION].tabl;
340 if ( T == 0 ) { t += t[1];
continue; }
342 if ( t[1] == FUNHEAD+2 && t[FUNHEAD+1] <= -FUNCTION )
break;
343 if ( t[1] < FUNHEAD+1+2*T->
numind ) { t += t[1];
continue; }
344 for ( i = 0; i < T->
numind; i++ ) {
345 if ( t[FUNHEAD+1+2*i] != -SYMBOL )
break;
347 if ( i >= T->
numind )
break;
350 if ( t >= stopper ) {
351 MesPrint(
"Internal error: Missing table_ function");
357 termout = AT.WorkPointer;
359 for ( i = 0; i < T->
totind; i++ ) {
363 m = termout + 1; mm = term + 1;
364 while ( mm < t ) *m++ = *mm++;
366 if ( t[1] == FUNHEAD+2 && t[FUNHEAD+1] <= -FUNCTION ) {
367 *m++ = -t[FUNHEAD+1];
368 *m++ = FUNHEAD+T->
numind*2;
369 for ( j = 2; j < FUNHEAD; j++ ) *m++ = 0;
371 for ( j = 0; j < T->
numind; j++ ) {
372 *m++ = -SNUMBER; *m++ = *tp++;
376 *m++ = SYMBOL; *m++ = 2+T->
numind*2; mm = t + FUNHEAD+1;
378 for ( j = 0; j < T->
numind; j++, mm += 2, tp++ ) {
379 if ( *tp != 0 ) { *m++ = mm[1]; *m++ = *tp; }
382 if ( r[1] == 2 ) m = r;
404 for ( j = 2; j < FUNHEAD; j++ ) *m++ = t[j];
406 for ( j = 0; j < T->
numind; j++ ) {
407 *m++ = -SNUMBER; *m++ = *tp++;
409 tp = t + FUNHEAD + 1 + 2*T->
numind;
411 while ( tp < mm ) *m++ = *tp++;
416 while ( mm < tstop ) *m++ = *mm++;
417 *termout = m - termout;
420 MesCall(
"DoTableExpand");
426 for ( i = 0; i < T->
totind; i++ ) {
427 #if TABLEEXTENSION == 2 432 m = termout + 1; mm = term + 1;
433 while ( mm < t ) *m++ = *mm++;
435 if ( t[1] == FUNHEAD+2 && t[FUNHEAD+1] <= -FUNCTION ) {
436 *m++ = -t[FUNHEAD+1];
437 *m++ = FUNHEAD+T->
numind*2;
438 for ( j = 2; j < FUNHEAD; j++ ) *m++ = 0;
440 for ( j = 0; j < T->
numind; j++ ) {
447 *m++ = -SNUMBER; *m++ = num;
451 *m++ = SYMBOL; *m++ = 2+T->
numind*2; mm = t + FUNHEAD+1;
452 for ( j = 0; j < T->
numind; j++, mm += 2 ) {
459 if ( num != 0 ) { *m++ = mm[1]; *m++ = num; }
462 if ( r[1] == 2 ) m = r;
484 for ( j = 2; j < FUNHEAD; j++ ) *m++ = t[j];
485 for ( j = 0; j < T->
numind; j++ ) {
492 *m++ = -SNUMBER; *m++ = num;
494 tp = t + FUNHEAD + 1 + 2*T->
numind;
496 while ( tp < mm ) *m++ = *tp++;
501 while ( mm < tstop ) *m++ = *mm++;
502 *termout = m - termout;
505 MesCall(
"DoTableExpand");
559 {
"addto", (TFUN)CoTBaddto, 0, PARTEST}
560 ,{
"audit", (TFUN)CoTBaudit, 0, PARTEST}
561 ,{
"cleanup", (TFUN)CoTBcleanup, 0, PARTEST}
562 ,{
"create", (TFUN)CoTBcreate, 0, PARTEST}
563 ,{
"enter", (TFUN)CoTBenter, 0, PARTEST}
564 ,{
"help", (TFUN)CoTBhelp, 0, PARTEST}
565 ,{
"load", (TFUN)CoTBload, 0, PARTEST}
566 ,{
"off", (TFUN)CoTBoff, 0, PARTEST}
567 ,{
"on", (TFUN)CoTBon, 0, PARTEST}
568 ,{
"open", (TFUN)CoTBopen, 0, PARTEST}
569 ,{
"replace", (TFUN)CoTBreplace, 0, PARTEST}
570 ,{
"use", (TFUN)CoTBuse, 0, PARTEST}
573 static UBYTE *tablebasename = 0;
575 int CoTableBase(UBYTE *s)
577 UBYTE *option, c, *t;
578 int i,optlistsize =
sizeof(tboptions)/
sizeof(
KEYWORD), error = 0;
579 while ( *s ==
' ' ) s++;
581 if ( ( tolower(*s) ==
'h' ) && ( tolower(s[1]) ==
'e' )
582 && ( tolower(s[2]) ==
'l' ) && ( tolower(s[3]) ==
'p' )
583 && ( FG.cTable[s[4]] > 1 ) ) {
588 MesPrint(
"&Proper syntax: TableBase \"filename\" options");
591 s++; tablebasename = s;
592 while ( *s && *s !=
'"' ) s++;
593 if ( *s !=
'"' )
goto proper;
595 while ( *s ==
' ' || *s ==
'\t' || *s ==
',' ) s++;
597 while ( FG.cTable[*s] == 0 ) s++;
599 for ( i = 0; i < optlistsize; i++ ) {
600 if ( StrICmp(option,(UBYTE *)(tboptions[i].name)) == 0 ) {
602 while ( *s ==
',' ) s++;
603 error = (tboptions[i].func)(s);
608 MesPrint(
"&Unrecognized option %s in TableBase statement",option);
623 if ( ( TT = T->
spare ) == 0 ) {
624 MesPrint(
"Error: trying to change mode on a table that has no tablebase");
627 if ( TT->
mode == type ) f->
tabl = TT;
668 T->
flags = (WORD *)Malloc1(T->
numind*
sizeof(WORD),
"table flags");
682 DBASE *FindTB(UBYTE *name)
686 for ( i = 0; i < NumTableBases; i++ ) {
688 if ( d->name && ( StrCmp(name,(UBYTE *)(d->name)) == 0 ) ) {
return(d); }
704 int CoTBcreate(UBYTE *s)
707 if ( FindTB(tablebasename) != 0 ) {
708 MesPrint(
"&There is already an open TableBase with the name %s",tablebasename);
711 NewDbase((
char *)tablebasename,0);
720 int CoTBopen(UBYTE *s)
724 if ( ( d = FindTB(tablebasename) ) != 0 ) {
725 MesPrint(
"&There is already an open TableBase with the name %s",tablebasename);
728 d = GetDbase((
char *)tablebasename);
729 if ( CheckTableDeclarations(d) )
return(-1);
738 int CoTBaddto(UBYTE *s)
742 UBYTE *tablename, c, *t, elementstring[ELEMENTSIZE+20], *ss, *es;
743 WORD type, funnum, lbrac, first, num, *expr, *w;
747 int i, j, error = 0, sum;
748 if ( ( d = FindTB(tablebasename) ) == 0 ) {
749 MesPrint(
"&No open tablebase with the name %s",tablebasename);
752 AO.DollarOutSizeBuffer = 32;
753 AO.DollarOutBuffer = (UBYTE *)Malloc1(AO.DollarOutSizeBuffer,
758 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
761 if ( ( s = SkipAName(s) ) == 0 )
goto tableabort;
763 if ( ( GetVar(tablename,&type,&funnum,CFUNCTION,NOAUTO) == NAMENOTFOUND )
764 || ( T = functions[funnum].tabl ) == 0 ) {
765 MesPrint(
"&%s should be a previously declared table",tablename);
766 *s = c;
goto tableabort;
769 MesPrint(
"&%s should be a sparse table",tablename);
770 *s = c;
goto tableabort;
772 basenumber = AddTableName(d,(
char *)tablename,T);
774 if ( basenumber < 0 ) basenumber = -basenumber;
775 else if ( basenumber == 0 ) { *s = c;
goto tableabort; }
779 for ( i = 0, w = AT.WorkPointer; i < T->numind; i++ ) {
780 ParseSignedNumber(x,s);
781 if ( FG.cTable[s[-1]] != 1 || ( *s !=
',' && *s !=
')' ) ) {
782 MesPrint(
"&Table arguments in TableBase addto statement should be numbers");
786 if ( *s ==
')' )
break;
789 if ( *s !=
')' || i < ( T->
numind - 1 ) ) {
790 MesPrint(
"&Incorrect number of table arguments in TableBase addto statement. Should be %d" 795 i = FindTableTree(T,AT.WorkPointer,1);
797 MesPrint(
"&Element %s has not been defined",es);
801 else if ( ExistsObject(d,basenumber,(
char *)es) ) {}
803 int dict = AO.CurrentDictionary;
804 AO.CurrentDictionary = 0;
809 AO.DollarInOutBuffer = 1;
811 ss = AO.DollarOutBuffer;
814 #if ( TABLEEXTENSION == 2 ) 819 lbrac = 0; first = 0;
821 if ( WriteTerm(expr,&lbrac,first,PRINTON,0) ) {
827 AddObject(d,basenumber,(
char *)es,(
char *)(AO.DollarOutBuffer));
829 AO.CurrentDictionary = dict;
837 for ( i = 0; i < T->
totind; i++ ) {
838 #if ( TABLEEXTENSION == 2 ) 843 sum = i * ( T->
numind + TABLEEXTENSION );
845 for ( j = 0; j < T->
numind; j++, sum++ ) {
846 if ( j > 0 ) *t++ =
',';
849 if ( ( t - elementstring ) >= ELEMENTSIZE ) {
850 MesPrint(
"&Table element specification takes more than %ld characters and cannot be handled",
855 if ( ExistsObject(d,basenumber,(
char *)elementstring) ) {
continue; }
862 AO.DollarInOutBuffer = 1;
864 ss = AO.DollarOutBuffer;
867 #if ( TABLEEXTENSION == 2 ) 872 lbrac = 0; first = 0;
874 if ( WriteTerm(expr,&lbrac,first,PRINTON,0) ) {
880 AddObject(d,basenumber,(
char *)elementstring,(
char *)(AO.DollarOutBuffer));
883 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
885 if ( WriteIniInfo(d) )
goto tableabort;
886 M_free(AO.DollarOutBuffer,
"DollarOutBuffer");
887 AO.DollarOutBuffer = 0;
888 AO.DollarOutSizeBuffer = 0;
891 M_free(AO.DollarOutBuffer,
"DollarOutBuffer");
892 AO.DollarOutBuffer = 0;
893 AO.DollarOutSizeBuffer = 0;
906 int CoTBenter(UBYTE *s)
910 UBYTE *arguments, *rhs, *buffer, *t, *u, c, *tablename;
912 int i, j, error = 0, error1 = 0, printall = 0;
915 int dict = AO.CurrentDictionary;
916 AO.CurrentDictionary = 0;
917 if ( ( d = FindTB(tablebasename) ) == 0 ) {
918 MesPrint(
"&No open tablebase with the name %s",tablebasename);
922 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
923 if ( *s ==
'!' ) { printall = 1; s++; }
924 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
928 if ( ( s = SkipAName(s) ) == 0 ) { error = 1;
goto Endofall; }
930 if ( ( GetVar(tablename,&type,&funnum,CFUNCTION,NOAUTO) == NAMENOTFOUND )
931 || ( T = functions[funnum].tabl ) == 0 ) {
932 MesPrint(
"&%s should be a previously declared table",tablename);
935 else if ( T->
sparse == 0 ) {
936 MesPrint(
"&%s should be a sparse table",tablename);
939 else { basenumber = GetTableName(d,(
char *)tablename); }
940 if ( T->
spare == 0 ) { SpareTable(T); }
941 if ( basenumber > 0 ) {
942 for ( i = 0; i < d->info.numberofindexblocks; i++ ) {
943 for ( j = 0; j < NUMOBJECTS; j++ ) {
944 if ( basenumber != d->iblocks[i]->objects[j].tablenumber )
946 arguments = (UBYTE *)(d->iblocks[i]->objects[j].element);
947 rhs = (UBYTE *)ReadObject(d,basenumber,(
char *)arguments);
950 MesPrint(
"%s(%s) = %s",tablename,arguments,rhs);
953 MesPrint(
"%s(%s) = 0",tablename,arguments);
957 u = rhs;
while ( *u ) u++;
959 u = arguments;
while ( *u ) u++;
961 u = tablename;
while ( *u ) u++;
964 buffer = (UBYTE *)Malloc1(size,
"TableBase copy");
965 t = tablename; u = buffer;
966 while ( *t ) *u++ = *t++;
969 while ( *t ) *u++ = *t++;
970 *u++ =
')'; *u++ =
'=';
972 while ( *t ) *u++ = *t++;
973 if ( t == rhs ) *u++ =
'0';
975 M_free(rhs,
"rhs in TBenter");
977 error1 = CoFill(buffer);
979 if ( error1 < 0 )
goto Endofall;
980 if ( error1 != 0 ) error = error1;
981 M_free(buffer,
"TableBase copy");
987 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
991 s = (UBYTE *)(d->tablenames); basenumber = 0;
994 tablename = s;
while ( *s ) s++; s++;
997 if ( ( GetVar(tablename,&type,&funnum,CFUNCTION,NOAUTO) == NAMENOTFOUND )
998 || ( T = functions[funnum].tabl ) == 0 ) {
999 MesPrint(
"&%s should be a previously declared table",tablename);
1001 else if ( T->
sparse == 0 ) {
1002 MesPrint(
"&%s should be a sparse table",tablename);
1004 if ( T->
spare == 0 ) { SpareTable(T); }
1005 for ( i = 0; i < d->info.numberofindexblocks; i++ ) {
1006 for ( j = 0; j < NUMOBJECTS; j++ ) {
1007 if ( d->iblocks[i]->objects[j].tablenumber == basenumber ) {
1008 arguments = (UBYTE *)(d->iblocks[i]->objects[j].element);
1009 rhs = (UBYTE *)ReadObject(d,basenumber,(
char *)arguments);
1012 MesPrint(
"%s%s = %s",tablename,arguments,rhs);
1015 MesPrint(
"%s%s = 0",tablename,arguments);
1019 u = rhs;
while ( *u ) u++;
1021 u = arguments;
while ( *u ) u++;
1022 size += u-arguments;
1023 u = tablename;
while ( *u ) u++;
1024 size += u-tablename;
1026 buffer = (UBYTE *)Malloc1(size,
"TableBase copy");
1027 t = tablename; u = buffer;
1028 while ( *t ) *u++ = *t++;
1031 while ( *t ) *u++ = *t++;
1032 *u++ =
')'; *u++ =
'=';
1034 while ( *t ) *u++ = *t++;
1035 if ( t == rhs ) *u++ =
'0';
1037 M_free(rhs,
"rhs in TBenter");
1039 error1 = CoFill(buffer);
1041 if ( error1 < 0 )
goto Endofall;
1042 if ( error1 != 0 ) error = error1;
1043 M_free(buffer,
"TableBase copy");
1051 AO.CurrentDictionary = dict;
1065 int CoTestUse(UBYTE *s)
1068 UBYTE *tablename, c;
1069 WORD type, funnum, *w;
1073 *w++ = TYPETESTUSE; *w++ = 2;
1076 if ( ( s = SkipAName(s) ) == 0 )
return(1);
1078 if ( ( GetVar(tablename,&type,&funnum,CFUNCTION,NOAUTO) == NAMENOTFOUND )
1079 || ( T = functions[funnum].tabl ) == 0 ) {
1080 MesPrint(
"&%s should be a previously declared table",tablename);
1083 else if ( T->
sparse == 0 ) {
1084 MesPrint(
"&%s should be a sparse table",tablename);
1087 *w++ = funnum + FUNCTION;
1089 while ( *s ==
' ' || *s ==
',' || *s ==
'\t' ) s++;
1091 AT.WorkPointer[1] = w - AT.WorkPointer;
1097 AddNtoL(AT.WorkPointer[1],AT.WorkPointer);
1110 int CheckTableDeclarations(
DBASE *d)
1113 UBYTE *s, *ss, *t, *command = 0;
1114 int k, error = 0, error1, i;
1116 LONG commandsize = 0;
1118 s = (UBYTE *)(d->tablenames);
1119 for ( k = 0; k < d->topnumber; k++ ) {
1120 if ( GetVar(s,&type,&funnum,ANYTYPE,NOAUTO) == NAMENOTFOUND ) {
1124 ss = s; i = 0;
while ( *ss ) { ss++; i++; }
1125 ss++;
while ( *ss ) { ss++; i++; }
1126 if ( commandsize == 0 ) {
1127 commandsize = i + 15;
1128 if ( commandsize < 100 ) commandsize = 100;
1130 if ( (i+11) > commandsize ) {
1131 if ( command ) { M_free(command,
"table command"); command = 0; }
1134 if ( command == 0 ) {
1135 command = (UBYTE *)Malloc1(commandsize,
"table command");
1137 t = command; ss = tablebase;
while ( *ss ) *t++ = *ss++;
1138 *t++ =
',';
while ( *s ) *t++ = *s++;
1139 s++;
while ( *s ) *t++ = *s++;
1140 *t++ =
')'; *t = 0; s++;
1141 error1 = DoTable(command,1);
1142 if ( error1 ) error = error1;
1144 else if ( ( type != CFUNCTION )
1145 || ( ( T = functions[funnum].tabl ) == 0 )
1146 || ( T->
sparse == 0 ) ) {
1147 MesPrint(
"&%s has been declared previously, but not as a sparse table.",s);
1163 MesPrint(
"&Declaration of table %s in %s different from previous declaration",ss,d->name);
1170 if ( command ) { M_free(command,
"table command"); }
1184 int CoTBload(UBYTE *ss)
1187 UBYTE *s, *name, *t, *r, *command, *arguments, *tail;
1189 int num, cs, es, ns, ts, i, j, error = 0, error1;
1190 if ( ( d = FindTB(tablebasename) ) == 0 ) {
1191 MesPrint(
"&No open tablebase with the name %s",tablebasename);
1195 command = (UBYTE *)Malloc1(commandsize,
"Fill command");
1198 while ( *ss ==
',' || *ss ==
' ' || *ss ==
'\t' ) ss++;
1200 name = ss; ss = SkipAName(ss); *ss = 0;
1201 s = (UBYTE *)(d->tablenames);
1205 if ( StrCmp(s,name) ) {
1213 name = s;
while ( *s ) s++; ns = s-name; s++;
1214 tail = s;
while ( *s ) s++; ts = s-tail; s++;
1215 tail++;
while ( FG.cTable[*tail] == 1 ) tail++;
1219 for ( i = 0; i < d->info.numberofindexblocks; i++ ) {
1220 for ( j = 0; j < NUMOBJECTS; j++ ) {
1221 if ( d->iblocks[i]->objects[j].tablenumber == num ) {
1222 t = arguments = (UBYTE *)(d->iblocks[i]->objects[j].element);
1225 cs = 2*es + 2*ns + ts + 10;
1226 if ( cs > commandsize ) {
1228 if ( command ) M_free(command,
"Fill command");
1229 command = (UBYTE *)Malloc1(commandsize,
"Fill command");
1231 r = command; t = name;
while ( *t ) *r++ = *t++;
1232 *r++ =
'('; t = arguments;
while ( *t ) *r++ = *t++;
1233 *r++ =
')'; *r++ =
'='; *r++ =
't'; *r++ =
'b'; *r++ =
'l';
1234 *r++ =
'_'; *r++ =
'('; t = name;
while ( *t ) *r++ = *t++;
1235 *r++ =
','; t = arguments;
while ( *t ) *r++ = *t++;
1236 t = tail;
while ( *t ) {
1237 if ( *t ==
'?' && r[-1] !=
',' ) {
1239 if ( FG.cTable[*t] == 0 || *t ==
'$' || *t ==
'[' ) {
1245 else if ( *t ==
'{' ) {
1248 else if ( *t ) { *r++ = *t++;
continue; }
1256 AC.vetotablebasefill = 1;
1257 error1 = CoFill(command);
1258 AC.vetotablebasefill = 0;
1259 if ( error1 < 0 )
goto finishup;
1260 if ( error1 != 0 ) error = error1;
1266 while ( *ss ==
',' || *ss ==
' ' || *ss ==
'\t' ) ss++;
1270 s = (UBYTE *)(d->tablenames);
1274 name = s;
while ( *s ) s++; ns = s-name; s++;
1275 tail = s;
while ( *s ) s++; ts = s-tail; s++;
1276 tail++;
while ( FG.cTable[*tail] == 1 ) tail++;
1280 for ( i = 0; i < d->info.numberofindexblocks; i++ ) {
1281 for ( j = 0; j < NUMOBJECTS; j++ ) {
1282 if ( d->iblocks[i]->objects[j].tablenumber == num ) {
1283 t = arguments = (UBYTE *)(d->iblocks[i]->objects[j].element);
1286 cs = 2*es + 2*ns + ts + 10;
1287 if ( cs > commandsize ) {
1289 if ( command ) M_free(command,
"Fill command");
1290 command = (UBYTE *)Malloc1(commandsize,
"Fill command");
1292 r = command; t = name;
while ( *t ) *r++ = *t++;
1293 *r++ =
'('; t = arguments;
while ( *t ) *r++ = *t++;
1294 *r++ =
')'; *r++ =
'='; *r++ =
't'; *r++ =
'b'; *r++ =
'l';
1295 *r++ =
'_'; *r++ =
'('; t = name;
while ( *t ) *r++ = *t++;
1296 *r++ =
','; t = arguments;
while ( *t ) *r++ = *t++;
1297 t = tail;
while ( *t ) {
1298 if ( *t ==
'?' && r[-1] !=
',' ) {
1300 if ( FG.cTable[*t] == 0 || *t ==
'$' || *t ==
'[' ) {
1306 else if ( *t ==
'{' ) {
1309 else if ( *t ) { *r++ = *t++;
continue; }
1317 AC.vetotablebasefill = 1;
1318 error1 = CoFill(command);
1319 AC.vetotablebasefill = 0;
1320 if ( error1 < 0 )
goto finishup;
1321 if ( error1 != 0 ) error = error1;
1329 if ( command ) M_free(command,
"Fill command");
1346 WORD TestUse(WORD *term, WORD level)
1348 WORD *tstop, *t, *m, *tstart, tabnum;
1349 WORD *funs, numfuns, error = 0;
1352 CBUF *C = cbuf+AM.rbufnum;
1355 numfuns = C->
lhs[level][1] - 2;
1356 funs = C->
lhs[level] + 2;
1357 GETSTOP(term,tstop);
1359 while ( t < tstop ) {
1360 if ( *t != TABLESTUB ) { t += t[1];
continue; }
1364 if ( *m >= -FUNCTION )
continue;
1366 if ( ( T = functions[tabnum-FUNCTION].tabl ) == 0 )
continue;
1367 if ( T->
sparse == 0 )
continue;
1371 if ( numfuns > 0 ) {
1372 for ( i = 0; i < numfuns; i++ ) {
1373 if ( tabnum == funs[i] )
break;
1375 if ( i >= numfuns && numfuns > 0 )
continue;
1383 for ( i = 0; i < T->
numind; i++, m += 2 ) {
1384 if ( m >= t || *m != -SNUMBER )
break;
1386 if ( ( i == T->
numind ) &&
1387 ( ( isp = FindTableTree(T,tstart+FUNHEAD+1,2) ) >= 0 ) ) {
1393 MesPrint(
"TestUse: Encountered a table element inside tbl_ that does not correspond to a tablebase element");
1405 int CoTBaudit(UBYTE *s)
1409 int i, j, error = 0, num;
1411 if ( ( d = FindTB(tablebasename) ) == 0 ) {
1412 MesPrint(
"&No open tablebase with the name %s",tablebasename);
1415 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
1423 s = (UBYTE *)(d->tablenames); num = 0;
1426 name = s;
while ( *s ) s++; s++;
1427 tail = s;
while ( *s ) s++; s++;
1428 MesPrint(
"Table,sparse,%s%s)",name,tail);
1429 for ( i = 0; i < d->info.numberofindexblocks; i++ ) {
1430 for ( j = 0; j < NUMOBJECTS; j++ ) {
1431 if ( d->iblocks[i]->objects[j].tablenumber == num ) {
1432 MesPrint(
" %s(%s)",name,d->iblocks[i]->objects[j].element);
1445 int CoTBon(UBYTE *s)
1450 if ( ( d = FindTB(tablebasename) ) == 0 ) {
1451 MesPrint(
"&No open tablebase with the name %s",tablebasename);
1454 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
1458 if ( StrICmp(s,(UBYTE *)(
"compress")) == 0 ) {
1459 d->mode &= ~NOCOMPRESS;
1462 MesPrint(
"&subkey %s not defined in TableBase On statement");
1466 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
1476 int CoTBoff(UBYTE *s)
1481 if ( ( d = FindTB(tablebasename) ) == 0 ) {
1482 MesPrint(
"&No open tablebase with the name %s",tablebasename);
1485 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
1489 if ( StrICmp(s,(UBYTE *)(
"compress")) == 0 ) {
1490 d->mode |= NOCOMPRESS;
1493 MesPrint(
"&subkey %s not defined in TableBase Off statement");
1497 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
1507 int CoTBcleanup(UBYTE *s)
1510 MesPrint(
"&TableBase Cleanup statement not yet implemented");
1519 int CoTBreplace(UBYTE *s)
1522 MesPrint(
"&TableBase Replace statement not yet implemented");
1534 int CoTBuse(UBYTE *s)
1539 UBYTE *arguments, *rhs, *buffer, *t, *u, c, *tablename, *p;
1541 int i, j, error = 0, error1 = 0, k;
1543 WORD type, funnum, mode, *w;
1544 if ( ( d = FindTB(tablebasename) ) == 0 ) {
1545 MesPrint(
"&No open tablebase with the name %s",tablebasename);
1548 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
1552 if ( ( s = SkipAName(s) ) == 0 )
return(1);
1554 if ( ( GetVar(tablename,&type,&funnum,CFUNCTION,NOAUTO) == NAMENOTFOUND )
1555 || ( T = functions[funnum].tabl ) == 0 ) {
1556 MesPrint(
"&%s should be a previously declared table",tablename);
1559 else if ( T->
sparse == 0 ) {
1560 MesPrint(
"&%s should be a sparse table",tablename);
1563 else { basenumber = GetTableName(d,(
char *)tablename); }
1565 if ( basenumber > 0 ) {
1566 for ( i = 0; i < d->info.numberofindexblocks; i++ ) {
1567 for ( j = 0; j < NUMOBJECTS; j++ ) {
1568 if ( d->iblocks[i]->objects[j].tablenumber != basenumber )
continue;
1569 arguments = p = (UBYTE *)(d->iblocks[i]->objects[j].element);
1574 for ( k = 0, w = AT.WorkPointer; k < T->numind; k++ ) {
1575 ParseSignedNumber(x,p);
1578 sum = FindTableTree(T,AT.WorkPointer,1);
1580 MesPrint(
"Table %s in tablebase %s has not been loaded properly" 1581 ,tablename,tablebasename);
1587 if ( ( mode & ELEMENTLOADED ) == ELEMENTLOADED ) {
1591 if ( ( mode & ELEMENTUSED ) == 0 )
continue;
1595 rhs = (UBYTE *)ReadijObject(d,i,j,(
char *)arguments);
1597 u = rhs;
while ( *u ) u++;
1599 u = arguments;
while ( *u ) u++;
1600 size += u-arguments;
1601 u = tablename;
while ( *u ) u++;
1602 size += u-tablename;
1604 buffer = (UBYTE *)Malloc1(size,
"TableBase copy");
1605 t = tablename; u = buffer;
1606 while ( *t ) *u++ = *t++;
1609 while ( *t ) *u++ = *t++;
1610 *u++ =
')'; *u++ =
'=';
1612 while ( *t ) *u++ = *t++;
1613 if ( t == rhs ) { *u++ =
'0'; }
1615 M_free(rhs,
"rhs in TBuse xxx");
1617 error1 = CoFill(buffer);
1619 if ( error1 < 0 ) {
return(error); }
1620 if ( error1 != 0 ) error = error1;
1621 M_free(buffer,
"TableBase copy");
1629 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
1633 s = (UBYTE *)(d->tablenames); basenumber = 0;
1641 if ( ( GetVar(tablename,&type,&funnum,CFUNCTION,NOAUTO) == NAMENOTFOUND )
1642 || ( T = functions[funnum].tabl ) == 0 ) {
1643 MesPrint(
"&%s should be a previously declared table",tablename);
1645 else if ( T->
sparse == 0 ) {
1646 MesPrint(
"&%s should be a sparse table",tablename);
1649 MesPrint(
"In table %s we have a problem with stubb orders in CoTBuse",tablename);
1653 for ( i = 0; i < d->info.numberofindexblocks; i++ ) {
1654 for ( j = 0; j < NUMOBJECTS; j++ ) {
1655 if ( d->iblocks[i]->objects[j].tablenumber == basenumber ) {
1656 arguments = p = (UBYTE *)(d->iblocks[i]->objects[j].element);
1661 for ( k = 0, w = AT.WorkPointer; k < T->numind; k++ ) {
1662 ParseSignedNumber(x,p);
1665 sum = FindTableTree(T,AT.WorkPointer,1);
1667 MesPrint(
"Table %s in tablebase %s has not been loaded properly" 1668 ,tablename,tablebasename);
1674 if ( ( mode & ELEMENTLOADED ) == ELEMENTLOADED ) {
1678 if ( ( mode & ELEMENTUSED ) == 0 )
continue;
1682 rhs = (UBYTE *)ReadijObject(d,i,j,(
char *)arguments);
1684 u = rhs;
while ( *u ) u++;
1686 u = arguments;
while ( *u ) u++;
1687 size += u-arguments;
1688 u = tablename;
while ( *u ) u++;
1689 size += u-tablename;
1691 buffer = (UBYTE *)Malloc1(size,
"TableBase copy");
1692 t = tablename; u = buffer;
1693 while ( *t ) *u++ = *t++;
1696 while ( *t ) *u++ = *t++;
1697 *u++ =
')'; *u++ =
'=';
1700 while ( *t ) *u++ = *t++;
1701 if ( t == rhs ) { *u++ =
'0'; }
1703 M_free(rhs,
"rhs in TBuse");
1705 error1 = CoFill(buffer);
1707 if ( error1 < 0 ) {
return(error); }
1708 if ( error1 != 0 ) error = error1;
1709 M_free(buffer,
"TableBase copy");
1728 int CoApply(UBYTE *s)
1731 UBYTE *tablename, c;
1732 WORD type, funnum, *w;
1734 LONG maxtogo = MAXPOSITIVE;
1737 if ( FG.cTable[*s] == 1 ) {
1739 while ( FG.cTable[*s] == 1 ) {
1740 maxtogo = maxtogo*10 + (*s-
'0');
1743 while ( *s ==
',' ) s++;
1744 if ( maxtogo > MAXPOSITIVE || maxtogo < 0 ) maxtogo = MAXPOSITIVE;
1746 *w++ = TYPEAPPLY; *w++ = 3; *w++ = maxtogo;
1749 if ( ( s = SkipAName(s) ) == 0 )
return(1);
1751 if ( ( GetVar(tablename,&type,&funnum,CFUNCTION,NOAUTO) == NAMENOTFOUND )
1752 || ( T = functions[funnum].tabl ) == 0 ) {
1753 MesPrint(
"&%s should be a previously declared table",tablename);
1756 else if ( T->
sparse == 0 ) {
1757 MesPrint(
"&%s should be a sparse table",tablename);
1760 *w++ = funnum + FUNCTION;
1762 while ( *s ==
' ' || *s ==
',' || *s ==
'\t' ) s++;
1764 AT.WorkPointer[1] = w - AT.WorkPointer;
1770 AddNtoL(AT.WorkPointer[1],AT.WorkPointer);
1784 "The TableBase statement is used as follows:" 1785 ,
"TableBase \"file.tbl\" keyword subkey(s)" 1786 ,
" in which we have" 1787 ,
"Keyword Subkey(s) Action" 1788 ,
"open Opens file.tbl for R/W" 1789 ,
"create Creates file.tbl for R/W. Old contents are lost" 1790 ,
"load Loads all stubs of all tables" 1791 ,
"load tablename(s) Loads all stubs the tables mentioned" 1792 ,
"enter Loads all stubs and rhs of all tables" 1793 ,
"enter tablename(s) Loads all stubs and rhs of the tables mentioned" 1794 ,
"audit Prints list of contents" 1798 ,
"addto tablename adds all elements if not yet there" 1799 ,
"addto tableelement adds element if not yet there" 1802 ,
"on compress elements are stored in gzip format (default)" 1803 ,
"off compress elements are stored in uncompressed format" 1804 ,
"use compiles all needed elements" 1805 ,
"use tablename(s) compiles all needed elements of these tables" 1807 ,
"Related commands are:" 1808 ,
"testuse marks which tbl_ elements occur for all tables" 1809 ,
"testuse tablename(s) marks which tbl_ elements occur for given tables" 1810 ,
"apply replaces tbl_ if rhs available" 1811 ,
"apply tablename(s) replaces tbl_ for given tables if rhs available" 1815 int CoTBhelp(UBYTE *s)
1817 int i, ii =
sizeof(helptb)/
sizeof(
char *);
1819 for ( i = 0; i < ii; i++ ) MesPrint(
"%s",helptb[i]);
1831 VOID ReWorkT(WORD *term, WORD *funs, WORD numfuns)
1833 WORD *tstop, *tend, *m, *t, *tt, *mm, *mmm, *r, *rr;
1835 tend = term + *term; tstop = tend - ABS(tend[-1]);
1837 while ( t < tstop ) {
1838 if ( *t == TABLESTUB ) {
1839 for ( i = 0; i < numfuns; i++ ) {
1840 if ( -t[FUNHEAD] == funs[i] )
break;
1842 if ( numfuns == 0 || i < numfuns ) {
1844 *m++ = -t[FUNHEAD]; *m++ = i; t += 2; i -= FUNHEAD;
1845 if ( m < t ) {
for ( j = 0; j < FUNHEAD-2; j++ ) *m++ = *t++; }
1846 else { m += FUNHEAD-2; t += FUNHEAD-2; }
1848 while ( i-- > 0 ) { *m++ = *t++; }
1851 while ( tt < tend ) *mm++ = *tt++;
1853 tend = term + *term; tstop = tend - ABS(tend[-1]);
1859 else if ( *t >= FUNCTION ) {
1862 for ( j = 0; j < FUNHEAD; j++ ) {
1863 if ( m == t ) { m++; t++; }
1867 if ( *t <= -FUNCTION ) {
1868 if ( m == t ) { m++; t++; }
1871 else if ( *t < 0 ) {
1872 if ( m == t ) { m += 2; t += 2; }
1873 else { *m++ = *t++; *m++ = *t++; }
1876 rr = t + *t; mmm = m;
1877 for ( j = 0; j < ARGHEAD; j++ ) {
1878 if ( m == t ) { m++; t++; }
1883 ReWorkT(t,funs,numfuns);
1885 if ( m == t ) { m += j; t += j; }
1886 else {
while ( j-- >= 0 ) *m++ = *t++; }
1897 if ( m < t ) {
while ( j-- >= 0 ) *m++ = *t++; }
1898 else { m += j; t += j; }
1902 while ( t < tend ) *m++ = *t++;
1912 WORD Apply(WORD *term, WORD level)
1914 WORD *funs, numfuns;
1917 CBUF *C = cbuf+AM.rbufnum;
1921 numfuns = C->
lhs[level][1] - 2;
1922 funs = C->
lhs[level] + 2;
1923 if ( numfuns > 0 ) {
1924 for ( i = MAXBUILTINFUNCTION-FUNCTION; i < AC.FunctionList.num; i++ ) {
1925 if ( ( T = functions[i].tabl ) != 0 ) {
1926 for ( j = 0; j < numfuns; j++ ) {
1927 if ( i == (funs[j]-FUNCTION) && T->
spare ) {
1928 FlipTable(&(functions[i]),0);
1936 for ( i = MAXBUILTINFUNCTION-FUNCTION; i < AC.FunctionList.num; i++ ) {
1937 if ( ( T = functions[i].tabl ) != 0 ) {
1938 if ( T->
spare ) FlipTable(&(functions[i]),0);
1948 ReWorkT(term,funs,numfuns);
1971 int ApplyExec(WORD *term,
int maxtogo, WORD level)
1974 WORD rhsnumber, *Tpattern, *funs, numfuns, funnum;
1975 WORD ii, *t, *t1, *w, *p, *m, *m1, *u, *r, tbufnum, csize, wilds;
1977 int i, j, isp, stilltogo;
1987 AT.NestPoin->termsize = t;
1988 if ( AT.NestPoin == AT.Nest ) AN.EndNest = t + *t;
1994 if ( *t < FUNCTION ) { t += t[1];
continue; }
1995 if ( functions[*t-FUNCTION].spec > 0 ) { t += t[1];
continue; }
1996 AT.NestPoin->funsize = t;
2000 if ( *t < 0 ) { NEXTARG(t);
continue; }
2001 AT.NestPoin->argsize = t1 = t;
2018 stilltogo = ApplyExec(t,maxtogo,level);
2019 if ( stilltogo != maxtogo ) {
2020 if ( stilltogo <= 0 ) {
2024 maxtogo = stilltogo;
2026 m = term + *term - csize;
2036 C = cbuf+AM.rbufnum;
2039 if ( *t != TABLESTUB ) { t += t[1];
continue; }
2040 funnum = -t[FUNHEAD];
2041 if ( ( funnum < FUNCTION )
2042 || ( funnum >= FUNCTION+WILDOFFSET )
2043 || ( ( T = functions[funnum-FUNCTION].tabl ) == 0 )
2045 || ( T->
spare == 0 ) ) { t += t[1];
continue; }
2046 numfuns = C->
lhs[level][1] - 3;
2047 funs = C->
lhs[level] + 3;
2048 if ( numfuns > 0 ) {
2049 for ( i = 0; i < numfuns; i++ ) {
2050 if ( funs[i] == funnum )
break;
2052 if ( i >= numfuns ) { t += t[1];
continue; }
2055 AT.NestPoin->funsize = t + 1;
2065 Tpattern = T->
pattern[identity];
2069 p = Tpattern+FUNHEAD+1;
2070 for ( i = 0; i < T->
numind; i++, t += 2 ) {
2071 if ( *t != -SNUMBER )
break;
2073 if ( i < T->numind ) { t = r;
continue; }
2074 isp = FindTableTree(T,t1+FUNHEAD+1,2);
2075 if ( isp < 0 ) { t = r;
continue; }
2077 #if ( TABLEEXTENSION == 2 ) 2084 while ( --ii >= 0 ) {
2085 *p = *t; t += 2; p += 2;
2097 AN.WildValue = AN.FullProto + SUBEXPSIZE;
2098 AN.WildStop = AN.FullProto+AN.FullProto[1];
2101 AN.RepFunList = AN.EndNest;
2102 AT.WorkPointer = (WORD *)(((UBYTE *)(AN.EndNest)) + AM.MaxTer/2);
2107 if ( AT.WorkPointer + t1[1] >= AT.WorkTop ) { MesWork(); }
2110 *w++ = -t1[FUNHEAD];
2112 for ( i = 2; i < FUNHEAD; i++ ) *w++ = t1[i];
2114 while ( t < r ) *w++ = *t++;
2117 if ( MatchFunction(BHEAD Tpattern,t,&wilds) > 0 ) {
2138 while ( r < m1 ) *t++ = *r++;
2142 while ( NN > AT.Nest ) {
2144 NN->termsize[0] += j;
2145 NN->funsize[1] += j;
2146 NN->argsize[0] += j;
2147 NN->funsize[2] |= DIRTYFLAG;
2148 NN->argsize[1] |= DIRTYFLAG;
2155 while ( t >= r ) { t[j] = *t; t--; }
2161 while ( NN > AT.Nest ) {
2163 NN->termsize[0] += j;
2164 NN->funsize[1] += j;
2165 NN->argsize[0] += j;
2166 NN->funsize[2] |= DIRTYFLAG;
2167 NN->argsize[1] |= DIRTYFLAG;
2176 if ( maxtogo <= 0 )
return(maxtogo);
2188 WORD ApplyReset(WORD level)
2190 WORD *funs, numfuns;
2193 CBUF *C = cbuf+AM.rbufnum;
2195 numfuns = C->
lhs[level][1] - 2;
2196 funs = C->
lhs[level] + 2;
2197 if ( numfuns > 0 ) {
2198 for ( i = MAXBUILTINFUNCTION-FUNCTION; i < AC.FunctionList.num; i++ ) {
2199 if ( ( T = functions[i].tabl ) != 0 ) {
2200 for ( j = 0; j < numfuns; j++ ) {
2201 if ( i == (funs[j]-FUNCTION) && T->
spare ) {
2202 FlipTable(&(functions[i]),1);
2210 for ( i = MAXBUILTINFUNCTION-FUNCTION; i < AC.FunctionList.num; i++ ) {
2211 if ( ( T = functions[i].tabl ) != 0 ) {
2212 if ( T->
spare ) FlipTable(&(functions[i]),1);
2229 for ( i = MAXBUILTINFUNCTION-FUNCTION; i < AC.FunctionList.num; i++ ) {
2230 if ( ( T = functions[i].tabl ) != 0 && T->
spare && T->
mode == 0 ) {
2231 functions[i].tabl = T->
spare;
2255 for ( i = NumTableBases - 1; i >= 0; i-- ) {
WORD Generator(PHEAD WORD *, WORD)