|
|
@ -12,13 +12,18 @@ namespace brainfuck_details { |
|
|
|
// recursive funtion (from http://www.xs4all.nl/~weegen/eelis/geordi/) but templated
|
|
|
|
template <typename T, typename InputIterator, typename OutputIterator> |
|
|
|
void b(char * c, InputIterator& in, T * & p, OutputIterator out){ |
|
|
|
for(; *c&&*c!=']'; ++c) { |
|
|
|
(*((p+=*c=='>')-=*c=='<')+=*c=='+') -=*c=='-'; |
|
|
|
if(*c=='.') *out++ = *p; |
|
|
|
if(*c==',') *p = *in++; |
|
|
|
if(*c=='[') { |
|
|
|
for(++c; *p;)b(c,in,p,out); |
|
|
|
for(int d=0; *c!=']'||d--; ++c)d+=*c=='['; |
|
|
|
for(; *c && *c != ']'; ++c) { |
|
|
|
switch(*c){ |
|
|
|
case '>' : ++p; break; |
|
|
|
case '<' : --p; break; |
|
|
|
case '+' : ++*p; break; |
|
|
|
case '-' : --*p; break; |
|
|
|
case '.' : *out++ = *p; break; |
|
|
|
case ',' : *p = *in++; break; |
|
|
|
case '[' : |
|
|
|
for(++c; *p;) b(c, in, p, out); |
|
|
|
for(int d = 0; *c!=']' || d--; ++c) if(*c == '[') d++; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|