Created
May 26, 2011 19:04
-
-
Save mosra/993799 to your computer and use it in GitHub Desktop.
Brainfuck interpreter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 99 bottles of beer on the wall | |
;; warning: alcohol destroys brain cells! (not as much as brainfuck) | |
;; written by Keymaker | |
++++[>+++++<-]>+++ | |
[ | |
[>+>+<<-] | |
>++++++[<+>-]+++++++++[<++++++++++>-] | |
>[<+>-]<- | |
] | |
+++>+++++++++[<+++++++++>-] | |
>++++++[<++++++++>-]<--[>+>+<<-]>>[<<+>>-]<-- | |
>>++++[<++++++++>-]++++++++++ | |
>+++++++++[>+++++++++++<-]> | |
[ | |
[>+>+>+<<<-]>[<+>-]>> | |
[ | |
[>+>+<<-]>>[<<+>>-]< | |
[<->-[<->-[<->-[<->-[<->-[<->-[<->-[<->-[<->-[<<<+>---------->->[-]]]]]]]]]]]< | |
] | |
<<[>>++++++++++++[<++++<++++>>-]<<[.[-]>]<<] | |
>[<++++++[>++++++++<-]>.[-]]<< | |
<<<.<<<<<.[<]>>>>>>>>>.<<<<<..>>>>>>>>.>>>>>>>. | |
[>]>[>+>+<<-]>[<+>-]>[-[[-]+++++++++[<+++++++++++++>-]<--.[-]>]]<< | |
<<<.[<]>>>>>>>>>.>>>>>>>>>.[>]<<.<<<<<.<<<..[<]>>>>>>.[>]<<. | |
[<]>>>>>>>>>.>.[>]<<.[<]>>>>.>>>>>>>>>>>>.>>>.[>]<<. | |
[<]>.[>]<<<<<<.<<<<<<<<<<<..[>]<<<.>. | |
[>]>[>+>+>+<<<-]>[<+>-]>> | |
[ | |
[>+>+<<-]>>[<<+>>-]< | |
[<->-[<->-[<->-[<->-[<->-[<->-[<->-[<->-[<->-[<<<+>---------->->[-]]]]]]]]]]]< | |
] | |
<<[>>++++++++++++[<++++<++++>>-]<<[.[-]>]<<] | |
>[<++++++[>++++++++<-]>.[-]]<< | |
<<<.<<<<<.[<]>>>>>>>>>.<<<<<..>>>>>>>>.>>>>>>>. | |
[>]>[>+>+<<-]>[<+>-]>[-[[-]+++++++++[<+++++++++++++>-]<--.[-]>]]<< | |
<<<.[<]>>>>>>>>>.>>>>>>>>>.[>]<<.<<<<<.<<<..[<]>>>>>>.[>]<<<<.>>>. | |
<<<<.<.<<<<<<<<<<.>>>>>>.[>]<<.[<]>>>>>>>>>.>.>>>>>>>>>.[>]<<. | |
<<<<<<<.[<]>>>>>>>>>.[<]>.>>>>>>>>>.[>]<<.<<<<.<<<<<<<<<<<<<.[>]<<<<<<<<<. | |
[>]<<.[<]>>>>>>>>.[>]<<<<<<.[<]>>>>>..[>]<<.<<<<<<<<<<<<.[<]>>>>. | |
[>]<<.<<<<.[<]>>>>>>.>>>.<<<<<<.>>>>>>>.>>>>>>>>>>.[>]<<<.>. | |
>>>- | |
[>+>+>+<<<-]>[<+>-]>> | |
[ | |
[>+>+<<-]>>[<<+>>-]< | |
[<->-[<->-[<->-[<->-[<->-[<->-[<->-[<->-[<->-[<<<+>---------->->[-]]]]]]]]]]]< | |
] | |
<<[>>++++++++++++[<++++<++++>>-]<<[.[-]>]<<] | |
>[<++++++[>++++++++<-]>.[-]]<< | |
[>+>+<<-]>[<+>-]+>[<->[-]]< | |
[-<<<[<]>>>>>>>>>>.<.[>]<<.[<]>>>>>>>>>>>.<<.<<<.[>]<<<<<<<<<<.[>]>>]< | |
<<<.<<<<<.[<]>>>>>>>>>.<<<<<..>>>>>>>>.>>>>>>>. | |
[>]>[>+>+<<-]>[<+>-]+> | |
[<->-[<+>[-]]]< | |
[++++++++[>+++++++++++++<-]>--.[-]<]< | |
<<<.[<]>>>>>>>>>.>>>>>>>>>.[>]<<.<<<<<.<<<..[<]>>>>>>.[>]<<. | |
[<]>>>>>>>>>.>.[>]<<.[<]>>>>.>>>>>>>>>>>>.>>>.[>]<<. | |
[<]>.[>]<<<<<<.<<<<<<<<<<<..[>]<<<<.>>>.. | |
>> | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 ""; | |
} | |
}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; factorials | |
;; this program prints out the sequence of factorials | |
;; written by Keymaker | |
;; does not terminate by itself | |
++++++++++>>>+>>>>+>+<[[+++++[>++++ | |
++++<-]>.<++++++[>--------<-]+<<]<< | |
[<<]<.>>>>+<[->[<+>-[<+>-[<+>-[<+>- | |
[<+>-[<+>-[<+>-[<+>-[<+>-[<[-]>-+>[ | |
<->-]<[->>>[>>]<<[->[>>+<<-]>+<<<<] | |
<]>[-]+>+<<]]]]]]]]]]<[>+<-]+>>]<<[ | |
<<]>>[->>[>>]>>[-<<[<<]<<[<<]>[>[>> | |
]>>[>>]>>[>>]>+>+<<<<[<<]<<[<<]<<[< | |
<]>-]>[>>]>>[>>]>>[>>]>[<<<[<<]<<[< | |
<]<<[<<]>+>[>>]>>[>>]>>[>>]>-]<<<[< | |
<]>[>[>>]>+>>+<<<<<[<<]>-]>[>>]>[<< | |
<[<<]>+>[>>]>-]>>[<[<<+>+>-]<[>>>+< | |
<<-]<[>>+<<-]>>>-]<[-]>>+[>[>>>>]>[ | |
>>>>]>[-]+>+<[<<<<]>-]>[>>>>]>[>>>> | |
]>->-[<<<+>>+>-]<[>+<-]>[[<<+>+>-]< | |
[<->-[<->-[<->-[<->-[<->-[<->-[<->- | |
[<->-[<->-[<-<---------->>[-]>>>>[- | |
]+>+<<<<<]]]]]]]]]]<[>>+<<-]>>]<+<+ | |
<[>>>+<<<-]<<[<<<<]<<<<<[<<]+>>]>>> | |
>>[>>>>]+>[>>>>]<<<<[-<<<<]>>>>>[<< | |
<<]<<<<<[<<]<<[<<]+>>]>>[>>]>>>>>[- | |
>>>>]<<[<<<<]>>>>[>>>>]<<<<<<<<[>>> | |
>[<<+>>->[<<+>>-]>]<<<<[<<]<<]<<<<< | |
[->[-]>>>>>>>>[<<+>>->[<<+>>-]>]<<< | |
<[<<]<<<<<<<]>>>>>>>>>[<<<<<<<+>>>> | |
>>>->[<<<<<<<+>>>>>>>-]>]<<<<<<<<<] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
++++++++++[>+++++++>++++++++++>+++>+<<<< | |
-]>++.>+.+++++++..+++.>++.<<++++++++++++ | |
+++.>.+++.------.--------.>+.>. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment