4 ms·
Forth-2012's local variable support includes support for {: ... :}. A "|" within this separates parameters (which are loaded from passed parameters) from local
by dwheeler 7y ago
Forth-2012's local variable support includes support for {: ... :}. A "|" within this separates parameters (which are loaded from passed parameters) from local variables. If I understand the spec correctly, it looks like you could do this:
: MY-NAME {: var1 var2 var3 :} var1 var2 + var3 * ;
Which is exactly what was desired.
- kragen 7y agoRight, but even without the Forth-2012 LOCALS word set, you can and often should say something like 0 value var1 0 value var2 0 value var3 : my-name to var3 to var2 to var1 var1 var2 + var3 * ; Maybe in this case you would just say : my-name >r + r> * ; or : my-name -rot + * ; but what I mean is that when you find that it's not obvious what's on the stacks, you can always just stick things in variables until it becomes obvious, just like you'd do in C or Python. A dangerous temptation with Forth is to use the stacks whenever you'd use local variables in other programming languages. A little bit of that can be good, but it only takes four or five variables before it becomes completely unreadable. It never becomes impossible, because with >r and r> you already have a Turing machine, and with dup and swap you can arbitrarily splice its tape, but it very quickly becomes a Turing tarpit. Instead, if you don't have {: :}, and maybe even if you do, your default replacement for local variables in other languages should be traditional Forth variables, even though they're statically allocated. The reasons to studiously avoid global variables in other programming languages are a lot weaker or inapplicable for traditional Forth variables.