Execute Brain****/Ruby: Difference between revisions
Content added Content deleted
(improvement of the debugging output, each_with_index->with_index) |
(I'm liking Ruby; concise AND tidy) |
||
Line 1: | Line 1: | ||
{{implementation|Brainf***}}{{collection|RCBF}} |
{{implementation|Brainf***}}{{collection|RCBF}} |
||
==Version 1== |
|||
An implementation of a [[Brainf***]] interpreter in [[Ruby]]. |
An implementation of a [[Brainf***]] interpreter in [[Ruby]]. |
||
More effort could be made to read a program from a file or from stdin. |
More effort could be made to read a program from a file or from stdin. |
||
Line 80: | Line 81: | ||
<pre>Hello World! |
<pre>Hello World! |
||
@</pre> |
@</pre> |
||
==Version 2== |
|||
This variant converts the brainfuck into Ruby code and runs that instead. |
|||
Do note that this requires Ruby1.9.1 or later, earlier versions need a somewhat more verbose variant. |
|||
BF code may be read from a file or taken from STDIN. |
|||
<lang ruby>eval 'm=Hash.new(p=0);'+ARGF.read.gsub( |
|||
/./, |
|||
'>' => 'p+=1;', |
|||
'<' => 'p-=1;', |
|||
'+' => 'm[p]+=1;', |
|||
'-' => 'm[p]-=1;', |
|||
'[' => '(;', |
|||
']' => ')while((m[p]&=255)!=0);', |
|||
'.' => 'putc(m[p]&=255);', |
|||
',' => 'm[p]=STDIN.getc.ord if !STDIN.eof;') |