Vocabularies cookbook
Factor handbook » Factor cookbook

Prev:Dynamic variables cookbook
Next:Application cookbook

Rather than being in one flat list, words belong to vocabularies; every word is contained in exactly one. When parsing a word name, the parser searches through vocabularies. When working at the listener, a useful set of vocabularies is already available. In a source file, all used vocabularies must be imported.

For example, a source file containing the following code will print a parse error if you try loading it:
"Hello world" print

The print word is contained inside the io vocabulary, which is available in the listener but must be explicitly added to the search path in source files:
USE: io "Hello world" print

Typically a source file will refer to words in multiple vocabularies, and they can all be added to the search path in one go:
USING: arrays kernel math ;

New words go into the scratchpad vocabulary by default. You can change this with IN::
IN: time-machine : time-travel ( when what -- ) frob fizz flap ;

Note that words must be defined before being referenced. The following is generally invalid:
: frob ( what -- ) accelerate particles ; : accelerate ( -- ) accelerator on ; : particles ( what -- ) [ (particles) ] each ;

You would have to place the first definition after the two others for the parser to accept the file. If you have a set of mutually recursive words, you can use DEFER:.

Parse-time word lookup
The parser