# # # # # # # # #
# /9/brainfuck_interpreter.rb
#
# by               Jan Lelis
# e-mail:          mail@janlelis.de
# type/version:    ruby 1.8
# snippet url:     http://rbJL.net/9/brainfuck_interpreter.rb
# original post:   http://rbJL.net/9-ruby-brainfuck-golf-update
# license:         CC-BY-SA (DE)
#
# (c) 2009 Jan Lelis.

f,e=$<.read.split'!'
e&&e=e.split('')
b=[a=i=n=0]*s=30000
while i<f.size
  case f[i]
  when ?>
    a+=1
  when ?<
    a-=1
  when ?+
    b[a]+=1
  when ?-
    b[a]-=1
  when ?.
    putc b[a]
  when ?,
    e==[]?i=s:b[a]=e.shift[0]
  when ?[
    n=d=1 if b[a]==0
  when ?]
  n,d=1,-1 if b[a]!=0
  end
  
  while n>0
    i+=d
    f[i]==?[&&n+=d
    f[i]==?]&&n-=d
  end
  i+=1
end