Evaluating strings with a different vocabulary search path
Factor handbook » The implementation » The parser » Evaluating strings at run time

Prev:eval ( str effect -- )
Next:eval>string ( str -- output )

Strings passed to eval are always evaluated with an initial vocabulary search path consisting of just the syntax vocabulary. This is the same search path that source files start out with. This behavior can be customized by taking advantage of the fact that eval is composed from two simpler words:
(eval) ( str effect -- )

with-file-vocabs ( quot -- )

Code in the listener tool starts out with a different initial search path, with more vocabularies available by default. Strings of code can be evaluated in this search path by using (eval) with a different combinator:
with-interactive-vocabs ( quot -- )

When using (eval), the quotation passed to with-file-vocabs and with-interactive-vocabs can also make specific vocabularies available to the evaluated string. This is done by having the quotation change the run-time vocabulary search path prior to calling (eval). For run-time analogues of the parse-time Syntax to control word lookup see Reflection support for vocabulary search path.

The vocabulary set used by with-interactive-vocabs can be altered by rebinding a dynamic variable:

In this example, a string is evaluated with a fictional cad.objects vocabulary in the search path by default, together with the listener's interactive-vocabs; the quotation is expected to produce a sequence on the stack:
USING: eval listener vocabs.parser ; [ "cad.objects" use-vocab ( -- seq ) (eval) ] with-interactive-vocabs

Note that the search path in the outer code (set by the USING: form) has no relation to the search path used when parsing the string parameter (this is determined by with-interactive-vocabs and use-vocab).