Next: , Previous: Anonymous Definitions, Up: Defining Words


5.9.7 Quotations

A quotation is an anonymous colon definition inside another colon definition. Quotations are useful when dealing with words that consume an execution token, like catch or outfile-execute. E.g. consider the following example of using outfile-execute (see Redirection):

     : some-warning ( n -- )
         cr ." warning# " . ;
     
     : print-some-warning ( n -- )
         ['] some-warning stderr outfile-execute ;

Here we defined some-warning as a helper word whose xt we could pass to outfile-execute. Instead, we can use a quotation to define such a word anonymously inside print-some-warning:

     : print-some-warning ( n -- )
       [: cr ." warning# " . ;] stderr outfile-execute ;

The quotation is bouded by [: and ;]. It produces an execution token at run-time.

[:       compile-time: – quotation-sys flag colon-sys         gforth       “bracket-colon”

Starts a quotation

;]       compile-time: quotation-sys – ; run-time: – xt         gforth       “semi-bracket”

ends a quotation