Tuesday, October 21st, 2008 | Tech

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


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?