21.3.4 CREATE..DOES>. 4tH 4tH Forth compiler
광고
광고
CHAPTER 21. PORTING GUIDE 385
That is why 4tH uses "1" as a true flag. Usually, it won’t make much difference. Except when you use ’INVERT’ to invert a flag or intend to make obfuscated programs. If you use
’0=’ instead, you won’t run in any trouble, not even when you port your program to ANS-
Forth. Clarity may introduce a little overhead, but in this age of multi-gigaherz machines, who is counting? E.g. you could program “>CHAR” like this:
\ convert a flag to a bit mask
: >MASK 0 SWAP IF INVERT THEN ; ( f -- mask)
\ convert a digit to ASCII
: >CHAR DUP 9 > >MASK 7 AND + ASCII 0 + ; ( n -- c)
If you still want to change the true flag, you can by simply changing a #define in cmds_4th.h
:
#define F_T ~(0L)
But we doubt whether it will be a great benefit to your programming style.
21.3.4
CREATE..DOES>
In both 4tH and ANS-Forth it is possible to change the runtime behavior of variables. E.g.
in ANS-Forth, ’CONSTANT’ is usually defined as:
: CONSTANT CREATE , DOES> @ ;
10 CONSTANT MY_CONST
MY_CONST . CR
Of course there is a predefined word in 4tH that does this, but if you wanted to mimic this behavior you would have to define it like this:
CREATE MY_CONST 10 , DOES> @C ;
MY_CONST . CR
\ Definition of CONSTANT
\ Works the same way
The point is that the ANS-Forth "CREATE DOES>" construct cannot be ported to 4tH, although all words seem to be supported. A rule of the thumb is that defining words cannot be used to define new defining words , like in ANS-Forth. Most errors will be trapped by
4tH’s compiler, though
2
.
Just remember that a ’:REDO’ definition can easily be ported to ANS-Forth. If you want to write a portable program, ’:REDO’ is the way to do it.
21.3.5
HERE
Be careful with ’HERE’. ’HERE’ looks and acts a lot like the ANS-Forth ’HERE’, but since the architecture is different it serves quite another function. When ’HERE’ is used for address arithmetic with definitions or arrays of constants, it works right out of the box.
If not, it usually doesn’t.
2
Note that the use of ’DOES>’ in 4tH is called ”interpretation” in ANS-Forth, which is explicitly forbidden.
Some Forth compilers (like gForth) have defined the interpretation semantics of ’DOES>’ and are consequently largely compatible with 4tH’s ’DOES>’.
다운로드
광고