IDL Organization

In order to properly write code to be linked with IDL, it is necessary to understand a little about its internal operation. This section is intended to give just enough background to understand the material that follows.

Traditional interpreted languages work according to the following algorithm:

while (statements remaining) {

Get next statement.

Perform lexical analysis and parse statement.

Execute statement.

}

This description is accurate at a conceptual level, and most early interpreters did their work in exactly this way. However, this scheme is inefficient because:

To avoid these problems, IDL uses a two-step process in which compilation and interpretation are separate.

The core of the system is the interpreter. The interpreter implements a simple, stack-based postfix language in which each instruction corresponds to a primitive of the IDL language. This internal form is a compact binary version of the IDL language routine. Routines written in the IDL language are compiled into this internal form by the IDL compiler when the .RUN executive command is issued, or when any other command requires a new routine to be executed. Once the IDL routine is compiled, the original version is ignored, and all references to the routine are to the compiled version.

Some of the advantages of this organization are:

The Interpreter Stack

The primary data structure in the interpreter is the stack. The stack contains pointers to variables, which are implemented by IDL_VARIABLE structures (see The IDL_VARIABLE Structure). Pointers to IDL_VARIABLEs are referred to as IDL_VPTRs. Most interpreter instructions work by removing a predefined number of elements from the stack, performing their function, and then pushing the IDL_VPTR to the resulting IDL_VARIABLE back onto the stack.

The removed items are the arguments to the instruction, and the new element represents the result. In this sense, the IDL interpreter is no different from any other postfix language interpreter. When an IDL routine is compiled, the compiler checks the number of arguments passed to each system routine against the minimum and maximum number specified in an internal table of routines, and signals an error if an invalid number of arguments is specified.

At execution time, the interpreter instructions that execute system procedures and functions operate as follows:

  1. Look up the requested routine in the internal table of routines.
  2. Execute the routine that implements the desired routine.
  3. Remove the arguments from the stack.
  4. If the routine was a function, push its result onto the stack.

Thus, the compiler checks for the proper number of arguments, and the interpreter does all the work related to pushing and popping elements from the stack. The called function need only worry about executing its operation and providing a result.