www.delorie.com/gnu/docs/gforth/gforth_118.html   search  
 
Buy GNU books!


Gforth Manual

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.12.2 Macros

Literal and friends compile data values into the current definition. You can also write words that compile other words into the current definition. E.g.,

 
: compile-+ ( -- ) \ compiled code: ( n1 n2 -- n )
  POSTPONE + ;

: foo ( n1 n2 -- n )
  [ compile-+ ] ;
1 2 foo .

This is equivalent to : foo + ; (see foo to check this). What happens in this example? Postpone compiles the compilation semantics of + into compile-+; later the text interpreter executes compile-+ and thus the compilation semantics of +, which compile (the execution semantics of) + into foo.(21)

doc-postpone doc-[compile]

Compiling words like compile-+ are usually immediate (or similar) so you do not have to switch to interpret state to execute them; mopifying the last example accordingly produces:

 
: [compile-+] ( compilation: --; interpretation: -- )
  \ compiled code: ( n1 n2 -- n )
  POSTPONE + ; immediate

: foo ( n1 n2 -- n )
  [compile-+] ;
1 2 foo .

Immediate compiling words are similar to macros in other languages (in particular, Lisp). The important differences to macros in, e.g., C are:

You may want the macro to compile a number into a word. The word to do it is literal, but you have to postpone it, so its compilation semantics take effect when the macro is executed, not when it is compiled:

 
: [compile-5] ( -- ) \ compiled code: ( -- n )
  5 POSTPONE literal ; immediate

: foo [compile-5] ;
foo .

You may want to pass parameters to a macro, that the macro should compile into the current definition. If the parameter is a number, then you can use postpone literal (similar for other values).

If you want to pass a word that is to be compiled, the usual way is to pass an execution token and compile, it:

 
: twice1 ( xt -- ) \ compiled code: ... -- ...
  dup compile, compile, ;

: 2+ ( n1 -- n2 )
  [ ' 1+ twice1 ] ;

doc-compile,

An alternative available in Gforth, that allows you to pass compile-only words as parameters is to use the compilation token (see section 5.11.2 Compilation token). The same example in this technique:

 
: twice ( ... ct -- ... ) \ compiled code: ... -- ...
  2dup 2>r execute 2r> execute ;

: 2+ ( n1 -- n2 )
  [ comp' 1+ twice ] ;

In the example above 2>r and 2r> ensure that twice works even if the executed compilation semantics has an effect on the data stack.

You can also define complete definitions with these words; this provides an alternative to using does> (see section 5.9.8 User-defined Defining Words). E.g., instead of

 
: curry+ ( n1 "name" -- )
    CREATE ,
DOES> ( n2 -- n1+n2 )
    @ + ;

you could define

 
: curry+ ( n1 "name" -- )
  \ name execution: ( n2 -- n1+n2 )
  >r : r> POSTPONE literal POSTPONE + POSTPONE ; ;

-3 curry+ 3-
see 3-

The sequence >r : r> is necessary, because : puts a colon-sys on the data stack that makes everything below it unaccessible.

This way of writing defining words is sometimes more, sometimes less convenient than using does> (see section 5.9.8.3 Advanced does> usage example). One advantage of this method is that it can be optimized better, because the compiler knows that the value compiled with literal is fixed, whereas the data associated with a created word can be changed.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003