User blog:Cloudy176/TM rulesets to Golly rule tables converter

This is a C source of a Turing machine rulesets to Golly rule tables converter.

How to use

 * 1) Get a Turing machine ruleset (such as the ones from here, here, and here).
 * 2) On the end of the page, add a new line only consisting of "!" (without the quotes) and save as name here.txt.
 * 3) Place the file on the same location you compiled the source, and run.
 * 4) Type name here (without the .txt) and press Enter.
 * 5) It'll output two files: name here.table and name here.colors. Copy them to the Golly rules folder. On Windows, it's on %appdata%/Golly/Rules (I don't know the directory for other operating systems)
 * 6) Run Golly, and on Control>Set rule..., enter name here and press OK.
 * 7) You'll see a number of colors: The black/white/bright colors being the symbols, and the other ones being the machine states (with the last one being the halt state). The information about what these colors mean is included on the created name here.table.

Limits

 * 1) The number of lines (not including comments or newlines) is limited to 5000 lines; and each line (including comments) should not exceed 1000 characters, and the name of a singe state must not exceed 100 characters.
 * 2) Due to Golly's limits, (the number of states)+(the number of colors) must not exceed 255.

Code!

 * 1) include
 * 2) include
 * 3) include

char filename[100]; char filenameI[100], filenameT[100], filenameC[100]; FILE *in,*outT,*outC;

char states[256][100]; int nstates=1; char colors[256]; int ncolors=1;

int nlines=0; int lst[5000],lcol[5000]; int lncol[5000],lndir[5000],lnst[5000];

char lincol[5000],linst[5000][100];

void getInput{ strcpy(states[0],"0"); colors[0]='_';

char txtin[1000]; int i;	while(1){ fscanf(in,"%s",txtin); if(txtin[0]=='!') break; if(txtin[0]==':'||txtin[0]==';'){ // comment fgets(txtin,1000,in); continue; }

if(nlines>=5000){ printf("The input is limited to 5000 lines (not counting comments.)\n"); exit(2); }

// before state for(i=0;i=255){ printf("number of states+number of colors shouldn't be larger than 255.\n"); exit(3); }			strcpy(states[nstates],txtin); nstates++; }		lst[nlines]=i;

// before color fscanf(in,"%s",txtin); if(txtin[0]=='*') lcol[nlines]=-1; else{ for(i=0;i=255){ printf("number of states+number of colors shouldn't be larger than 255.\n"); exit(3); }				colors[ncolors]=txtin[0]; ncolors++; }			lcol[nlines]=i; }

// after color fscanf(in,"%s",txtin); lincol[nlines]=txtin[0];

// after direction fscanf(in,"%s",txtin); if(txtin[0]=='l') lndir[nlines]=-1; else if(txtin[0]=='r') lndir[nlines]=1; else lndir[nlines]=0;

// after state fscanf(in,"%s",linst[nlines]);

nlines++; } }

void checkInput{ int i,j;

for(i=0;i=255){ printf("number of states+number of colors shouldn't be larger than 255.\n"); exit(3); }				colors[ncolors]=lincol[i]; ncolors++; }			lncol[i]=j; }

// after state if(!strcmp(linst[i],"halt")) lnst[i]=-1; else{ for(j=0;j=255){ printf("number of states+number of colors shouldn't be larger than 255.\n"); exit(3); }				colors[ncolors]=lincol[i]; nstates++; }			lnst[i]=j; }	} }

int n_states;

void initial_print{ n_states=nstates+ncolors+1;

int i,j; for(i=0;i<ncolors;i++){ fprintf(outT,"# %3d: color %c\n",i,colors[i]); }	for(i=0;i<nstates;i++){ fprintf(outT,"# %3d: state %s\n",i+ncolors,states[i]); }	fprintf(outT,"# %3d: state halt\n",nstates+ncolors);

fprintf(outT,"n_states:%d\n",n_states); fprintf(outT,"neighborhood:Moore\nsymmetries:none\n");

for(i=0;i<7;i++){ fprintf(outT,"var %c=",i+'s'); for(j=0;j<n_states;j++) fprintf(outT,"%c%d",j?',':'{',j); fprintf(outT,"}\n"); }	fprintf(outT,"var a="); for(j=0;j<ncolors;j++) fprintf(outT,"%c%d",j?',':'{',j); fprintf(outT,"}\n\n"); }

