A little more on compiling

While compiling, it is possible to temporarily switch to interpreter mode with the word [ and switch back into compile mode with ]. The following example defines the word feet that converts a number representing a length in feet to an equivalent number of millimetres. The intermediate result is in tenths of a millimetre so that precision is retained and, to make the conversion clear, the numeric conversion factor is computed as we compile the word.
: feet ( u1 -- u2) 
  [ #254 #12 * ] literal #10 u*/mod 
  swap drop ;  ok<#,ram>
10 feet  ok<#,ram>3048
The word literal is used to compile the data value in TOS into the definition of feet. At run-time, that data value will be placed onto the stack.




Peter Jacobs 2013-06-12