Tech

Whitespace

Tuesday, October 21st, 2008 | Tech | No Comments

I’ve got a soft spot for stack-based languages — and not just because the bytecode for my current favourite language happens to be one (albeit one with a fairly extensive set of relatively complex operations). Another soft spot is toy languages, so it shouldn’t be surprising that I’ve been meaning to play around with something like Whitespace or Brainfuck for a while. All I lacked was an itch [1,2] and yesterday I got one.

In 2008, if you’re going to write code to convert tabs to spaces, there really seems to be only one choice. Not only is Whitespace one of the few languages to fully support literate programming but it also has advanced security built right into the syntax and the very name is tailored to the problem domain at hand.

So, armed with a tutorial, an assembler and a Python Whitespace interpreter [3], I set to work. An hour or two later I had a result, which you can find here. It can be called using something like:

echo -e “a\tb c” | python main.py ../tabs2spaces/tabs2spaces.ws

or

echo -e “a\tb c” | python main.py ../tabs2spaces/comment.embedded

for the text embedded version (here main.py refers to Phillip Bradbury’s interpreter).

I opted for Phillip’s Python version of the Whitespace interpreter rather than the more standard Haskell one, because Phillip’s one sensibly reports -1 when the end of standard input is reached, while the Haskell one raises an error. I had to make one minor change to Phillip’s interpreter to stop parsing the source file once the end-of-program token had been reached, which allows it to process embedded Whitespace where the text extends beyond the program source. The modified version is available here under the GPL.

[1] “good work” may be something of a misnomer in this case.

[2] And I’m dubious as to whether this constitutes “knowing what to write”.

[3] What better way of implementing one stack-based language than with another stack-based language?

Tags:

The Dragon Curve

Tuesday, October 21st, 2008 | Tech | No Comments

My front page needed something to distinguish it a bit from the sea of internet homepages and the obvious thing was a fractal. After hunting around a little on the Wikipedia list I opted for the Dragon Curve.

The dragon curve or Heighway dragon is one of the many named space-filling (you can see it has Hausdorff dimension 2 in the list) curves and happens to look quite nice while being slightly less well-known. You can generate it either by recursively adding detail to a straight line (as is often done for the Koch curve and Sierpinski triangles) or by taking the limit set of an iterated function system (like for the Mandelbrot set).

Of course it would have been a bit boring to just slap up an image, and I felt it would be cool to have something that actually used the refinement algorithm to construct the dragon curve on screen, so with the help of jQuery and jQuery.svg, I made a little Javascript and SVG (sorry, IE users) implementation that performs the first 14 iterations.

The result is here and the Javascript code here.

Tags: , , , ,

Meta

Search