Skip to content

Instantly share code, notes, and snippets.

@mosra
Created May 26, 2011 19:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mosra/993799 to your computer and use it in GitHub Desktop.
Save mosra/993799 to your computer and use it in GitHub Desktop.
Brainfuck interpreter
;; 99 bottles of beer on the wall
;; warning: alcohol destroys brain cells! (not as much as brainfuck)
;; written by Keymaker
++++[>+++++<-]>+++
[
[>+>+<<-]
>++++++[<+>-]+++++++++[<++++++++++>-]
>[<+>-]<-
]
+++>+++++++++[<+++++++++>-]
>++++++[<++++++++>-]<--[>+>+<<-]>>[<<+>>-]<--
>>++++[<++++++++>-]++++++++++
>+++++++++[>+++++++++++<-]>
[
[>+>+>+<<<-]>[<+>-]>>
[
[>+>+<<-]>>[<<+>>-]<
[<->-[<->-[<->-[<->-[<->-[<->-[<->-[<->-[<->-[<<<+>---------->->[-]]]]]]]]]]]<
]
<<[>>++++++++++++[<++++<++++>>-]<<[.[-]>]<<]
>[<++++++[>++++++++<-]>.[-]]<<
<<<.<<<<<.[<]>>>>>>>>>.<<<<<..>>>>>>>>.>>>>>>>.
[>]>[>+>+<<-]>[<+>-]>[-[[-]+++++++++[<+++++++++++++>-]<--.[-]>]]<<
<<<.[<]>>>>>>>>>.>>>>>>>>>.[>]<<.<<<<<.<<<..[<]>>>>>>.[>]<<.
[<]>>>>>>>>>.>.[>]<<.[<]>>>>.>>>>>>>>>>>>.>>>.[>]<<.
[<]>.[>]<<<<<<.<<<<<<<<<<<..[>]<<<.>.
[>]>[>+>+>+<<<-]>[<+>-]>>
[
[>+>+<<-]>>[<<+>>-]<
[<->-[<->-[<->-[<->-[<->-[<->-[<->-[<->-[<->-[<<<+>---------->->[-]]]]]]]]]]]<
]
<<[>>++++++++++++[<++++<++++>>-]<<[.[-]>]<<]
>[<++++++[>++++++++<-]>.[-]]<<
<<<.<<<<<.[<]>>>>>>>>>.<<<<<..>>>>>>>>.>>>>>>>.
[>]>[>+>+<<-]>[<+>-]>[-[[-]+++++++++[<+++++++++++++>-]<--.[-]>]]<<
<<<.[<]>>>>>>>>>.>>>>>>>>>.[>]<<.<<<<<.<<<..[<]>>>>>>.[>]<<<<.>>>.
<<<<.<.<<<<<<<<<<.>>>>>>.[>]<<.[<]>>>>>>>>>.>.>>>>>>>>>.[>]<<.
<<<<<<<.[<]>>>>>>>>>.[<]>.>>>>>>>>>.[>]<<.<<<<.<<<<<<<<<<<<<.[>]<<<<<<<<<.
[>]<<.[<]>>>>>>>>.[>]<<<<<<.[<]>>>>>..[>]<<.<<<<<<<<<<<<.[<]>>>>.
[>]<<.<<<<.[<]>>>>>>.>>>.<<<<<<.>>>>>>>.>>>>>>>>>>.[>]<<<.>.
>>>-
[>+>+>+<<<-]>[<+>-]>>
[
[>+>+<<-]>>[<<+>>-]<
[<->-[<->-[<->-[<->-[<->-[<->-[<->-[<->-[<->-[<<<+>---------->->[-]]]]]]]]]]]<
]
<<[>>++++++++++++[<++++<++++>>-]<<[.[-]>]<<]
>[<++++++[>++++++++<-]>.[-]]<<
[>+>+<<-]>[<+>-]+>[<->[-]]<
[-<<<[<]>>>>>>>>>>.<.[>]<<.[<]>>>>>>>>>>>.<<.<<<.[>]<<<<<<<<<<.[>]>>]<
<<<.<<<<<.[<]>>>>>>>>>.<<<<<..>>>>>>>>.>>>>>>>.
[>]>[>+>+<<-]>[<+>-]+>
[<->-[<+>[-]]]<
[++++++++[>+++++++++++++<-]>--.[-]<]<
<<<.[<]>>>>>>>>>.>>>>>>>>>.[>]<<.<<<<<.<<<..[<]>>>>>>.[>]<<.
[<]>>>>>>>>>.>.[>]<<.[<]>>>>.>>>>>>>>>>>>.>>>.[>]<<.
[<]>.[>]<<<<<<.<<<<<<<<<<<..[>]<<<<.>>>..
>>
]
#!/bin/sh
ascii=0
while getopts a name ; do
case $name in
a) ascii=1 ;;
esac
done
shift $(expr $OPTIND - 1)
cat $1 | awk '
BEGIN {
RS="\0"; FS=""; p=0; inputp=0; mem[0]=0; ascii='$ascii';
} {
for(i = 0; i != NF ; ++i)
input[i]=$(i+1);
end=NF;
} END {
while(inputp != end) {
if(input[inputp] == ">") {
p++;
if(mem[p]=="") mem[p]=0;
} else if(input[inputp] == "<") {
if(p == 0) {
print "Error: decrementing out of range";
exit;
}
p--;
if(mem[p]=="") mem[p]=0;
} else if(input[inputp] == "+") {
if(mem[p]==255) mem[p]=0;
else mem[p]++;
} else if(input[inputp] == "-") {
if(mem[p]==0) mem[p]=255;
else mem[p]--;
} else if(input[inputp] == ".") {
if(ascii == 1)
printf("%c", mem[p]);
else
printf("%d ", mem[p]);
} else if(input[inputp] == "[") {
if(mem[p] == 0) {
level = 0;
for(;;) {
if(inputp == end-1) {
print "Error: non-matching right bracket";
exit;
}
inputp++;
if(input[inputp] == "[")
level++;
else if(input[inputp] == "]") {
if(level == 0) break;
level--;
}
}
}
} else if(input[inputp] == "]") {
if(mem[p] != 0) {
level = 0;
for(;;) {
if(inputp == 0) {
print "Error: non-matching left bracket";
exit;
}
inputp--;
if(input[inputp] == "]")
level++;
else if(input[inputp] == "[") {
if(level == 0) break;
level--;
}
}
}
}
inputp++;
}
if(ascii == 0) {
ORS="\n";
print "";
}
}'
;; factorials
;; this program prints out the sequence of factorials
;; written by Keymaker
;; does not terminate by itself
++++++++++>>>+>>>>+>+<[[+++++[>++++
++++<-]>.<++++++[>--------<-]+<<]<<
[<<]<.>>>>+<[->[<+>-[<+>-[<+>-[<+>-
[<+>-[<+>-[<+>-[<+>-[<+>-[<[-]>-+>[
<->-]<[->>>[>>]<<[->[>>+<<-]>+<<<<]
<]>[-]+>+<<]]]]]]]]]]<[>+<-]+>>]<<[
<<]>>[->>[>>]>>[-<<[<<]<<[<<]>[>[>>
]>>[>>]>>[>>]>+>+<<<<[<<]<<[<<]<<[<
<]>-]>[>>]>>[>>]>>[>>]>[<<<[<<]<<[<
<]<<[<<]>+>[>>]>>[>>]>>[>>]>-]<<<[<
<]>[>[>>]>+>>+<<<<<[<<]>-]>[>>]>[<<
<[<<]>+>[>>]>-]>>[<[<<+>+>-]<[>>>+<
<<-]<[>>+<<-]>>>-]<[-]>>+[>[>>>>]>[
>>>>]>[-]+>+<[<<<<]>-]>[>>>>]>[>>>>
]>->-[<<<+>>+>-]<[>+<-]>[[<<+>+>-]<
[<->-[<->-[<->-[<->-[<->-[<->-[<->-
[<->-[<->-[<-<---------->>[-]>>>>[-
]+>+<<<<<]]]]]]]]]]<[>>+<<-]>>]<+<+
<[>>>+<<<-]<<[<<<<]<<<<<[<<]+>>]>>>
>>[>>>>]+>[>>>>]<<<<[-<<<<]>>>>>[<<
<<]<<<<<[<<]<<[<<]+>>]>>[>>]>>>>>[-
>>>>]<<[<<<<]>>>>[>>>>]<<<<<<<<[>>>
>[<<+>>->[<<+>>-]>]<<<<[<<]<<]<<<<<
[->[-]>>>>>>>>[<<+>>->[<<+>>-]>]<<<
<[<<]<<<<<<<]>>>>>>>>>[<<<<<<<+>>>>
>>>->[<<<<<<<+>>>>>>>-]>]<<<<<<<<<]
++++++++++[>+++++++>++++++++++>+++>+<<<<
-]>++.>+.+++++++..+++.>++.<<++++++++++++
+++.>.+++.------.--------.>+.>.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment