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);
175 MesPrint(
"!>Serious problems in InsTableTree!\n");
187 if ( q->
blnce == 0 )
break;
188 if ( q->
blnce == -2 ) {
189 if ( p->blnce == -1 ) {
194 if ( boomlijst[p->parent].left == iq ) boomlijst[p->parent].
left = ip;
195 else boomlijst[p->parent].
right = ip;
197 q->
blnce = p->blnce = 0;
205 if ( p->right >= 0 ) boomlijst[p->right].
parent = ip;
210 if ( boomlijst[s->
parent].left == iq )
219 else if ( q->
blnce == 2 ) {
220 if ( p->blnce == 1 ) {
225 if ( boomlijst[p->parent].left == iq ) boomlijst[p->parent].
left = ip;
226 else boomlijst[p->parent].
right = ip;
228 q->
blnce = p->blnce = 0;
236 if ( p->left >= 0 ) boomlijst[p->left].
parent = ip;
268void RedoTableTree(
TABLES T,
int newsize)
275 tp += ABS(T->
numind)+TABLEEXTENSION;
293int FindTableTree(
TABLES T, WORD *tp,
int inc)
296 WORD *v1, *v2, *v3, xstop;
298 if ( q->
right == -1 )
return(-1);
300 if ( inc > 1 ) tp += inc-1;
303 if ( *tp <= 0 )
return(-1);
309 v2 = tp; v3 = v1 + xstop;
310 while ( *v1 == *v2 && v1 < v3 ) { v1++; v2 += inc; }
317 if ( iq >= 0 ) { ip = iq; }
320 else if ( *v1 < *v2 ) {
322 if ( iq >= 0 ) { ip = iq; }
327 MesPrint(
"!>Serious problems in FindTableTree\n");
338int DoTableExpansion(WORD *term, WORD level)
341 WORD *t, *tstop, *stopper, *termout, *m, *mm, *tp, *r, xx;
342 WORD numsubexp, numbuf;
345 AN.TeInFun = AR.TePos = 0;
346 tstop = term + *term;
347 stopper = tstop - ABS(tstop[-1]);
349 while ( t < stopper ) {
350 if ( *t != TABLEFUNCTION ) { t += t[1];
continue; }
351 if ( t[FUNHEAD] > -FUNCTION ) { t += t[1];
continue; }
352 T = functions[-t[FUNHEAD]-FUNCTION].tabl;
353 if ( T == 0 ) { t += t[1];
continue; }
355 if ( t[1] == FUNHEAD+2 && t[FUNHEAD+1] <= -FUNCTION )
break;
356 if ( t[1] < FUNHEAD+1+2*ABS(T->
numind) ) { t += t[1];
continue; }
357 for ( i = 0; i < ABS(T->
numind); i++ ) {
358 if ( t[FUNHEAD+1+2*i] != -SYMBOL )
break;
360 if ( i >= ABS(T->
numind) )
break;
363 if ( t >= stopper ) {
365 MesPrint(
"!>Internal error: Missing table_ function");
372 termout = AT.WorkPointer;
374 for ( i = 0; i < T->
totind; i++ ) {
378 m = termout + 1; mm = term + 1;
379 while ( mm < t ) *m++ = *mm++;
381 if ( t[1] == FUNHEAD+2 && t[FUNHEAD+1] <= -FUNCTION ) {
382 *m++ = -t[FUNHEAD+1];
387 for ( j = 2; j < FUNHEAD; j++ ) *m++ = 0;
388 for ( j = 0; j < xx; j++ ) {
389 *m++ = -SNUMBER; *m++ = *tp++;
393 *m++ = FUNHEAD+T->
numind*2;
394 for ( j = 2; j < FUNHEAD; j++ ) *m++ = 0;
395 for ( j = 0; j < T->
numind; j++ ) {
396 *m++ = -SNUMBER; *m++ = *tp++;
400 else if ( T->
numind < 0 ) {
403 *m++ = SYMBOL; *m++ = 2+xx*2; mm = t + FUNHEAD+1;
404 for ( j = 0; j < xx; j++, mm += 2, tp++ ) {
405 if ( *tp != 0 ) { *m++ = mm[1]; *m++ = *tp; }
408 if ( r[1] == 2 ) m = r;
411 *m++ = SYMBOL; *m++ = 2+T->
numind*2; mm = t + FUNHEAD+1;
413 for ( j = 0; j < T->
numind; j++, mm += 2, tp++ ) {
414 if ( *tp != 0 ) { *m++ = mm[1]; *m++ = *tp; }
417 if ( r[1] == 2 ) m = r;
438 numsubexp = tp[0]; numbuf = tp[1];
445 for ( j = 0; j < tp[1]; j++ ) *m++ = tp[j];
446 r[2] = numsubexp; r[4] = numbuf;
472 while ( mm < tstop ) *m++ = *mm++;
473 *termout = m - termout;
476 MesCall(
"DoTableExpand");
479 AT.WorkPointer = termout;
483 for ( i = 0; i < T->
totind; i++ ) {
484#if TABLEEXTENSION == 2
489 m = termout + 1; mm = term + 1;
490 while ( mm < t ) *m++ = *mm++;
492 if ( t[1] == FUNHEAD+2 && t[FUNHEAD+1] <= -FUNCTION ) {
493 *m++ = -t[FUNHEAD+1];
494 *m++ = FUNHEAD+T->
numind*2;
495 for ( j = 2; j < FUNHEAD; j++ ) *m++ = 0;
497 for ( j = 0; j < T->
numind; j++ ) {
504 *m++ = -SNUMBER; *m++ = num;
508 *m++ = SYMBOL; *m++ = 2+T->
numind*2; mm = t + FUNHEAD+1;
509 for ( j = 0; j < T->
numind; j++, mm += 2 ) {
516 if ( num != 0 ) { *m++ = mm[1]; *m++ = num; }
519 if ( r[1] == 2 ) m = r;
541 for ( j = 2; j < FUNHEAD; j++ ) *m++ = t[j];
542 for ( j = 0; j < T->
numind; j++ ) {
549 *m++ = -SNUMBER; *m++ = num;
551 tp = t + FUNHEAD + 1 + 2*T->
numind;
553 while ( tp < mm ) *m++ = *tp++;
558 while ( mm < tstop ) *m++ = *mm++;
559 *termout = m - termout;
562 MesCall(
"DoTableExpand");
617 {
"addto", (TFUN)CoTBaddto, 0, PARTEST}
618 ,{
"audit", (TFUN)CoTBaudit, 0, PARTEST}
619 ,{
"cleanup", (TFUN)CoTBcleanup, 0, PARTEST}
620 ,{
"create", (TFUN)CoTBcreate, 0, PARTEST}
621 ,{
"enter", (TFUN)CoTBenter, 0, PARTEST}
622 ,{
"help", (TFUN)CoTBhelp, 0, PARTEST}
623 ,{
"load", (TFUN)CoTBload, 0, PARTEST}
624 ,{
"off", (TFUN)CoTBoff, 0, PARTEST}
625 ,{
"on", (TFUN)CoTBon, 0, PARTEST}
626 ,{
"open", (TFUN)CoTBopen, 0, PARTEST}
627 ,{
"replace", (TFUN)CoTBreplace, 0, PARTEST}
628 ,{
"use", (TFUN)CoTBuse, 0, PARTEST}
631static UBYTE *tablebasename = 0;
633int CoTableBase(UBYTE *s)
635 UBYTE *option, c, *t;
636 int i,optlistsize =
sizeof(tboptions)/
sizeof(
KEYWORD), error = 0;
637 while ( *s ==
' ' ) s++;
639 if ( ( tolower(*s) ==
'h' ) && ( tolower(s[1]) ==
'e' )
640 && ( tolower(s[2]) ==
'l' ) && ( tolower(s[3]) ==
'p' )
641 && ( FG.cTable[s[4]] > 1 ) ) {
646 MesPrint(
"&Proper syntax: TableBase \"filename\" options");
649 s++; tablebasename = s;
650 while ( *s && *s !=
'"' ) s++;
651 if ( *s !=
'"' )
goto proper;
653 while ( *s ==
' ' || *s ==
'\t' || *s ==
',' ) s++;
655 while ( FG.cTable[*s] == 0 ) s++;
657 for ( i = 0; i < optlistsize; i++ ) {
658 if ( StrICmp(option,(UBYTE *)(tboptions[i].name)) == 0 ) {
660 while ( *s ==
',' ) s++;
661 error = (tboptions[i].func)(s);
666 MesPrint(
"&Unrecognized option %s in TableBase statement",option);
681 if ( ( TT = T->
spare ) == 0 ) {
682 MesPrint(
"Error: trying to change mode on a table that has no tablebase");
685 if ( TT->
mode == type ) f->
tabl = TT;
726 T->
flags = (WORD *)Malloc1(ABS(T->
numind)*
sizeof(WORD),
"table flags");
740DBASE *FindTB(UBYTE *name)
744 for ( i = 0; i < NumTableBases; i++ ) {
746 if ( d->name && ( StrCmp(name,(UBYTE *)(d->name)) == 0 ) ) {
return(d); }
762int CoTBcreate(UBYTE *s)
765 if ( FindTB(tablebasename) != 0 ) {
766 MesPrint(
"&There is already an open TableBase with the name %s",tablebasename);
769 NewDbase((
char *)tablebasename,0);
778int CoTBopen(UBYTE *s)
786 if ( ConsumeOption(&s,
"readonly") != 0 ) {
789 MesPrint(
"&Invalid option for TableBase open: %s, ignoring", s);
793 if ( ( d = FindTB(tablebasename) ) != 0 ) {
794 MesPrint(
"&There is already an open TableBase with the name %s",tablebasename);
797 d = GetDbase((
char *)tablebasename, rw);
798 if ( CheckTableDeclarations(d) )
return(-1);
807int CoTBaddto(UBYTE *s)
811 UBYTE *tablename, c, *t, elementstring[ELEMENTSIZE+20], *ss, *es;
812 WORD type, funnum, lbrac, first, num, *expr, *w;
816 int i, j, error = 0, sum;
817 if ( ( d = FindTB(tablebasename) ) == 0 ) {
818 MesPrint(
"&No open tablebase with the name %s",tablebasename);
822 if ( ( d->rwmode ) == 0 ) {
823 MesPrint(
"&Tablebase with the name %s opened in read only mode",tablebasename);
826 AO.DollarOutSizeBuffer = 32;
827 AO.DollarOutBuffer = (UBYTE *)Malloc1(AO.DollarOutSizeBuffer,
832 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
835 if ( ( s =
SkipAName(s) ) == 0 )
goto tableabort;
837 if ( ( GetVar(tablename,&type,&funnum,CFUNCTION,NOAUTO) == NAMENOTFOUND )
838 || ( T = functions[funnum].tabl ) == 0 ) {
839 MesPrint(
"&%s should be a previously declared table",tablename);
840 *s = c;
goto tableabort;
843 MesPrint(
"&%s should be a sparse table",tablename);
844 *s = c;
goto tableabort;
846 basenumber = AddTableName(d,(
char *)tablename,T);
848 if ( basenumber < 0 ) basenumber = -basenumber;
849 else if ( basenumber == 0 ) { *s = c;
goto tableabort; }
853 for ( i = 0, w = AT.WorkPointer; i < ABS(T->
numind); i++ ) {
854 ParseSignedNumber(x,s);
855 if ( FG.cTable[s[-1]] != 1 || ( *s !=
',' && *s !=
')' ) ) {
856 MesPrint(
"&Table arguments in TableBase addto statement should be numbers");
860 if ( *s ==
')' )
break;
863 if ( *s !=
')' || i < ( ABS(T->
numind) - 1 ) ) {
864 MesPrint(
"&Incorrect number of table arguments in TableBase addto statement. Should be %d"
869 i = FindTableTree(T,AT.WorkPointer,1);
871 MesPrint(
"&Element %s has not been defined",es);
875 else if ( ExistsObject(d,basenumber,(
char *)es) ) {}
877 int dict = AO.CurrentDictionary;
878 AO.CurrentDictionary = 0;
883 AO.DollarInOutBuffer = 1;
885 ss = AO.DollarOutBuffer;
888#if ( TABLEEXTENSION == 2 )
893 lbrac = 0; first = 0;
895 if ( WriteTerm(expr,&lbrac,first,PRINTON,0) ) {
901 AddObject(d,basenumber,(
char *)es,(
char *)(AO.DollarOutBuffer));
903 AO.CurrentDictionary = dict;
911 for ( i = 0; i < T->
totind; i++ ) {
912#if ( TABLEEXTENSION == 2 )
917 sum = i * ( ABS(T->
numind) + TABLEEXTENSION );
919 for ( j = 0; j < ABS(T->
numind); j++, sum++ ) {
920 if ( j > 0 ) *t++ =
',';
923 if ( ( t - elementstring ) >= ELEMENTSIZE ) {
924 MesPrint(
"&Table element specification takes more than %ld characters and cannot be handled",
929 if ( ExistsObject(d,basenumber,(
char *)elementstring) ) {
continue; }
936 AO.DollarInOutBuffer = 1;
938 ss = AO.DollarOutBuffer;
941#if ( TABLEEXTENSION == 2 )
946 lbrac = 0; first = 0;
948 if ( WriteTerm(expr,&lbrac,first,PRINTON,0) ) {
954 AddObject(d,basenumber,(
char *)elementstring,(
char *)(AO.DollarOutBuffer));
957 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
959 if ( WriteIniInfo(d) )
goto tableabort;
960 M_free(AO.DollarOutBuffer,
"DollarOutBuffer");
961 AO.DollarOutBuffer = 0;
962 AO.DollarOutSizeBuffer = 0;
965 M_free(AO.DollarOutBuffer,
"DollarOutBuffer");
966 AO.DollarOutBuffer = 0;
967 AO.DollarOutSizeBuffer = 0;
980int CoTBenter(UBYTE *s)
984 UBYTE *arguments, *rhs, *buffer, *t, *u, c, *tablename;
986 int i, j, error = 0, error1 = 0, printall = 0;
989 int dict = AO.CurrentDictionary;
990 AO.CurrentDictionary = 0;
991 if ( ( d = FindTB(tablebasename) ) == 0 ) {
992 MesPrint(
"&No open tablebase with the name %s, check for existence of file or try readonly mode when opening.",tablebasename);
996 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
997 if ( *s ==
'!' ) { printall = 1; s++; }
998 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
1002 if ( ( s =
SkipAName(s) ) == 0 ) { error = 1;
goto Endofall; }
1004 if ( ( GetVar(tablename,&type,&funnum,CFUNCTION,NOAUTO) == NAMENOTFOUND )
1005 || ( T = functions[funnum].tabl ) == 0 ) {
1006 MesPrint(
"&%s should be a previously declared table",tablename);
1009 else if ( T->
sparse == 0 ) {
1010 MesPrint(
"&%s should be a sparse table",tablename);
1013 else { basenumber = GetTableName(d,(
char *)tablename); }
1014 if ( T->
spare == 0 ) { SpareTable(T); }
1015 if ( basenumber > 0 ) {
1016 for ( i = 0; i < d->info.numberofindexblocks; i++ ) {
1017 for ( j = 0; j < NUMOBJECTS; j++ ) {
1018 if ( basenumber != d->iblocks[i]->objects[j].tablenumber )
1020 arguments = (UBYTE *)(d->iblocks[i]->objects[j].element);
1021 rhs = (UBYTE *)ReadObject(d,basenumber,(
char *)arguments);
1024 MesPrint(
"%s(%s) = %s",tablename,arguments,rhs);
1027 MesPrint(
"%s(%s) = 0",tablename,arguments);
1031 u = rhs;
while ( *u ) u++;
1033 u = arguments;
while ( *u ) u++;
1034 size += u-arguments;
1035 u = tablename;
while ( *u ) u++;
1036 size += u-tablename;
1038 buffer = (UBYTE *)Malloc1(size,
"TableBase copy");
1039 t = tablename; u = buffer;
1040 while ( *t ) *u++ = *t++;
1043 while ( *t ) *u++ = *t++;
1044 *u++ =
')'; *u++ =
'=';
1046 while ( *t ) *u++ = *t++;
1047 if ( t == rhs ) *u++ =
'0';
1049 M_free(rhs,
"rhs in TBenter");
1051 error1 = CoFill(buffer);
1053 if ( error1 < 0 )
goto Endofall;
1054 if ( error1 != 0 ) error = error1;
1055 M_free(buffer,
"TableBase copy");
1061 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
1065 s = (UBYTE *)(d->tablenames); basenumber = 0;
1068 tablename = s;
while ( *s ) s++; s++;
1071 if ( ( GetVar(tablename,&type,&funnum,CFUNCTION,NOAUTO) == NAMENOTFOUND )
1072 || ( T = functions[funnum].tabl ) == 0 ) {
1073 MesPrint(
"&%s should be a previously declared table",tablename);
1075 else if ( T->
sparse == 0 ) {
1076 MesPrint(
"&%s should be a sparse table",tablename);
1078 if ( T->
spare == 0 ) { SpareTable(T); }
1079 for ( i = 0; i < d->info.numberofindexblocks; i++ ) {
1080 for ( j = 0; j < NUMOBJECTS; j++ ) {
1081 if ( d->iblocks[i]->objects[j].tablenumber == basenumber ) {
1082 arguments = (UBYTE *)(d->iblocks[i]->objects[j].element);
1083 rhs = (UBYTE *)ReadObject(d,basenumber,(
char *)arguments);
1086 MesPrint(
"%s%s = %s",tablename,arguments,rhs);
1089 MesPrint(
"%s%s = 0",tablename,arguments);
1093 u = rhs;
while ( *u ) u++;
1095 u = arguments;
while ( *u ) u++;
1096 size += u-arguments;
1097 u = tablename;
while ( *u ) u++;
1098 size += u-tablename;
1100 buffer = (UBYTE *)Malloc1(size,
"TableBase copy");
1101 t = tablename; u = buffer;
1102 while ( *t ) *u++ = *t++;
1105 while ( *t ) *u++ = *t++;
1106 *u++ =
')'; *u++ =
'=';
1108 while ( *t ) *u++ = *t++;
1109 if ( t == rhs ) *u++ =
'0';
1111 M_free(rhs,
"rhs in TBenter");
1113 error1 = CoFill(buffer);
1115 if ( error1 < 0 )
goto Endofall;
1116 if ( error1 != 0 ) error = error1;
1117 M_free(buffer,
"TableBase copy");
1125 AO.CurrentDictionary = dict;
1139int CoTestUse(UBYTE *s)
1142 UBYTE *tablename, c;
1143 WORD type, funnum, *w;
1147 *w++ = TYPETESTUSE; *w++ = 2;
1150 if ( ( s =
SkipAName(s) ) == 0 )
return(1);
1152 if ( ( GetVar(tablename,&type,&funnum,CFUNCTION,NOAUTO) == NAMENOTFOUND )
1153 || ( T = functions[funnum].tabl ) == 0 ) {
1154 MesPrint(
"&%s should be a previously declared table",tablename);
1157 else if ( T->
sparse == 0 ) {
1158 MesPrint(
"&%s should be a sparse table",tablename);
1161 *w++ = funnum + FUNCTION;
1163 while ( *s ==
' ' || *s ==
',' || *s ==
'\t' ) s++;
1165 AT.WorkPointer[1] = w - AT.WorkPointer;
1171 AddNtoL(AT.WorkPointer[1],AT.WorkPointer);
1184int CheckTableDeclarations(
DBASE *d)
1187 UBYTE *s, *ss, *t, *command = 0;
1188 int k, error = 0, error1, i;
1190 LONG commandsize = 0;
1192 s = (UBYTE *)(d->tablenames);
1193 for ( k = 0; k < d->topnumber; k++ ) {
1194 if ( GetVar(s,&type,&funnum,ANYTYPE,NOAUTO) == NAMENOTFOUND ) {
1198 ss = s; i = 0;
while ( *ss ) { ss++; i++; }
1199 ss++;
while ( *ss ) { ss++; i++; }
1200 if ( commandsize == 0 ) {
1201 commandsize = i + 15;
1202 if ( commandsize < 100 ) commandsize = 100;
1204 if ( (i+11) > commandsize ) {
1205 if ( command ) { M_free(command,
"table command"); command = 0; }
1208 if ( command == 0 ) {
1209 command = (UBYTE *)Malloc1(commandsize,
"table command");
1211 t = command; ss = tablebase;
while ( *ss ) *t++ = *ss++;
1212 *t++ =
',';
while ( *s ) *t++ = *s++;
1213 s++;
while ( *s ) *t++ = *s++;
1214 *t++ =
')'; *t = 0; s++;
1215 error1 = DoTable(command,1);
1216 if ( error1 ) error = error1;
1218 else if ( ( type != CFUNCTION )
1219 || ( ( T = functions[funnum].tabl ) == 0 )
1220 || ( T->
sparse == 0 ) ) {
1221 MesPrint(
"&%s has been declared previously, but not as a sparse table.",s);
1237 MesPrint(
"&Declaration of table %s in %s different from previous declaration",ss,d->name);
1244 if ( command ) { M_free(command,
"table command"); }
1258int CoTBload(UBYTE *ss)
1261 UBYTE *s, *name, *t, *r, *command, *arguments, *tail;
1263 int num, cs, es, ns, ts, i, j, error = 0, error1;
1264 if ( ( d = FindTB(tablebasename) ) == 0 ) {
1265 MesPrint(
"&No open tablebase with the name %s",tablebasename);
1269 command = (UBYTE *)Malloc1(commandsize,
"Fill command");
1272 while ( *ss ==
',' || *ss ==
' ' || *ss ==
'\t' ) ss++;
1275 s = (UBYTE *)(d->tablenames);
1279 if ( StrCmp(s,name) ) {
1287 name = s;
while ( *s ) s++; ns = s-name; s++;
1288 tail = s;
while ( *s ) s++; ts = s-tail; s++;
1289 tail++;
while ( FG.cTable[*tail] == 1 ) tail++;
1293 for ( i = 0; i < d->info.numberofindexblocks; i++ ) {
1294 for ( j = 0; j < NUMOBJECTS; j++ ) {
1295 if ( d->iblocks[i]->objects[j].tablenumber == num ) {
1296 t = arguments = (UBYTE *)(d->iblocks[i]->objects[j].element);
1299 cs = 2*es + 2*ns + ts + 10;
1300 if ( cs > commandsize ) {
1302 if ( command ) M_free(command,
"Fill command");
1303 command = (UBYTE *)Malloc1(commandsize,
"Fill command");
1305 r = command; t = name;
while ( *t ) *r++ = *t++;
1306 *r++ =
'('; t = arguments;
while ( *t ) *r++ = *t++;
1307 *r++ =
')'; *r++ =
'='; *r++ =
't'; *r++ =
'b'; *r++ =
'l';
1308 *r++ =
'_'; *r++ =
'('; t = name;
while ( *t ) *r++ = *t++;
1309 *r++ =
','; t = arguments;
while ( *t ) *r++ = *t++;
1310 t = tail;
while ( *t ) {
1311 if ( *t ==
'?' && r[-1] !=
',' ) {
1313 if ( FG.cTable[*t] == 0 || *t ==
'$' || *t ==
'[' ) {
1319 else if ( *t ==
'{' ) {
1322 else if ( *t ) { *r++ = *t++;
continue; }
1330 AC.vetotablebasefill = 1;
1331 error1 = CoFill(command);
1332 AC.vetotablebasefill = 0;
1333 if ( error1 < 0 )
goto finishup;
1334 if ( error1 != 0 ) error = error1;
1340 while ( *ss ==
',' || *ss ==
' ' || *ss ==
'\t' ) ss++;
1344 s = (UBYTE *)(d->tablenames);
1348 name = s;
while ( *s ) s++; ns = s-name; s++;
1349 tail = s;
while ( *s ) s++; ts = s-tail; s++;
1350 tail++;
while ( FG.cTable[*tail] == 1 ) tail++;
1354 for ( i = 0; i < d->info.numberofindexblocks; i++ ) {
1355 for ( j = 0; j < NUMOBJECTS; j++ ) {
1356 if ( d->iblocks[i]->objects[j].tablenumber == num ) {
1357 t = arguments = (UBYTE *)(d->iblocks[i]->objects[j].element);
1360 cs = 2*es + 2*ns + ts + 10;
1361 if ( cs > commandsize ) {
1363 if ( command ) M_free(command,
"Fill command");
1364 command = (UBYTE *)Malloc1(commandsize,
"Fill command");
1366 r = command; t = name;
while ( *t ) *r++ = *t++;
1367 *r++ =
'('; t = arguments;
while ( *t ) *r++ = *t++;
1368 *r++ =
')'; *r++ =
'='; *r++ =
't'; *r++ =
'b'; *r++ =
'l';
1369 *r++ =
'_'; *r++ =
'('; t = name;
while ( *t ) *r++ = *t++;
1370 *r++ =
','; t = arguments;
while ( *t ) *r++ = *t++;
1371 t = tail;
while ( *t ) {
1372 if ( *t ==
'?' && r[-1] !=
',' ) {
1374 if ( FG.cTable[*t] == 0 || *t ==
'$' || *t ==
'[' ) {
1380 else if ( *t ==
'{' ) {
1383 else if ( *t ) { *r++ = *t++;
continue; }
1391 AC.vetotablebasefill = 1;
1392 error1 = CoFill(command);
1393 AC.vetotablebasefill = 0;
1394 if ( error1 < 0 )
goto finishup;
1395 if ( error1 != 0 ) error = error1;
1403 if ( command ) M_free(command,
"Fill command");
1420int TestUse(WORD *term, WORD level)
1422 WORD *tstop, *t, *m, *tstart, tabnum;
1423 WORD *funs, numfuns;
1427 CBUF *C = cbuf+AM.rbufnum;
1430 numfuns = C->
lhs[level][1] - 2;
1431 funs = C->
lhs[level] + 2;
1432 GETSTOP(term,tstop);
1434 while ( t < tstop ) {
1435 if ( *t != TABLESTUB ) { t += t[1];
continue; }
1439 if ( *m >= -FUNCTION )
continue;
1441 if ( ( T = functions[tabnum-FUNCTION].tabl ) == 0 )
continue;
1442 if ( T->
sparse == 0 )
continue;
1446 if ( numfuns > 0 ) {
1447 for ( i = 0; i < numfuns; i++ ) {
1448 if ( tabnum == funs[i] )
break;
1450 if ( i >= numfuns && numfuns > 0 )
continue;
1458 for ( i = 0; i < ABS(T->
numind); i++, m += 2 ) {
1459 if ( m >= t || *m != -SNUMBER )
break;
1461 if ( ( i == ABS(T->
numind) ) &&
1462 ( ( isp = FindTableTree(T,tstart+FUNHEAD+1,2) ) >= 0 ) ) {
1469 MesPrint(
"!>TestUse: Encountered a table element inside tbl_ that does not correspond to a tablebase element");
1482int CoTBaudit(UBYTE *s)
1486 int i, j, error = 0, num;
1488 if ( ( d = FindTB(tablebasename) ) == 0 ) {
1489 MesPrint(
"&No open tablebase with the name %s",tablebasename);
1492 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
1500 s = (UBYTE *)(d->tablenames); num = 0;
1503 name = s;
while ( *s ) s++; s++;
1504 tail = s;
while ( *s ) s++; s++;
1505 MesPrint(
"Table,sparse,%s%s)",name,tail);
1506 for ( i = 0; i < d->info.numberofindexblocks; i++ ) {
1507 for ( j = 0; j < NUMOBJECTS; j++ ) {
1508 if ( d->iblocks[i]->objects[j].tablenumber == num ) {
1509 MesPrint(
" %s(%s)",name,d->iblocks[i]->objects[j].element);
1527 if ( ( d = FindTB(tablebasename) ) == 0 ) {
1528 MesPrint(
"&No open tablebase with the name %s",tablebasename);
1531 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
1535 if ( StrICmp(s,(UBYTE *)(
"compress")) == 0 ) {
1536 d->mode &= ~NOCOMPRESS;
1539 MesPrint(
"&subkey %s not defined in TableBase On statement");
1543 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
1553int CoTBoff(UBYTE *s)
1558 if ( ( d = FindTB(tablebasename) ) == 0 ) {
1559 MesPrint(
"&No open tablebase with the name %s",tablebasename);
1562 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
1566 if ( StrICmp(s,(UBYTE *)(
"compress")) == 0 ) {
1567 d->mode |= NOCOMPRESS;
1570 MesPrint(
"&subkey %s not defined in TableBase Off statement");
1574 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
1584int CoTBcleanup(UBYTE *s)
1587 MesPrint(
"&TableBase Cleanup statement not yet implemented");
1596int CoTBreplace(UBYTE *s)
1599 MesPrint(
"&TableBase Replace statement not yet implemented");
1611int CoTBuse(UBYTE *s)
1616 UBYTE *arguments, *rhs, *buffer, *t, *u, c, *tablename, *p;
1618 int i, j, error = 0, error1 = 0, k;
1620 WORD type, funnum, mode, *w;
1621 if ( ( d = FindTB(tablebasename) ) == 0 ) {
1622 MesPrint(
"&No open tablebase with the name %s",tablebasename);
1625 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
1629 if ( ( s =
SkipAName(s) ) == 0 )
return(1);
1631 if ( ( GetVar(tablename,&type,&funnum,CFUNCTION,NOAUTO) == NAMENOTFOUND )
1632 || ( T = functions[funnum].tabl ) == 0 ) {
1633 MesPrint(
"&%s should be a previously declared table",tablename);
1636 else if ( T->
sparse == 0 ) {
1637 MesPrint(
"&%s should be a sparse table",tablename);
1640 else { basenumber = GetTableName(d,(
char *)tablename); }
1642 if ( basenumber > 0 ) {
1643 for ( i = 0; i < d->info.numberofindexblocks; i++ ) {
1644 for ( j = 0; j < NUMOBJECTS; j++ ) {
1645 if ( d->iblocks[i]->objects[j].tablenumber != basenumber )
continue;
1646 arguments = p = (UBYTE *)(d->iblocks[i]->objects[j].element);
1651 for ( k = 0, w = AT.WorkPointer; k < ABS(T->
numind); k++ ) {
1652 ParseSignedNumber(x,p);
1655 sum = FindTableTree(T,AT.WorkPointer,1);
1657 MesPrint(
"Table %s in tablebase %s has not been loaded properly"
1658 ,tablename,tablebasename);
1662 sum += ABS(T->
numind) + 4;
1664 if ( ( mode & ELEMENTLOADED ) == ELEMENTLOADED ) {
1668 if ( ( mode & ELEMENTUSED ) == 0 )
continue;
1672 rhs = (UBYTE *)ReadijObject(d,i,j,(
char *)arguments);
1674 u = rhs;
while ( *u ) u++;
1676 u = arguments;
while ( *u ) u++;
1677 size += u-arguments;
1678 u = tablename;
while ( *u ) u++;
1679 size += u-tablename;
1681 buffer = (UBYTE *)Malloc1(size,
"TableBase copy");
1682 t = tablename; u = buffer;
1683 while ( *t ) *u++ = *t++;
1686 while ( *t ) *u++ = *t++;
1687 *u++ =
')'; *u++ =
'=';
1689 while ( *t ) *u++ = *t++;
1690 if ( t == rhs ) { *u++ =
'0'; }
1692 M_free(rhs,
"rhs in TBuse xxx");
1694 error1 = CoFill(buffer);
1696 if ( error1 < 0 ) {
return(error); }
1697 if ( error1 != 0 ) error = error1;
1698 M_free(buffer,
"TableBase copy");
1706 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
1710 s = (UBYTE *)(d->tablenames); basenumber = 0;
1718 if ( ( GetVar(tablename,&type,&funnum,CFUNCTION,NOAUTO) == NAMENOTFOUND )
1719 || ( T = functions[funnum].tabl ) == 0 ) {
1720 MesPrint(
"&%s should be a previously declared table",tablename);
1722 else if ( T->
sparse == 0 ) {
1723 MesPrint(
"&%s should be a sparse table",tablename);
1727 MesPrint(
"!>In table %s we have a problem with stubb orders in CoTBuse",tablename);
1732 for ( i = 0; i < d->info.numberofindexblocks; i++ ) {
1733 for ( j = 0; j < NUMOBJECTS; j++ ) {
1734 if ( d->iblocks[i]->objects[j].tablenumber == basenumber ) {
1735 arguments = p = (UBYTE *)(d->iblocks[i]->objects[j].element);
1740 for ( k = 0, w = AT.WorkPointer; k < ABS(T->
numind); k++ ) {
1741 ParseSignedNumber(x,p);
1744 sum = FindTableTree(T,AT.WorkPointer,1);
1747 MesPrint(
"!>Table %s in tablebase %s has not been loaded properly"
1748 ,tablename,tablebasename);
1753 sum += ABS(T->
numind) + 4;
1755 if ( ( mode & ELEMENTLOADED ) == ELEMENTLOADED ) {
1759 if ( ( mode & ELEMENTUSED ) == 0 )
continue;
1763 rhs = (UBYTE *)ReadijObject(d,i,j,(
char *)arguments);
1765 u = rhs;
while ( *u ) u++;
1767 u = arguments;
while ( *u ) u++;
1768 size += u-arguments;
1769 u = tablename;
while ( *u ) u++;
1770 size += u-tablename;
1772 buffer = (UBYTE *)Malloc1(size,
"TableBase copy");
1773 t = tablename; u = buffer;
1774 while ( *t ) *u++ = *t++;
1777 while ( *t ) *u++ = *t++;
1778 *u++ =
')'; *u++ =
'=';
1781 while ( *t ) *u++ = *t++;
1782 if ( t == rhs ) { *u++ =
'0'; }
1784 M_free(rhs,
"rhs in TBuse");
1786 error1 = CoFill(buffer);
1788 if ( error1 < 0 ) {
return(error); }
1789 if ( error1 != 0 ) error = error1;
1790 M_free(buffer,
"TableBase copy");
1809int CoApply(UBYTE *s)
1812 UBYTE *tablename, c;
1813 WORD type, funnum, *w;
1815 LONG maxtogo = MAXPOSITIVE;
1818 if ( FG.cTable[*s] == 1 ) {
1820 while ( FG.cTable[*s] == 1 ) {
1821 maxtogo = maxtogo*10 + (*s-
'0');
1824 while ( *s ==
',' ) s++;
1825 if ( maxtogo > MAXPOSITIVE || maxtogo < 0 ) maxtogo = MAXPOSITIVE;
1827 *w++ = TYPEAPPLY; *w++ = 3; *w++ = maxtogo;
1830 if ( ( s =
SkipAName(s) ) == 0 )
return(1);
1832 if ( ( GetVar(tablename,&type,&funnum,CFUNCTION,NOAUTO) == NAMENOTFOUND )
1833 || ( T = functions[funnum].tabl ) == 0 ) {
1834 MesPrint(
"&%s should be a previously declared table",tablename);
1837 else if ( T->
sparse == 0 ) {
1838 MesPrint(
"&%s should be a sparse table",tablename);
1841 *w++ = funnum + FUNCTION;
1843 while ( *s ==
' ' || *s ==
',' || *s ==
'\t' ) s++;
1845 AT.WorkPointer[1] = w - AT.WorkPointer;
1851 AddNtoL(AT.WorkPointer[1],AT.WorkPointer);
1865 "The TableBase statement is used as follows:"
1866 ,
"TableBase \"file.tbl\" keyword subkey(s)"
1867 ,
" in which we have"
1868 ,
"Keyword Subkey(s) Action"
1869 ,
"open Opens file.tbl for R/W"
1870 ,
"create Creates file.tbl for R/W. Old contents are lost"
1871 ,
"load Loads all stubs of all tables"
1872 ,
"load tablename(s) Loads all stubs the tables mentioned"
1873 ,
"enter Loads all stubs and rhs of all tables"
1874 ,
"enter tablename(s) Loads all stubs and rhs of the tables mentioned"
1875 ,
"audit Prints list of contents"
1879 ,
"addto tablename adds all elements if not yet there"
1880 ,
"addto tableelement adds element if not yet there"
1883 ,
"on compress elements are stored in gzip format (default)"
1884 ,
"off compress elements are stored in uncompressed format"
1885 ,
"use compiles all needed elements"
1886 ,
"use tablename(s) compiles all needed elements of these tables"
1888 ,
"Related commands are:"
1889 ,
"testuse marks which tbl_ elements occur for all tables"
1890 ,
"testuse tablename(s) marks which tbl_ elements occur for given tables"
1891 ,
"apply replaces tbl_ if rhs available"
1892 ,
"apply tablename(s) replaces tbl_ for given tables if rhs available"
1896int CoTBhelp(UBYTE *s)
1898 int i, ii =
sizeof(helptb)/
sizeof(
char *);
1900 for ( i = 0; i < ii; i++ ) MesPrint(
"%s",helptb[i]);
1912void ReWorkT(WORD *term, WORD *funs, WORD numfuns)
1914 WORD *tstop, *tend, *m, *t, *tt, *mm, *mmm, *r, *rr;
1916 tend = term + *term; tstop = tend - ABS(tend[-1]);
1918 while ( t < tstop ) {
1919 if ( *t == TABLESTUB ) {
1920 for ( i = 0; i < numfuns; i++ ) {
1921 if ( -t[FUNHEAD] == funs[i] )
break;
1923 if ( numfuns == 0 || i < numfuns ) {
1925 *m++ = -t[FUNHEAD]; *m++ = i; t += 2; i -= FUNHEAD;
1926 if ( m < t ) {
for ( j = 0; j < FUNHEAD-2; j++ ) *m++ = *t++; }
1927 else { m += FUNHEAD-2; t += FUNHEAD-2; }
1929 while ( i-- > 0 ) { *m++ = *t++; }
1932 while ( tt < tend ) *mm++ = *tt++;
1934 tend = term + *term; tstop = tend - ABS(tend[-1]);
1940 else if ( *t >= FUNCTION ) {
1943 for ( j = 0; j < FUNHEAD; j++ ) {
1944 if ( m == t ) { m++; t++; }
1948 if ( *t <= -FUNCTION ) {
1949 if ( m == t ) { m++; t++; }
1952 else if ( *t < 0 ) {
1953 if ( m == t ) { m += 2; t += 2; }
1954 else { *m++ = *t++; *m++ = *t++; }
1957 rr = t + *t; mmm = m;
1958 for ( j = 0; j < ARGHEAD; j++ ) {
1959 if ( m == t ) { m++; t++; }
1964 ReWorkT(t,funs,numfuns);
1966 if ( m == t ) { m += j; t += j; }
1967 else {
while ( j-- >= 0 ) *m++ = *t++; }
1978 if ( m < t ) {
while ( j-- >= 0 ) *m++ = *t++; }
1979 else { m += j; t += j; }
1983 while ( t < tend ) *m++ = *t++;
1993void Apply(WORD *term, WORD level)
1995 WORD *funs, numfuns;
1998 CBUF *C = cbuf+AM.rbufnum;
2002 numfuns = C->
lhs[level][1] - 2;
2003 funs = C->
lhs[level] + 2;
2004 if ( numfuns > 0 ) {
2005 for ( i = MAXBUILTINFUNCTION-FUNCTION; i < AC.FunctionList.num; i++ ) {
2006 if ( ( T = functions[i].tabl ) != 0 ) {
2007 for ( j = 0; j < numfuns; j++ ) {
2008 if ( i == (funs[j]-FUNCTION) && T->
spare ) {
2009 FlipTable(&(functions[i]),0);
2017 for ( i = MAXBUILTINFUNCTION-FUNCTION; i < AC.FunctionList.num; i++ ) {
2018 if ( ( T = functions[i].tabl ) != 0 ) {
2019 if ( T->
spare ) FlipTable(&(functions[i]),0);
2029 ReWorkT(term,funs,numfuns);
2051int ApplyExec(WORD *term,
int maxtogo, WORD level)
2054 WORD rhsnumber, *Tpattern, *funs, numfuns, funnum;
2055 WORD ii, *t, *t1, *w, *p, *m, *m1, *u, *r, tbufnum, csize, wilds;
2057 int i, j, isp, stilltogo;
2067 AT.NestPoin->termsize = t;
2068 if ( AT.NestPoin == AT.Nest ) AN.EndNest = t + *t;
2074 if ( *t < FUNCTION ) { t += t[1];
continue; }
2075 if ( functions[*t-FUNCTION].spec > 0 ) { t += t[1];
continue; }
2076 AT.NestPoin->funsize = t;
2080 if ( *t < 0 ) { NEXTARG(t);
continue; }
2081 AT.NestPoin->argsize = t1 = t;
2098 stilltogo = ApplyExec(t,maxtogo,level);
2099 if ( stilltogo != maxtogo ) {
2100 if ( stilltogo <= 0 ) {
2104 maxtogo = stilltogo;
2106 m = term + *term - csize;
2116 C = cbuf+AM.rbufnum;
2119 if ( *t != TABLESTUB ) { t += t[1];
continue; }
2120 funnum = -t[FUNHEAD];
2121 if ( ( funnum < FUNCTION )
2122 || ( funnum >= FUNCTION+WILDOFFSET )
2123 || ( ( T = functions[funnum-FUNCTION].tabl ) == 0 )
2125 || ( T->
spare == 0 ) ) { t += t[1];
continue; }
2126 numfuns = C->
lhs[level][1] - 3;
2127 funs = C->
lhs[level] + 3;
2128 if ( numfuns > 0 ) {
2129 for ( i = 0; i < numfuns; i++ ) {
2130 if ( funs[i] == funnum )
break;
2132 if ( i >= numfuns ) { t += t[1];
continue; }
2135 AT.NestPoin->funsize = t + 1;
2145 Tpattern = T->
pattern[identity];
2149 p = Tpattern+FUNHEAD+1;
2150 for ( i = 0; i < ABS(T->
numind); i++, t += 2 ) {
2151 if ( *t != -SNUMBER )
break;
2153 if ( i < ABS(T->
numind) ) { t = r;
continue; }
2154 isp = FindTableTree(T,t1+FUNHEAD+1,2);
2155 if ( isp < 0 ) { t = r;
continue; }
2157#if ( TABLEEXTENSION == 2 )
2164 while ( --ii >= 0 ) {
2165 *p = *t; t += 2; p += 2;
2177 AN.WildValue = AN.FullProto + SUBEXPSIZE;
2178 AN.WildStop = AN.FullProto+AN.FullProto[1];
2181 AN.RepFunList = AN.EndNest;
2182 AT.WorkPointer = (WORD *)(((UBYTE *)(AN.EndNest)) + AM.MaxTer/2);
2187 if ( AT.WorkPointer + t1[1] >= AT.WorkTop ) { MesWork(); }
2190 *w++ = -t1[FUNHEAD];
2192 for ( i = 2; i < FUNHEAD; i++ ) *w++ = t1[i];
2194 while ( t < r ) *w++ = *t++;
2197 if ( MatchFunction(BHEAD Tpattern,t,&wilds) > 0 ) {
2218 while ( r < m1 ) *t++ = *r++;
2222 while ( NN > AT.Nest ) {
2224 NN->termsize[0] += j;
2225 NN->funsize[1] += j;
2226 NN->argsize[0] += j;
2227 NN->funsize[2] |= DIRTYFLAG;
2228 NN->argsize[1] |= DIRTYFLAG;
2235 while ( t >= r ) { t[j] = *t; t--; }
2241 while ( NN > AT.Nest ) {
2243 NN->termsize[0] += j;
2244 NN->funsize[1] += j;
2245 NN->argsize[0] += j;
2246 NN->funsize[2] |= DIRTYFLAG;
2247 NN->argsize[1] |= DIRTYFLAG;
2256 if ( maxtogo <= 0 )
return(maxtogo);
2268void ApplyReset(WORD level)
2270 WORD *funs, numfuns;
2273 CBUF *C = cbuf+AM.rbufnum;
2275 numfuns = C->
lhs[level][1] - 2;
2276 funs = C->
lhs[level] + 2;
2277 if ( numfuns > 0 ) {
2278 for ( i = MAXBUILTINFUNCTION-FUNCTION; i < AC.FunctionList.num; i++ ) {
2279 if ( ( T = functions[i].tabl ) != 0 ) {
2280 for ( j = 0; j < numfuns; j++ ) {
2281 if ( i == (funs[j]-FUNCTION) && T->
spare ) {
2282 FlipTable(&(functions[i]),1);
2290 for ( i = MAXBUILTINFUNCTION-FUNCTION; i < AC.FunctionList.num; i++ ) {
2291 if ( ( T = functions[i].tabl ) != 0 ) {
2292 if ( T->
spare ) FlipTable(&(functions[i]),1);
2303void TableReset(
void)
2308 for ( i = MAXBUILTINFUNCTION-FUNCTION; i < AC.FunctionList.num; i++ ) {
2309 if ( ( T = functions[i].tabl ) != 0 && T->
spare && T->
mode == 0 ) {
2310 functions[i].tabl = T->
spare;
2333 for ( i = NumTableBases - 1; i >= 0; i-- ) {
UBYTE * SkipAName(UBYTE *s)
int Generator(PHEAD WORD *, WORD)