Skip to content

Instantly share code, notes, and snippets.

@fj
Created March 18, 2014 18:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fj/9625966 to your computer and use it in GitHub Desktop.
Save fj/9625966 to your computer and use it in GitHub Desktop.
Brainfuck interpreter in Ruby
$_=$$/$$;@_=$_+$_;$-_=$_-@_
$__=->_{_==[]||_==''?$.:$_+$__[_[$_..$-_]]}
@__=->_,&__{_==[]?[]:[__[_[$.]]]+@__[_[$_..$-_],&__]}
$_____=->_{@__[[*_],&->__{__[$.]}]}
@_____=->_{@__[[*_],&->__{__[$-_]}]}
$______=->_{___,______=$_____[_],@_____[_];_____=$__[___];____={};__=$.;(_=->{
____[______[__]]=___[__];(__+=$_)==_____ ?____:_[]})[]}
@______=->_,__{_=[*_]+[*__];____=$__[_];___={};__=$.;(_____=->{
___[_[__][$.]]=_[__][$_];(__+=$_)==____ ?___:_____[]})[]}
$_______=->_{$___=[];@___=$__[_];__=___=____=$.;$____,@____={},[]
(_____=->{
_[____]=='['?(@____<<____):$.
_[____]==']'?($____[@____[$-_]]=____;@____=@____[$...$.-@_]):$.
(____+=$_)==@___?$.:_____[]})[]
$____=$____=={}?{}:@______[$____,$______[$____]]
(______=->{_[__]==
'+'?($___[___]||=$.;$___[___]+=$_):_[__]==
'-'?($___[___]||=$.;$___[___]-=$_):_[__]==
'>'?(___+=$_):_[__]==
'<'?(___-=$_):_[__]==
'['?(__=$___[___]==$.?$____[__]:__):_[__]==
']'?(__=$___[___]!=$.?$____[__]:__):_[__]==
'.'?($><<(''<<$___[___])):$.
(__+=$_)==@___?_:______[]})[]}
$_______['++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment