more readable code, better testcase
This commit is contained in:
parent
2420a8c61a
commit
b210eb3d81
2 changed files with 17 additions and 9 deletions
|
@ -13,12 +13,17 @@ namespace brainfuck_details {
|
||||||
template <typename T, typename InputIterator, typename OutputIterator>
|
template <typename T, typename InputIterator, typename OutputIterator>
|
||||||
void b(char * c, InputIterator& in, T * & p, OutputIterator out){
|
void b(char * c, InputIterator& in, T * & p, OutputIterator out){
|
||||||
for(; *c && *c != ']'; ++c) {
|
for(; *c && *c != ']'; ++c) {
|
||||||
(*((p+=*c=='>')-=*c=='<')+=*c=='+') -=*c=='-';
|
switch(*c){
|
||||||
if(*c=='.') *out++ = *p;
|
case '>' : ++p; break;
|
||||||
if(*c==',') *p = *in++;
|
case '<' : --p; break;
|
||||||
if(*c=='[') {
|
case '+' : ++*p; break;
|
||||||
|
case '-' : --*p; break;
|
||||||
|
case '.' : *out++ = *p; break;
|
||||||
|
case ',' : *p = *in++; break;
|
||||||
|
case '[' :
|
||||||
for(++c; *p;) b(c, in, p, out);
|
for(++c; *p;) b(c, in, p, out);
|
||||||
for(int d=0; *c!=']'||d--; ++c)d+=*c=='[';
|
for(int d = 0; *c!=']' || d--; ++c) if(*c == '[') d++;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,8 +4,11 @@
|
||||||
#include "brainfuck.hpp"
|
#include "brainfuck.hpp"
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
brainfuck<int>("+[,.]", std::istream_iterator<int>(std::cin));
|
// will print "Hello World", or something like that.
|
||||||
brainfuck<int>(">++++++++++++++++++++++++++++++++>+>+[[+++++[>++++++++<-]>.<++++++[>--------<-]+<<<]>.>>[[-]<[>+<-]>>[<<+>+>-]<[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>[-]>+>+<<<-[>+<-]]]]]]]]]]]+>>>]<<<]");
|
brainfuck("++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.");
|
||||||
|
|
||||||
|
// will print the fibonacci sequence
|
||||||
|
brainfuck(">++++++++++++++++++++++++++++++++>+>+[[+++++[>++++++++<-]>.<++++++[>--------<-]+<<<]>.>>[[-]<[>+<-]>>[<<+>+>-]<[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>[-]>+>+<<<-[>+<-]]]]]]]]]]]+>>>]<<<]");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue