diff options
Diffstat (limited to 'test/brace16.c')
-rw-r--r-- | test/brace16.c | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/test/brace16.c b/test/brace16.c new file mode 100644 index 0000000..58273d3 --- /dev/null +++ b/test/brace16.c @@ -0,0 +1,58 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +int expand(char *dst,char ch,int amount){ +#define WRITE(s__) {int l__=strlen(s__); if(dst){memcpy(dst,s__,l__+1); dst+=l__;} written+=l__;} +#define WGOTO(i__) {while(curpos<i__){WRITE(">"); curpos++;} while(curpos>i__){WRITE("<"); curpos--;}} +#define WMOVE(i1__,i2__) {int p__=curpos; WGOTO(i1__); WRITE("[-"); WGOTO(i2__); WRITE("+"); WGOTO(i1__); WRITE("]"); WGOTO(p__);} + int written=0,curpos=0; + switch(str[i]){ + case '+': //strcpy(p,"+>+[->+<]>[<<->>[-<+>]]<<"); p+=25; break; + case '+': //strcpy(p,"+>+[<->[->+<]]>[-<+>]<<"); p+=23; break; + case '+': + WRITE("+"); + for(int i=1;i<amount;i++){WGOTO(i); WRITE("+");} + for(int i=1;i<amount;i++){ + WGOTO(i); + WRITE("["); + WRITE("<->"); + } + case '-': strcpy(p,"->[->+<]>[<<+>>[-<+>]]<-<"); p+=25; break; + case '[': strcpy(p,"[->>+>>>+<<<<<]>>>>>[<<<[-<<+>>]+>>>[-]]<<<<[->+>>>+<<<<]>>>>[[-<<<-<+>>>>]<<<+>>>]<<<[[-]<<"); p+=92; break; + case ']': strcpy(p,"[->>+>>>+<<<<<]>>>>>[<<<[-<<+>>]+>>>[-]]<<<<[->+>>>+<<<<]>>>>[[-<<<-<+>>>>]<<<+>>>]<<<]<<"); p+=89; break; + case '>': strcpy(p,">>>"); p+=3; break; + case '<': strcpy(p,"<<<"); p+=3; break; + case ',': strcpy(p,"[-]>,<"); p+=6; break; + case '.': strcpy(p,">.<"); p+=3; break; + default: *(p++)=str[i]; break; + } + return res; +#undef WRITE +#undef WGOTO +#undef WMOVE +} + +int main(void){ + int amount=1; + char c; + while((c=getchar())!=EOF){ + if(c=='{'){ + amount++; + } else if(c=='}'){ + amount--; + if(amount<=0){ + fprintf(stderr,"More '}' than '{'\n"); + return 1; + } + } else if(amount==1){ + putchar(c); + } else { + int len=expand(NULL,c,amount); + char *str=malloc(len+1); + expand(str,c,amount); + printf("%s",str); + free(str); + } + } +} |