6.15.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.26

postpone ( "name" –  ) core “postpone”

Compiles the compilation semantics of name.

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

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

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

You will occassionally find the need to POSTPONE several words; putting POSTPONE before each such word is cumbersome, so Gforth provides a more convenient syntax: ]] ... [[. This allows us to write [compile-+] as:

: [compile-+] ( compilation: --; interpretation: -- )
  ]] + [[ ; immediate
]] ( ) gforth-0.6 “right-bracket-bracket”

Switch into postpone state: All words and recognizers are processed as if they were preceded by postpone. Postpone state ends when [[ is recognized.

The unusual direction of the brackets indicates their function: ]] switches from compilation to postponing (i.e., compilation of compilation), just like ] switches from immediate execution (interpretation) to compilation. Conversely, [[ switches from postponing to compilation, ananlogous to [ which switches from compilation to immediate execution.

The real advantage of ]] ... [[ becomes apparent when there are many words to POSTPONE. E.g., the word compile-map-array (see Advanced macros) can be written much shorter as follows:

: compile-map-array ( compilation: xt -- ; run-time: ... addr u -- ... )
\ at run-time, execute xt ( ... x -- ... ) for each element of the
\ array beginning at addr and containing u elements
  {: xt: xt :}
  ]] cells over + swap ?do
    i @ xt 1 cells +loop [[ ;

: sum-array ( addr u -- n )
  0 [ ' + compile-map-array ] ;

If you then say see sum-array, it shows the following code:

: sum-array
  #0 over + swap ?do
    i  + #8 +LOOP
;

In addition to ]]...[[, this example shows off some other features:

Note that parsing words such as s\" don’t parse at postpone time and therefore not inside ]]...[[. Instead of s\" mystring\n" you can use the string recognizer and write "mystring\n", which works inside ]]...[[. Likewise for the parsing word ['] and the recognizer notation starting with `.

But if you prefer to use s\" (or have a parsing word that has no recognizer replacement), you can do it by switching back to compilation:

]] ... [[ s\" mystring\n" ]] 2literal ... [[

Definitions of ]] and friends in Standard Forth are provided in compat/macros.fs.

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 ] ;
compile, ( xt –  ) core-ext “compile-comma”

Append the semantics represented by xt to the current definition. When the resulting code fragment is run, it behaves the same as if xt is executed.

2compile, ( xt1 xt2 –  ) gforth-experimental “two-compile-comma”

equivalent to xt1 compile, xt2 compile,, but also applies peephole optimization.

An alternative available in Gforth, that allows you to pass the compilation semantics as parameters is to use the compilation token (see 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 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 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.

[compile] ( compilation "name" – ; run-time ? – ?  ) core-ext “bracket-compile”

Legacy word. Use postpone instead. Works like postpone if name has non-default compilation semantics. If name has default compilation semantics (i.e., is a normal word), compiling [compile] name is equivalent to compiling name (i.e. [compile] is redundant in this case.


Footnotes

(26)

A recent RFI answer requires that compiling words should only be executed in compile state, so this example is not guaranteed to work on all standard systems, but on any decent system it will work.