Wednesday, July 20, 2005

What's your mental stack capacity ?

[Warning: programming language geekery, probably best to ignore]

You learn something new every day, whether you want to or not: yesterday, I had to figure out how to programmatically modify a dynamically-generated Postscript file to do something fairly simple: show a particular bit of text in red, and another bit in blue. Initially, I figured this would be pretty simple and that Postscript would turn out to be a lot like HTML ie throw in a few angle brackets and you're done.

Of course, nothing is ever that simple. Postscript turns out to be an actual programming language/environment, with a 900-page reference manual. And, to make matters worse, it's not a straightforward imperative programming language, like C, Python etc -- it's a stack-based language, like those damn Reverse Polish Notation [RPN] calculators. In other words, instead of writing the [to me, at least] straightforward "3 + 4", you write "3 4 +". It's basically a programming language invented by Yoda -- hard it is to understand, to write harder even.

The "operator in an ass-backwards place" bit isn't that bad, it's the stack that really gets you, because it means that when you're trying to understand a Postscript program, you have to mentally keep track of what's on the stack so you can understand what values a given operation is being applied to and what gets put back on the stack. In other words, there are always "hidden" variables that you need to keep track of and that aren't obvious from just looking at a given line of code. For a program of even medium complexity, that gets intractable pretty quickly, so I had to resort to the time-tested method of understanding a program: 'print' statements scattered liberally throughout the code. At least the nice folks at Adobe were kind enough to include a command in the language that dumps the entire stack ...

I'm sure there are good computer science reasons for inventing a stack-based language [at least, I hope there are] but I can't for the life of me think of any [probably because I'm one of those heathen "systems" guys]. I suspect they're mostly reasons like "elegance" and "provability" ie stuff that nobody who is actually trying to use the language to perform a real-world task is likely to care much about. Then again, Adobe has done pretty well with Postscript, so there must be something to it that I just haven't understood based on 3 hours of exposure to the language ...

2 Comments:

Anonymous Bill Tozier said...

Well, not that I'd breed PostScript (hmmm...), but genetic programming is a dream in stack-based languages. Lee Spector's Push is fun, and somewhat legible.

10:23 AM  
Anonymous Anonymous said...

The stack is a central component of x86 processors. Having a stack based language must have something to do that inherent piece of computers.

1:39 AM  

Post a Comment

<< Home