View Full Version : BLINKGRN very cryptic for me
This code
: -LOOPVAR <BUILDS HERE P, 1- DUP , , DOES>
P@ DUP @ 0= IF DUP 1 + @ SWAP ! TRUE ELSE 1-! FALSE THEN ;
30 -LOOPVAR CNT
is totally cryptic to me and not explained anywhere :confused: , but it works :D . I understand the state machine, but what is CNT? Should be a variable and not a condition, what is called when and by whom? :confused:
Is this : -LOOPVAR stuff called everytime the condition is checked?
Perhaps this is explained somewhere in the docs, but up to now I didn't find it.
BTW where are the MaxForth manuals for ServoPod? Are they the same as for HC11/12.
Thx
RMDumse
01-21-06, 04:29 PM
The manuals for IsoPod(TM) were written back with version 0.3 of the language. At that time, we didn't have a LOOPINDEX kind of word yet.
So -LOOPVAR was my attempt to make one, and show what I wanted to our programmer. So I used some of the deep tricks in Forth to program the word. You shouldn't let it worry you too much.
The word -LOOPVAR is in a class called a "defining word". So when we want a word type in the language the language doesn't offer, we can still expand the language by defining a defining word.
It also looks like the BBS stole the BUILDS and the DOES out of the definition you copied, and tried to turn them into some sort of display format tag or command. Only the DOES portion shows in your post.
But that's enough to explain. When a word defined by a defining word runs, it leaves a pointer to the parameter field of that newly defined word. The BUILDS part of the definition leaves a pointer to a place for a RAM variable. When the DOES portion of the code runs, it picks up this pointer and gets what's located there in the program space with a P@. The pointer is duplicated DUP and that address (a place in RAM) is fetched @. If the value is non zero, it is decremented, and a FALSE flag is left (meaning that variable isn't zero yet). If it was zero, the value from the original definitions is reloaded into the cnt variable and a TRUE flag is left (meaning that variable is/was zero).
So the BUILDS portion of LOOPVAR creates a special variable. In this case the word CNT. When CNT is executed, it links to the DOES portion of the word that defined it to tell it what to do, that being, decrement the variable if it hasn't reached 0, and leave a flag telling if it was non-zero, or not.
That said, I have to admit I'm working from memory, and haven't found the place this was used in the manual to see the real source code before this BBS software changed it in your post above. So my description might not be perfect.
In any case, don't worry about it. As you say it works, and now that we've had some time to improve the language we've settled on another way to do looping variables.
Yes, the MaxForth(TM) under IsoMax(TM) is the (very nearly) the same as we implemented on the HC11 and HC12.
RMDumse
01-21-06, 04:32 PM
Ah... I found original source. I've put a space between < and Builds, and Does and > so it will show up in this BBS
: -LOOPVAR < BUILDS HERE P, 1- DUP , , DOES >
P@ DUP @ 0= IF DUP 1 + @ SWAP ! TRUE ELSE 1-! FALSE THEN ;
Thx very much.
The "old" documentation is sometimes somewhat confusing, especially for a forth beginner. But as I said, we progress a little every day... and we get a feeling for the immense flexibility of the language. But little mistakes are very easily made, so " DOUBLE @ instead of DOUBLE D@, all those things are even more dangerous as pointer arithmetic in C, but as always, with freedom comes responsibility.
I'm still trying to establish an efficient workflow, up to now it is editing in an external editor, copying into NMITerm and going on from there.
Pacetech
04-04-06, 09:11 PM
... and now that we've had some time to improve the language we've settled on another way to do looping variables.
Just curious on what the improved way would be?
RMDumse
04-08-06, 04:19 PM
We settled on a construct we called another name because it worked slightly differently, called LOOP-INDEX summarized here:
14.7.6. Summary of Loopindex Operations
LOOPINDEX name
Defines a "loop index" variable with the given name. For example, LOOPINDEX COUNTER1
START
END
STEP
These words set the start value, the end value, or the step value (increment) for the given loop index. All of these expect an integer argument and the name of a loopindex variable. Examples: 1 COUNTER1 START 100 COUNTER1 END 3 COUNTER1 STEP These can be specified in any order. If any of them is not specified, the default values will be used (START=0, END=1, STEP=1).
COUNT
This causes the given loop index to increment by the STEP value, and returns a true or false value: true (-1) if the end of count was reached, false (0) otherwise. For example: COUNTER1 COUNTEnd of count is determined after the loop index is incremented, as follows: If STEP is positive, "end of count" is when the index is greater than the END value. If STEP is negative, "end of count" is when the index is less than the END value. Signed integer comparisons are used. In either case, when the end of count is reached, the loop index is reset to its START value.
RESET
This word manually resets the given loop index to its START value. Example: COUNTER1 RESET
VALUE
This returns the current index value (counter value) of the given loop index. It will return a signed integer in the range -32768..+32767. For example: COUNTER1 VALUE . ...prints the loop index COUNTER1
SET
This sets the given loop index to the given value. Example: 5 COUNTER1 SET
vBulletin v3.0.7, Copyright ©2000-2012, Jelsoft Enterprises Ltd.