void makeTable{ int i;	for(i=0;i<nlines;i++){ if(lcol[i]==-1) continue; if(lndir[i]==0){ fprintf(outT,"%d,s,t,u,v,%d,w,x,y,%d\n",				lst[i]+ncolors,lcol[i],(lnst[i]==-1?nstates:lnst[i])+ncolors); if(lncol[i]!=-1) fprintf(outT,"%d,%d,s,t,u,v,w,x,y,%d\n",lcol[i],lst[i]+ncolors,lncol[i]); fprintf(outT,"0,s,t,%d,%d,u,v,w,x,0\n",lst[i]+ncolors,lcol[i]); fprintf(outT,"0,s,t,u,v,w,%d,%d,x,0\n",lcol[i],lst[i]+ncolors); }		else{ fprintf(outT,"%d,s,t,u,v,%d,w,x,y,0\n",				   lst[i]+ncolors,lcol[i]); if(lncol[i]!=-1) fprintf(outT,"%d,%d,s,t,u,v,w,x,y,%d\n",lcol[i],lst[i]+ncolors,lncol[i]); if(lndir[i]==-1){ fprintf(outT,"0,s,t,%d,%d,u,v,w,x,%d\n",lst[i]+ncolors,lcol[i],(lnst[i]==-1?nstates:lnst[i])+ncolors); fprintf(outT,"0,s,t,u,v,w,%d,%d,x,0\n",lcol[i],lst[i]+ncolors); }			else{ fprintf(outT,"0,s,t,%d,%d,u,v,w,x,0\n",lst[i]+ncolors,lcol[i]); fprintf(outT,"0,s,t,u,v,w,%d,%d,x,%d\n",lcol[i],lst[i]+ncolors,(lnst[i]==-1?nstates:lnst[i])+ncolors); }		}		fprintf(outT,"\n"); }

for(i=0;i<nlines;i++){ if(lcol[i]!=-1) continue; if(lndir[i]==0){ fprintf(outT,"%d,s,t,u,v,a,w,x,y,%d\n",				lst[i]+ncolors,(lnst[i]==-1?nstates:lnst[i])+ncolors); if(lncol[i]!=-1) fprintf(outT,"a,%d,s,t,u,v,w,x,y,%d\n",lst[i]+ncolors,lncol[i]); fprintf(outT,"0,s,t,%d,a,u,v,w,x,0\n",lst[i]+ncolors); fprintf(outT,"0,s,t,u,v,w,a,%d,x,0\n",lst[i]+ncolors); }		else{ fprintf(outT,"%d,s,t,u,v,a,w,x,y,0\n",				   lst[i]+ncolors); if(lncol[i]!=-1) fprintf(outT,"a,%d,s,t,u,v,w,x,y,%d\n",lst[i]+ncolors,lncol[i]); if(lndir[i]==-1){ fprintf(outT,"0,s,t,%d,a,u,v,w,x,%d\n",lst[i]+ncolors,(lnst[i]==-1?nstates:lnst[i])+ncolors); fprintf(outT,"0,s,t,u,v,w,a,%d,x,0\n",lst[i]+ncolors); }			else{ fprintf(outT,"0,s,t,%d,a,u,v,w,x,0\n",lst[i]+ncolors); fprintf(outT,"0,s,t,u,v,w,a,%d,x,%d\n",lst[i]+ncolors,(lnst[i]==-1?nstates:lnst[i])+ncolors); }		}		fprintf(outT,"\n"); } }

void makeColors{ fprintf(outC,"color=0 0 0 0\n"); fprintf(outC,"color=1 255 255 255\n");

int i,c; for(i=0;i<ncolors-2;i++){ fprintf(outC,"color=%d",i+2); c=(63*6*i)/(ncolors-2); if(c<63){fprintf(outC," %d %d %d\n",255 ,c+192,192  ); continue;} c-=63; if(c<63){fprintf(outC," %d %d %d\n",255-c,255 ,192  ); continue;} c-=63; if(c<63){fprintf(outC," %d %d %d\n",192 ,255  ,c+192); continue;} c-=63; if(c<63){fprintf(outC," %d %d %d\n",192 ,255-c,255  ); continue;} c-=63; if(c<63){fprintf(outC," %d %d %d\n",c+192,192 ,255  ); continue;} c-=63; if(c<63){fprintf(outC," %d %d %d\n",255 ,192  ,255-c); continue;} }	for(i=0;i<nstates;i++){ fprintf(outC,"color=%d",i+ncolors); c=(255*6*i)/nstates; if(c<255){fprintf(outC," %d %d %d\n",255 ,c    ,0    ); continue;} c-=255; if(c<255){fprintf(outC," %d %d %d\n",255-c,255 ,0    ); continue;} c-=255; if(c<255){fprintf(outC," %d %d %d\n",0   ,255  ,c    ); continue;} c-=255; if(c<255){fprintf(outC," %d %d %d\n",0   ,255-c,255  ); continue;} c-=255; if(c<255){fprintf(outC," %d %d %d\n",c   ,0    ,255  ); continue;} c-=255; if(c<255){fprintf(outC," %d %d %d\n",255 ,0    ,255-c); continue;} }	fprintf(outC,"color=%d 128 128 128\n",nstates+ncolors); }

int main{ fgets(filename,100,stdin); filename[strlen(filename)-1]=0; strcpy(filenameI,filename); strcpy(filenameT,filename); strcpy(filenameC,filename); strcat(filenameI,".txt"); in=fopen(filenameI,"r"); if(in==NULL){ printf("Error while loading file: %s\n",filenameI); return 1; }

getInput; checkInput;

strcat(filenameT,".table"); outT=fopen(filenameT,"w"); strcat(filenameC,".colors"); outC=fopen(filenameC,"w");

initial_print; makeTable; makeColors;

return 0; }