17 ms·
Forth: Stack-Manipulation Operators
- jnwatson 5y agoThis reminds me of the excellent shareware game for the Mac called RoboWar [1] by David Harris. In it, you programmed a robot to destroy other programmed robots in an arena. You could spend points on different weapons, shields, and processor speed. The programming language was a custom stack-based language like Forth. It worked quite well for its purpose, as you could easily count instructions. 1. https://en.wikipedia.org/wiki/RoboWar https://en.wikipedia.org/wiki/RoboWar
- huachimingo 5y agoMinecraft had a mod with Forth support: https://technicpack.fandom.com/wiki/Forth_language https://technicpack.fandom.com/wiki/Forth_language
- astrobe_ 5y agoAnd so those Minetest: https://github.com/Ekdohibs/forth_computer https://github.com/Ekdohibs/forth_computer (it seems to be an unsafe mod tho cause I see it loads shared libraries).
- kragen 5y agoThere's less demand for this in Minetest, though, because modding Minetest is already super easy in Lua—you don't have to learn Java like you do for Minecraft. But you can't load a Lua mod even in Minetest if you don't run the server. Mobile-code environments for mutually distrusting code turn out to be more difficult than people used to think, but now we have multiple off-the-shelf battle-tested implementations of JS for that. And you can coerce Lua to do it, kind of.
- threatofrain 5y agoIt seems that as interesting as Forth is, the community of such languages is very niche and small, even smaller than Lisp. Can anyone speak to recent changes in velocity or energy?
- openfuture 5y agoI don't think forth has a smaller community than lisp just a less web-friendly one. Lots of embedded people. Lisp is top down, forth is bottom up.
- catketch 5y agoWhat would you call HP’s RPL then :)
- openfuture 5y agoA lattice? (But touché :)
- PaulHoule 5y agoWho cares? If you need some minimal programming language with some special characteristics, you can write a FORTH in 2k lines of assembler or so. The first time I used 32 bit ints on an 8 bit computer was in a FORTH that implemented 32 bit math, for instance.
- protomyth 5y agoBecause of the original 128K Mac's limited memory, FORTH was one of the first actually useful language you could host on it.
- PaulHoule 5y agoThe 128K Mac had a lot more memory than most other computers in 1984, but it was not a lot of room to fit a GUI in. Oddly, within a few years people had shoehorned GUIs into much smaller machines, such as https://en.wikipedia.org/wiki/GEOS_(8-bit_operating_system) https://en.wikipedia.org/wiki/GEOS_(8-bit_operating_system)
- anthk 5y agoSadly for CollapseOS' users, this Forth implementation doesn't have ".S".
- sigjuice 5y agoI came across this implementation of .S a couple of days ago (Starting-FORTH.pdf, page 50). : .S CR 'S S0 @ 2- DO I @ . -2 +LOOP ; I'm not sure if this could be adapted to work on CollapseOS. For starters, I can't quite figure out what 'S is. S0 is the (non-standard) stack pointer according to https://www.forth.com/starting-forth/9-forth-execution/ https://www.forth.com/starting-forth/9-forth-execution/ . The rest of it appears to be standard Forth, but I'm not sure how much of it is actually implemented.
- anthk 5y agoHeh, thanks, I just was in the 14th page reviewing it, I forgot about that :D. Also, 4th implements that with "anscore.4th" I think. EDIT: Neither 'S is implemented in Collapseos.
- deleted 5y ago[deleted]
- kragen 5y agoAs I wrote here a month ago, I worry that the stack manipulation operators can be a cognitive danger to people who are newly learning Forth: https://news.ycombinator.com/item?id=26884231 https://news.ycombinator.com/item?id=26884231 It's cool to be able to write : CAB ( c a b -- ) - SWAP / ; but that very coolness can easily lead you into giving up on Forth. It's natural to work to do everything on the stacks, since you can, but that's not a practical way to program; this function is fine, but in sufficiently complicated cases, you end up with unreadable code where you're struggling to play mental chess with the stack. And then you sigh, decide that Forth is for people smarter than you (which is reinforced by some of the self-aggrandizing nonsense around Forth), and you go back to Python. Maybe a better way to teach Forth would be to teach people about VARIABLEs or VALUEs first, so they can write 0 VALUE C : CAB TO C - C / ; or possibly VARIABLE C : CAB C ! - C @ / ; and then only later introduce SWAP, DUP, and the rest as shortcuts. VALUE or VARIABLE is simpler than stack manipulation operators — with either VALUE and TO, or with @, !, and VARIABLE, you can write every possible stack manipulation operator. So it's less to learn than the whole zoo of DUP, DROP, SWAP, 2DUP, 2DROP, OVER, >R, R>, TUCK, NIP, 2SWAP, ROT, 2OVER, -ROT, R@, and RDROP. But then, later on — look! With this new SWAP shortcut, you can write this CAB function without a variable! And you probably should. But if you ever find yourself writing code with -ROT you probably should seriously consider rewriting it. Of course mutable variables are themselves bug-prone — you can forget to set them on some control path, for example, or forget to redeclare them and so accidentally share a variable with some subroutine you're calling — but in my experience these are much less serious pitfalls than ending up with a bunch of @ ROT + SWAP and OVER + SWAP 0 >R. On the other hand, I don't have a great track record with either writing difficult programs in Forth or teaching people things. So I might be off base here.
- retrac 5y agoIt's Forth tradition to make your own slightly incompatible Forth. Honouring that tradition, I suggest an extension to the VM. Either with a few registers, or a second stack. For pointers. That way, memory access to a single or handful of variables becomes a single word. Add things like dereference-and-increment load/store primitives and it becomes very C-like, and a rather nice target for compilers. (I can't take credit for this idea: https://www.complang.tuwien.ac.at/anton/euroforth/ef08/papers/pelc.pdf https://www.complang.tuwien.ac.at/anton/euroforth/ef08/paper... "Updating the Forth Virtual Machine" by Stephen Pelc for EuroForth 2008, which is probably also not the first time someone suggested this.)
- floatingatoll 5y agoApparently I read the book that these images are taken from, because I recognize having read these stack manipulation comics as a kid! Is this site from a book?
- wiml 5y agoI think they're from Leo Brodie's _Starting Forth_, which I also read as a kid.
- cyberdelica 5y agoYou're right. The linked page, is just Chapter 2 of Starting Forth.
- susam 5y agoIncidentally, a couple of weeks ago I wrote a tiny Star Wars-themed Forth program to celebrate Star Wars Day: https://github.com/susam/may4 https://github.com/susam/may4 Forth brings back the fun in computing for me that I once experienced when I began learning to code with Logo. Simple, distraction-free, and fun! "I think that it's extraordinarily important that we in computer science keep fun in computing. When it started out, it was an awful lot of fun. Of course, the paying customers got shafted every now and then, and after a while we began to take their complaints seriously. We began to feel as if we really were responsible for the successful, error-free perfect use of these machines. I don't think we are. I think we're responsible for stretching them, setting them off in new directions, and keeping fun in the house. I hope the field of computer science never loses its sense of fun." -- Alan J. Perlis
- ollran 5y agoProgramming in Lisp or Forth is such fun since you can stretch the programming language itself. For me, the reflective approach keeps the fun in programming.
- nils-m-holm 5y agoThe dragon cartoon (search for SWAP on the page) is one of the cutest in the history of CS books! :)