Execute Brain****/Ruby: Difference between revisions

m
Fixed syntax highlighting.
(I'm liking Ruby; concise AND tidy)
m (Fixed syntax highlighting.)
 
Line 1:
{{implementation|Brainf***}}{{collection|RCBF}}
===Version 1===
An implementation of a [[Brainf***]] interpreter in [[Ruby]].
More effort could be made to read a program from a file or from stdin.
 
<langsyntaxhighlight lang="ruby">class RCBF
def initialize(program)
@d = [0] * 30_000
Line 76:
# use nested loop to increment count to 64 and print (should be '@')
# followed by a newline
RCBF.new('>>++++[<++++[<++++>-]>-]<<.[-]++++++++++.').run</langsyntaxhighlight>
 
{{out}}
Line 82:
@</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.
Line 88:
BF code may be read from a file or taken from STDIN.
 
<langsyntaxhighlight lang="ruby">eval 'm=Hash.new(p=0);'+ARGF.read.gsub(
/./,
'>' => 'p+=1;',
Line 97:
']' => ')while((m[p]&=255)!=0);',
'.' => 'putc(m[p]&=255);',
',' => 'm[p]=STDIN.getc.ord if !STDIN.eof;')</syntaxhighlight>
9,476

edits