View Full Version : Character handling
03-16-12, 01:36 PM
looking through the list of forth words, I am not sure I can accomplish what I am setting out to do. I am designing an autonavigating wheelchair. Words have been developed for possible paths within the building. For example, ATSDH is used for going between assistive technology and the small dining hall. I would like the wheelchair to remember that it is presently at assistive technology so the user will only have to enter SDH to move to the small dining hall. How do I have the system remember its present location in a character format, wait for two or three characters to be entered, and then join these characters together into a word to be executed? (I realize I will have to deal with two or three characters being entered but I can always use a leading or trailing )
There are several ways to do what you're trying to accomplish. Probably the most elegant would be to use wordlists (vocabularies) for each location, but that would require typing two words (one for location, one for destination). Also, I get the impression that you want the command to execute as soon as the three characters are entered, not requiring an "Enter" key.
Since RAM allocation is different on different Forth systems, I'll assume you've defined a word RAMBUFFER to declare a RAM buffer of arbitrary size. (This might be as simple as : RAMBUFFER VARIABLE 2 - ALLOT ; ) Also, the word FIND has changed over the years; I'll assume the Isomax definition ( addr1 -- addr2 n ) that expects a counted string and returns execution address and n<>0 if the word is found, string address and n=0 if the word is not found).
3 RAMBUFFER PREFIX ( will hold the "location" counted string )
6 RAMBUFFER WORDNAME ( will hold the assembled word name )
( commit to "memory" the string which follows, " delimited )
: COMMIT" ( a -- ) 22 WORD
DUP C@ 1+ PREFIX SWAP CMOVE ;
( recall the memorized string to the WORDNAME buffer )
: RECALL ( -- ) PREFIX DUP C@ 1+ WORDNAME SWAP CMOVE ;
( append a character to WORDNAME )
: APPEND ( c -- ) WORDNAME COUNT + C! ( store char )
WORDNAME C@ 1+ WORDNAME C! ; ( increment length )
( accept three keys and do that word )
: COMMAND ( -- )
RECALL ( first get the prefix )
3 0 DO KEY APPEND LOOP ( append three keys )
WORDNAME FIND ( look for the combined wordname )
EXECUTE ( found - perform the word )
COUNT TYPE ( not found - display the wordname )
." ?" ( followed by a question mark)
Finally, inside the word ATSDH (and all words like it), you must remember the new position in the PREFIX buffer. That's what the word COMMIT" is for:
( ...whatever is needed to move to the new position... )
COMMIT" AT" ( remember the AT prefix )
This is just off the top of my head; I haven't tested any of it. It should be fairly easy to debug. Handling the problem of two or three keys is left as an exercise for the student. Once you know how to use FIND, you can even write it to accept a variable number of keys until a Forth word is recognized. (In which case you'll need a way to clear the WORDNAME and start over.)
Whoops...an error in my ATSDH. I forgot this is going from AT to SDH. So when you get to the small dining hall, you'll want to COMMIT" SD" instead of COMMIT" AT". You get the idea. You can use a longer prefix, but you'll need to increase the size of the RAMBUFFERs (each must hold N characters plus one count byte).
vBulletin v3.0.7, Copyright ©2000-2013, Jelsoft Enterprises Ltd.