r/codegolf • • Oct 16 '20

BrainF*** interpreter in 248 bytes of Ruby

t={};s=ARGV[t.default=p=w=0];m={"+"=>"z+=1","-"=>"z-=1","."=>"puts z.chr",","=>"z=STDIN.getc.ord","<"=>"p-=1",">"=>"p+=1","]"=>"z==0||w-=Z'['","["=>"z==0&&w+=Z']'"};until w==s.length;eval m[s[w]].gsub(/Z/,"1until s[w]==").gsub(/z/,"t[p]");w+=1;end

Unfortunatly dosn't do nested brackets (I have enough trouble doing that normally).

16 Upvotes

2 comments sorted by

5

u/madsohm Oct 17 '20

Here’s mine in 234 bytes

a,*b=$<.read,c=d=n=0
(t,m=a[d],-1
t[?>]&&b[c-=m]||=n
t[?<]&&c+=m
t[?+]&&b[c]-=m
t[?-]&&b[c]+=m
t[?.]&&$><<b[c].chr
t[?,]&&b[c]=STDIN.getc.ord
n==b[c]?t[?[]&&n=m=1:t[?]]&&n=1
(a[d+=m][?[]&&n+=m
a[d][?]]&&n-=m)until n<1
d+=1)while a[d]

2

u/binarycat64 Oct 17 '20

uh, how?

Edit: now I remember, charachter literals. clever.