Hacker News new | past | comments | ask | show | jobs | submit login
Brainfuck compiler in brainfuck (code.google.com)
79 points by jacquesm on July 10, 2009 | hide | past | favorite | 40 comments



Source code contains (slightly NSFW) ASCII art--that's awesome.

http://code.google.com/p/awib/source/browse/builds/awib-1.0r...


Apologies to anybody that got fired over that.

Brainfuck tutorial:

http://www.nieko.net/bf.php


Ah, that illustration is a classic.


For those of you who like this stuff, the "J Incunabulum" will probably be really interesting. It's an early draft of a J interpreter, written in J-styled C.

http://www.jsoftware.com/jwiki/Essays/Incunabulum

Also, unlike Brainfuck, unlambda, lolcode, malborge, etc., J and related languages are used for real work. They're just really dense, like regular expressions for vector/matrix math.


This is a really interesting piece of code. The style of C programming looks unreadable for the uninitiated, but is in fact very close to what in do in mathematics. I will try to play with this style myself (not for working code, of course).


That's some interesting code, but would be more interesting if it worked. Segfaulted.

K is also fun, but considerably less fun to toy around with now that GUI functionality was taken out of the standard lib.


It's very old, pre-K&R C. It segfaults for me, too, in ga.

It got me interested in the language, but rather than trying to rewrite it, I decided to go through "J For C Programmers" (http://www.jsoftware.com/help/jforc/contents.htm) on my lunch breaks. The APL language family has several interesting ideas, the fanatically terse syntax is just the first one everybody notices.


Considerably less fun to toy around with now that the only way to get a non completely crippled version is to pay megabucks (or work at a company that develops in it). If I'm wrong, please please clue me in...


What you really have to love is the complete lack of comments.


It's a one-page draft of an interpreter for a subset of a language, by one of the main designers involved with it, written to the initial designer. It's written in C, but idiomatically written as closely to the language as possible. Having comments would be pretty strange, honestly.

There's an unpacked and commented version of it here, FWIW: http://www.kuro5hin.org/comments/2002/8/30/175531/763/1#1


Yeah, except he does the same thing in every language, even in written english.

I'm not criticizing it anyway. His K is very clean... usually.

edit: I do wish he'd be a little less terse in the code for interfacing with other C code. Just as a favor to us normal humans.


Agreed. I think it's atrocious C style, it just makes sense for people who write J natively to style the C that way in a context where it will be understood.


The 'J-styled C' reminds me of how the original Bourne shell was written with ALGOL syntax in C using cpp macros, and implements a language with ALGOL-flavored syntax.


Checkout my Brainfuck to Brainfuck compiler in Brainfuck!

  ,[.,]


I wonder if putting brainfuck on my resume would get me in the door, or thrown out the door.


How much patience is necessary for something like this? If I could start a slow clap, I would.


I think you could write some sort of simple language that would get expanded into BF.

e.g.

  +6 -> ++++++


You could compile to brainfuck, treating it as a very simple virtual machine. It's just simplified to the point of being incredibly inefficient, like factoring Church-encoded numbers with Prolog's unification, rather than using its built-in mathematical primitives.

(Whether the bf compiler was actually written or just generated by compiling to bf is neither here nor there.)


in fact, there is work on a gcc-backend which outputs brainfuck (name's gcc-bf)


It goes from awib v1.0-rc1, rc2, up to awib-1.0rc6-BETA.b... and then the final release is awib-0.1 (not awib-1.0).

Am I missing something/is this part of the joke/is it just a typo? It's... ah... messing with my mind a little.


Well, with an increasing version system, you can never tell when you're finished. Is version 3 FINAL the final version, or is some new maintainer going to come make version 4 anyways? If you iterate toward 0, your final product can be awib-0, and you'd be hard pressed to find a new version number without further confusing the three people who got what you were going for.

(I'm guessing it's just a typo.)


Someone ought to port it to Ook! (http://www.dangermouse.net/esoteric/ook.html)

That would be something to read.


that's just a search & replace, they're equivalent.


I didn't say it would be a difficult port....

It would sure look different, though.


I don't want to sound offensive, but do wonder what is the purpose of this exercise? Is there any problem this project is solving? Or is it 'just' for educational purpose?


Writing a brainfuck interpreter in a language is a good way to size it up. Unlike "hello world", it actually gives you a meaningful impression of how string handling, data structures, etc. work. It's not hard, but it's just enough to work up a sweat -- like a web-programmer writing a blogging engine or a wiki to try out a web framework. (If it is hard in the language, that should also tell you something.)

See also:

* Project Euler (http://projecteuler.net/), for math problems

* Programming Praxis (http://programmingpraxis.com/)


Yeah but that doesn't explain why one would write a brainfuck interpreter in brainfuck.

Day 163: Project complete. Strength nearly gone. Must.. complete... evaluation.... Data structures, godawful... string handling, non-existent... writing a brainfuck interpreter, gigantic pain in the ass.... Conclusion: this better get me laid. Oh sh.. signal lost


I think it is quite an amazing feat to write in brainfuck at all (to me it means you have both too much time on your hands and that you grok turing machines), to write a self-hosted compiler (ok, a brainfuck to C translator) is a very neat hack.

If that doesn't work for you think of it as art. Not everything has to have a direct purpose.


I just love these kind of things.

If it was fun developing or fun doeing something so strange, than this was the purpose.

I really don't like it if purpose is reduced to getting richer, making career or impressing other people.

If people think that's the purpose, than they might loose the purpose of life.


Impressing people might have something to this :)

At least I'm impressed.


It's kinda like mountain climbing for hackers I think.


I'd liken it more to a hot dog eating competition for hackers.


Or like extruding peacock feathers for any animal that doesn't have peacock feathers.


Because they can, you can't, and you will never understand it even given all the source code.

Also nice ascii art.

Its not educational, its b0w b3f0r3 m4h sk1llz!

I must also agree with one of the posts here... It is art. Its geek art but it really is art. Judges who say source code is not art should also admit michalengelo was just some asshole who put grafitti on some holy buildings.


I'm sorry, what?

Michelangelo's frescoes were commissioned. I utterly fail to see why anyone should admit they were graffiti.


>> Judges who say source code is not art should also admit michalengelo was just some asshole who put grafitti on some holy buildings.

> Michelangelo's frescoes were commissioned. I utterly fail to see why anyone should admit they were graffiti.

The negatives confuse the sentence, but that was his point. Michelangelo's works were not graffiti, and similarly source code should be considered art, even if it is geek art.


I don't see the connection. One could maintain that source code is not art and that Michelangelo's works were not graffiti.


Awib is an optimizing compiler:

o) Sequences of '-','<','>' and '+' are contracted into single instructions. E.g. "----" is replaced with a single ADD(4).

SUB(4)?


Someone should write a Forth in brainfuck.


is that a challenge ?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: