2 ms·
Lots of virtual machines are stack machines and lots of languages compile to bytecode for a virtual machine. I thought that bytecode being a language in its own
by JonChesterfield 4y ago
Lots of virtual machines are stack machines and lots of languages compile to bytecode for a virtual machine. I thought that bytecode being a language in its own right would be a good idea so tried using Forth for it. Had a rough plan of a toolchain resembling a ML implemented in a lisp compiled to a forth. There's an article (by Baker?) which showed recursion and iteration folding to the same code on a stack machine which is beautiful.
That project was an abject failure. I was not good at programming stack machines. I can't keep track of where things are on the stack. The Forth way seems to be using lots of short functions (uh, words) with predictable stack behaviour that compose to more complicated behaviour, and I struggle to keep track of many small functions too. Both combined is really tough. I essentially need to use a compiler to reliably target the stack VM.
It turns out I type while I think and that yields large, sprawling functions with dead paths and mistakes. That's then refined and factored as my thinking clears. A repl sort of works for this but there's a lot of reaching back to gather the pieces where the working ones are not clearly distinguished from the failed experiments. The large messy work in progress function strategy did not work at all in forth.
It's surely possible to retrain oneself to think the Forth way. Might be worthwhile, though it's hard to guess the value trade-off before doing so. People tell me it's worthwhile, and they were right about that with lisp, so I'm probably missing out.
- MaxBarraclough 4y agoForth people like to factor code into very short words, to help avoid confusion with the stack. Words should be properly commented so that their stack effects are clear. https://www.complang.tuwien.ac.at/forth/gforth/Docs-html/Factoring-Tutorial.html https://www.complang.tuwien.ac.at/forth/gforth/Docs-html/Fac...