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

Gforth Manual

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

5.13.3 Interpret/Compile states

A standard program is not permitted to change state explicitly. However, it can change state implicitly, using the words [ and ]. When [ is executed it switches state to interpret state, and therefore the text interpreter starts interpreting. When ] is executed it switches state to compile state and therefore the text interpreter starts compiling. The most common usage for these words is for switching into interpret state and back from within a colon definition; this technique can be used to compile a literal (for an example, see section 5.12.1 Literals) or for conditional compilation (for an example, see section 5.13.4 Interpreter Directives).

[ and ] also give you the ability to switch into compile state and back, but we cannot think of any useful Standard application for this ability. Pre-ANS Forth textbooks have examples like this:

: AA ." this is A" ;
: BB ." this is B" ;
: CC ." this is C" ;

create table ] aa bb cc [

: go ( n -- ) \ n is offset into table.. 0 for 1st entry
  cells table +  execute ;

This example builds a jump table; 0 go will display "this is A". Using [ and ] in this example is equivalent to defining table like this:

create table ' aa COMPILE, ' bb COMPILE, ' cc COMPILE,

The problem with this code is that the definition of table is not portable -- it compiles execution tokens into code space. Whilst it may work on systems where code space and data space co-incide, the Standard only allows data space to be assigned for a CREATEd word. In addition, the Standard only allows @ to access data space, whilst this example is using it to access code space. The only portable, Standard way to build this table is to build it in data space, like this:

create table ' aa , ' bb , ' cc ,


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

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