# Brainfuck interpreter in Thutu2.
# Don't do anything before the program is entered.
/=1//
/=9/=!=x/
# Remove comments from the program ASAP.
/[^][,.<>+=-]([^!]*?)=!/$1=!/
/==/=/
/=%//
# =x can screw up looping.
/=x/%x/
/([^%])x/$1/
# Fold later lines after earlier ones.
/^([^x]*)%x([^x]*)%x/$2$1%x/
# If there's an exclamation mark (end of program):
/=!/*
# Remove any remaining line-end markers.
/=x//
# Restore temporary -- for data pointer and/or ? for command being processed
# and/or {} for [] (used when duplicating loops)
/\-\-/\+\+/
/^\?//
/\{\{/=\[/
/\}\}/=\]/
# If the data pointer's at the end, create a new tape cell.
/\+\+$/\+\+\|/
/\+\+/!
# If there isn't a data pointer, create a new empty tape.
/$/\|\+\+/
/[^|]$/<
/^=\+/\+\+/@
/\+\+/\-\-\./
/^=\+/\?/
/^=\-/\+\+/@
/\+\+\./\-\-/
/^=\-/\?/
/^=\>/\+\+/@
/\|\+\+(\.*?)\|/\|$1\|\-\-/
/^=\>/\?/
/^=\</\+\+/@
/\|(\.*?)\|\+\+/\|\-\-$1\|/
/^=\</\?/
/^=\,/\+\+/@
/^=\,/\?/
/\+\+\./\+\+/
/\+\+/\-\-/
,
*
/=x/%%1%%/
# 0 on EOF
/^=9/0%%1%%/
/%%1%%/*
/^0%%1%%//
/^([0-9]+)%%(1)%%/#
/^[0-9]+%%1%%(.*?)\-\-/$d%%1%%$1\-\-\./
.
.
/^=\./\+\+/@
/^=\./\?/
/\+\+(\.*?)(\|)/$
/\+\+/%%$g%0%%/
/%%([0-9]+)%(0)%%/#
/%%[0-9]+%0%%/%%>$c<%%/
/%%>([^a]|[^b])<%%/|
/%%>([^a]|[^b])<%%/\-\-/
/^=!.*$/=9/
/\-\-/<
# Delete a [] loop and any loops nested in it if there's a loop at the IP
# and the data pointer points to a 0.
/\|\+\+\|/^=\[/^([^]]*)=\[[^][]*?=\]/$1/
/\|\+\+\|/^=\[[^][]*?=\]//
# Duplicate a [] loop if it's at the IP and it got this far.
/^=\[/*
# Change inner loops to use {} not []
/^=\[([^]]*)=\[([^][]*?)=\]/=\[$1\{\{$2\}\}/
# Copy the contents of the loop to the start
/^=\[([^]]*)=\]/$1=\[$1=\]/
/=\[.*?=!/<
.
.