next up previous contents index
Next: Static and Global Variables Up: The Xic Scripting Language Previous: User-Defined Functions   Contents   Index


The exec Keyword -- Immediate Execution

Script execution is a two-step process: first, the text of the script is parsed, and executable data structures are created internally, and second, the execution is performed. Consider the following script:

Set("ScriptPath", "/path/to/library_dir")
some_library_function()

Naively, the first line will set the script path to the directory containing the library (see 18.2) file, and the second line will execute a function from the library. However, this will not run, since the library function must be resolved before the parser can process the function call. Somehow, we must ensure that the Set line is executed before the following line is parsed.

The exec keyword will perform this trick. When an exec keyword is encountered, the remainder of the line (or to the next semicolon) is parsed and executed immediately, and is not added to the parse tree for scheduled execution with the other lines. Thus, the example above should be

exec Set("ScriptPath", "/path/to/library_dir")
some_library_function()

Multiple exec lines are executed in order of appearance. Variables can be used and set, but remember that this will be done before any manipulation from the normal script lines. For example, the ScriptPath switch can be hidden:

exec tmppath = GetPath("ScriptPath")
exec Set("ScriptPath", "/path/to/library_dir")
some_library_function()
Set("ScriptPath", tmppath)

The tmppath variable will be set first, and is used to reset the ScriptPath as a final operation.


next up previous contents index
Next: Static and Global Variables Up: The Xic Scripting Language Previous: User-Defined Functions   Contents   Index
Stephen R. Whiteley 2022-05-28