Created
April 11, 2013 18:02
-
-
Save FireyFly/5365694 to your computer and use it in GitHub Desktop.
Brainfuck implementation in `sed`
This file contains hidden or 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/sed -rf | |
| # Sedfuck - brainfuck interpreter in sed. | |
| # Written by Jonas Höglund (FireFly, firefly.nu). | |
| h | |
| s/^(.|[^.])*$/0/ | |
| # Setup some memory. | |
| s/0/0000000000/g | |
| #s/0/0000000000/g | |
| #s/0/0000000000/g | |
| s/0/\x0/g | |
| x | |
| s/[{}]//g # Strip braces. | |
| # Transform [] to {} (use {} for looping). | |
| y/\[\]/{}/ | |
| s/[^-+<>{},\.]//g # Strip non-instructions. | |
| s/$/q/ # Add q to end of program ('quit' instruction). | |
| :parse | |
| #l;x;l;x | |
| /^\+/ { | |
| x | |
| /^\xff/ { # To not collide with the later s/^0/1/! | |
| s/^\xff/\x0/ | |
| x | |
| b next | |
| } | |
| s/^\xfe/\xff/ | |
| s/^\xfd/\xfe/ | |
| s/^\xfc/\xfd/ | |
| s/^\xfb/\xfc/ | |
| s/^\xfa/\xfb/ | |
| s/^\xf9/\xfa/ | |
| s/^\xf8/\xf9/ | |
| s/^\xf7/\xf8/ | |
| s/^\xf6/\xf7/ | |
| s/^\xf5/\xf6/ | |
| s/^\xf4/\xf5/ | |
| s/^\xf3/\xf4/ | |
| s/^\xf2/\xf3/ | |
| s/^\xf1/\xf2/ | |
| s/^\xf0/\xf1/ | |
| s/^\xef/\xf0/ | |
| s/^\xee/\xef/ | |
| s/^\xed/\xee/ | |
| s/^\xec/\xed/ | |
| s/^\xeb/\xec/ | |
| s/^\xea/\xeb/ | |
| s/^\xe9/\xea/ | |
| s/^\xe8/\xe9/ | |
| s/^\xe7/\xe8/ | |
| s/^\xe6/\xe7/ | |
| s/^\xe5/\xe6/ | |
| s/^\xe4/\xe5/ | |
| s/^\xe3/\xe4/ | |
| s/^\xe2/\xe3/ | |
| s/^\xe1/\xe2/ | |
| s/^\xe0/\xe1/ | |
| s/^\xdf/\xe0/ | |
| s/^\xde/\xdf/ | |
| s/^\xdd/\xde/ | |
| s/^\xdc/\xdd/ | |
| s/^\xdb/\xdc/ | |
| s/^\xda/\xdb/ | |
| s/^\xd9/\xda/ | |
| s/^\xd8/\xd9/ | |
| s/^\xd7/\xd8/ | |
| s/^\xd6/\xd7/ | |
| s/^\xd5/\xd6/ | |
| s/^\xd4/\xd5/ | |
| s/^\xd3/\xd4/ | |
| s/^\xd2/\xd3/ | |
| s/^\xd1/\xd2/ | |
| s/^\xd0/\xd1/ | |
| s/^\xcf/\xd0/ | |
| s/^\xce/\xcf/ | |
| s/^\xcd/\xce/ | |
| s/^\xcc/\xcd/ | |
| s/^\xcb/\xcc/ | |
| s/^\xca/\xcb/ | |
| s/^\xc9/\xca/ | |
| s/^\xc8/\xc9/ | |
| s/^\xc7/\xc8/ | |
| s/^\xc6/\xc7/ | |
| s/^\xc5/\xc6/ | |
| s/^\xc4/\xc5/ | |
| s/^\xc3/\xc4/ | |
| s/^\xc2/\xc3/ | |
| s/^\xc1/\xc2/ | |
| s/^\xc0/\xc1/ | |
| s/^\xbf/\xc0/ | |
| s/^\xbe/\xbf/ | |
| s/^\xbd/\xbe/ | |
| s/^\xbc/\xbd/ | |
| s/^\xbb/\xbc/ | |
| s/^\xba/\xbb/ | |
| s/^\xb9/\xba/ | |
| s/^\xb8/\xb9/ | |
| s/^\xb7/\xb8/ | |
| s/^\xb6/\xb7/ | |
| s/^\xb5/\xb6/ | |
| s/^\xb4/\xb5/ | |
| s/^\xb3/\xb4/ | |
| s/^\xb2/\xb3/ | |
| s/^\xb1/\xb2/ | |
| s/^\xb0/\xb1/ | |
| s/^\xaf/\xb0/ | |
| s/^\xae/\xaf/ | |
| s/^\xad/\xae/ | |
| s/^\xac/\xad/ | |
| s/^\xab/\xac/ | |
| s/^\xaa/\xab/ | |
| s/^\xa9/\xaa/ | |
| s/^\xa8/\xa9/ | |
| s/^\xa7/\xa8/ | |
| s/^\xa6/\xa7/ | |
| s/^\xa5/\xa6/ | |
| s/^\xa4/\xa5/ | |
| s/^\xa3/\xa4/ | |
| s/^\xa2/\xa3/ | |
| s/^\xa1/\xa2/ | |
| s/^\xa0/\xa1/ | |
| s/^\x9f/\xa0/ | |
| s/^\x9e/\x9f/ | |
| s/^\x9d/\x9e/ | |
| s/^\x9c/\x9d/ | |
| s/^\x9b/\x9c/ | |
| s/^\x9a/\x9b/ | |
| s/^\x99/\x9a/ | |
| s/^\x98/\x99/ | |
| s/^\x97/\x98/ | |
| s/^\x96/\x97/ | |
| s/^\x95/\x96/ | |
| s/^\x94/\x95/ | |
| s/^\x93/\x94/ | |
| s/^\x92/\x93/ | |
| s/^\x91/\x92/ | |
| s/^\x90/\x91/ | |
| s/^\x8f/\x90/ | |
| s/^\x8e/\x8f/ | |
| s/^\x8d/\x8e/ | |
| s/^\x8c/\x8d/ | |
| s/^\x8b/\x8c/ | |
| s/^\x8a/\x8b/ | |
| s/^\x89/\x8a/ | |
| s/^\x88/\x89/ | |
| s/^\x87/\x88/ | |
| s/^\x86/\x87/ | |
| s/^\x85/\x86/ | |
| s/^\x84/\x85/ | |
| s/^\x83/\x84/ | |
| s/^\x82/\x83/ | |
| s/^\x81/\x82/ | |
| s/^\x80/\x81/ | |
| s/^\x7f/\x80/ | |
| s/^\~/\x7f/ | |
| s/^\}/~/ | |
| s/^\|/}/ | |
| s/^\{/|/ | |
| s/^z/{/ | |
| s/^y/z/ | |
| s/^x/y/ | |
| s/^w/x/ | |
| s/^v/w/ | |
| s/^u/v/ | |
| s/^t/u/ | |
| s/^s/t/ | |
| s/^r/s/ | |
| s/^q/r/ | |
| s/^p/q/ | |
| s/^o/p/ | |
| s/^n/o/ | |
| s/^m/n/ | |
| s/^l/m/ | |
| s/^k/l/ | |
| s/^j/k/ | |
| s/^i/j/ | |
| s/^h/i/ | |
| s/^g/h/ | |
| s/^f/g/ | |
| s/^e/f/ | |
| s/^d/e/ | |
| s/^c/d/ | |
| s/^b/c/ | |
| s/^a/b/ | |
| s/^`/a/ | |
| s/^_/`/ | |
| s/^\^/_/ | |
| s/^\]/\^/ | |
| s/^\\/\]/ | |
| s/^\[/\\/ | |
| s/^Z/\[/ | |
| s/^Y/Z/ | |
| s/^X/Y/ | |
| s/^W/X/ | |
| s/^V/W/ | |
| s/^U/V/ | |
| s/^T/U/ | |
| s/^S/T/ | |
| s/^R/S/ | |
| s/^Q/R/ | |
| s/^P/Q/ | |
| s/^O/P/ | |
| s/^N/O/ | |
| s/^M/N/ | |
| s/^L/M/ | |
| s/^K/L/ | |
| s/^J/K/ | |
| s/^I/J/ | |
| s/^H/I/ | |
| s/^G/H/ | |
| s/^F/G/ | |
| s/^E/F/ | |
| s/^D/E/ | |
| s/^C/D/ | |
| s/^B/C/ | |
| s/^A/B/ | |
| s/^@/A/ | |
| s/^\?/@/ | |
| s/^>/\?/ | |
| s/^=/>/ | |
| s/^</=/ | |
| s/^;/</ | |
| s/^:/;/ | |
| s/^9/:/ | |
| s/^8/9/ | |
| s/^7/8/ | |
| s/^6/7/ | |
| s/^5/6/ | |
| s/^4/5/ | |
| s/^3/4/ | |
| s/^2/3/ | |
| s/^1/2/ | |
| s/^0/1/ | |
| s/^\//0/ | |
| s/^\./\// | |
| s/^\-/\./ | |
| s/^,/\-/ | |
| s/^\+/,/ | |
| s/^\*/\+/ | |
| s/^\)/\*/ | |
| s/^\(/\)/ | |
| s/^'/\(/ | |
| s/^\&/'/ | |
| s/^%/\&/ | |
| s/^\$/%/ | |
| s/^#/\$/ | |
| s/^"/#/ | |
| s/^!/"/ | |
| s/^ /!/ | |
| s/^\x1f/ / | |
| s/^\x1e/\x1f/ | |
| s/^\x1d/\x1e/ | |
| s/^\x1c/\x1d/ | |
| s/^\x1b/\x1c/ | |
| s/^\x1a/\x1b/ | |
| s/^\x19/\x1a/ | |
| s/^\x18/\x19/ | |
| s/^\x17/\x18/ | |
| s/^\x16/\x17/ | |
| s/^\x15/\x16/ | |
| s/^\x14/\x15/ | |
| s/^\x13/\x14/ | |
| s/^\x12/\x13/ | |
| s/^\x11/\x12/ | |
| s/^\x10/\x11/ | |
| s/^\xf/\x10/ | |
| s/^\xe/\xf/ | |
| s/^\xd/\xe/ | |
| s/^\xc/\xd/ | |
| s/^\xb/\xc/ | |
| s/^\xa/\xb/ | |
| s/^\x9/\xa/ | |
| s/^\x8/\x9/ | |
| s/^\x7/\x8/ | |
| s/^\x6/\x7/ | |
| s/^\x5/\x6/ | |
| s/^\x4/\x5/ | |
| s/^\x3/\x4/ | |
| s/^\x2/\x3/ | |
| s/^\x1/\x2/ | |
| s/^\x0/\x1/ | |
| x | |
| } | |
| /^-/ { | |
| x | |
| /^\x0/ { # See '+' above. | |
| s/^\x0/\xff/ | |
| x | |
| b next | |
| } | |
| s/^\x1/\x0/ | |
| s/^\x2/\x1/ | |
| s/^\x3/\x2/ | |
| s/^\x4/\x3/ | |
| s/^\x5/\x4/ | |
| s/^\x6/\x5/ | |
| s/^\x7/\x6/ | |
| s/^\x8/\x7/ | |
| s/^\x9/\x8/ | |
| s/^\xa/\x9/ | |
| s/^\xb/\xa/ | |
| s/^\xc/\xb/ | |
| s/^\xd/\xc/ | |
| s/^\xe/\xd/ | |
| s/^\xf/\xe/ | |
| s/^\x10/\xf/ | |
| s/^\x11/\x10/ | |
| s/^\x12/\x11/ | |
| s/^\x13/\x12/ | |
| s/^\x14/\x13/ | |
| s/^\x15/\x14/ | |
| s/^\x16/\x15/ | |
| s/^\x17/\x16/ | |
| s/^\x18/\x17/ | |
| s/^\x19/\x18/ | |
| s/^\x1a/\x19/ | |
| s/^\x1b/\x1a/ | |
| s/^\x1c/\x1b/ | |
| s/^\x1d/\x1c/ | |
| s/^\x1e/\x1d/ | |
| s/^\x1f/\x1e/ | |
| s/^ /\x1f/ | |
| s/^!/ / | |
| s/^"/!/ | |
| s/^#/"/ | |
| s/^\$/#/ | |
| s/^%/\$/ | |
| s/^\&/%/ | |
| s/^'/\&/ | |
| s/^\(/'/ | |
| s/^\)/\(/ | |
| s/^\*/\)/ | |
| s/^\+/\*/ | |
| s/^,/\+/ | |
| s/^\-/,/ | |
| s/^\./\-/ | |
| s/^\//\./ | |
| s/^0/\// | |
| s/^1/0/ | |
| s/^2/1/ | |
| s/^3/2/ | |
| s/^4/3/ | |
| s/^5/4/ | |
| s/^6/5/ | |
| s/^7/6/ | |
| s/^8/7/ | |
| s/^9/8/ | |
| s/^:/9/ | |
| s/^;/:/ | |
| s/^</;/ | |
| s/^=/</ | |
| s/^>/=/ | |
| s/^\?/>/ | |
| s/^@/\?/ | |
| s/^A/@/ | |
| s/^B/A/ | |
| s/^C/B/ | |
| s/^D/C/ | |
| s/^E/D/ | |
| s/^F/E/ | |
| s/^G/F/ | |
| s/^H/G/ | |
| s/^I/H/ | |
| s/^J/I/ | |
| s/^K/J/ | |
| s/^L/K/ | |
| s/^M/L/ | |
| s/^N/M/ | |
| s/^O/N/ | |
| s/^P/O/ | |
| s/^Q/P/ | |
| s/^R/Q/ | |
| s/^S/R/ | |
| s/^T/S/ | |
| s/^U/T/ | |
| s/^V/U/ | |
| s/^W/V/ | |
| s/^X/W/ | |
| s/^Y/X/ | |
| s/^Z/Y/ | |
| s/^\[/Z/ | |
| s/^\\/\[/ | |
| s/^\]/\\/ | |
| s/^\^/\]/ | |
| s/^_/\^/ | |
| s/^`/_/ | |
| s/^a/`/ | |
| s/^b/a/ | |
| s/^c/b/ | |
| s/^d/c/ | |
| s/^e/d/ | |
| s/^f/e/ | |
| s/^g/f/ | |
| s/^h/g/ | |
| s/^i/h/ | |
| s/^j/i/ | |
| s/^k/j/ | |
| s/^l/k/ | |
| s/^m/l/ | |
| s/^n/m/ | |
| s/^o/n/ | |
| s/^p/o/ | |
| s/^q/p/ | |
| s/^r/q/ | |
| s/^s/r/ | |
| s/^t/s/ | |
| s/^u/t/ | |
| s/^v/u/ | |
| s/^w/v/ | |
| s/^x/w/ | |
| s/^y/x/ | |
| s/^z/y/ | |
| s/^\{/z/ | |
| s/^\|/{/ | |
| s/^\}/|/ | |
| s/^\~/}/ | |
| s/^\x7f/~/ | |
| s/^\x80/\x7f/ | |
| s/^\x81/\x80/ | |
| s/^\x82/\x81/ | |
| s/^\x83/\x82/ | |
| s/^\x84/\x83/ | |
| s/^\x85/\x84/ | |
| s/^\x86/\x85/ | |
| s/^\x87/\x86/ | |
| s/^\x88/\x87/ | |
| s/^\x89/\x88/ | |
| s/^\x8a/\x89/ | |
| s/^\x8b/\x8a/ | |
| s/^\x8c/\x8b/ | |
| s/^\x8d/\x8c/ | |
| s/^\x8e/\x8d/ | |
| s/^\x8f/\x8e/ | |
| s/^\x90/\x8f/ | |
| s/^\x91/\x90/ | |
| s/^\x92/\x91/ | |
| s/^\x93/\x92/ | |
| s/^\x94/\x93/ | |
| s/^\x95/\x94/ | |
| s/^\x96/\x95/ | |
| s/^\x97/\x96/ | |
| s/^\x98/\x97/ | |
| s/^\x99/\x98/ | |
| s/^\x9a/\x99/ | |
| s/^\x9b/\x9a/ | |
| s/^\x9c/\x9b/ | |
| s/^\x9d/\x9c/ | |
| s/^\x9e/\x9d/ | |
| s/^\x9f/\x9e/ | |
| s/^\xa0/\x9f/ | |
| s/^\xa1/\xa0/ | |
| s/^\xa2/\xa1/ | |
| s/^\xa3/\xa2/ | |
| s/^\xa4/\xa3/ | |
| s/^\xa5/\xa4/ | |
| s/^\xa6/\xa5/ | |
| s/^\xa7/\xa6/ | |
| s/^\xa8/\xa7/ | |
| s/^\xa9/\xa8/ | |
| s/^\xaa/\xa9/ | |
| s/^\xab/\xaa/ | |
| s/^\xac/\xab/ | |
| s/^\xad/\xac/ | |
| s/^\xae/\xad/ | |
| s/^\xaf/\xae/ | |
| s/^\xb0/\xaf/ | |
| s/^\xb1/\xb0/ | |
| s/^\xb2/\xb1/ | |
| s/^\xb3/\xb2/ | |
| s/^\xb4/\xb3/ | |
| s/^\xb5/\xb4/ | |
| s/^\xb6/\xb5/ | |
| s/^\xb7/\xb6/ | |
| s/^\xb8/\xb7/ | |
| s/^\xb9/\xb8/ | |
| s/^\xba/\xb9/ | |
| s/^\xbb/\xba/ | |
| s/^\xbc/\xbb/ | |
| s/^\xbd/\xbc/ | |
| s/^\xbe/\xbd/ | |
| s/^\xbf/\xbe/ | |
| s/^\xc0/\xbf/ | |
| s/^\xc1/\xc0/ | |
| s/^\xc2/\xc1/ | |
| s/^\xc3/\xc2/ | |
| s/^\xc4/\xc3/ | |
| s/^\xc5/\xc4/ | |
| s/^\xc6/\xc5/ | |
| s/^\xc7/\xc6/ | |
| s/^\xc8/\xc7/ | |
| s/^\xc9/\xc8/ | |
| s/^\xca/\xc9/ | |
| s/^\xcb/\xca/ | |
| s/^\xcc/\xcb/ | |
| s/^\xcd/\xcc/ | |
| s/^\xce/\xcd/ | |
| s/^\xcf/\xce/ | |
| s/^\xd0/\xcf/ | |
| s/^\xd1/\xd0/ | |
| s/^\xd2/\xd1/ | |
| s/^\xd3/\xd2/ | |
| s/^\xd4/\xd3/ | |
| s/^\xd5/\xd4/ | |
| s/^\xd6/\xd5/ | |
| s/^\xd7/\xd6/ | |
| s/^\xd8/\xd7/ | |
| s/^\xd9/\xd8/ | |
| s/^\xda/\xd9/ | |
| s/^\xdb/\xda/ | |
| s/^\xdc/\xdb/ | |
| s/^\xdd/\xdc/ | |
| s/^\xde/\xdd/ | |
| s/^\xdf/\xde/ | |
| s/^\xe0/\xdf/ | |
| s/^\xe1/\xe0/ | |
| s/^\xe2/\xe1/ | |
| s/^\xe3/\xe2/ | |
| s/^\xe4/\xe3/ | |
| s/^\xe5/\xe4/ | |
| s/^\xe6/\xe5/ | |
| s/^\xe7/\xe6/ | |
| s/^\xe8/\xe7/ | |
| s/^\xe9/\xe8/ | |
| s/^\xea/\xe9/ | |
| s/^\xeb/\xea/ | |
| s/^\xec/\xeb/ | |
| s/^\xed/\xec/ | |
| s/^\xee/\xed/ | |
| s/^\xef/\xee/ | |
| s/^\xf0/\xef/ | |
| s/^\xf1/\xf0/ | |
| s/^\xf2/\xf1/ | |
| s/^\xf3/\xf2/ | |
| s/^\xf4/\xf3/ | |
| s/^\xf5/\xf4/ | |
| s/^\xf6/\xf5/ | |
| s/^\xf7/\xf6/ | |
| s/^\xf8/\xf7/ | |
| s/^\xf9/\xf8/ | |
| s/^\xfa/\xf9/ | |
| s/^\xfb/\xfa/ | |
| s/^\xfc/\xfb/ | |
| s/^\xfd/\xfc/ | |
| s/^\xfe/\xfd/ | |
| s/^\xff/\xfe/ | |
| x | |
| } | |
| /^</ { | |
| x | |
| s/((.|[^.])*)(.|[^.])$/\3\1/ # Rotate mem to the right. (MP--) | |
| x | |
| } | |
| /^>/ { | |
| x | |
| s/^(.|[^.])((.|[^.])*)$/\2\1/ # Rotate mem to the left. (MP++) | |
| x | |
| } | |
| /^\{/ { # '['; start of loop. | |
| x | |
| /^\x0/ { # If curr mem cell is zero: | |
| s/^/-:/ # Insert a counter to keep track of nested looping depth. | |
| :loopCountStart | |
| x | |
| s/^(.)(.*)$/\2\1/ # IP++ | |
| /^\{/ { | |
| x | |
| s/^/-/ # Increase loop depth. | |
| x | |
| } | |
| /^}/ { | |
| x | |
| s/^-// # Decrease loop depth. | |
| x | |
| } | |
| x | |
| /^-/ b loopCountStart | |
| s/^:// # Strip leading colon (temp to separate loopcount from mem). | |
| } | |
| x | |
| } | |
| /^}/ { # ']'; end of loop. | |
| x | |
| /^\x0/ !{ # If curr mem cell is nonzero: | |
| s/^/-:/ # Insert a counter to keep track of nested looping depth. | |
| :loopCountEnd | |
| x | |
| s/^(.*)(.)$/\2\1/ # IP-- | |
| /^\{/ { | |
| x | |
| s/^-// # Decrease loop depth. | |
| x | |
| } | |
| /^}/ { | |
| x | |
| s/^/-/ # Increase loop depth. | |
| x | |
| } | |
| x | |
| /^-/ b loopCountEnd | |
| s/^:// # Strip leading colon (temp to separate loopcount from mem). | |
| } | |
| x | |
| } | |
| /^\./ { | |
| x;p;x | |
| } | |
| /^,/ { } # I/O - unimplemented. | |
| /^q/ { | |
| x | |
| s/(.|[^.])/&, /g # Neater memory output. | |
| s/, $// # Strip the trailing ', '. | |
| s/^(.|[^.])*$/{ & }/ # Contain output in bracers. | |
| # d # Delete the standard output! | |
| q | |
| } | |
| :next | |
| s/^(.)(.*)$/\2\1/ # Rotate commands to the left. | |
| b parse |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment