Reva Tips and Tricks
From RevaWiki
- Using singles and doubles
- If you use a word which expects a 'double' on the stack, you need to ensure you actually have a double on the stack! For example, 1 d. will not print 1! Instead, you must do one of the following:
1 s>d d.
or:
1L d.
- Enter dates as if they were numbers
- This can be done by assigning your own handler to word?. See "numberparse.f" in the examples directory for how it's done.
- Recursion in Reva
- To have a word call itself, simply use the name of the word inside the body. Unlike ANS Forths, Reva does not "smudge" the dictionary while defining a word. This allows you to easily recurse, as the following silly example shows:
: my-word ." Going to recurse ..." my-word ;
- "Subclassing" a word
- As the previous tips showed, Reva does not call the previous definition of a word if you invoke its name while it is being redefined. If, however, you do want to do that, simply use the word prior:
: my-word ." Going to call previous version ..." prior my-word ;
If you want to call the previous value of a deferred word, you need to use chain:
:: dup . chain emit ; is emit
- Cleanup on application shut-down
- There are two ways to do this:
- Assign your own word to bye (which is deferred).
- Use the onexit word. Here is an example of the second technique:
3 variable, countdown
noname: countdown @ dup 0; 1- countdown ! ." Not yet..." cr r> drop ;
onexit
bye
Not yet!
bye
Not yet!
bye
Not yet!
bye
- After the last bye you will leave Reva. Note the r> drop trick, which makes you leave the calling word! This is a way to keep from calling (bye) which is the os-specific implementation of bye.
- How to get double-quotes in a string
- Reva borrows a trick from the "C" family of languages - you "escape" a character by preceding it with a backslash. So:
" \"Mary!\", her mother said, \"Come in for dinner!\""
Results in: "Mary!", her mother said, "Come in for dinner!".
