Let's Write a PostScript Interpreter beyondloom.com · 17 points · 4 comments · H...
HN Best 200 @best
Let's Write a PostScript Interpreter beyondloom.com · 17 points · 4 comments · HN thread
TL;DR Ruleset-style walkthrough by Beyondloom of building a small PostScript interpreter in a terse JavaScript style, covering tokenizing, the operand and dictionary stacks, and a REPL in roughly a page of code. The article also discusses PostScript's dynamic scoping, its lack of a programmer-accessible return stack, and exercises such as defining if, Fibonacci and array reversal.
- The interpreter is built around two global stacks: an operand stack for arguments and a dictionary stack for name bindings.
- Procedures are parsed into tokens grouped in a proc wrapper and executed later, while names are looked up through the dictionary stack.
- Primitives are plain JavaScript functions, with arity taken from function length, so most operators are one-liners.
- The author argues PostScript's dynamic scope and missing return stack make higher-order functions and lexical-style abstractions hard to write.
- The article ends with exercises such as abs, min, cond, and better error handling, and a REPL runs under Node.js or QuickJS.
Discussion The thread is short and mostly appreciative. One commenter praises the learning value of writing a tiny interpreter, while another pushes back that the language core is the easy part and that a real PostScript implementation is dominated by graphics and font rendering. The mood is mild and lightly sceptical rather than heated.
- Building a small interpreter teaches more than using languages (some): Commenters say writing a toy interpreter like this builds deep understanding of programming language design that everyday use does not. One points to the PostScript Language Reference Manual as a pleasure to read for its careful detail.
- Language core is trivial; graphics are the real work (some): A commenter dismisses the article as a yawner because PostScript's interpreter machinery is run-of-the-mill, and argues that a working PostScript system's difficulty lies in graphics and font rendering.
- Still fun for many readers (a few): One reply argues the piece will be novel and enjoyable for plenty of people even if experts find it basic, linking an xkcd comic about the ten-thousandth such post.
Links from the comments
#PostScript #interpreters #JavaScript #programmingLanguages #Forth