[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

[ANN] Brrain, another Brainf*** interpreter

Bertram Scharpf

3/4/2005 11:48:00 AM

Hi,


yesterday, I had fun to write the ultimate interpreter the
world has been waiting for.

Brainf*** is a programming language consisting of eight
instructions and yielding weird pieces of code even when
doing simple things. Maybe you like to have a look at it.

http://projects.bertram-scharpf...

I hope this is at least a good example of programming Ruby.

Bertram


--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-...


2 Answers

Florian Gross

3/4/2005 1:13:00 PM

0

Bertram Scharpf wrote:

> yesterday, I had fun to write the ultimate interpreter the
> world has been waiting for.
>
> Brainf*** is a programming language consisting of eight
> instructions and yielding weird pieces of code even when
> doing simple things. Maybe you like to have a look at it.
>
> http://projects.bertram-scharpf...

Here's mine, it uses continuations and swaps out the instruction table
for implementing the '[' and ']' instructions meaning that it does no
special casing for them.

Regarding non-standard instructions it provides a '#' instruction for
outputting the stack position and elements which is useful for debugging
code.

Here's a usage example that implements a few more non-standard
instructions as well:

> instructions = {
> 'r' => lambda { @data[@pointer] = rand(@data[@pointer]) },
> '%' => lambda { @data[@pointer] %= @data[@pointer + 1].to_f },
> '/' => lambda { @data[@pointer] /= @data[@pointer + 1].to_f },
> '*' => lambda { @data[@pointer] *= @data[@pointer + 1] },
> 's' => lambda { @data[@pointer].to_s.split(//).each { |x| putc x[0] } },
> 'i' => lambda do
> input = gets
> number = /\./.match(input) ? input.to_f : input.to_i
> @data[@pointer] = number
> end,
> 't' => lambda { @data[@pointer] = @data[@pointer].to_i rescue @data[@pointer] },
> 'C' => lambda do
> @continuations ||= []
> cc, result, called, rpi = Continuation.create(@data[@pointer], false, nil)
> @continuations << cc unless called
> @data[@pointer] = rpi || @continuations.size - 1
> @data[@pointer + 1] = result
> end,
> 'c' => lambda do
> @continuations ||= []
> cc = @continuations[@data[@pointer]]
> cc.call(@data[@pointer + 1], true, nil) if cc
> end
> }
> context = BrainfuckContext.new(nil, instructions)

The above would then allow you to implement the calculation of two
numbers as i>i*s.

A notable differences to your implementation: Mine has infinite memory
that grows into both directions from zero. I've also seen
implementations that wrap around to the last element when < is executed
at memory position 0.

Martin DeMello

3/4/2005 1:28:00 PM

0

Florian Gross <flgr@ccan.de> wrote:
> that grows into both directions from zero. I've also seen
> implementations that wrap around to the last element when < is executed
> at memory position 0.

That should allow some nice tricks if you ever wanted to write
obfuscated brainfuck

martin