User blog:Cloudy176/Updated TM ruleset to Golly rule table converter

This is a C source code of a Turing machine ruleset to Golly rule table converter.

It works on Golly version 2.5. For older versions, see here.

How to use

 * 1) Get a Turing machine ruleset (such as the ones from here, here, and here) and paste it into a text editor.
 * 2) On the end of the text, add a new line only consisting of "!" (without the quotes) and save as name here.txt.
 * 3) Place that file on the same location you compiled the source, and run.
 * 4) Type name here (without the .txt) and press Enter.
 * 5) A file named name here.rule will appear on the folder. Open it and copy all of its content to the clipboard.
 * 6) Run Golly, then select the Open Clipboard command (in the File menu)
 * 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 single state must not exceed 100 characters.
 * 2) Due to Golly's limits, (the number of states)+(the number of colors) must not exceed 255.
 * 3) Giving the current state value (the first value on a line) '*' (i.e. all states) is not yet supported.

Code!

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

char filename[100]; char filenameI[100], filenameR[100]; FILE *in,*out;

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; }	} }

void initial_print{ int i,j;

//@RULE fprintf(out,"@RULE %s\n\n",filename); for(i=0;i<ncolors;i++){ fprintf(out,"%3d: color %c\n",i,colors[i]); }	for(i=0;i<nstates;i++){ fprintf(out,"%3d: state %s\n",i+ncolors,states[i]); }	fprintf(out,"%3d: state halt\n\n",nstates+ncolors);

//@TABLE fprintf(out,"@TABLE\n\n");

int output_n_states=nstates+ncolors+1; fprintf(out,"n_states:%d\n",output_n_states); fprintf(out,"neighborhood:Moore\nsymmetries:none\n");

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

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

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

void printColors(int v,int m,int cm){ int cm2=255-cm; int c=(cm2*6*v)/m; if(c<cm2){fprintf(out," %d %d %d\n",255 ,c+cm ,cm   ); return;} c-=cm2; if(c<cm2){fprintf(out," %d %d %d\n",255-c,255 ,cm   ); return;} c-=cm2; if(c<cm2){fprintf(out," %d %d %d\n",cm  ,255  ,c+cm ); return;} c-=cm2; if(c<cm2){fprintf(out," %d %d %d\n",cm  ,255-c,255  ); return;} c-=cm2; if(c<cm2){fprintf(out," %d %d %d\n",c+cm ,cm  ,255  ); return;} c-=cm2; if(c<cm2){fprintf(out," %d %d %d\n",255 ,cm   ,255-c); return;} }

void makeColors{ //@COLORS fprintf(out,"@COLORS\n\n");

fprintf(out,"0 0 0 0\n"); fprintf(out,"1 255 255 255\n");

int i,c; for(i=0;i<ncolors-2;i++){ fprintf(out,"%d",i+2); printColors(i,ncolors-2,128); }	for(i=0;i<nstates;i++){ fprintf(out,"%d",i+ncolors); printColors(i,nstates,0); }	fprintf(out,"%d 128 128 128\n",nstates+ncolors); }

int main{ fgets(filename,100,stdin); filename[strlen(filename)-1]=0; strcpy(filenameI,filename); strcpy(filenameR,filename); strcat(filenameI,".txt"); in=fopen(filenameI,"r"); if(in==NULL){ printf("%s.txt does not exist, or is unreadable.\n",filenameI); return 1; }

getInput; checkInput;

strcat(filenameR,".rule"); out=fopen(filenameR,"w");

initial_print; makeTable; makeColors;

return 0; }