Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.61 KB | None | 0 0
  1. code = '+[,.]'
  2.  
  3. ip = p = 0
  4. stack = mem = []
  5.  
  6. while true
  7.   case code[ip]
  8.   when '+'
  9.     if mem[p] == nil
  10.       mem[p] = 1
  11.     else
  12.       mem[p] += 1
  13.     end
  14.  
  15.   when '-'
  16.     if mem[p] == nil
  17.       mem[p] = -1
  18.     else
  19.       mem[p] -= 1
  20.     end
  21.  
  22.   when '>'
  23.     p += 1
  24.  
  25.   when '<'
  26.     p -= 1
  27.  
  28.   when '.'
  29.     print mem[p].chr
  30.  
  31.   when ','
  32.     STDOUT.flush
  33.    
  34.     input = gets.chomp[0]
  35.    
  36.     if input != nil
  37.       mem[p] = input
  38.     end
  39.  
  40.   when '['
  41.     stack.push ip
  42.  
  43.   when ']'
  44.     if mem[p] != 0
  45.       ip = stack[-1]
  46.     else
  47.       stack.pop
  48.     end
  49.   end
  50.  
  51.   ip += 1
  52.  
  53.   if ip == code.length
  54.     break
  55.   end
  56. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement