Tuesday, December 9, 2008

Hullo, I am Guff and I am bored and I have a Python installation.

And goddamnit, I'm going to use it.

Brainfuck is a very silly programming language. But I suppose that's to be expected when you set out with the intention of making a language that's as frustrating as possible to use. There are far more difficult esoteric languages out there (Esolang is a good resource), and Brainfuck is very easy to understand. The difficulty comes from actually getting it to do anything. For example, here's a 'hello world' program written in Brainfuck (from Wikipedia):
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++
..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.
(the line break is not significant and should be ignored by most interpreters)

Simple, right?

Now, how does this all work (I'd recommend skipping this section if you're already familiar with Brainfuck)? Well, first off, the standard implementation only recognizes eight characters as Brainfuck operations: <>[]+-.,

Secondly, think of the Brainfuck environment as a contiguous series of cells. The size of cells can vary based on the implementation, but the most common would seem to be a single byte (meaning the maximum value a cell can hold is 255, assuming unsigned integers). The number of such cells also varies; the original implementation, I believe, used 30,000 cells. Now think of there as being a cursor that points to one of these cells. When your program starts, it will be pointing at the first cell.

Now, back to the operators. The <> operators move the cursor to the previous cell and the next cell, repsectively.
[ signifies the start of a loop, and ] marks the end. When the end of a loop is reached, if the current cell is not equal to 0, the program goes back to the corresponding [ operator.
+ and - increment and decrement the current cell, respectively.
Finally, . prints out the ASCII character corresponding to the value of the current cell, and , takes a character as input and sets the current cell's value to the ASCII value of the input (e.g. 'a' -> 97).

Okay, so now why am I rambling on about this? I dunno. I'm kind of bored and I've been learning Python for some time now and yet I still kind of suck at it. It's either this or a Lifetime movie about an evil baby or something. Also, I find Brainfuck's simplicity fascinating. It's a very interesting language, even if it's almost entirely useless and a pain to write in.
So my goal is to write an interpreter in Python. I'm currently just in the planning stage, but here's what I've got so far:

The basics:
  • Take a file or string as input
  • Get rid of every character that is not a Brainfuck instruction, to make things easier
  • Check the string to make sure the code is well-formed, i.e. the braces are paired up correctly
Now, that's not that hard. And actually implementing the instructions properly shouldn't be either. The problem is that Brainfuck is not a rigorously specified language. How should I implement it?
  • What should the cell size be? Well, I think I'll allow for user-definable cell sizes. By default, cells will store Python's int type, which would also automatically switch over to long when the value becomes too large for int to hold (Python 3.0 doesn't make such a distinction; ints can be arbitrarily large). If this is the case, negative values would also be allowed. At least, I can't think of a reason not to. If a maximum cell size is specified, however, overflow will reset the cell to zero, and decrementing a cell of value zero will set it to its maximum value.
  • How many cells? User-definable, or unlimited? The latter would automatically create new cells when the need arises, and I'm thinking this is the most sensible solution.

I think that's about it for now. I'll post again after I make some actual progress.

1 comment:

  1. What if you have to do a lot of + signs and you miss one? ;_;

    ReplyDelete

Please be civil. Or if you're going to be uncivil, don't hold back. It's more entertaining that way.