Naive solution.
I think this would be more interesting if the challenge was to generate as few characters of b**infuck as possible.
maybe add a limit to *ra*nfuck characters in the test cases? might be fun.
#include<stddef.h> #include<string.h> #include<stdlib.h> char buf[4096] = {0}; char* s2bf(char *arr) { char cv = 0; char *r; while(*arr){ while(cv != *arr){ strcat(buf, "+"); cv++; } strcat(buf, "."); arr++; } r = malloc(4096); strcpy(r, buf); return r; }
- #include<stddef.h>
- #include<string.h>
- #include<stdlib.h>
char s[4096] = "";- char buf[4096] = {0};
- char*
s2bf(char *argv) {register int i = 0,j;register int k,m;register int l,last = 0;while (argv[++i]!=0)continue;for (j=0; j<i; j++) {register int t;t = argv[j];l = k = t-last;last = t;#ifdef DEBUGprintf("\n%d, %d, %d, %d\n",last,k,j,i);#endifif (k >= 0) {k >>= 3; // k /= 8l &= 7; //在更多的机器工作,求余if (k == 1)strcat(s,">++++++++");else {if (k != 0) {strcat(s,"++++++++[>");for (m=0; m<k; m++)strcat(s,"+");strcat(s,"<-]>");} else {strcat(s,">");}}for (m=0; m<l; m++)strcat(s,"+");strcat(s,".");} else {k = -k >> 3; // k /= 8l = -l & 7; //在更多的机器工作,求余if (k == 1)strcat(s,">--------");else {strcat(s,"++++++++[>");for (m=0; m<k; m++)strcat(s,"-");strcat(s,"<-]>");}for (m=0; m<l; m++)strcat(s,"-");strcat(s,".");- s2bf(char *arr) {
- char cv = 0;
- char *r;
- while(*arr){
- while(cv != *arr){
- strcat(buf, "+");
- cv++;
- }
#ifdef DEBUGprintf("\n%d, %d, %d\n",m,k,l);#endifstrcat(s,"<");- strcat(buf, ".");
- arr++;
- }
char* q = malloc(4096);strcpy(q, s);return q;- r = malloc(4096);
- strcpy(r, buf);
- return r;
- }
#include<criterion/criterion.h> #include<string.h> #include<stdlib.h> #include<stdio.h> #include<stdlib.h> char* s2bf(char*); int validbf(char *a) { int b; b = 0; while(*a){ switch(*a){ case '[': b++; break; case ']': b--; if(b < 0) return 0; break; } a++; } if(b) return 0; return 1; } void runbf(char**cell, char*bf, char **o) { int lc; while(*bf){ switch(*bf){ case '[': if(**cell){ runbf(cell, bf+1, o); bf--; }else{ lc = 0; do{ switch(*bf){ case '[': lc++; break; case ']': lc--; break; } bf++; }while(lc); continue; } break; case ']': return; break; case ',': **cell = rand(); break; case '.': **o = **cell; (*o)++; break; case '<': (*cell)--; break; case '>': (*cell)++; break; case '+': (**cell)++; break; case '-': (**cell)--; break; } bf++; } } char* evalbf(char *bf) { char out[400] = {0}; char cells[4096] = {0}; char *cp = cells + 2048; char *o = out; runbf(&cp, bf, &o); char*r = malloc(o - out + 1); strcpy(r, out); return r; } Test(s2bf, bf_validity) { char *bf; bf = s2bf("ab"); cr_assert_eq(validbf(bf), 1); free(bf); } Test(s2bf, hello_world){ char *bf; char *op; bf = s2bf("Hello World!"); printf("%s\n", bf); op = evalbf(bf); cr_assert_eq(strcmp(op,"Hello World!"),0); free(bf); free(op); }
- #include<criterion/criterion.h>
- #include<string.h>
- #include<stdlib.h>
- #include<stdio.h>
- #include<stdlib.h>
- char* s2bf(char*);
- int
- validbf(char *a)
- {
- int b;
- b = 0;
- while(*a){
- switch(*a){
- case '[':
- b++;
- break;
- case ']':
- b--;
- if(b < 0)
- return 0;
- break;
- }
- a++;
- }
- if(b)
- return 0;
- return 1;
- }
- void
- runbf(char**cell, char*bf, char **o)
- {
- int lc;
- while(*bf){
- switch(*bf){
- case '[':
- if(**cell){
- runbf(cell, bf+1, o);
- bf--;
- }else{
- lc = 0;
- do{
- switch(*bf){
- case '[':
- lc++;
- break;
- case ']':
- lc--;
- break;
- }
- bf++;
- }while(lc);
- continue;
- }
- break;
- case ']':
- return;
- break;
- case ',':
- **cell = rand();
- break;
- case '.':
- **o = **cell;
- (*o)++;
- break;
- case '<':
- (*cell)--;
- break;
- case '>':
- (*cell)++;
- break;
- case '+':
- (**cell)++;
- break;
- case '-':
- (**cell)--;
- break;
- }
- bf++;
- }
- }
- char*
- evalbf(char *bf)
- {
- char out[400] = {0};
- char cells[4096] = {0};
- char *cp = cells + 2048;
- char *o = out;
- runbf(&cp, bf, &o);
- char*r = malloc(o - out + 1);
- strcpy(r, out);
- return r;
- }
- Test(s2bf, bf_validity) {
- char *bf;
- bf = s2bf("ab");
- cr_assert_eq(validbf(bf), 1);
- free(bf);
- }
- Test(s2bf, hello_world){
- char *bf;
- char *op;
- bf = s2bf("Hello World!");
- printf("%s\n", bf);
- op = evalbf(bf);
- cr_assert_eq(strcmp(op,"Hello World!"),0);
- free(bf);
- free(op);
- }
added a proper test system.
#include<stddef.h> #include<string.h> #include<stdlib.h> char s[4096] = ""; char* s2bf(char *argv) { register int i = 0,j; register int k,m; register int l,last = 0; while (argv[++i]!=0) continue; for (j=0; j<i; j++) { register int t; t = argv[j]; l = k = t-last; last = t; #ifdef DEBUG printf("\n%d, %d, %d, %d\n",last,k,j,i); #endif if (k >= 0) { k >>= 3; // k /= 8 l &= 7; //在更多的机器工作,求余 if (k == 1) strcat(s,">++++++++"); else { if (k != 0) { strcat(s,"++++++++[>"); for (m=0; m<k; m++) strcat(s,"+"); strcat(s,"<-]>"); } else { strcat(s,">"); } } for (m=0; m<l; m++) strcat(s,"+"); strcat(s,"."); } else { k = -k >> 3; // k /= 8 l = -l & 7; //在更多的机器工作,求余 if (k == 1) strcat(s,">--------"); else { strcat(s,"++++++++[>"); for (m=0; m<k; m++) strcat(s,"-"); strcat(s,"<-]>"); } for (m=0; m<l; m++) strcat(s,"-"); strcat(s,"."); } #ifdef DEBUG printf("\n%d, %d, %d\n",m,k,l); #endif strcat(s,"<"); } char* q = malloc(4096); strcpy(q, s); return q; }
#include <stdio.h>#include <string.h>// #define DEBUG- #include<stddef.h>
- #include<string.h>
- #include<stdlib.h>
- char s[4096] = "";
int s2b(int argc, char *argv[]) {- char*
- s2bf(char *argv) {
- register int i = 0,j;
- register int k,m;
- register int l,last = 0;
if (argc != 2) {printf("Usage: %s [string]",*argv);return 1;}while (argv[1][++i]!=0)- while (argv[++i]!=0)
- continue;
- for (j=0; j<i; j++) {
- register int t;
t = argv[1][j];- t = argv[j];
- l = k = t-last;
- last = t;
- #ifdef DEBUG
- printf("\n%d, %d, %d, %d\n",last,k,j,i);
- #endif
- if (k >= 0) {
- k >>= 3; // k /= 8
- l &= 7; //在更多的机器工作,求余
- if (k == 1)
- strcat(s,">++++++++");
- else {
- if (k != 0) {
- strcat(s,"++++++++[>");
- for (m=0; m<k; m++)
- strcat(s,"+");
- strcat(s,"<-]>");
- } else {
- strcat(s,">");
- }
- }
- for (m=0; m<l; m++)
- strcat(s,"+");
- strcat(s,".");
- } else {
- k = -k >> 3; // k /= 8
- l = -l & 7; //在更多的机器工作,求余
- if (k == 1)
- strcat(s,">--------");
- else {
- strcat(s,"++++++++[>");
- for (m=0; m<k; m++)
- strcat(s,"-");
- strcat(s,"<-]>");
- }
- for (m=0; m<l; m++)
- strcat(s,"-");
- strcat(s,".");
- }
- #ifdef DEBUG
- printf("\n%d, %d, %d\n",m,k,l);
- #endif
- strcat(s,"<");
- }
return 0;- char* q = malloc(4096);
- strcpy(q, s);
- return q;
- }
#include<criterion/criterion.h> #include<string.h> #include<stdlib.h> #include<stdio.h> #include<stdlib.h> char* s2bf(char*); int validbf(char *a) { int b; b = 0; while(*a){ switch(*a){ case '[': b++; break; case ']': b--; if(b < 0) return 0; break; } a++; } if(b) return 0; return 1; } void runbf(char**cell, char*bf, char **o) { int lc; while(*bf){ switch(*bf){ case '[': if(**cell){ runbf(cell, bf+1, o); bf--; }else{ lc = 0; do{ switch(*bf){ case '[': lc++; break; case ']': lc--; break; } bf++; }while(lc); continue; } break; case ']': return; break; case ',': **cell = rand(); break; case '.': **o = **cell; (*o)++; break; case '<': (*cell)--; break; case '>': (*cell)++; break; case '+': (**cell)++; break; case '-': (**cell)--; break; } bf++; } } char* evalbf(char *bf) { char out[400] = {0}; char cells[4096] = {0}; char *cp = cells + 2048; char *o = out; runbf(&cp, bf, &o); char*r = malloc(o - out + 1); strcpy(r, out); return r; } Test(s2bf, bf_validity) { char *bf; bf = s2bf("ab"); cr_assert_eq(validbf(bf), 1); free(bf); } Test(s2bf, hello_world){ char *bf; char *op; bf = s2bf("Hello World!"); op = evalbf(bf); cr_assert_eq(strcmp(op,"Hello World!"),0); free(bf); free(op); }
// TODO: Replace examples and use TDD by writing your own tests. The code provided here is just a how-to example.- #include<criterion/criterion.h>
- #include<string.h>
- #include<stdlib.h>
- #include<stdio.h>
- #include<stdlib.h>
#include <criterion/criterion.h>#include <string.h>// replace with the actual method being testedint s2b(int argc, char *argv[]);extern char s[];Test(the_multiply_function, should_pass_all_the_tests_provided) {char *argv[] ={"","hello, world!"};s2b(2,argv);cr_assert_eq(strcmp(s,"++++++++[>+++++++++++++<-]>.<++++++++[><-]>---.<>+++++++.<>.<>+++.<++++++++[>--------<-]>---.<>------------.<++++++++[>++++++++++<-]>+++++++.<>--------.<>+++.<++++++++[><-]>------.<>--------.<++++++++[>--------<-]>---.<"), 0);- char* s2bf(char*);
- int
- validbf(char *a)
- {
- int b;
- b = 0;
- while(*a){
- switch(*a){
- case '[':
- b++;
- break;
- case ']':
- b--;
- if(b < 0)
- return 0;
- break;
- }
- a++;
- }
- if(b)
- return 0;
- return 1;
- }
- void
- runbf(char**cell, char*bf, char **o)
- {
- int lc;
- while(*bf){
- switch(*bf){
- case '[':
- if(**cell){
- runbf(cell, bf+1, o);
- bf--;
- }else{
- lc = 0;
- do{
- switch(*bf){
- case '[':
- lc++;
- break;
- case ']':
- lc--;
- break;
- }
- bf++;
- }while(lc);
- continue;
- }
- break;
- case ']':
- return;
- break;
- case ',':
- **cell = rand();
- break;
- case '.':
- **o = **cell;
- (*o)++;
- break;
- case '<':
- (*cell)--;
- break;
- case '>':
- (*cell)++;
- break;
- case '+':
- (**cell)++;
- break;
- case '-':
- (**cell)--;
- break;
- }
- bf++;
- }
- }
- char*
- evalbf(char *bf)
- {
- char out[400] = {0};
- char cells[4096] = {0};
- char *cp = cells + 2048;
- char *o = out;
- runbf(&cp, bf, &o);
- char*r = malloc(o - out + 1);
- strcpy(r, out);
- return r;
- }
- Test(s2bf, bf_validity) {
- char *bf;
- bf = s2bf("ab");
- cr_assert_eq(validbf(bf), 1);
- free(bf);
- }
- Test(s2bf, hello_world){
- char *bf;
- char *op;
- bf = s2bf("Hello World!");
- op = evalbf(bf);
- cr_assert_eq(strcmp(op,"Hello World!"),0);
- free(bf);
- free(op);
- }