65static UBYTE *tablebase = (UBYTE *)
"tablebase";
72void ClearTableTree(
TABLES T)
103int InsTableTree(
TABLES T, WORD *tp)
106 WORD *v1, *v2, *v3, xstop;
121 if ( q->
right == -1 ) {
134 else xstop = *tp + 1;
138 v2 = tp; v3 = tp + xstop;
139 while ( *v1 == *v2 && v2 < v3 ) { v1++; v2++; }
140 if ( v2 >= v3 )
return(-p->value);
143 if ( iq >= 0 ) { ip = iq; }
153 if ( p->blnce == 0 )
return(T->
numtree);
157 else if ( *v1 < *v2 ) {
159 if ( iq >= 0 ) { ip = iq; }
169 if ( p->blnce == 0 )
return(T->
numtree);
174 MesPrint(
"Serious problems in InsTableTree!\n");
185 if ( q->
blnce == 0 )
break;
186 if ( q->
blnce == -2 ) {
187 if ( p->blnce == -1 ) {
192 if ( boomlijst[p->parent].left == iq ) boomlijst[p->parent].
left = ip;
193 else boomlijst[p->parent].
right = ip;
195 q->
blnce = p->blnce = 0;
203 if ( p->right >= 0 ) boomlijst[p->right].
parent = ip;
208 if ( boomlijst[s->
parent].left == iq )
217 else if ( q->
blnce == 2 ) {
218 if ( p->blnce == 1 ) {
223 if ( boomlijst[p->parent].left == iq ) boomlijst[p->parent].
left = ip;
224 else boomlijst[p->parent].
right = ip;
226 q->
blnce = p->blnce = 0;
234 if ( p->left >= 0 ) boomlijst[p->left].
parent = ip;
266void RedoTableTree(
TABLES T,
int newsize)
273 tp += ABS(T->
numind)+TABLEEXTENSION;
291int FindTableTree(
TABLES T, WORD *tp,
int inc)
294 WORD *v1, *v2, *v3, xstop;
296 if ( q->
right == -1 )
return(-1);
298 if ( inc > 1 ) tp += inc-1;
301 if ( *tp <= 0 )
return(-1);
307 v2 = tp; v3 = v1 + xstop;
308 while ( *v1 == *v2 && v1 < v3 ) { v1++; v2 += inc; }
315 if ( iq >= 0 ) { ip = iq; }
318 else if ( *v1 < *v2 ) {
320 if ( iq >= 0 ) { ip = iq; }
324 MesPrint(
"Serious problems in FindTableTree\n");
334int DoTableExpansion(WORD *term, WORD level)
337 WORD *t, *tstop, *stopper, *termout, *m, *mm, *tp, *r, xx;
338 WORD numsubexp, numbuf;
341 AN.TeInFun = AR.TePos = 0;
342 tstop = term + *term;
343 stopper = tstop - ABS(tstop[-1]);
345 while ( t < stopper ) {
346 if ( *t != TABLEFUNCTION ) { t += t[1];
continue; }
347 if ( t[FUNHEAD] > -FUNCTION ) { t += t[1];
continue; }
348 T = functions[-t[FUNHEAD]-FUNCTION].tabl;
349 if ( T == 0 ) { t += t[1];
continue; }
351 if ( t[1] == FUNHEAD+2 && t[FUNHEAD+1] <= -FUNCTION )
break;
352 if ( t[1] < FUNHEAD+1+2*ABS(T->
numind) ) { t += t[1];
continue; }
353 for ( i = 0; i < ABS(T->
numind); i++ ) {
354 if ( t[FUNHEAD+1+2*i] != -SYMBOL )
break;
356 if ( i >= ABS(T->
numind) )
break;
359 if ( t >= stopper ) {
360 MesPrint(
"Internal error: Missing table_ function");
366 termout = AT.WorkPointer;
368 for ( i = 0; i < T->
totind; i++ ) {
372 m = termout + 1; mm = term + 1;
373 while ( mm < t ) *m++ = *mm++;
375 if ( t[1] == FUNHEAD+2 && t[FUNHEAD+1] <= -FUNCTION ) {
376 *m++ = -t[FUNHEAD+1];
381 for ( j = 2; j < FUNHEAD; j++ ) *m++ = 0;
382 for ( j = 0; j < xx; j++ ) {
383 *m++ = -SNUMBER; *m++ = *tp++;
387 *m++ = FUNHEAD+T->
numind*2;
388 for ( j = 2; j < FUNHEAD; j++ ) *m++ = 0;
389 for ( j = 0; j < T->
numind; j++ ) {
390 *m++ = -SNUMBER; *m++ = *tp++;
394 else if ( T->
numind < 0 ) {
397 *m++ = SYMBOL; *m++ = 2+xx*2; mm = t + FUNHEAD+1;
398 for ( j = 0; j < xx; j++, mm += 2, tp++ ) {
399 if ( *tp != 0 ) { *m++ = mm[1]; *m++ = *tp; }
402 if ( r[1] == 2 ) m = r;
405 *m++ = SYMBOL; *m++ = 2+T->
numind*2; mm = t + FUNHEAD+1;
407 for ( j = 0; j < T->
numind; j++, mm += 2, tp++ ) {
408 if ( *tp != 0 ) { *m++ = mm[1]; *m++ = *tp; }
411 if ( r[1] == 2 ) m = r;
432 numsubexp = tp[0]; numbuf = tp[1];
439 for ( j = 0; j < tp[1]; j++ ) *m++ = tp[j];
440 r[2] = numsubexp; r[4] = numbuf;
466 while ( mm < tstop ) *m++ = *mm++;
467 *termout = m - termout;
470 MesCall(
"DoTableExpand");
473 AT.WorkPointer = termout;
477 for ( i = 0; i < T->
totind; i++ ) {
478#if TABLEEXTENSION == 2
483 m = termout + 1; mm = term + 1;
484 while ( mm < t ) *m++ = *mm++;
486 if ( t[1] == FUNHEAD+2 && t[FUNHEAD+1] <= -FUNCTION ) {
487 *m++ = -t[FUNHEAD+1];
488 *m++ = FUNHEAD+T->
numind*2;
489 for ( j = 2; j < FUNHEAD; j++ ) *m++ = 0;
491 for ( j = 0; j < T->
numind; j++ ) {
498 *m++ = -SNUMBER; *m++ = num;
502 *m++ = SYMBOL; *m++ = 2+T->
numind*2; mm = t + FUNHEAD+1;
503 for ( j = 0; j < T->
numind; j++, mm += 2 ) {
510 if ( num != 0 ) { *m++ = mm[1]; *m++ = num; }
513 if ( r[1] == 2 ) m = r;
535 for ( j = 2; j < FUNHEAD; j++ ) *m++ = t[j];
536 for ( j = 0; j < T->
numind; j++ ) {
543 *m++ = -SNUMBER; *m++ = num;
545 tp = t + FUNHEAD + 1 + 2*T->
numind;
547 while ( tp < mm ) *m++ = *tp++;
552 while ( mm < tstop ) *m++ = *mm++;
553 *termout = m - termout;
556 MesCall(
"DoTableExpand");
611 {
"addto", (TFUN)CoTBaddto, 0, PARTEST}
612 ,{
"audit", (TFUN)CoTBaudit, 0, PARTEST}
613 ,{
"cleanup", (TFUN)CoTBcleanup, 0, PARTEST}
614 ,{
"create", (TFUN)CoTBcreate, 0, PARTEST}
615 ,{
"enter", (TFUN)CoTBenter, 0, PARTEST}
616 ,{
"help", (TFUN)CoTBhelp, 0, PARTEST}
617 ,{
"load", (TFUN)CoTBload, 0, PARTEST}
618 ,{
"off", (TFUN)CoTBoff, 0, PARTEST}
619 ,{
"on", (TFUN)CoTBon, 0, PARTEST}
620 ,{
"open", (TFUN)CoTBopen, 0, PARTEST}
621 ,{
"replace", (TFUN)CoTBreplace, 0, PARTEST}
622 ,{
"use", (TFUN)CoTBuse, 0, PARTEST}
625static UBYTE *tablebasename = 0;
627int CoTableBase(UBYTE *s)
629 UBYTE *option, c, *t;
630 int i,optlistsize =
sizeof(tboptions)/
sizeof(
KEYWORD), error = 0;
631 while ( *s ==
' ' ) s++;
633 if ( ( tolower(*s) ==
'h' ) && ( tolower(s[1]) ==
'e' )
634 && ( tolower(s[2]) ==
'l' ) && ( tolower(s[3]) ==
'p' )
635 && ( FG.cTable[s[4]] > 1 ) ) {
640 MesPrint(
"&Proper syntax: TableBase \"filename\" options");
643 s++; tablebasename = s;
644 while ( *s && *s !=
'"' ) s++;
645 if ( *s !=
'"' )
goto proper;
647 while ( *s ==
' ' || *s ==
'\t' || *s ==
',' ) s++;
649 while ( FG.cTable[*s] == 0 ) s++;
651 for ( i = 0; i < optlistsize; i++ ) {
652 if ( StrICmp(option,(UBYTE *)(tboptions[i].name)) == 0 ) {
654 while ( *s ==
',' ) s++;
655 error = (tboptions[i].func)(s);
660 MesPrint(
"&Unrecognized option %s in TableBase statement",option);
675 if ( ( TT = T->
spare ) == 0 ) {
676 MesPrint(
"Error: trying to change mode on a table that has no tablebase");
679 if ( TT->
mode == type ) f->
tabl = TT;
720 T->
flags = (WORD *)Malloc1(ABS(T->
numind)*
sizeof(WORD),
"table flags");
734DBASE *FindTB(UBYTE *name)
738 for ( i = 0; i < NumTableBases; i++ ) {
740 if ( d->name && ( StrCmp(name,(UBYTE *)(d->name)) == 0 ) ) {
return(d); }
756int CoTBcreate(UBYTE *s)
759 if ( FindTB(tablebasename) != 0 ) {
760 MesPrint(
"&There is already an open TableBase with the name %s",tablebasename);
763 NewDbase((
char *)tablebasename,0);
772int CoTBopen(UBYTE *s)
780 if ( ConsumeOption(&s,
"readonly") != 0 ) {
783 MesPrint(
"&Invalid option for TableBase open: %s, ignoring", s);
787 if ( ( d = FindTB(tablebasename) ) != 0 ) {
788 MesPrint(
"&There is already an open TableBase with the name %s",tablebasename);
791 d = GetDbase((
char *)tablebasename, rw);
792 if ( CheckTableDeclarations(d) )
return(-1);
801int CoTBaddto(UBYTE *s)
805 UBYTE *tablename, c, *t, elementstring[ELEMENTSIZE+20], *ss, *es;
806 WORD type, funnum, lbrac, first, num, *expr, *w;
810 int i, j, error = 0, sum;
811 if ( ( d = FindTB(tablebasename) ) == 0 ) {
812 MesPrint(
"&No open tablebase with the name %s",tablebasename);
816 if ( ( d->rwmode ) == 0 ) {
817 MesPrint(
"&Tablebase with the name %s opened in read only mode",tablebasename);
820 AO.DollarOutSizeBuffer = 32;
821 AO.DollarOutBuffer = (UBYTE *)Malloc1(AO.DollarOutSizeBuffer,
826 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
829 if ( ( s =
SkipAName(s) ) == 0 )
goto tableabort;
831 if ( ( GetVar(tablename,&type,&funnum,CFUNCTION,NOAUTO) == NAMENOTFOUND )
832 || ( T = functions[funnum].tabl ) == 0 ) {
833 MesPrint(
"&%s should be a previously declared table",tablename);
834 *s = c;
goto tableabort;
837 MesPrint(
"&%s should be a sparse table",tablename);
838 *s = c;
goto tableabort;
840 basenumber = AddTableName(d,(
char *)tablename,T);
842 if ( basenumber < 0 ) basenumber = -basenumber;
843 else if ( basenumber == 0 ) { *s = c;
goto tableabort; }
847 for ( i = 0, w = AT.WorkPointer; i < ABS(T->
numind); i++ ) {
848 ParseSignedNumber(x,s);
849 if ( FG.cTable[s[-1]] != 1 || ( *s !=
',' && *s !=
')' ) ) {
850 MesPrint(
"&Table arguments in TableBase addto statement should be numbers");
854 if ( *s ==
')' )
break;
857 if ( *s !=
')' || i < ( ABS(T->
numind) - 1 ) ) {
858 MesPrint(
"&Incorrect number of table arguments in TableBase addto statement. Should be %d"
863 i = FindTableTree(T,AT.WorkPointer,1);
865 MesPrint(
"&Element %s has not been defined",es);
869 else if ( ExistsObject(d,basenumber,(
char *)es) ) {}
871 int dict = AO.CurrentDictionary;
872 AO.CurrentDictionary = 0;
877 AO.DollarInOutBuffer = 1;
879 ss = AO.DollarOutBuffer;
882#if ( TABLEEXTENSION == 2 )
887 lbrac = 0; first = 0;
889 if ( WriteTerm(expr,&lbrac,first,PRINTON,0) ) {
895 AddObject(d,basenumber,(
char *)es,(
char *)(AO.DollarOutBuffer));
897 AO.CurrentDictionary = dict;
905 for ( i = 0; i < T->
totind; i++ ) {
906#if ( TABLEEXTENSION == 2 )
911 sum = i * ( ABS(T->
numind) + TABLEEXTENSION );
913 for ( j = 0; j < ABS(T->
numind); j++, sum++ ) {
914 if ( j > 0 ) *t++ =
',';
917 if ( ( t - elementstring ) >= ELEMENTSIZE ) {
918 MesPrint(
"&Table element specification takes more than %ld characters and cannot be handled",
923 if ( ExistsObject(d,basenumber,(
char *)elementstring) ) {
continue; }
930 AO.DollarInOutBuffer = 1;
932 ss = AO.DollarOutBuffer;
935#if ( TABLEEXTENSION == 2 )
940 lbrac = 0; first = 0;
942 if ( WriteTerm(expr,&lbrac,first,PRINTON,0) ) {
948 AddObject(d,basenumber,(
char *)elementstring,(
char *)(AO.DollarOutBuffer));
951 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
953 if ( WriteIniInfo(d) )
goto tableabort;
954 M_free(AO.DollarOutBuffer,
"DollarOutBuffer");
955 AO.DollarOutBuffer = 0;
956 AO.DollarOutSizeBuffer = 0;
959 M_free(AO.DollarOutBuffer,
"DollarOutBuffer");
960 AO.DollarOutBuffer = 0;
961 AO.DollarOutSizeBuffer = 0;
974int CoTBenter(UBYTE *s)
978 UBYTE *arguments, *rhs, *buffer, *t, *u, c, *tablename;
980 int i, j, error = 0, error1 = 0, printall = 0;
983 int dict = AO.CurrentDictionary;
984 AO.CurrentDictionary = 0;
985 if ( ( d = FindTB(tablebasename) ) == 0 ) {
986 MesPrint(
"&No open tablebase with the name %s, check for existence of file or try readonly mode when opening.",tablebasename);
990 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
991 if ( *s ==
'!' ) { printall = 1; s++; }
992 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
996 if ( ( s =
SkipAName(s) ) == 0 ) { error = 1;
goto Endofall; }
998 if ( ( GetVar(tablename,&type,&funnum,CFUNCTION,NOAUTO) == NAMENOTFOUND )
999 || ( T = functions[funnum].tabl ) == 0 ) {
1000 MesPrint(
"&%s should be a previously declared table",tablename);
1003 else if ( T->
sparse == 0 ) {
1004 MesPrint(
"&%s should be a sparse table",tablename);
1007 else { basenumber = GetTableName(d,(
char *)tablename); }
1008 if ( T->
spare == 0 ) { SpareTable(T); }
1009 if ( basenumber > 0 ) {
1010 for ( i = 0; i < d->info.numberofindexblocks; i++ ) {
1011 for ( j = 0; j < NUMOBJECTS; j++ ) {
1012 if ( basenumber != d->iblocks[i]->objects[j].tablenumber )
1014 arguments = (UBYTE *)(d->iblocks[i]->objects[j].element);
1015 rhs = (UBYTE *)ReadObject(d,basenumber,(
char *)arguments);
1018 MesPrint(
"%s(%s) = %s",tablename,arguments,rhs);
1021 MesPrint(
"%s(%s) = 0",tablename,arguments);
1025 u = rhs;
while ( *u ) u++;
1027 u = arguments;
while ( *u ) u++;
1028 size += u-arguments;
1029 u = tablename;
while ( *u ) u++;
1030 size += u-tablename;
1032 buffer = (UBYTE *)Malloc1(size,
"TableBase copy");
1033 t = tablename; u = buffer;
1034 while ( *t ) *u++ = *t++;
1037 while ( *t ) *u++ = *t++;
1038 *u++ =
')'; *u++ =
'=';
1040 while ( *t ) *u++ = *t++;
1041 if ( t == rhs ) *u++ =
'0';
1043 M_free(rhs,
"rhs in TBenter");
1045 error1 = CoFill(buffer);
1047 if ( error1 < 0 )
goto Endofall;
1048 if ( error1 != 0 ) error = error1;
1049 M_free(buffer,
"TableBase copy");
1055 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
1059 s = (UBYTE *)(d->tablenames); basenumber = 0;
1062 tablename = s;
while ( *s ) s++; s++;
1065 if ( ( GetVar(tablename,&type,&funnum,CFUNCTION,NOAUTO) == NAMENOTFOUND )
1066 || ( T = functions[funnum].tabl ) == 0 ) {
1067 MesPrint(
"&%s should be a previously declared table",tablename);
1069 else if ( T->
sparse == 0 ) {
1070 MesPrint(
"&%s should be a sparse table",tablename);
1072 if ( T->
spare == 0 ) { SpareTable(T); }
1073 for ( i = 0; i < d->info.numberofindexblocks; i++ ) {
1074 for ( j = 0; j < NUMOBJECTS; j++ ) {
1075 if ( d->iblocks[i]->objects[j].tablenumber == basenumber ) {
1076 arguments = (UBYTE *)(d->iblocks[i]->objects[j].element);
1077 rhs = (UBYTE *)ReadObject(d,basenumber,(
char *)arguments);
1080 MesPrint(
"%s%s = %s",tablename,arguments,rhs);
1083 MesPrint(
"%s%s = 0",tablename,arguments);
1087 u = rhs;
while ( *u ) u++;
1089 u = arguments;
while ( *u ) u++;
1090 size += u-arguments;
1091 u = tablename;
while ( *u ) u++;
1092 size += u-tablename;
1094 buffer = (UBYTE *)Malloc1(size,
"TableBase copy");
1095 t = tablename; u = buffer;
1096 while ( *t ) *u++ = *t++;
1099 while ( *t ) *u++ = *t++;
1100 *u++ =
')'; *u++ =
'=';
1102 while ( *t ) *u++ = *t++;
1103 if ( t == rhs ) *u++ =
'0';
1105 M_free(rhs,
"rhs in TBenter");
1107 error1 = CoFill(buffer);
1109 if ( error1 < 0 )
goto Endofall;
1110 if ( error1 != 0 ) error = error1;
1111 M_free(buffer,
"TableBase copy");
1119 AO.CurrentDictionary = dict;
1133int CoTestUse(UBYTE *s)
1136 UBYTE *tablename, c;
1137 WORD type, funnum, *w;
1141 *w++ = TYPETESTUSE; *w++ = 2;
1144 if ( ( s =
SkipAName(s) ) == 0 )
return(1);
1146 if ( ( GetVar(tablename,&type,&funnum,CFUNCTION,NOAUTO) == NAMENOTFOUND )
1147 || ( T = functions[funnum].tabl ) == 0 ) {
1148 MesPrint(
"&%s should be a previously declared table",tablename);
1151 else if ( T->
sparse == 0 ) {
1152 MesPrint(
"&%s should be a sparse table",tablename);
1155 *w++ = funnum + FUNCTION;
1157 while ( *s ==
' ' || *s ==
',' || *s ==
'\t' ) s++;
1159 AT.WorkPointer[1] = w - AT.WorkPointer;
1165 AddNtoL(AT.WorkPointer[1],AT.WorkPointer);
1178int CheckTableDeclarations(
DBASE *d)
1181 UBYTE *s, *ss, *t, *command = 0;
1182 int k, error = 0, error1, i;
1184 LONG commandsize = 0;
1186 s = (UBYTE *)(d->tablenames);
1187 for ( k = 0; k < d->topnumber; k++ ) {
1188 if ( GetVar(s,&type,&funnum,ANYTYPE,NOAUTO) == NAMENOTFOUND ) {
1192 ss = s; i = 0;
while ( *ss ) { ss++; i++; }
1193 ss++;
while ( *ss ) { ss++; i++; }
1194 if ( commandsize == 0 ) {
1195 commandsize = i + 15;
1196 if ( commandsize < 100 ) commandsize = 100;
1198 if ( (i+11) > commandsize ) {
1199 if ( command ) { M_free(command,
"table command"); command = 0; }
1202 if ( command == 0 ) {
1203 command = (UBYTE *)Malloc1(commandsize,
"table command");
1205 t = command; ss = tablebase;
while ( *ss ) *t++ = *ss++;
1206 *t++ =
',';
while ( *s ) *t++ = *s++;
1207 s++;
while ( *s ) *t++ = *s++;
1208 *t++ =
')'; *t = 0; s++;
1209 error1 = DoTable(command,1);
1210 if ( error1 ) error = error1;
1212 else if ( ( type != CFUNCTION )
1213 || ( ( T = functions[funnum].tabl ) == 0 )
1214 || ( T->
sparse == 0 ) ) {
1215 MesPrint(
"&%s has been declared previously, but not as a sparse table.",s);
1231 MesPrint(
"&Declaration of table %s in %s different from previous declaration",ss,d->name);
1238 if ( command ) { M_free(command,
"table command"); }
1252int CoTBload(UBYTE *ss)
1255 UBYTE *s, *name, *t, *r, *command, *arguments, *tail;
1257 int num, cs, es, ns, ts, i, j, error = 0, error1;
1258 if ( ( d = FindTB(tablebasename) ) == 0 ) {
1259 MesPrint(
"&No open tablebase with the name %s",tablebasename);
1263 command = (UBYTE *)Malloc1(commandsize,
"Fill command");
1266 while ( *ss ==
',' || *ss ==
' ' || *ss ==
'\t' ) ss++;
1269 s = (UBYTE *)(d->tablenames);
1273 if ( StrCmp(s,name) ) {
1281 name = s;
while ( *s ) s++; ns = s-name; s++;
1282 tail = s;
while ( *s ) s++; ts = s-tail; s++;
1283 tail++;
while ( FG.cTable[*tail] == 1 ) tail++;
1287 for ( i = 0; i < d->info.numberofindexblocks; i++ ) {
1288 for ( j = 0; j < NUMOBJECTS; j++ ) {
1289 if ( d->iblocks[i]->objects[j].tablenumber == num ) {
1290 t = arguments = (UBYTE *)(d->iblocks[i]->objects[j].element);
1293 cs = 2*es + 2*ns + ts + 10;
1294 if ( cs > commandsize ) {
1296 if ( command ) M_free(command,
"Fill command");
1297 command = (UBYTE *)Malloc1(commandsize,
"Fill command");
1299 r = command; t = name;
while ( *t ) *r++ = *t++;
1300 *r++ =
'('; t = arguments;
while ( *t ) *r++ = *t++;
1301 *r++ =
')'; *r++ =
'='; *r++ =
't'; *r++ =
'b'; *r++ =
'l';
1302 *r++ =
'_'; *r++ =
'('; t = name;
while ( *t ) *r++ = *t++;
1303 *r++ =
','; t = arguments;
while ( *t ) *r++ = *t++;
1304 t = tail;
while ( *t ) {
1305 if ( *t ==
'?' && r[-1] !=
',' ) {
1307 if ( FG.cTable[*t] == 0 || *t ==
'$' || *t ==
'[' ) {
1313 else if ( *t ==
'{' ) {
1316 else if ( *t ) { *r++ = *t++;
continue; }
1324 AC.vetotablebasefill = 1;
1325 error1 = CoFill(command);
1326 AC.vetotablebasefill = 0;
1327 if ( error1 < 0 )
goto finishup;
1328 if ( error1 != 0 ) error = error1;
1334 while ( *ss ==
',' || *ss ==
' ' || *ss ==
'\t' ) ss++;
1338 s = (UBYTE *)(d->tablenames);
1342 name = s;
while ( *s ) s++; ns = s-name; s++;
1343 tail = s;
while ( *s ) s++; ts = s-tail; s++;
1344 tail++;
while ( FG.cTable[*tail] == 1 ) tail++;
1348 for ( i = 0; i < d->info.numberofindexblocks; i++ ) {
1349 for ( j = 0; j < NUMOBJECTS; j++ ) {
1350 if ( d->iblocks[i]->objects[j].tablenumber == num ) {
1351 t = arguments = (UBYTE *)(d->iblocks[i]->objects[j].element);
1354 cs = 2*es + 2*ns + ts + 10;
1355 if ( cs > commandsize ) {
1357 if ( command ) M_free(command,
"Fill command");
1358 command = (UBYTE *)Malloc1(commandsize,
"Fill command");
1360 r = command; t = name;
while ( *t ) *r++ = *t++;
1361 *r++ =
'('; t = arguments;
while ( *t ) *r++ = *t++;
1362 *r++ =
')'; *r++ =
'='; *r++ =
't'; *r++ =
'b'; *r++ =
'l';
1363 *r++ =
'_'; *r++ =
'('; t = name;
while ( *t ) *r++ = *t++;
1364 *r++ =
','; t = arguments;
while ( *t ) *r++ = *t++;
1365 t = tail;
while ( *t ) {
1366 if ( *t ==
'?' && r[-1] !=
',' ) {
1368 if ( FG.cTable[*t] == 0 || *t ==
'$' || *t ==
'[' ) {
1374 else if ( *t ==
'{' ) {
1377 else if ( *t ) { *r++ = *t++;
continue; }
1385 AC.vetotablebasefill = 1;
1386 error1 = CoFill(command);
1387 AC.vetotablebasefill = 0;
1388 if ( error1 < 0 )
goto finishup;
1389 if ( error1 != 0 ) error = error1;
1397 if ( command ) M_free(command,
"Fill command");
1414int TestUse(WORD *term, WORD level)
1416 WORD *tstop, *t, *m, *tstart, tabnum;
1417 WORD *funs, numfuns;
1421 CBUF *C = cbuf+AM.rbufnum;
1424 numfuns = C->
lhs[level][1] - 2;
1425 funs = C->
lhs[level] + 2;
1426 GETSTOP(term,tstop);
1428 while ( t < tstop ) {
1429 if ( *t != TABLESTUB ) { t += t[1];
continue; }
1433 if ( *m >= -FUNCTION )
continue;
1435 if ( ( T = functions[tabnum-FUNCTION].tabl ) == 0 )
continue;
1436 if ( T->
sparse == 0 )
continue;
1440 if ( numfuns > 0 ) {
1441 for ( i = 0; i < numfuns; i++ ) {
1442 if ( tabnum == funs[i] )
break;
1444 if ( i >= numfuns && numfuns > 0 )
continue;
1452 for ( i = 0; i < ABS(T->
numind); i++, m += 2 ) {
1453 if ( m >= t || *m != -SNUMBER )
break;
1455 if ( ( i == ABS(T->
numind) ) &&
1456 ( ( isp = FindTableTree(T,tstart+FUNHEAD+1,2) ) >= 0 ) ) {
1462 MesPrint(
"TestUse: Encountered a table element inside tbl_ that does not correspond to a tablebase element");
1474int CoTBaudit(UBYTE *s)
1478 int i, j, error = 0, num;
1480 if ( ( d = FindTB(tablebasename) ) == 0 ) {
1481 MesPrint(
"&No open tablebase with the name %s",tablebasename);
1484 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
1492 s = (UBYTE *)(d->tablenames); num = 0;
1495 name = s;
while ( *s ) s++; s++;
1496 tail = s;
while ( *s ) s++; s++;
1497 MesPrint(
"Table,sparse,%s%s)",name,tail);
1498 for ( i = 0; i < d->info.numberofindexblocks; i++ ) {
1499 for ( j = 0; j < NUMOBJECTS; j++ ) {
1500 if ( d->iblocks[i]->objects[j].tablenumber == num ) {
1501 MesPrint(
" %s(%s)",name,d->iblocks[i]->objects[j].element);
1519 if ( ( d = FindTB(tablebasename) ) == 0 ) {
1520 MesPrint(
"&No open tablebase with the name %s",tablebasename);
1523 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
1527 if ( StrICmp(s,(UBYTE *)(
"compress")) == 0 ) {
1528 d->mode &= ~NOCOMPRESS;
1531 MesPrint(
"&subkey %s not defined in TableBase On statement");
1535 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
1545int CoTBoff(UBYTE *s)
1550 if ( ( d = FindTB(tablebasename) ) == 0 ) {
1551 MesPrint(
"&No open tablebase with the name %s",tablebasename);
1554 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
1558 if ( StrICmp(s,(UBYTE *)(
"compress")) == 0 ) {
1559 d->mode |= NOCOMPRESS;
1562 MesPrint(
"&subkey %s not defined in TableBase Off statement");
1566 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
1576int CoTBcleanup(UBYTE *s)
1579 MesPrint(
"&TableBase Cleanup statement not yet implemented");
1588int CoTBreplace(UBYTE *s)
1591 MesPrint(
"&TableBase Replace statement not yet implemented");
1603int CoTBuse(UBYTE *s)
1608 UBYTE *arguments, *rhs, *buffer, *t, *u, c, *tablename, *p;
1610 int i, j, error = 0, error1 = 0, k;
1612 WORD type, funnum, mode, *w;
1613 if ( ( d = FindTB(tablebasename) ) == 0 ) {
1614 MesPrint(
"&No open tablebase with the name %s",tablebasename);
1617 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
1621 if ( ( s =
SkipAName(s) ) == 0 )
return(1);
1623 if ( ( GetVar(tablename,&type,&funnum,CFUNCTION,NOAUTO) == NAMENOTFOUND )
1624 || ( T = functions[funnum].tabl ) == 0 ) {
1625 MesPrint(
"&%s should be a previously declared table",tablename);
1628 else if ( T->
sparse == 0 ) {
1629 MesPrint(
"&%s should be a sparse table",tablename);
1632 else { basenumber = GetTableName(d,(
char *)tablename); }
1634 if ( basenumber > 0 ) {
1635 for ( i = 0; i < d->info.numberofindexblocks; i++ ) {
1636 for ( j = 0; j < NUMOBJECTS; j++ ) {
1637 if ( d->iblocks[i]->objects[j].tablenumber != basenumber )
continue;
1638 arguments = p = (UBYTE *)(d->iblocks[i]->objects[j].element);
1643 for ( k = 0, w = AT.WorkPointer; k < ABS(T->
numind); k++ ) {
1644 ParseSignedNumber(x,p);
1647 sum = FindTableTree(T,AT.WorkPointer,1);
1649 MesPrint(
"Table %s in tablebase %s has not been loaded properly"
1650 ,tablename,tablebasename);
1654 sum += ABS(T->
numind) + 4;
1656 if ( ( mode & ELEMENTLOADED ) == ELEMENTLOADED ) {
1660 if ( ( mode & ELEMENTUSED ) == 0 )
continue;
1664 rhs = (UBYTE *)ReadijObject(d,i,j,(
char *)arguments);
1666 u = rhs;
while ( *u ) u++;
1668 u = arguments;
while ( *u ) u++;
1669 size += u-arguments;
1670 u = tablename;
while ( *u ) u++;
1671 size += u-tablename;
1673 buffer = (UBYTE *)Malloc1(size,
"TableBase copy");
1674 t = tablename; u = buffer;
1675 while ( *t ) *u++ = *t++;
1678 while ( *t ) *u++ = *t++;
1679 *u++ =
')'; *u++ =
'=';
1681 while ( *t ) *u++ = *t++;
1682 if ( t == rhs ) { *u++ =
'0'; }
1684 M_free(rhs,
"rhs in TBuse xxx");
1686 error1 = CoFill(buffer);
1688 if ( error1 < 0 ) {
return(error); }
1689 if ( error1 != 0 ) error = error1;
1690 M_free(buffer,
"TableBase copy");
1698 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
1702 s = (UBYTE *)(d->tablenames); basenumber = 0;
1710 if ( ( GetVar(tablename,&type,&funnum,CFUNCTION,NOAUTO) == NAMENOTFOUND )
1711 || ( T = functions[funnum].tabl ) == 0 ) {
1712 MesPrint(
"&%s should be a previously declared table",tablename);
1714 else if ( T->
sparse == 0 ) {
1715 MesPrint(
"&%s should be a sparse table",tablename);
1718 MesPrint(
"In table %s we have a problem with stubb orders in CoTBuse",tablename);
1722 for ( i = 0; i < d->info.numberofindexblocks; i++ ) {
1723 for ( j = 0; j < NUMOBJECTS; j++ ) {
1724 if ( d->iblocks[i]->objects[j].tablenumber == basenumber ) {
1725 arguments = p = (UBYTE *)(d->iblocks[i]->objects[j].element);
1730 for ( k = 0, w = AT.WorkPointer; k < ABS(T->
numind); k++ ) {
1731 ParseSignedNumber(x,p);
1734 sum = FindTableTree(T,AT.WorkPointer,1);
1736 MesPrint(
"Table %s in tablebase %s has not been loaded properly"
1737 ,tablename,tablebasename);
1741 sum += ABS(T->
numind) + 4;
1743 if ( ( mode & ELEMENTLOADED ) == ELEMENTLOADED ) {
1747 if ( ( mode & ELEMENTUSED ) == 0 )
continue;
1751 rhs = (UBYTE *)ReadijObject(d,i,j,(
char *)arguments);
1753 u = rhs;
while ( *u ) u++;
1755 u = arguments;
while ( *u ) u++;
1756 size += u-arguments;
1757 u = tablename;
while ( *u ) u++;
1758 size += u-tablename;
1760 buffer = (UBYTE *)Malloc1(size,
"TableBase copy");
1761 t = tablename; u = buffer;
1762 while ( *t ) *u++ = *t++;
1765 while ( *t ) *u++ = *t++;
1766 *u++ =
')'; *u++ =
'=';
1769 while ( *t ) *u++ = *t++;
1770 if ( t == rhs ) { *u++ =
'0'; }
1772 M_free(rhs,
"rhs in TBuse");
1774 error1 = CoFill(buffer);
1776 if ( error1 < 0 ) {
return(error); }
1777 if ( error1 != 0 ) error = error1;
1778 M_free(buffer,
"TableBase copy");
1797int CoApply(UBYTE *s)
1800 UBYTE *tablename, c;
1801 WORD type, funnum, *w;
1803 LONG maxtogo = MAXPOSITIVE;
1806 if ( FG.cTable[*s] == 1 ) {
1808 while ( FG.cTable[*s] == 1 ) {
1809 maxtogo = maxtogo*10 + (*s-
'0');
1812 while ( *s ==
',' ) s++;
1813 if ( maxtogo > MAXPOSITIVE || maxtogo < 0 ) maxtogo = MAXPOSITIVE;
1815 *w++ = TYPEAPPLY; *w++ = 3; *w++ = maxtogo;
1818 if ( ( s =
SkipAName(s) ) == 0 )
return(1);
1820 if ( ( GetVar(tablename,&type,&funnum,CFUNCTION,NOAUTO) == NAMENOTFOUND )
1821 || ( T = functions[funnum].tabl ) == 0 ) {
1822 MesPrint(
"&%s should be a previously declared table",tablename);
1825 else if ( T->
sparse == 0 ) {
1826 MesPrint(
"&%s should be a sparse table",tablename);
1829 *w++ = funnum + FUNCTION;
1831 while ( *s ==
' ' || *s ==
',' || *s ==
'\t' ) s++;
1833 AT.WorkPointer[1] = w - AT.WorkPointer;
1839 AddNtoL(AT.WorkPointer[1],AT.WorkPointer);
1853 "The TableBase statement is used as follows:"
1854 ,
"TableBase \"file.tbl\" keyword subkey(s)"
1855 ,
" in which we have"
1856 ,
"Keyword Subkey(s) Action"
1857 ,
"open Opens file.tbl for R/W"
1858 ,
"create Creates file.tbl for R/W. Old contents are lost"
1859 ,
"load Loads all stubs of all tables"
1860 ,
"load tablename(s) Loads all stubs the tables mentioned"
1861 ,
"enter Loads all stubs and rhs of all tables"
1862 ,
"enter tablename(s) Loads all stubs and rhs of the tables mentioned"
1863 ,
"audit Prints list of contents"
1867 ,
"addto tablename adds all elements if not yet there"
1868 ,
"addto tableelement adds element if not yet there"
1871 ,
"on compress elements are stored in gzip format (default)"
1872 ,
"off compress elements are stored in uncompressed format"
1873 ,
"use compiles all needed elements"
1874 ,
"use tablename(s) compiles all needed elements of these tables"
1876 ,
"Related commands are:"
1877 ,
"testuse marks which tbl_ elements occur for all tables"
1878 ,
"testuse tablename(s) marks which tbl_ elements occur for given tables"
1879 ,
"apply replaces tbl_ if rhs available"
1880 ,
"apply tablename(s) replaces tbl_ for given tables if rhs available"
1884int CoTBhelp(UBYTE *s)
1886 int i, ii =
sizeof(helptb)/
sizeof(
char *);
1888 for ( i = 0; i < ii; i++ ) MesPrint(
"%s",helptb[i]);
1900void ReWorkT(WORD *term, WORD *funs, WORD numfuns)
1902 WORD *tstop, *tend, *m, *t, *tt, *mm, *mmm, *r, *rr;
1904 tend = term + *term; tstop = tend - ABS(tend[-1]);
1906 while ( t < tstop ) {
1907 if ( *t == TABLESTUB ) {
1908 for ( i = 0; i < numfuns; i++ ) {
1909 if ( -t[FUNHEAD] == funs[i] )
break;
1911 if ( numfuns == 0 || i < numfuns ) {
1913 *m++ = -t[FUNHEAD]; *m++ = i; t += 2; i -= FUNHEAD;
1914 if ( m < t ) {
for ( j = 0; j < FUNHEAD-2; j++ ) *m++ = *t++; }
1915 else { m += FUNHEAD-2; t += FUNHEAD-2; }
1917 while ( i-- > 0 ) { *m++ = *t++; }
1920 while ( tt < tend ) *mm++ = *tt++;
1922 tend = term + *term; tstop = tend - ABS(tend[-1]);
1928 else if ( *t >= FUNCTION ) {
1931 for ( j = 0; j < FUNHEAD; j++ ) {
1932 if ( m == t ) { m++; t++; }
1936 if ( *t <= -FUNCTION ) {
1937 if ( m == t ) { m++; t++; }
1940 else if ( *t < 0 ) {
1941 if ( m == t ) { m += 2; t += 2; }
1942 else { *m++ = *t++; *m++ = *t++; }
1945 rr = t + *t; mmm = m;
1946 for ( j = 0; j < ARGHEAD; j++ ) {
1947 if ( m == t ) { m++; t++; }
1952 ReWorkT(t,funs,numfuns);
1954 if ( m == t ) { m += j; t += j; }
1955 else {
while ( j-- >= 0 ) *m++ = *t++; }
1966 if ( m < t ) {
while ( j-- >= 0 ) *m++ = *t++; }
1967 else { m += j; t += j; }
1971 while ( t < tend ) *m++ = *t++;
1981void Apply(WORD *term, WORD level)
1983 WORD *funs, numfuns;
1986 CBUF *C = cbuf+AM.rbufnum;
1990 numfuns = C->
lhs[level][1] - 2;
1991 funs = C->
lhs[level] + 2;
1992 if ( numfuns > 0 ) {
1993 for ( i = MAXBUILTINFUNCTION-FUNCTION; i < AC.FunctionList.num; i++ ) {
1994 if ( ( T = functions[i].tabl ) != 0 ) {
1995 for ( j = 0; j < numfuns; j++ ) {
1996 if ( i == (funs[j]-FUNCTION) && T->
spare ) {
1997 FlipTable(&(functions[i]),0);
2005 for ( i = MAXBUILTINFUNCTION-FUNCTION; i < AC.FunctionList.num; i++ ) {
2006 if ( ( T = functions[i].tabl ) != 0 ) {
2007 if ( T->
spare ) FlipTable(&(functions[i]),0);
2017 ReWorkT(term,funs,numfuns);
2039int ApplyExec(WORD *term,
int maxtogo, WORD level)
2042 WORD rhsnumber, *Tpattern, *funs, numfuns, funnum;
2043 WORD ii, *t, *t1, *w, *p, *m, *m1, *u, *r, tbufnum, csize, wilds;
2045 int i, j, isp, stilltogo;
2055 AT.NestPoin->termsize = t;
2056 if ( AT.NestPoin == AT.Nest ) AN.EndNest = t + *t;
2062 if ( *t < FUNCTION ) { t += t[1];
continue; }
2063 if ( functions[*t-FUNCTION].spec > 0 ) { t += t[1];
continue; }
2064 AT.NestPoin->funsize = t;
2068 if ( *t < 0 ) { NEXTARG(t);
continue; }
2069 AT.NestPoin->argsize = t1 = t;
2086 stilltogo = ApplyExec(t,maxtogo,level);
2087 if ( stilltogo != maxtogo ) {
2088 if ( stilltogo <= 0 ) {
2092 maxtogo = stilltogo;
2094 m = term + *term - csize;
2104 C = cbuf+AM.rbufnum;
2107 if ( *t != TABLESTUB ) { t += t[1];
continue; }
2108 funnum = -t[FUNHEAD];
2109 if ( ( funnum < FUNCTION )
2110 || ( funnum >= FUNCTION+WILDOFFSET )
2111 || ( ( T = functions[funnum-FUNCTION].tabl ) == 0 )
2113 || ( T->
spare == 0 ) ) { t += t[1];
continue; }
2114 numfuns = C->
lhs[level][1] - 3;
2115 funs = C->
lhs[level] + 3;
2116 if ( numfuns > 0 ) {
2117 for ( i = 0; i < numfuns; i++ ) {
2118 if ( funs[i] == funnum )
break;
2120 if ( i >= numfuns ) { t += t[1];
continue; }
2123 AT.NestPoin->funsize = t + 1;
2133 Tpattern = T->
pattern[identity];
2137 p = Tpattern+FUNHEAD+1;
2138 for ( i = 0; i < ABS(T->
numind); i++, t += 2 ) {
2139 if ( *t != -SNUMBER )
break;
2141 if ( i < ABS(T->
numind) ) { t = r;
continue; }
2142 isp = FindTableTree(T,t1+FUNHEAD+1,2);
2143 if ( isp < 0 ) { t = r;
continue; }
2145#if ( TABLEEXTENSION == 2 )
2152 while ( --ii >= 0 ) {
2153 *p = *t; t += 2; p += 2;
2165 AN.WildValue = AN.FullProto + SUBEXPSIZE;
2166 AN.WildStop = AN.FullProto+AN.FullProto[1];
2169 AN.RepFunList = AN.EndNest;
2170 AT.WorkPointer = (WORD *)(((UBYTE *)(AN.EndNest)) + AM.MaxTer/2);
2175 if ( AT.WorkPointer + t1[1] >= AT.WorkTop ) { MesWork(); }
2178 *w++ = -t1[FUNHEAD];
2180 for ( i = 2; i < FUNHEAD; i++ ) *w++ = t1[i];
2182 while ( t < r ) *w++ = *t++;
2185 if ( MatchFunction(BHEAD Tpattern,t,&wilds) > 0 ) {
2206 while ( r < m1 ) *t++ = *r++;
2210 while ( NN > AT.Nest ) {
2212 NN->termsize[0] += j;
2213 NN->funsize[1] += j;
2214 NN->argsize[0] += j;
2215 NN->funsize[2] |= DIRTYFLAG;
2216 NN->argsize[1] |= DIRTYFLAG;
2223 while ( t >= r ) { t[j] = *t; t--; }
2229 while ( NN > AT.Nest ) {
2231 NN->termsize[0] += j;
2232 NN->funsize[1] += j;
2233 NN->argsize[0] += j;
2234 NN->funsize[2] |= DIRTYFLAG;
2235 NN->argsize[1] |= DIRTYFLAG;
2244 if ( maxtogo <= 0 )
return(maxtogo);
2256void ApplyReset(WORD level)
2258 WORD *funs, numfuns;
2261 CBUF *C = cbuf+AM.rbufnum;
2263 numfuns = C->
lhs[level][1] - 2;
2264 funs = C->
lhs[level] + 2;
2265 if ( numfuns > 0 ) {
2266 for ( i = MAXBUILTINFUNCTION-FUNCTION; i < AC.FunctionList.num; i++ ) {
2267 if ( ( T = functions[i].tabl ) != 0 ) {
2268 for ( j = 0; j < numfuns; j++ ) {
2269 if ( i == (funs[j]-FUNCTION) && T->
spare ) {
2270 FlipTable(&(functions[i]),1);
2278 for ( i = MAXBUILTINFUNCTION-FUNCTION; i < AC.FunctionList.num; i++ ) {
2279 if ( ( T = functions[i].tabl ) != 0 ) {
2280 if ( T->
spare ) FlipTable(&(functions[i]),1);
2291void TableReset(
void)
2296 for ( i = MAXBUILTINFUNCTION-FUNCTION; i < AC.FunctionList.num; i++ ) {
2297 if ( ( T = functions[i].tabl ) != 0 && T->
spare && T->
mode == 0 ) {
2298 functions[i].tabl = T->
spare;
2321 for ( i = NumTableBases - 1; i >= 0; i-- ) {
UBYTE * SkipAName(UBYTE *s)
int Generator(PHEAD WORD *, WORD)