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, writeini = 0;
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++;
961 if ( FlushDirtyIndexBlocks(d) ) error = 1;
962 if ( writeini && WriteIniInfo(d) ) error = 1;
963 M_free(AO.DollarOutBuffer,
"DollarOutBuffer");
964 AO.DollarOutBuffer = 0;
965 AO.DollarOutSizeBuffer = 0;
981int CoTBenter(UBYTE *s)
985 UBYTE *arguments, *rhs, *buffer, *t, *u, c, *tablename;
987 int i, j, error = 0, error1 = 0, printall = 0;
990 int dict = AO.CurrentDictionary;
991 AO.CurrentDictionary = 0;
992 if ( ( d = FindTB(tablebasename) ) == 0 ) {
993 MesPrint(
"&No open tablebase with the name %s, check for existence of file or try readonly mode when opening.",tablebasename);
997 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
998 if ( *s ==
'!' ) { printall = 1; s++; }
999 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
1003 if ( ( s =
SkipAName(s) ) == 0 ) { error = 1;
goto Endofall; }
1005 if ( ( GetVar(tablename,&type,&funnum,CFUNCTION,NOAUTO) == NAMENOTFOUND )
1006 || ( T = functions[funnum].tabl ) == 0 ) {
1007 MesPrint(
"&%s should be a previously declared table",tablename);
1010 else if ( T->
sparse == 0 ) {
1011 MesPrint(
"&%s should be a sparse table",tablename);
1014 else { basenumber = GetTableName(d,(
char *)tablename); }
1015 if ( T->
spare == 0 ) { SpareTable(T); }
1016 if ( basenumber > 0 ) {
1017 for ( i = 0; i < d->info.numberofindexblocks; i++ ) {
1018 for ( j = 0; j < NUMOBJECTS; j++ ) {
1019 if ( basenumber != d->iblocks[i]->objects[j].tablenumber )
1021 arguments = (UBYTE *)(d->iblocks[i]->objects[j].element);
1022 rhs = (UBYTE *)ReadObject(d,basenumber,(
char *)arguments);
1025 MesPrint(
"%s(%s) = %s",tablename,arguments,rhs);
1028 MesPrint(
"%s(%s) = 0",tablename,arguments);
1032 u = rhs;
while ( *u ) u++;
1034 u = arguments;
while ( *u ) u++;
1035 size += u-arguments;
1036 u = tablename;
while ( *u ) u++;
1037 size += u-tablename;
1039 buffer = (UBYTE *)Malloc1(size,
"TableBase copy");
1040 t = tablename; u = buffer;
1041 while ( *t ) *u++ = *t++;
1044 while ( *t ) *u++ = *t++;
1045 *u++ =
')'; *u++ =
'=';
1047 while ( *t ) *u++ = *t++;
1048 if ( t == rhs ) *u++ =
'0';
1050 M_free(rhs,
"rhs in TBenter");
1052 error1 = CoFill(buffer);
1054 if ( error1 < 0 )
goto Endofall;
1055 if ( error1 != 0 ) error = error1;
1056 M_free(buffer,
"TableBase copy");
1062 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
1066 s = (UBYTE *)(d->tablenames); basenumber = 0;
1069 tablename = s;
while ( *s ) s++; s++;
1072 if ( ( GetVar(tablename,&type,&funnum,CFUNCTION,NOAUTO) == NAMENOTFOUND )
1073 || ( T = functions[funnum].tabl ) == 0 ) {
1074 MesPrint(
"&%s should be a previously declared table",tablename);
1076 else if ( T->
sparse == 0 ) {
1077 MesPrint(
"&%s should be a sparse table",tablename);
1079 if ( T->
spare == 0 ) { SpareTable(T); }
1080 for ( i = 0; i < d->info.numberofindexblocks; i++ ) {
1081 for ( j = 0; j < NUMOBJECTS; j++ ) {
1082 if ( d->iblocks[i]->objects[j].tablenumber == basenumber ) {
1083 arguments = (UBYTE *)(d->iblocks[i]->objects[j].element);
1084 rhs = (UBYTE *)ReadObject(d,basenumber,(
char *)arguments);
1087 MesPrint(
"%s%s = %s",tablename,arguments,rhs);
1090 MesPrint(
"%s%s = 0",tablename,arguments);
1094 u = rhs;
while ( *u ) u++;
1096 u = arguments;
while ( *u ) u++;
1097 size += u-arguments;
1098 u = tablename;
while ( *u ) u++;
1099 size += u-tablename;
1101 buffer = (UBYTE *)Malloc1(size,
"TableBase copy");
1102 t = tablename; u = buffer;
1103 while ( *t ) *u++ = *t++;
1106 while ( *t ) *u++ = *t++;
1107 *u++ =
')'; *u++ =
'=';
1109 while ( *t ) *u++ = *t++;
1110 if ( t == rhs ) *u++ =
'0';
1112 M_free(rhs,
"rhs in TBenter");
1114 error1 = CoFill(buffer);
1116 if ( error1 < 0 )
goto Endofall;
1117 if ( error1 != 0 ) error = error1;
1118 M_free(buffer,
"TableBase copy");
1126 AO.CurrentDictionary = dict;
1140int CoTestUse(UBYTE *s)
1143 UBYTE *tablename, c;
1144 WORD type, funnum, *w;
1148 *w++ = TYPETESTUSE; *w++ = 2;
1151 if ( ( s =
SkipAName(s) ) == 0 )
return(1);
1153 if ( ( GetVar(tablename,&type,&funnum,CFUNCTION,NOAUTO) == NAMENOTFOUND )
1154 || ( T = functions[funnum].tabl ) == 0 ) {
1155 MesPrint(
"&%s should be a previously declared table",tablename);
1158 else if ( T->
sparse == 0 ) {
1159 MesPrint(
"&%s should be a sparse table",tablename);
1162 *w++ = funnum + FUNCTION;
1164 while ( *s ==
' ' || *s ==
',' || *s ==
'\t' ) s++;
1166 AT.WorkPointer[1] = w - AT.WorkPointer;
1172 AddNtoL(AT.WorkPointer[1],AT.WorkPointer);
1185int CheckTableDeclarations(
DBASE *d)
1188 UBYTE *s, *ss, *t, *command = 0;
1189 int k, error = 0, error1, i;
1191 LONG commandsize = 0;
1193 s = (UBYTE *)(d->tablenames);
1194 for ( k = 0; k < d->topnumber; k++ ) {
1195 if ( GetVar(s,&type,&funnum,ANYTYPE,NOAUTO) == NAMENOTFOUND ) {
1199 ss = s; i = 0;
while ( *ss ) { ss++; i++; }
1200 ss++;
while ( *ss ) { ss++; i++; }
1201 if ( commandsize == 0 ) {
1202 commandsize = i + 15;
1203 if ( commandsize < 100 ) commandsize = 100;
1205 if ( (i+11) > commandsize ) {
1206 if ( command ) { M_free(command,
"table command"); command = 0; }
1209 if ( command == 0 ) {
1210 command = (UBYTE *)Malloc1(commandsize,
"table command");
1212 t = command; ss = tablebase;
while ( *ss ) *t++ = *ss++;
1213 *t++ =
',';
while ( *s ) *t++ = *s++;
1214 s++;
while ( *s ) *t++ = *s++;
1215 *t++ =
')'; *t = 0; s++;
1216 error1 = DoTable(command,1);
1217 if ( error1 ) error = error1;
1219 else if ( ( type != CFUNCTION )
1220 || ( ( T = functions[funnum].tabl ) == 0 )
1221 || ( T->
sparse == 0 ) ) {
1222 MesPrint(
"&%s has been declared previously, but not as a sparse table.",s);
1238 MesPrint(
"&Declaration of table %s in %s different from previous declaration",ss,d->name);
1245 if ( command ) { M_free(command,
"table command"); }
1259int CoTBload(UBYTE *ss)
1262 UBYTE *s, *name, *t, *r, *command, *arguments, *tail;
1264 int num, cs, es, ns, ts, i, j, error = 0, error1;
1265 if ( ( d = FindTB(tablebasename) ) == 0 ) {
1266 MesPrint(
"&No open tablebase with the name %s",tablebasename);
1270 command = (UBYTE *)Malloc1(commandsize,
"Fill command");
1273 while ( *ss ==
',' || *ss ==
' ' || *ss ==
'\t' ) ss++;
1276 s = (UBYTE *)(d->tablenames);
1280 if ( StrCmp(s,name) ) {
1288 name = s;
while ( *s ) s++; ns = s-name; s++;
1289 tail = s;
while ( *s ) s++; ts = s-tail; s++;
1290 tail++;
while ( FG.cTable[*tail] == 1 ) tail++;
1294 for ( i = 0; i < d->info.numberofindexblocks; i++ ) {
1295 for ( j = 0; j < NUMOBJECTS; j++ ) {
1296 if ( d->iblocks[i]->objects[j].tablenumber == num ) {
1297 t = arguments = (UBYTE *)(d->iblocks[i]->objects[j].element);
1300 cs = 2*es + 2*ns + ts + 10;
1301 if ( cs > commandsize ) {
1303 if ( command ) M_free(command,
"Fill command");
1304 command = (UBYTE *)Malloc1(commandsize,
"Fill command");
1306 r = command; t = name;
while ( *t ) *r++ = *t++;
1307 *r++ =
'('; t = arguments;
while ( *t ) *r++ = *t++;
1308 *r++ =
')'; *r++ =
'='; *r++ =
't'; *r++ =
'b'; *r++ =
'l';
1309 *r++ =
'_'; *r++ =
'('; t = name;
while ( *t ) *r++ = *t++;
1310 *r++ =
','; t = arguments;
while ( *t ) *r++ = *t++;
1311 t = tail;
while ( *t ) {
1312 if ( *t ==
'?' && r[-1] !=
',' ) {
1314 if ( FG.cTable[*t] == 0 || *t ==
'$' || *t ==
'[' ) {
1320 else if ( *t ==
'{' ) {
1323 else if ( *t ) { *r++ = *t++;
continue; }
1331 AC.vetotablebasefill = 1;
1332 error1 = CoFill(command);
1333 AC.vetotablebasefill = 0;
1334 if ( error1 < 0 )
goto finishup;
1335 if ( error1 != 0 ) error = error1;
1341 while ( *ss ==
',' || *ss ==
' ' || *ss ==
'\t' ) ss++;
1345 s = (UBYTE *)(d->tablenames);
1349 name = s;
while ( *s ) s++; ns = s-name; s++;
1350 tail = s;
while ( *s ) s++; ts = s-tail; s++;
1351 tail++;
while ( FG.cTable[*tail] == 1 ) tail++;
1355 for ( i = 0; i < d->info.numberofindexblocks; i++ ) {
1356 for ( j = 0; j < NUMOBJECTS; j++ ) {
1357 if ( d->iblocks[i]->objects[j].tablenumber == num ) {
1358 t = arguments = (UBYTE *)(d->iblocks[i]->objects[j].element);
1361 cs = 2*es + 2*ns + ts + 10;
1362 if ( cs > commandsize ) {
1364 if ( command ) M_free(command,
"Fill command");
1365 command = (UBYTE *)Malloc1(commandsize,
"Fill command");
1367 r = command; t = name;
while ( *t ) *r++ = *t++;
1368 *r++ =
'('; t = arguments;
while ( *t ) *r++ = *t++;
1369 *r++ =
')'; *r++ =
'='; *r++ =
't'; *r++ =
'b'; *r++ =
'l';
1370 *r++ =
'_'; *r++ =
'('; t = name;
while ( *t ) *r++ = *t++;
1371 *r++ =
','; t = arguments;
while ( *t ) *r++ = *t++;
1372 t = tail;
while ( *t ) {
1373 if ( *t ==
'?' && r[-1] !=
',' ) {
1375 if ( FG.cTable[*t] == 0 || *t ==
'$' || *t ==
'[' ) {
1381 else if ( *t ==
'{' ) {
1384 else if ( *t ) { *r++ = *t++;
continue; }
1392 AC.vetotablebasefill = 1;
1393 error1 = CoFill(command);
1394 AC.vetotablebasefill = 0;
1395 if ( error1 < 0 )
goto finishup;
1396 if ( error1 != 0 ) error = error1;
1404 if ( command ) M_free(command,
"Fill command");
1421int TestUse(WORD *term, WORD level)
1423 WORD *tstop, *t, *m, *tstart, tabnum;
1424 WORD *funs, numfuns;
1428 CBUF *C = cbuf+AM.rbufnum;
1431 numfuns = C->
lhs[level][1] - 2;
1432 funs = C->
lhs[level] + 2;
1433 GETSTOP(term,tstop);
1435 while ( t < tstop ) {
1436 if ( *t != TABLESTUB ) { t += t[1];
continue; }
1440 if ( *m >= -FUNCTION )
continue;
1442 if ( ( T = functions[tabnum-FUNCTION].tabl ) == 0 )
continue;
1443 if ( T->
sparse == 0 )
continue;
1447 if ( numfuns > 0 ) {
1448 for ( i = 0; i < numfuns; i++ ) {
1449 if ( tabnum == funs[i] )
break;
1451 if ( i >= numfuns && numfuns > 0 )
continue;
1459 for ( i = 0; i < ABS(T->
numind); i++, m += 2 ) {
1460 if ( m >= t || *m != -SNUMBER )
break;
1462 if ( ( i == ABS(T->
numind) ) &&
1463 ( ( isp = FindTableTree(T,tstart+FUNHEAD+1,2) ) >= 0 ) ) {
1470 MesPrint(
"!>TestUse: Encountered a table element inside tbl_ that does not correspond to a tablebase element");
1483int CoTBaudit(UBYTE *s)
1487 int i, j, error = 0, num;
1489 if ( ( d = FindTB(tablebasename) ) == 0 ) {
1490 MesPrint(
"&No open tablebase with the name %s",tablebasename);
1493 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
1501 s = (UBYTE *)(d->tablenames); num = 0;
1504 name = s;
while ( *s ) s++; s++;
1505 tail = s;
while ( *s ) s++; s++;
1506 MesPrint(
"Table,sparse,%s%s)",name,tail);
1507 for ( i = 0; i < d->info.numberofindexblocks; i++ ) {
1508 for ( j = 0; j < NUMOBJECTS; j++ ) {
1509 if ( d->iblocks[i]->objects[j].tablenumber == num ) {
1510 MesPrint(
" %s(%s)",name,d->iblocks[i]->objects[j].element);
1528 if ( ( d = FindTB(tablebasename) ) == 0 ) {
1529 MesPrint(
"&No open tablebase with the name %s",tablebasename);
1532 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
1536 if ( StrICmp(s,(UBYTE *)(
"compress")) == 0 ) {
1537 d->mode &= ~NOCOMPRESS;
1540 MesPrint(
"&subkey %s not defined in TableBase On statement");
1544 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
1554int CoTBoff(UBYTE *s)
1559 if ( ( d = FindTB(tablebasename) ) == 0 ) {
1560 MesPrint(
"&No open tablebase with the name %s",tablebasename);
1563 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
1567 if ( StrICmp(s,(UBYTE *)(
"compress")) == 0 ) {
1568 d->mode |= NOCOMPRESS;
1571 MesPrint(
"&subkey %s not defined in TableBase Off statement");
1575 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
1585int CoTBcleanup(UBYTE *s)
1588 MesPrint(
"&TableBase Cleanup statement not yet implemented");
1597int CoTBreplace(UBYTE *s)
1600 MesPrint(
"&TableBase Replace statement not yet implemented");
1612int CoTBuse(UBYTE *s)
1617 UBYTE *arguments, *rhs, *buffer, *t, *u, c, *tablename, *p;
1619 int i, j, error = 0, error1 = 0, k;
1621 WORD type, funnum, mode, *w;
1622 if ( ( d = FindTB(tablebasename) ) == 0 ) {
1623 MesPrint(
"&No open tablebase with the name %s",tablebasename);
1626 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
1630 if ( ( s =
SkipAName(s) ) == 0 )
return(1);
1632 if ( ( GetVar(tablename,&type,&funnum,CFUNCTION,NOAUTO) == NAMENOTFOUND )
1633 || ( T = functions[funnum].tabl ) == 0 ) {
1634 MesPrint(
"&%s should be a previously declared table",tablename);
1637 else if ( T->
sparse == 0 ) {
1638 MesPrint(
"&%s should be a sparse table",tablename);
1641 else { basenumber = GetTableName(d,(
char *)tablename); }
1643 if ( basenumber > 0 ) {
1644 for ( i = 0; i < d->info.numberofindexblocks; i++ ) {
1645 for ( j = 0; j < NUMOBJECTS; j++ ) {
1646 if ( d->iblocks[i]->objects[j].tablenumber != basenumber )
continue;
1647 arguments = p = (UBYTE *)(d->iblocks[i]->objects[j].element);
1652 for ( k = 0, w = AT.WorkPointer; k < ABS(T->
numind); k++ ) {
1653 ParseSignedNumber(x,p);
1656 sum = FindTableTree(T,AT.WorkPointer,1);
1658 MesPrint(
"Table %s in tablebase %s has not been loaded properly"
1659 ,tablename,tablebasename);
1663 sum += ABS(T->
numind) + 4;
1665 if ( ( mode & ELEMENTLOADED ) == ELEMENTLOADED ) {
1669 if ( ( mode & ELEMENTUSED ) == 0 )
continue;
1673 rhs = (UBYTE *)ReadijObject(d,i,j,(
char *)arguments);
1675 u = rhs;
while ( *u ) u++;
1677 u = arguments;
while ( *u ) u++;
1678 size += u-arguments;
1679 u = tablename;
while ( *u ) u++;
1680 size += u-tablename;
1682 buffer = (UBYTE *)Malloc1(size,
"TableBase copy");
1683 t = tablename; u = buffer;
1684 while ( *t ) *u++ = *t++;
1687 while ( *t ) *u++ = *t++;
1688 *u++ =
')'; *u++ =
'=';
1690 while ( *t ) *u++ = *t++;
1691 if ( t == rhs ) { *u++ =
'0'; }
1693 M_free(rhs,
"rhs in TBuse xxx");
1695 error1 = CoFill(buffer);
1697 if ( error1 < 0 ) {
return(error); }
1698 if ( error1 != 0 ) error = error1;
1699 M_free(buffer,
"TableBase copy");
1707 while ( *s ==
',' || *s ==
' ' || *s ==
'\t' ) s++;
1711 s = (UBYTE *)(d->tablenames); basenumber = 0;
1719 if ( ( GetVar(tablename,&type,&funnum,CFUNCTION,NOAUTO) == NAMENOTFOUND )
1720 || ( T = functions[funnum].tabl ) == 0 ) {
1721 MesPrint(
"&%s should be a previously declared table",tablename);
1723 else if ( T->
sparse == 0 ) {
1724 MesPrint(
"&%s should be a sparse table",tablename);
1728 MesPrint(
"!>In table %s we have a problem with stubb orders in CoTBuse",tablename);
1733 for ( i = 0; i < d->info.numberofindexblocks; i++ ) {
1734 for ( j = 0; j < NUMOBJECTS; j++ ) {
1735 if ( d->iblocks[i]->objects[j].tablenumber == basenumber ) {
1736 arguments = p = (UBYTE *)(d->iblocks[i]->objects[j].element);
1741 for ( k = 0, w = AT.WorkPointer; k < ABS(T->
numind); k++ ) {
1742 ParseSignedNumber(x,p);
1745 sum = FindTableTree(T,AT.WorkPointer,1);
1748 MesPrint(
"!>Table %s in tablebase %s has not been loaded properly"
1749 ,tablename,tablebasename);
1754 sum += ABS(T->
numind) + 4;
1756 if ( ( mode & ELEMENTLOADED ) == ELEMENTLOADED ) {
1760 if ( ( mode & ELEMENTUSED ) == 0 )
continue;
1764 rhs = (UBYTE *)ReadijObject(d,i,j,(
char *)arguments);
1766 u = rhs;
while ( *u ) u++;
1768 u = arguments;
while ( *u ) u++;
1769 size += u-arguments;
1770 u = tablename;
while ( *u ) u++;
1771 size += u-tablename;
1773 buffer = (UBYTE *)Malloc1(size,
"TableBase copy");
1774 t = tablename; u = buffer;
1775 while ( *t ) *u++ = *t++;
1778 while ( *t ) *u++ = *t++;
1779 *u++ =
')'; *u++ =
'=';
1782 while ( *t ) *u++ = *t++;
1783 if ( t == rhs ) { *u++ =
'0'; }
1785 M_free(rhs,
"rhs in TBuse");
1787 error1 = CoFill(buffer);
1789 if ( error1 < 0 ) {
return(error); }
1790 if ( error1 != 0 ) error = error1;
1791 M_free(buffer,
"TableBase copy");
1810int CoApply(UBYTE *s)
1813 UBYTE *tablename, c;
1814 WORD type, funnum, *w;
1816 LONG maxtogo = MAXPOSITIVE;
1819 if ( FG.cTable[*s] == 1 ) {
1821 while ( FG.cTable[*s] == 1 ) {
1822 maxtogo = maxtogo*10 + (*s-
'0');
1825 while ( *s ==
',' ) s++;
1826 if ( maxtogo > MAXPOSITIVE || maxtogo < 0 ) maxtogo = MAXPOSITIVE;
1828 *w++ = TYPEAPPLY; *w++ = 3; *w++ = maxtogo;
1831 if ( ( s =
SkipAName(s) ) == 0 )
return(1);
1833 if ( ( GetVar(tablename,&type,&funnum,CFUNCTION,NOAUTO) == NAMENOTFOUND )
1834 || ( T = functions[funnum].tabl ) == 0 ) {
1835 MesPrint(
"&%s should be a previously declared table",tablename);
1838 else if ( T->
sparse == 0 ) {
1839 MesPrint(
"&%s should be a sparse table",tablename);
1842 *w++ = funnum + FUNCTION;
1844 while ( *s ==
' ' || *s ==
',' || *s ==
'\t' ) s++;
1846 AT.WorkPointer[1] = w - AT.WorkPointer;
1852 AddNtoL(AT.WorkPointer[1],AT.WorkPointer);
1866 "The TableBase statement is used as follows:"
1867 ,
"TableBase \"file.tbl\" keyword subkey(s)"
1868 ,
" in which we have"
1869 ,
"Keyword Subkey(s) Action"
1870 ,
"open Opens file.tbl for R/W"
1871 ,
"create Creates file.tbl for R/W. Old contents are lost"
1872 ,
"load Loads all stubs of all tables"
1873 ,
"load tablename(s) Loads all stubs the tables mentioned"
1874 ,
"enter Loads all stubs and rhs of all tables"
1875 ,
"enter tablename(s) Loads all stubs and rhs of the tables mentioned"
1876 ,
"audit Prints list of contents"
1880 ,
"addto tablename adds all elements if not yet there"
1881 ,
"addto tableelement adds element if not yet there"
1884 ,
"on compress elements are stored in gzip format (default)"
1885 ,
"off compress elements are stored in uncompressed format"
1886 ,
"use compiles all needed elements"
1887 ,
"use tablename(s) compiles all needed elements of these tables"
1889 ,
"Related commands are:"
1890 ,
"testuse marks which tbl_ elements occur for all tables"
1891 ,
"testuse tablename(s) marks which tbl_ elements occur for given tables"
1892 ,
"apply replaces tbl_ if rhs available"
1893 ,
"apply tablename(s) replaces tbl_ for given tables if rhs available"
1897int CoTBhelp(UBYTE *s)
1899 int i, ii =
sizeof(helptb)/
sizeof(
char *);
1901 for ( i = 0; i < ii; i++ ) MesPrint(
"%s",helptb[i]);
1913void ReWorkT(WORD *term, WORD *funs, WORD numfuns)
1915 WORD *tstop, *tend, *m, *t, *tt, *mm, *mmm, *r, *rr;
1917 tend = term + *term; tstop = tend - ABS(tend[-1]);
1919 while ( t < tstop ) {
1920 if ( *t == TABLESTUB ) {
1921 for ( i = 0; i < numfuns; i++ ) {
1922 if ( -t[FUNHEAD] == funs[i] )
break;
1924 if ( numfuns == 0 || i < numfuns ) {
1926 *m++ = -t[FUNHEAD]; *m++ = i; t += 2; i -= FUNHEAD;
1927 if ( m < t ) {
for ( j = 0; j < FUNHEAD-2; j++ ) *m++ = *t++; }
1928 else { m += FUNHEAD-2; t += FUNHEAD-2; }
1930 while ( i-- > 0 ) { *m++ = *t++; }
1933 while ( tt < tend ) *mm++ = *tt++;
1935 tend = term + *term; tstop = tend - ABS(tend[-1]);
1941 else if ( *t >= FUNCTION ) {
1944 for ( j = 0; j < FUNHEAD; j++ ) {
1945 if ( m == t ) { m++; t++; }
1949 if ( *t <= -FUNCTION ) {
1950 if ( m == t ) { m++; t++; }
1953 else if ( *t < 0 ) {
1954 if ( m == t ) { m += 2; t += 2; }
1955 else { *m++ = *t++; *m++ = *t++; }
1958 rr = t + *t; mmm = m;
1959 for ( j = 0; j < ARGHEAD; j++ ) {
1960 if ( m == t ) { m++; t++; }
1965 ReWorkT(t,funs,numfuns);
1967 if ( m == t ) { m += j; t += j; }
1968 else {
while ( j-- >= 0 ) *m++ = *t++; }
1979 if ( m < t ) {
while ( j-- >= 0 ) *m++ = *t++; }
1980 else { m += j; t += j; }
1984 while ( t < tend ) *m++ = *t++;
1994void Apply(WORD *term, WORD level)
1996 WORD *funs, numfuns;
1999 CBUF *C = cbuf+AM.rbufnum;
2003 numfuns = C->
lhs[level][1] - 2;
2004 funs = C->
lhs[level] + 2;
2005 if ( numfuns > 0 ) {
2006 for ( i = MAXBUILTINFUNCTION-FUNCTION; i < AC.FunctionList.num; i++ ) {
2007 if ( ( T = functions[i].tabl ) != 0 ) {
2008 for ( j = 0; j < numfuns; j++ ) {
2009 if ( i == (funs[j]-FUNCTION) && T->
spare ) {
2010 FlipTable(&(functions[i]),0);
2018 for ( i = MAXBUILTINFUNCTION-FUNCTION; i < AC.FunctionList.num; i++ ) {
2019 if ( ( T = functions[i].tabl ) != 0 ) {
2020 if ( T->
spare ) FlipTable(&(functions[i]),0);
2030 ReWorkT(term,funs,numfuns);
2052int ApplyExec(WORD *term,
int maxtogo, WORD level)
2055 WORD rhsnumber, *Tpattern, *funs, numfuns, funnum;
2056 WORD ii, *t, *t1, *w, *p, *m, *m1, *u, *r, tbufnum, csize, wilds;
2058 int i, j, isp, stilltogo;
2068 AT.NestPoin->termsize = t;
2069 if ( AT.NestPoin == AT.Nest ) AN.EndNest = t + *t;
2075 if ( *t < FUNCTION ) { t += t[1];
continue; }
2076 if ( functions[*t-FUNCTION].spec > 0 ) { t += t[1];
continue; }
2077 AT.NestPoin->funsize = t;
2081 if ( *t < 0 ) { NEXTARG(t);
continue; }
2082 AT.NestPoin->argsize = t1 = t;
2099 stilltogo = ApplyExec(t,maxtogo,level);
2100 if ( stilltogo != maxtogo ) {
2101 if ( stilltogo <= 0 ) {
2105 maxtogo = stilltogo;
2107 m = term + *term - csize;
2117 C = cbuf+AM.rbufnum;
2120 if ( *t != TABLESTUB ) { t += t[1];
continue; }
2121 funnum = -t[FUNHEAD];
2122 if ( ( funnum < FUNCTION )
2123 || ( funnum >= FUNCTION+WILDOFFSET )
2124 || ( ( T = functions[funnum-FUNCTION].tabl ) == 0 )
2126 || ( T->
spare == 0 ) ) { t += t[1];
continue; }
2127 numfuns = C->
lhs[level][1] - 3;
2128 funs = C->
lhs[level] + 3;
2129 if ( numfuns > 0 ) {
2130 for ( i = 0; i < numfuns; i++ ) {
2131 if ( funs[i] == funnum )
break;
2133 if ( i >= numfuns ) { t += t[1];
continue; }
2136 AT.NestPoin->funsize = t + 1;
2146 Tpattern = T->
pattern[identity];
2150 p = Tpattern+FUNHEAD+1;
2151 for ( i = 0; i < ABS(T->
numind); i++, t += 2 ) {
2152 if ( *t != -SNUMBER )
break;
2154 if ( i < ABS(T->
numind) ) { t = r;
continue; }
2155 isp = FindTableTree(T,t1+FUNHEAD+1,2);
2156 if ( isp < 0 ) { t = r;
continue; }
2158#if ( TABLEEXTENSION == 2 )
2165 while ( --ii >= 0 ) {
2166 *p = *t; t += 2; p += 2;
2178 AN.WildValue = AN.FullProto + SUBEXPSIZE;
2179 AN.WildStop = AN.FullProto+AN.FullProto[1];
2182 AN.RepFunList = AN.EndNest;
2183 AT.WorkPointer = (WORD *)(((UBYTE *)(AN.EndNest)) + AM.MaxTer/2);
2188 if ( AT.WorkPointer + t1[1] >= AT.WorkTop ) { MesWork(); }
2191 *w++ = -t1[FUNHEAD];
2193 for ( i = 2; i < FUNHEAD; i++ ) *w++ = t1[i];
2195 while ( t < r ) *w++ = *t++;
2198 if ( MatchFunction(BHEAD Tpattern,t,&wilds) > 0 ) {
2219 while ( r < m1 ) *t++ = *r++;
2223 while ( NN > AT.Nest ) {
2225 NN->termsize[0] += j;
2226 NN->funsize[1] += j;
2227 NN->argsize[0] += j;
2228 NN->funsize[2] |= DIRTYFLAG;
2229 NN->argsize[1] |= DIRTYFLAG;
2236 while ( t >= r ) { t[j] = *t; t--; }
2242 while ( NN > AT.Nest ) {
2244 NN->termsize[0] += j;
2245 NN->funsize[1] += j;
2246 NN->argsize[0] += j;
2247 NN->funsize[2] |= DIRTYFLAG;
2248 NN->argsize[1] |= DIRTYFLAG;
2257 if ( maxtogo <= 0 )
return(maxtogo);
2269void ApplyReset(WORD level)
2271 WORD *funs, numfuns;
2274 CBUF *C = cbuf+AM.rbufnum;
2276 numfuns = C->
lhs[level][1] - 2;
2277 funs = C->
lhs[level] + 2;
2278 if ( numfuns > 0 ) {
2279 for ( i = MAXBUILTINFUNCTION-FUNCTION; i < AC.FunctionList.num; i++ ) {
2280 if ( ( T = functions[i].tabl ) != 0 ) {
2281 for ( j = 0; j < numfuns; j++ ) {
2282 if ( i == (funs[j]-FUNCTION) && T->
spare ) {
2283 FlipTable(&(functions[i]),1);
2291 for ( i = MAXBUILTINFUNCTION-FUNCTION; i < AC.FunctionList.num; i++ ) {
2292 if ( ( T = functions[i].tabl ) != 0 ) {
2293 if ( T->
spare ) FlipTable(&(functions[i]),1);
2304void TableReset(
void)
2309 for ( i = MAXBUILTINFUNCTION-FUNCTION; i < AC.FunctionList.num; i++ ) {
2310 if ( ( T = functions[i].tabl ) != 0 && T->
spare && T->
mode == 0 ) {
2311 functions[i].tabl = T->
spare;
2334 for ( i = NumTableBases - 1; i >= 0; i-- ) {
UBYTE * SkipAName(UBYTE *s)
int Generator(PHEAD WORD *, WORD)