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; }
154 else if ( *v1 < *v2 ) {
156 if ( iq >= 0 ) { ip = iq; }
171 MesPrint(
"Serious problems in InsTableTree!\n");
182 if ( q->
blnce == 0 )
break;
183 if ( q->
blnce == -2 ) {
184 if ( p->
blnce == -1 ) {
214 else if ( q->
blnce == 2 ) {
215 if ( p->
blnce == 1 ) {
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) ) {}
807 AO.DollarInOutBuffer = 1;
809 ss = AO.DollarOutBuffer;
812 #if ( TABLEEXTENSION == 2 ) 817 lbrac = 0; first = 0;
819 if ( WriteTerm(expr,&lbrac,first,PRINTON,0) ) {
825 AddObject(d,basenumber,(
char *)es,(
char *)(AO.DollarOutBuffer));
834 for ( i = 0; i < T->
totind; i++ ) {
835 #if ( TABLEEXTENSION == 2 ) 840 sum = i * ( T->
numind + TABLEEXTENSION );
842 for ( j = 0; j < T->
numind; j++, sum++ ) {
843 if ( j > 0 ) *t++ =
',';
846 if ( ( t - elementstring ) >= ELEMENTSIZE ) {
847 MesPrint(
"&Table element specification takes more than %ld characters and cannot be handled",
852 if ( ExistsObject(d,basenumber,(
char *)elementstring) ) {
continue; }
859 AO.DollarInOutBuffer = 1;
861 ss = AO.DollarOutBuffer;
864 #if ( TABLEEXTENSION == 2 ) 869 lbrac = 0; first = 0;
871 if ( WriteTerm(expr,&lbrac,first,PRINTON,0) ) {
877 AddObject(d,basenumber,(
char *)elementstring,(
char *)(AO.DollarOutBuffer));
880 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
882 if ( WriteIniInfo(d) )
goto tableabort;
883 M_free(AO.DollarOutBuffer,
"DollarOutBuffer");
884 AO.DollarOutBuffer = 0;
885 AO.DollarOutSizeBuffer = 0;
888 M_free(AO.DollarOutBuffer,
"DollarOutBuffer");
889 AO.DollarOutBuffer = 0;
890 AO.DollarOutSizeBuffer = 0;
903 int CoTBenter(UBYTE *s)
907 UBYTE *arguments, *rhs, *buffer, *t, *u, c, *tablename;
909 int i, j, error = 0, error1 = 0, printall = 0;
912 if ( ( d = FindTB(tablebasename) ) == 0 ) {
913 MesPrint(
"&No open tablebase with the name %s",tablebasename);
916 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
917 if ( *s ==
'!' ) { printall = 1; s++; }
918 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
922 if ( ( s = SkipAName(s) ) == 0 )
return(1);
924 if ( ( GetVar(tablename,&type,&funnum,CFUNCTION,NOAUTO) == NAMENOTFOUND )
925 || ( T = functions[funnum].tabl ) == 0 ) {
926 MesPrint(
"&%s should be a previously declared table",tablename);
929 else if ( T->
sparse == 0 ) {
930 MesPrint(
"&%s should be a sparse table",tablename);
933 else { basenumber = GetTableName(d,(
char *)tablename); }
934 if ( T->
spare == 0 ) { SpareTable(T); }
935 if ( basenumber > 0 ) {
936 for ( i = 0; i < d->info.numberofindexblocks; i++ ) {
937 for ( j = 0; j < NUMOBJECTS; j++ ) {
938 if ( basenumber != d->iblocks[i]->objects[j].tablenumber )
940 arguments = (UBYTE *)(d->iblocks[i]->objects[j].element);
941 rhs = (UBYTE *)ReadObject(d,basenumber,(
char *)arguments);
944 MesPrint(
"%s(%s) = %s",tablename,arguments,rhs);
947 MesPrint(
"%s(%s) = 0",tablename,arguments);
951 u = rhs;
while ( *u ) u++;
953 u = arguments;
while ( *u ) u++;
955 u = tablename;
while ( *u ) u++;
958 buffer = (UBYTE *)Malloc1(size,
"TableBase copy");
959 t = tablename; u = buffer;
960 while ( *t ) *u++ = *t++;
963 while ( *t ) *u++ = *t++;
964 *u++ =
')'; *u++ =
'=';
966 while ( *t ) *u++ = *t++;
967 if ( t == rhs ) *u++ =
'0';
969 M_free(rhs,
"rhs in TBenter");
971 error1 = CoFill(buffer);
973 if ( error1 < 0 ) {
return(error); }
974 if ( error1 != 0 ) error = error1;
975 M_free(buffer,
"TableBase copy");
981 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
985 s = (UBYTE *)(d->tablenames); basenumber = 0;
988 tablename = s;
while ( *s ) s++; s++;
989 while ( *s ) s++; s++;
990 if ( ( GetVar(tablename,&type,&funnum,CFUNCTION,NOAUTO) == NAMENOTFOUND )
991 || ( T = functions[funnum].tabl ) == 0 ) {
992 MesPrint(
"&%s should be a previously declared table",tablename);
994 else if ( T->
sparse == 0 ) {
995 MesPrint(
"&%s should be a sparse table",tablename);
997 if ( T->
spare == 0 ) { SpareTable(T); }
998 for ( i = 0; i < d->info.numberofindexblocks; i++ ) {
999 for ( j = 0; j < NUMOBJECTS; j++ ) {
1000 if ( d->iblocks[i]->objects[j].tablenumber == basenumber ) {
1001 arguments = (UBYTE *)(d->iblocks[i]->objects[j].element);
1002 rhs = (UBYTE *)ReadObject(d,basenumber,(
char *)arguments);
1005 MesPrint(
"%s%s = %s",tablename,arguments,rhs);
1008 MesPrint(
"%s%s = 0",tablename,arguments);
1012 u = rhs;
while ( *u ) u++;
1014 u = arguments;
while ( *u ) u++;
1015 size += u-arguments;
1016 u = tablename;
while ( *u ) u++;
1017 size += u-tablename;
1019 buffer = (UBYTE *)Malloc1(size,
"TableBase copy");
1020 t = tablename; u = buffer;
1021 while ( *t ) *u++ = *t++;
1024 while ( *t ) *u++ = *t++;
1025 *u++ =
')'; *u++ =
'=';
1027 while ( *t ) *u++ = *t++;
1028 if ( t == rhs ) *u++ =
'0';
1030 M_free(rhs,
"rhs in TBenter");
1032 error1 = CoFill(buffer);
1034 if ( error1 < 0 ) {
return(error); }
1035 if ( error1 != 0 ) error = error1;
1036 M_free(buffer,
"TableBase copy");
1056 int CoTestUse(UBYTE *s)
1059 UBYTE *tablename, c;
1060 WORD type, funnum, *w;
1064 *w++ = TYPETESTUSE; *w++ = 2;
1067 if ( ( s = SkipAName(s) ) == 0 )
return(1);
1069 if ( ( GetVar(tablename,&type,&funnum,CFUNCTION,NOAUTO) == NAMENOTFOUND )
1070 || ( T = functions[funnum].tabl ) == 0 ) {
1071 MesPrint(
"&%s should be a previously declared table",tablename);
1074 else if ( T->
sparse == 0 ) {
1075 MesPrint(
"&%s should be a sparse table",tablename);
1078 *w++ = funnum + FUNCTION;
1080 while ( *s ==
' ' || *s ==
',' || *s ==
'\t' ) s++;
1082 AT.WorkPointer[1] = w - AT.WorkPointer;
1088 AddNtoL(AT.WorkPointer[1],AT.WorkPointer);
1101 int CheckTableDeclarations(
DBASE *d)
1104 UBYTE *s, *ss, *t, *command = 0;
1105 int k, error = 0, error1, i;
1107 LONG commandsize = 0;
1109 s = (UBYTE *)(d->tablenames);
1110 for ( k = 0; k < d->topnumber; k++ ) {
1111 if ( GetVar(s,&type,&funnum,ANYTYPE,NOAUTO) == NAMENOTFOUND ) {
1115 ss = s; i = 0;
while ( *ss ) { ss++; i++; }
1116 ss++;
while ( *ss ) { ss++; i++; }
1117 if ( commandsize == 0 ) {
1118 commandsize = i + 15;
1119 if ( commandsize < 100 ) commandsize = 100;
1121 if ( (i+11) > commandsize ) {
1122 if ( command ) { M_free(command,
"table command"); command = 0; }
1125 if ( command == 0 ) {
1126 command = (UBYTE *)Malloc1(commandsize,
"table command");
1128 t = command; ss = tablebase;
while ( *ss ) *t++ = *ss++;
1129 *t++ =
',';
while ( *s ) *t++ = *s++;
1130 s++;
while ( *s ) *t++ = *s++;
1131 *t++ =
')'; *t = 0; s++;
1132 error1 = DoTable(command,1);
1133 if ( error1 ) error = error1;
1135 else if ( ( type != CFUNCTION )
1136 || ( ( T = functions[funnum].tabl ) == 0 )
1137 || ( T->
sparse == 0 ) ) {
1138 MesPrint(
"&%s has been declared previously, but not as a sparse table.",s);
1140 while ( *s ) s++; s++;
while ( *s ) s++; s++;
1148 while ( *s ) s++; s++;
1150 MesPrint(
"&Declaration of table %s in %s different from previous declaration",ss,d->name);
1153 while ( *s ) s++; s++;
1156 if ( command ) { M_free(command,
"table command"); }
1170 int CoTBload(UBYTE *ss)
1173 UBYTE *s, *name, *t, *r, *command, *arguments, *tail;
1175 int num, cs, es, ns, ts, i, j, error = 0, error1;
1176 if ( ( d = FindTB(tablebasename) ) == 0 ) {
1177 MesPrint(
"&No open tablebase with the name %s",tablebasename);
1181 command = (UBYTE *)Malloc1(commandsize,
"Fill command");
1184 while ( *ss ==
',' || *ss ==
' ' || *ss ==
'\t' ) ss++;
1186 name = ss; ss = SkipAName(ss); *ss = 0;
1187 s = (UBYTE *)(d->tablenames);
1191 if ( StrCmp(s,name) ) {
1192 while ( *s ) s++; s++;
while ( *s ) s++; s++; num++;
1195 name = s;
while ( *s ) s++; ns = s-name; s++;
1196 tail = s;
while ( *s ) s++; ts = s-tail; s++;
1197 tail++;
while ( FG.cTable[*tail] == 1 ) tail++;
1201 for ( i = 0; i < d->info.numberofindexblocks; i++ ) {
1202 for ( j = 0; j < NUMOBJECTS; j++ ) {
1203 if ( d->iblocks[i]->objects[j].tablenumber == num ) {
1204 t = arguments = (UBYTE *)(d->iblocks[i]->objects[j].element);
1205 while ( *t ) t++; es = t - arguments;
1206 cs = 2*es + 2*ns + ts + 10;
1207 if ( cs > commandsize ) {
1209 if ( command ) M_free(command,
"Fill command");
1210 command = (UBYTE *)Malloc1(commandsize,
"Fill command");
1212 r = command; t = name;
while ( *t ) *r++ = *t++;
1213 *r++ =
'('; t = arguments;
while ( *t ) *r++ = *t++;
1214 *r++ =
')'; *r++ =
'='; *r++ =
't'; *r++ =
'b'; *r++ =
'l';
1215 *r++ =
'_'; *r++ =
'('; t = name;
while ( *t ) *r++ = *t++;
1216 *r++ =
','; t = arguments;
while ( *t ) *r++ = *t++;
1217 t = tail;
while ( *t ) {
1218 if ( *t ==
'?' && r[-1] !=
',' ) {
1220 if ( FG.cTable[*t] == 0 || *t ==
'$' || *t ==
'[' ) {
1226 else if ( *t ==
'{' ) {
1229 else if ( *t ) { *r++ = *t++;
continue; }
1237 AC.vetotablebasefill = 1;
1238 error1 = CoFill(command);
1239 AC.vetotablebasefill = 0;
1240 if ( error1 < 0 )
goto finishup;
1241 if ( error1 != 0 ) error = error1;
1247 while ( *ss ==
',' || *ss ==
' ' || *ss ==
'\t' ) ss++;
1251 s = (UBYTE *)(d->tablenames);
1255 name = s;
while ( *s ) s++; ns = s-name; s++;
1256 tail = s;
while ( *s ) s++; ts = s-tail; s++;
1257 tail++;
while ( FG.cTable[*tail] == 1 ) tail++;
1261 for ( i = 0; i < d->info.numberofindexblocks; i++ ) {
1262 for ( j = 0; j < NUMOBJECTS; j++ ) {
1263 if ( d->iblocks[i]->objects[j].tablenumber == num ) {
1264 t = arguments = (UBYTE *)(d->iblocks[i]->objects[j].element);
1265 while ( *t ) t++; es = t - arguments;
1266 cs = 2*es + 2*ns + ts + 10;
1267 if ( cs > commandsize ) {
1269 if ( command ) M_free(command,
"Fill command");
1270 command = (UBYTE *)Malloc1(commandsize,
"Fill command");
1272 r = command; t = name;
while ( *t ) *r++ = *t++;
1273 *r++ =
'('; t = arguments;
while ( *t ) *r++ = *t++;
1274 *r++ =
')'; *r++ =
'='; *r++ =
't'; *r++ =
'b'; *r++ =
'l';
1275 *r++ =
'_'; *r++ =
'('; t = name;
while ( *t ) *r++ = *t++;
1276 *r++ =
','; t = arguments;
while ( *t ) *r++ = *t++;
1277 t = tail;
while ( *t ) {
1278 if ( *t ==
'?' && r[-1] !=
',' ) {
1280 if ( FG.cTable[*t] == 0 || *t ==
'$' || *t ==
'[' ) {
1286 else if ( *t ==
'{' ) {
1289 else if ( *t ) { *r++ = *t++;
continue; }
1297 AC.vetotablebasefill = 1;
1298 error1 = CoFill(command);
1299 AC.vetotablebasefill = 0;
1300 if ( error1 < 0 )
goto finishup;
1301 if ( error1 != 0 ) error = error1;
1309 if ( command ) M_free(command,
"Fill command");
1326 WORD TestUse(WORD *term, WORD level)
1328 WORD *tstop, *t, *m, *tstart, tabnum;
1329 WORD *funs, numfuns, error = 0;
1332 CBUF *C = cbuf+AM.rbufnum;
1335 numfuns = C->
lhs[level][1] - 2;
1336 funs = C->
lhs[level] + 2;
1337 GETSTOP(term,tstop);
1339 while ( t < tstop ) {
1340 if ( *t != TABLESTUB ) { t += t[1];
continue; }
1344 if ( *m >= -FUNCTION )
continue;
1346 if ( ( T = functions[tabnum-FUNCTION].tabl ) == 0 )
continue;
1347 if ( T->
sparse == 0 )
continue;
1351 if ( numfuns > 0 ) {
1352 for ( i = 0; i < numfuns; i++ ) {
1353 if ( tabnum == funs[i] )
break;
1355 if ( i >= numfuns && numfuns > 0 )
continue;
1363 for ( i = 0; i < T->
numind; i++, m += 2 ) {
1364 if ( m >= t || *m != -SNUMBER )
break;
1366 if ( ( i == T->
numind ) &&
1367 ( ( isp = FindTableTree(T,tstart+FUNHEAD+1,2) ) >= 0 ) ) {
1373 MesPrint(
"TestUse: Encountered a table element inside tbl_ that does not correspond to a tablebase element");
1385 int CoTBaudit(UBYTE *s)
1389 int i, j, error = 0, num;
1391 if ( ( d = FindTB(tablebasename) ) == 0 ) {
1392 MesPrint(
"&No open tablebase with the name %s",tablebasename);
1395 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
1403 s = (UBYTE *)(d->tablenames); num = 0;
1406 name = s;
while ( *s ) s++; s++;
1407 tail = s;
while ( *s ) s++; s++;
1408 MesPrint(
"Table,sparse,%s%s)",name,tail);
1409 for ( i = 0; i < d->info.numberofindexblocks; i++ ) {
1410 for ( j = 0; j < NUMOBJECTS; j++ ) {
1411 if ( d->iblocks[i]->objects[j].tablenumber == num ) {
1412 MesPrint(
" %s(%s)",name,d->iblocks[i]->objects[j].element);
1425 int CoTBon(UBYTE *s)
1430 if ( ( d = FindTB(tablebasename) ) == 0 ) {
1431 MesPrint(
"&No open tablebase with the name %s",tablebasename);
1434 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
1438 if ( StrICmp(s,(UBYTE *)(
"compress")) == 0 ) {
1439 d->mode &= ~NOCOMPRESS;
1442 MesPrint(
"&subkey %s not defined in TableBase On statement");
1446 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
1456 int CoTBoff(UBYTE *s)
1461 if ( ( d = FindTB(tablebasename) ) == 0 ) {
1462 MesPrint(
"&No open tablebase with the name %s",tablebasename);
1465 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
1469 if ( StrICmp(s,(UBYTE *)(
"compress")) == 0 ) {
1470 d->mode |= NOCOMPRESS;
1473 MesPrint(
"&subkey %s not defined in TableBase Off statement");
1477 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
1487 int CoTBcleanup(UBYTE *s)
1490 MesPrint(
"&TableBase Cleanup statement not yet implemented");
1499 int CoTBreplace(UBYTE *s)
1502 MesPrint(
"&TableBase Replace statement not yet implemented");
1514 int CoTBuse(UBYTE *s)
1519 UBYTE *arguments, *rhs, *buffer, *t, *u, c, *tablename, *p;
1521 int i, j, error = 0, error1 = 0, k;
1523 WORD type, funnum, mode, *w;
1524 if ( ( d = FindTB(tablebasename) ) == 0 ) {
1525 MesPrint(
"&No open tablebase with the name %s",tablebasename);
1528 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
1532 if ( ( s = SkipAName(s) ) == 0 )
return(1);
1534 if ( ( GetVar(tablename,&type,&funnum,CFUNCTION,NOAUTO) == NAMENOTFOUND )
1535 || ( T = functions[funnum].tabl ) == 0 ) {
1536 MesPrint(
"&%s should be a previously declared table",tablename);
1539 else if ( T->
sparse == 0 ) {
1540 MesPrint(
"&%s should be a sparse table",tablename);
1543 else { basenumber = GetTableName(d,(
char *)tablename); }
1545 if ( basenumber > 0 ) {
1546 for ( i = 0; i < d->info.numberofindexblocks; i++ ) {
1547 for ( j = 0; j < NUMOBJECTS; j++ ) {
1548 if ( d->iblocks[i]->objects[j].tablenumber != basenumber )
continue;
1549 arguments = p = (UBYTE *)(d->iblocks[i]->objects[j].element);
1554 for ( k = 0, w = AT.WorkPointer; k < T->numind; k++ ) {
1555 ParseSignedNumber(x,p);
1558 sum = FindTableTree(T,AT.WorkPointer,1);
1560 MesPrint(
"Table %s in tablebase %s has not been loaded properly" 1561 ,tablename,tablebasename);
1567 if ( ( mode & ELEMENTLOADED ) == ELEMENTLOADED ) {
1571 if ( ( mode & ELEMENTUSED ) == 0 )
continue;
1575 rhs = (UBYTE *)ReadijObject(d,i,j,(
char *)arguments);
1577 u = rhs;
while ( *u ) u++;
1579 u = arguments;
while ( *u ) u++;
1580 size += u-arguments;
1581 u = tablename;
while ( *u ) u++;
1582 size += u-tablename;
1584 buffer = (UBYTE *)Malloc1(size,
"TableBase copy");
1585 t = tablename; u = buffer;
1586 while ( *t ) *u++ = *t++;
1589 while ( *t ) *u++ = *t++;
1590 *u++ =
')'; *u++ =
'=';
1592 while ( *t ) *u++ = *t++;
1593 if ( t == rhs ) { *u++ =
'0'; }
1595 M_free(rhs,
"rhs in TBuse xxx");
1597 error1 = CoFill(buffer);
1599 if ( error1 < 0 ) {
return(error); }
1600 if ( error1 != 0 ) error = error1;
1601 M_free(buffer,
"TableBase copy");
1609 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
1613 s = (UBYTE *)(d->tablenames); basenumber = 0;
1616 tablename = s;
while ( *s ) s++; s++;
1617 while ( *s ) s++; s++;
1618 if ( ( GetVar(tablename,&type,&funnum,CFUNCTION,NOAUTO) == NAMENOTFOUND )
1619 || ( T = functions[funnum].tabl ) == 0 ) {
1620 MesPrint(
"&%s should be a previously declared table",tablename);
1622 else if ( T->
sparse == 0 ) {
1623 MesPrint(
"&%s should be a sparse table",tablename);
1626 MesPrint(
"In table %s we have a problem with stubb orders in CoTBuse",tablename);
1630 for ( i = 0; i < d->info.numberofindexblocks; i++ ) {
1631 for ( j = 0; j < NUMOBJECTS; j++ ) {
1632 if ( d->iblocks[i]->objects[j].tablenumber == basenumber ) {
1633 arguments = p = (UBYTE *)(d->iblocks[i]->objects[j].element);
1638 for ( k = 0, w = AT.WorkPointer; k < T->numind; k++ ) {
1639 ParseSignedNumber(x,p);
1642 sum = FindTableTree(T,AT.WorkPointer,1);
1644 MesPrint(
"Table %s in tablebase %s has not been loaded properly" 1645 ,tablename,tablebasename);
1651 if ( ( mode & ELEMENTLOADED ) == ELEMENTLOADED ) {
1655 if ( ( mode & ELEMENTUSED ) == 0 )
continue;
1659 rhs = (UBYTE *)ReadijObject(d,i,j,(
char *)arguments);
1661 u = rhs;
while ( *u ) u++;
1663 u = arguments;
while ( *u ) u++;
1664 size += u-arguments;
1665 u = tablename;
while ( *u ) u++;
1666 size += u-tablename;
1668 buffer = (UBYTE *)Malloc1(size,
"TableBase copy");
1669 t = tablename; u = buffer;
1670 while ( *t ) *u++ = *t++;
1673 while ( *t ) *u++ = *t++;
1674 *u++ =
')'; *u++ =
'=';
1677 while ( *t ) *u++ = *t++;
1678 if ( t == rhs ) { *u++ =
'0'; }
1680 M_free(rhs,
"rhs in TBuse");
1682 error1 = CoFill(buffer);
1684 if ( error1 < 0 ) {
return(error); }
1685 if ( error1 != 0 ) error = error1;
1686 M_free(buffer,
"TableBase copy");
1705 int CoApply(UBYTE *s)
1708 UBYTE *tablename, c;
1709 WORD type, funnum, *w;
1711 LONG maxtogo = MAXPOSITIVE;
1714 if ( FG.cTable[*s] == 1 ) {
1716 while ( FG.cTable[*s] == 1 ) {
1717 maxtogo = maxtogo*10 + (*s-
'0');
1720 while ( *s ==
',' ) s++;
1721 if ( maxtogo > MAXPOSITIVE || maxtogo < 0 ) maxtogo = MAXPOSITIVE;
1723 *w++ = TYPEAPPLY; *w++ = 3; *w++ = maxtogo;
1726 if ( ( s = SkipAName(s) ) == 0 )
return(1);
1728 if ( ( GetVar(tablename,&type,&funnum,CFUNCTION,NOAUTO) == NAMENOTFOUND )
1729 || ( T = functions[funnum].tabl ) == 0 ) {
1730 MesPrint(
"&%s should be a previously declared table",tablename);
1733 else if ( T->
sparse == 0 ) {
1734 MesPrint(
"&%s should be a sparse table",tablename);
1737 *w++ = funnum + FUNCTION;
1739 while ( *s ==
' ' || *s ==
',' || *s ==
'\t' ) s++;
1741 AT.WorkPointer[1] = w - AT.WorkPointer;
1747 AddNtoL(AT.WorkPointer[1],AT.WorkPointer);
1761 "The TableBase statement is used as follows:" 1762 ,
"TableBase \"file.tbl\" keyword subkey(s)" 1763 ,
" in which we have" 1764 ,
"Keyword Subkey(s) Action" 1765 ,
"open Opens file.tbl for R/W" 1766 ,
"create Creates file.tbl for R/W. Old contents are lost" 1767 ,
"load Loads all stubs of all tables" 1768 ,
"load tablename(s) Loads all stubs the tables mentioned" 1769 ,
"enter Loads all stubs and rhs of all tables" 1770 ,
"enter tablename(s) Loads all stubs and rhs of the tables mentioned" 1771 ,
"audit Prints list of contents" 1775 ,
"addto tablename adds all elements if not yet there" 1776 ,
"addto tableelement adds element if not yet there" 1779 ,
"on compress elements are stored in gzip format (default)" 1780 ,
"off compress elements are stored in uncompressed format" 1781 ,
"use compiles all needed elements" 1782 ,
"use tablename(s) compiles all needed elements of these tables" 1784 ,
"Related commands are:" 1785 ,
"testuse marks which tbl_ elements occur for all tables" 1786 ,
"testuse tablename(s) marks which tbl_ elements occur for given tables" 1787 ,
"apply replaces tbl_ if rhs available" 1788 ,
"apply tablename(s) replaces tbl_ for given tables if rhs available" 1792 int CoTBhelp(UBYTE *s)
1794 int i, ii =
sizeof(helptb)/
sizeof(
char *);
1796 for ( i = 0; i < ii; i++ ) MesPrint(
"%s",helptb[i]);
1808 VOID ReWorkT(WORD *term, WORD *funs, WORD numfuns)
1810 WORD *tstop, *tend, *m, *t, *tt, *mm, *mmm, *r, *rr;
1812 tend = term + *term; tstop = tend - ABS(tend[-1]);
1814 while ( t < tstop ) {
1815 if ( *t == TABLESTUB ) {
1816 for ( i = 0; i < numfuns; i++ ) {
1817 if ( -t[FUNHEAD] == funs[i] )
break;
1819 if ( numfuns == 0 || i < numfuns ) {
1821 *m++ = -t[FUNHEAD]; *m++ = i; t += 2; i -= FUNHEAD;
1822 if ( m < t ) {
for ( j = 0; j < FUNHEAD-2; j++ ) *m++ = *t++; }
1823 else { m += FUNHEAD-2; t += FUNHEAD-2; }
1825 while ( i-- > 0 ) { *m++ = *t++; }
1828 while ( tt < tend ) *mm++ = *tt++;
1830 tend = term + *term; tstop = tend - ABS(tend[-1]);
1836 else if ( *t >= FUNCTION ) {
1839 for ( j = 0; j < FUNHEAD; j++ ) {
1840 if ( m == t ) { m++; t++; }
1844 if ( *t <= -FUNCTION ) {
1845 if ( m == t ) { m++; t++; }
1848 else if ( *t < 0 ) {
1849 if ( m == t ) { m += 2; t += 2; }
1850 else { *m++ = *t++; *m++ = *t++; }
1853 rr = t + *t; mmm = m;
1854 for ( j = 0; j < ARGHEAD; j++ ) {
1855 if ( m == t ) { m++; t++; }
1860 ReWorkT(t,funs,numfuns);
1862 if ( m == t ) { m += j; t += j; }
1863 else {
while ( j-- >= 0 ) *m++ = *t++; }
1874 if ( m < t ) {
while ( j-- >= 0 ) *m++ = *t++; }
1875 else { m += j; t += j; }
1879 while ( t < tend ) *m++ = *t++;
1889 WORD Apply(WORD *term, WORD level)
1891 WORD *funs, numfuns;
1894 CBUF *C = cbuf+AM.rbufnum;
1898 numfuns = C->
lhs[level][1] - 2;
1899 funs = C->
lhs[level] + 2;
1900 if ( numfuns > 0 ) {
1901 for ( i = MAXBUILTINFUNCTION-FUNCTION; i < AC.FunctionList.num; i++ ) {
1902 if ( ( T = functions[i].tabl ) != 0 ) {
1903 for ( j = 0; j < numfuns; j++ ) {
1904 if ( i == (funs[j]-FUNCTION) && T->
spare ) {
1905 FlipTable(&(functions[i]),0);
1913 for ( i = MAXBUILTINFUNCTION-FUNCTION; i < AC.FunctionList.num; i++ ) {
1914 if ( ( T = functions[i].tabl ) != 0 ) {
1915 if ( T->
spare ) FlipTable(&(functions[i]),0);
1925 ReWorkT(term,funs,numfuns);
1948 int ApplyExec(WORD *term,
int maxtogo, WORD level)
1951 WORD rhsnumber, *Tpattern, *funs, numfuns, funnum;
1952 WORD ii, *t, *t1, *w, *p, *m, *m1, *u, *r, tbufnum, csize, wilds;
1954 int i, j, isp, stilltogo;
1964 AT.NestPoin->termsize = t;
1965 if ( AT.NestPoin == AT.Nest ) AN.EndNest = t + *t;
1971 if ( *t < FUNCTION ) { t += t[1];
continue; }
1972 if ( functions[*t-FUNCTION].spec > 0 ) { t += t[1];
continue; }
1973 AT.NestPoin->funsize = t;
1977 if ( *t < 0 ) { NEXTARG(t);
continue; }
1978 AT.NestPoin->argsize = t1 = t;
1995 stilltogo = ApplyExec(t,maxtogo,level);
1996 if ( stilltogo != maxtogo ) {
1997 if ( stilltogo <= 0 ) {
2001 maxtogo = stilltogo;
2003 m = term + *term - csize;
2013 C = cbuf+AM.rbufnum;
2016 if ( *t != TABLESTUB ) { t += t[1];
continue; }
2017 funnum = -t[FUNHEAD];
2018 if ( ( funnum < FUNCTION )
2019 || ( funnum >= FUNCTION+WILDOFFSET )
2020 || ( ( T = functions[funnum-FUNCTION].tabl ) == 0 )
2022 || ( T->
spare == 0 ) ) { t += t[1];
continue; }
2023 numfuns = C->
lhs[level][1] - 3;
2024 funs = C->
lhs[level] + 3;
2025 if ( numfuns > 0 ) {
2026 for ( i = 0; i < numfuns; i++ ) {
2027 if ( funs[i] == funnum )
break;
2029 if ( i >= numfuns ) { t += t[1];
continue; }
2032 AT.NestPoin->funsize = t + 1;
2042 Tpattern = T->
pattern[identity];
2046 p = Tpattern+FUNHEAD+1;
2047 for ( i = 0; i < T->
numind; i++, t += 2 ) {
2048 if ( *t != -SNUMBER )
break;
2050 if ( i < T->numind ) { t = r;
continue; }
2051 isp = FindTableTree(T,t1+FUNHEAD+1,2);
2052 if ( isp < 0 ) { t = r;
continue; }
2054 #if ( TABLEEXTENSION == 2 ) 2061 while ( --ii >= 0 ) {
2062 *p = *t; t += 2; p += 2;
2074 AN.WildValue = AN.FullProto + SUBEXPSIZE;
2075 AN.WildStop = AN.FullProto+AN.FullProto[1];
2078 AN.RepFunList = AN.EndNest;
2079 AT.WorkPointer = (WORD *)(((UBYTE *)(AN.EndNest)) + AM.MaxTer/2);
2084 if ( AT.WorkPointer + t1[1] >= AT.WorkTop ) { MesWork(); }
2087 *w++ = -t1[FUNHEAD];
2089 for ( i = 2; i < FUNHEAD; i++ ) *w++ = t1[i];
2091 while ( t < r ) *w++ = *t++;
2094 if ( MatchFunction(BHEAD Tpattern,t,&wilds) > 0 ) {
2115 while ( r < m1 ) *t++ = *r++;
2119 while ( NN > AT.Nest ) {
2121 NN->termsize[0] += j;
2122 NN->funsize[1] += j;
2123 NN->argsize[0] += j;
2124 NN->funsize[2] |= DIRTYFLAG;
2125 NN->argsize[1] |= DIRTYFLAG;
2132 while ( t >= r ) { t[j] = *t; t--; }
2138 while ( NN > AT.Nest ) {
2140 NN->termsize[0] += j;
2141 NN->funsize[1] += j;
2142 NN->argsize[0] += j;
2143 NN->funsize[2] |= DIRTYFLAG;
2144 NN->argsize[1] |= DIRTYFLAG;
2153 if ( maxtogo <= 0 )
return(maxtogo);
2165 WORD ApplyReset(WORD level)
2167 WORD *funs, numfuns;
2170 CBUF *C = cbuf+AM.rbufnum;
2172 numfuns = C->
lhs[level][1] - 2;
2173 funs = C->
lhs[level] + 2;
2174 if ( numfuns > 0 ) {
2175 for ( i = MAXBUILTINFUNCTION-FUNCTION; i < AC.FunctionList.num; i++ ) {
2176 if ( ( T = functions[i].tabl ) != 0 ) {
2177 for ( j = 0; j < numfuns; j++ ) {
2178 if ( i == (funs[j]-FUNCTION) && T->
spare ) {
2179 FlipTable(&(functions[i]),1);
2187 for ( i = MAXBUILTINFUNCTION-FUNCTION; i < AC.FunctionList.num; i++ ) {
2188 if ( ( T = functions[i].tabl ) != 0 ) {
2189 if ( T->
spare ) FlipTable(&(functions[i]),1);
2206 for ( i = MAXBUILTINFUNCTION-FUNCTION; i < AC.FunctionList.num; i++ ) {
2207 if ( ( T = functions[i].tabl ) != 0 && T->
spare && T->
mode == 0 ) {
2208 functions[i].tabl = T->
spare;
2232 for ( i = NumTableBases - 1; i >= 0; i-- ) {
WORD Generator(PHEAD WORD *, WORD)