DCL Computed GOTO

Multiple choice transfers of control are one of the most time-honored facilities in programming languages, be they conventional or scripting, interpreted or compiled, assembler or very high level. It is often mistakenly believed that indirection is needed for implementation, but this is not the case. Perfectly serviceable multi-way branches can be implemented without indirection. All that is necessary is some method of re-computing the location of the next instruction (or line in a scripting language) to be executed.

The syntax of multi-way transfers of control depend on the individual syntax of the underlying programming language, and may take several forms, including:

Beyond the most basic ability to compute the next statement to be executed, the most important requirement in implementing a multi-way branch is ensuring that the branch value is within the appropriate range. Indeed, the VAX architecture contains the CASE machine instruction to implement bounds checking and transfer of control via an inline table of offsets.

At its most basic, a multi-way transfer of control depends on an integer index. This is the most basic form, and it is shared by most assembler and third generation languages. C/C++/JAVA enums are integers, for most intents and purposes. DCL, PERL, EnScript® and other interpreted scripting languages often extend this concept to strings. This extension transforms the range check from an integer bounds check to a check based on the presence of an index string value within an enumeration of potential values. This check can be most simply implemented by using a string index function. In DCL the string index operation is provided by the F$LOCATE lexical function.

However, some caution is needed when using the F$LOCATE function, or for that matter, any index function to locate a substring within the context of a larger string. Consider an index string that can contain the values "THEE", and "EEM". Admittedly, this selection of values has more than a small potential for confusion, but it is a useful example.

Simply checking that the index string is contained within a list of possible strings is insufficient (e.g., the string "THE" is not a valid index, yet is within the valid index string "THEE"). The solution is to ensure that the string containing the valid values starts and ends with the delimiter character. This technique was discussed in a previous column entitled: Pitfalls of F$LOCATE and other functions.

Using the extra delimiters, the validation code is simplicity itself, to wit (INDEX_VALUE contains the index string):

$       VALID_INDICES = "/ONE/TWO/THREE/FOUR/"
$       IF F$LOCATE("/"+indexvalue+"/", VALID_INDICES) -
           .EQ. F$LENGTH(VALID_INDICES) THEN {error processing}

Once the string index has been validated, the actual transfer of control consists of combining a simple GOTO statement with symbolic substitution, to wit:

$       GOTO TARGET_'INDEXVALUE'
$ TARGET_ONE:
$       ...
$       GOTO EXITTARGET
$ TARGET_TWO:
$       ...
$       GOTO EXITTARGET
$ TARGET_THREE:
$       ...
$       GOTO EXITTARGET
$ TARGET_THREE:
$       ...
$       GOTO EXITTARGET
$ TARGET_FOUR:
$       ...
$ EXITTARGET:
$ ...

This approach is far safer than attempting to nest IF-THEN-ELSE conditionals. The option is processed directly. If an invalid index is presented, it is detected immediately and dealt with. Adding a new possibility is simply a matter of adding the new index possibility to VALID_STRING and adding the appropriate processing code.

A segment of DCL implementing and demonstrating the above technique can be found on my www site under "DCL GOTO" under the Demonstrations heading.

URLs for referencing this entry

Picture of Robert Gezelter, CDP
RSS Feed Icon RSS Feed Icon
Add to Technorati Favorites
Follow us on Twitter
Bringing Details into Focus, Focused Innovation, Focused Solutions
Robert Gezelter Software Consultant Logo
http://www.rlgsc.com
+1 (718) 463 1079