4 ms·
To get a glimpse of stack-oriented programming, I really like and recommend PostScript, because this also automatically introduces you to a widely used programm
by triska 7y ago
To get a glimpse of stack-oriented programming, I really like and recommend PostScript, because this also automatically introduces you to a widely used programming language with important practical applications, and it's also great fun to learn.
So, for instance, let's elicit an error using Ghostscript, a freely available and efficient PostScript interpreter:
$ gs
GPL Ghostscript 9.27 (2019-04-04)
...
At the "GS>" prompt, I now define "f" via:
/f { /hello g } def
Note that "f" refers to "g", which is not yet defined.
Therefore, when I then invoke "f" via:
GS>f
I get an error:
Error: /undefined in g
And importantly, the stack is retained, so I can inspect it at this point, interactively, using for example "pstack":
GS<1>pstack
/hello
This shows that /hello is still on the stack. From there, we can resume in various ways, for example by clearing the stack (with "clear"), by redefining and reinvoking "f" etc.
- haolez 7y agoIs PostScript used beyond page description? I'm really curious :)
- PeCaN 7y agoSun made a windowing system based on (Display) PostScript way back https://en.wikipedia.org/wiki/NeWS https://en.wikipedia.org/wiki/NeWS
- hnick 7y agoIt's Turing-complete, which makes writing programs to analyse documents a bit of a problem at times. But I think maybe you meant in the sense of "in the real world", and I can't think of any examples. Prefixing some postscript to a document to manipulate that file is something it can be suited for, rather than using an external language to do so.
- RodgerTheGreat 7y agoOnce I wrote a bingo card in PostScript which would randomize its cells every time you printed it. That's almost useful.
- kortex 7y agoI found a .ps that generates timing wheel patterns (discs with alternating black and white radial bands). you'd plug in inner/outer radius, number of bands, and you'd get a printable document from that file. As in, the .ps file was code, which printed as an image. I now know this is the origin of pdf but it blew my mind.
- pedrow 7y agoI recently saw an announcement[0] of a Z-machine in Postscript. The Z-machine is a runtime for text adventure games. Also posted on HN[1] but no comments were made. [0]: https://groups.google.com/d/msg/rec.arts.int-fiction/HNtntxCpcHA/IB0EiadzDgAJ https://groups.google.com/d/msg/rec.arts.int-fiction/HNtntxC... [1]: https://news.ycombinator.com/item?id=21367412 https://news.ycombinator.com/item?id=21367412
- zzo38computer 7y agoYes; I wrote that program, and the Usenet message and the message on HN (which mentions the message ID for the Usenet message). If you have comments about it, I suggest writing a follow-up message on Usenet. PostScript is a real programming language and can be used for many things (especially graphics). I also wrote a JSON parser in PostScript (see message <1567977875.bystand@zzo38computer.org> on comp.lang.postscript), and then someone else post their JSON parser in PostScript too. I think PostScript isn't such a good protocol or document format, but it is good as a programming language. It is what I might use if I want to make a diagram (but unfortunately Ghostscript cannot produce SVG output; it can output PDF though, as well as many raster formats).
- haolez 7y agoNice! Thanks for answering! Would you choose PostScript over Forth as a general purpose language?
- zzo38computer 7y agoI think it would depend on what I am trying to make; there are advantages and disadvantages to each. In the case of the JSON parser, I think it is helpful that PostScript has data structures (such as arrays and dictionaries) and arbitrarily typed values like JavaScript and many other scripting languages have. In the case of Z-machine, Forth would probably do as well as PostScript (or probably Forth would do better, actually). Someone was unsure how useful would be the JSON parser I wrote in PostScript, but if you want to print some diagram based on data in JSON format, then it can be helpful. (I could implement Z-machine in Forth too if I wanted to do, but I have not done so yet because I have other things to do; I don't know if I might do so in future or not. I could implement Z-machine in any programming language with byte arrays, including assembly language (I started but haven't finished one in 6502).)
- rwmj 7y agohttps://www-cdf.fnal.gov/offline/PostScript/fractal-fern.ps https://www-cdf.fnal.gov/offline/PostScript/fractal-fern.ps is a fractal fern. This is page description, but not in the normal way that it's used. Compare the output in a PS or PDF viewer with the source code which is under 1.5K.
- Mikhail_Edoshin 7y agoEven in page description alone the programming capabilities are rather useful. E.g. to produce barcodes you can just generate them from data.