FORTH - 2swap
prof_braino
Posts: 4,313
If you don't care about FORTH, no need to keep reading or responding.
A question came up about a less commonly used FORTH word - 2swap
SWAP - swtches the top two values on the stack
: swap \ ( n1 n2 - n2 n1 )
2swap - switches the top PAIR of values on the stack
: 2swap \ ( n1 n2 n3 n4 - n3 n4 n1 n2 )
This is not included in the default propforth dictionary. A user found a reference to this word in a thrid party example.
To my knowledge, this is the first time somebody noticed its absence when writing code in propforth.
Should this word be added to the kernel, since it is defined in the ANS forth standard DPANS94?
Should this word be left to the user to add as an extension, since it is seldom used and would consume limited dictionary space?
What do you think? Other reasons for or against?
A question came up about a less commonly used FORTH word - 2swap
SWAP - swtches the top two values on the stack
: swap \ ( n1 n2 - n2 n1 )
2swap - switches the top PAIR of values on the stack
: 2swap \ ( n1 n2 n3 n4 - n3 n4 n1 n2 )
This is not included in the default propforth dictionary. A user found a reference to this word in a thrid party example.
To my knowledge, this is the first time somebody noticed its absence when writing code in propforth.
Should this word be added to the kernel, since it is defined in the ANS forth standard DPANS94?
Should this word be left to the user to add as an extension, since it is seldom used and would consume limited dictionary space?
What do you think? Other reasons for or against?
Comments
BUT as non-implemented ANSI words are recognized, I think there needs to be an addendum document that gives you a PropForth ready example to implement the word if you want it. Some folks may run across the use of an ANSI word and not know how to best implement it to get over their current roadblock. If there was a file of ready to go words to add in, it would make stumbling a little less painful.
I like eForth for a quick reference of anything that needs to be quickly added. The ANSI Forth spec does NOT provide the info to make words from other words and factoring for yourself is really a bit mental for a language that has this maturity.
I believe that Dave factored his ANS Forth core words independently for the init.fth before discovering eForth was available.
Noted...
This is a really good idea. After the "build the bot" sessions at the start of class next fall, I will attempt to create a "programming" class. I think I will have the kids impleiment non-kernel ANS words in propforth, using init.fth and eforth examples. Then the kids can get used to adding to the extensions and documentation.
Muhuhahah! My evil plan is working!
It may be a good time to get started. I think we have finished with the debate of standard versus non-standard. The Propeller really requires BOTH for users to really be able to do their own thing in Forth.
Of course, some of the legacy and PC supported words will never take hold.... it is an all new world for Forth on the Propeller.
This might become the plan to finally address the ANS support for standard intro materials.
In Forth, programs seem to evolve very quickly. I love the rapid deployment.
It is an excellent example of how to use CASE but PropForth has stolen ( and ) for other purposes... that comment must be removed before Prof Brano will accept this.
There a non DPANS word THENS that might be useful. It resolves all pending IF statements, and makes the code more readable.
Hey! That's cool!
Can I post this? Does it work in propforth? I want to use this in the programming portion of the LittleRobot workshop.
Be my guest. I've only tried in in gforth, but I'm willing to try it in propforth over the weekend. It's pretty basic as I avoided most of the areas where I know propforth differs from gforth.
BTW Here's a line for line translation of the program from FORTH into C++ using MS Visual Studio IDE.
But that's not how I would write the program in C++. I would use arrays and iteration as follows:
I checked the resultant code size and this indeed results in a more compact program. The result might be less clear to a novice programmer. What I may do it translate the second program into FORTH to get a better handle on strings arrays. This will likely bring this post back on topic as I suspect I'll need 2swap at some point.
I don't know, but I don't think so. As I recall, THENS just closes out any outstanding IF's. If I rermember correctly, there can even be some nested if's that were explicitley resolved by specific then. But I'll have to dig through the code and find the example. Only used it once. Sal uses it often, so he can leave out the case words.
There might be a more concise way to construct the arrays and statically initialize them. But reading this program you can see how much detail the C compiler is hiding from you in two lines of code.
http://en.wikipedia.org/wiki/Smile
or perhaps "Whitespace":
http://en.wikipedia.org/wiki/Whitespace_(programming_language)
Wouldn't it be easier to write programs in raw hex or binary like we had to in the old days before we got hold of assemblers?
Here's my version of the roman numeral program. I haven't tried it yet, so I don't know if it has any errors in it.
But that's was nothing compared to trying to get Forth to even do simple arithmetic in a readable manner, that is epilepsy time and total nervous collapse.
After therapy and counceling I'm OK, as long as keep taking the meds. My shrink warns me not to go near Forth again. I start shaking just reading this thread.
Basically I have been reduced to a JavaScript monkey....
@Dave, I like your approach. But you use the s" operator to allocate a string in a function. Does that allocate it each execution, or at compile time? I'm asking because I've wondered about memory leaks of unreferenced allocations.
I found an error in my code where I used >=0. I replaced this with "0 >=" and it works correctly.
!@heater: (don't want him to have a relapse)
Off topic, but there's these two examples to show how its done the easy way. At least it looks easy to me, but I don't really understand it yet. Apparently the Hartley transform derivative of the fourrier is the way to go on the prop.
http://propforth.googlecode.com/files/FHT11.rtf
http://propforth.googlecode.com/files/FHT_Examples.f
Basically FORTH feels a bit like an assembly language that prevents access to the JMP instruction, so labels can only be used for subroutine names and variable storage. But makes up for it with one heck of a macro facility (its flow of control structures), so you don't mind.