REXX (REstructured eXtended eXecutor) is an interpreted programming language which was developed at IBM. It is a structured high-level programming language which was designed to be both easy to learn and easy to read. Both commercial and open source interpreters for REXX are available on a wide range of computing platforms, and compilers are available for IBM mainframes.
REXX has just twenty-three, largely self-evident, instructions (e.g., call, parse, and select) with minimal punctuation and formatting requirements. It is essentially an almost free-form language with only one data-type, the character string; this philosophy means that all data are visible (symbolic) and debugging and tracing are simplified.
REXX syntax looks similar to PL/I, but has fewer notations; this makes it harder to parse (by program) but easier to use.
It was first described in public at the SHARE 56 conference in Houston, Texas, in 1981, where customer reaction, championed by Ted Johnston of SLAC, led to it being shipped as an IBM product in 1982.
Over the years IBM included REXX in almost all of its operating systems (VM/CMS, VM/GCS, MVS TSO/E, AS/400, OS/2, VSE/ESA, AIX, CICS/ESA, PC-DOS, and OS/2 ), and has made versions available for Novell Netware, Windows, Java, and Linux.
The first non-IBM version was written for PC-DOS by Charles Daney in 1984/5. Other versions have also been developed for Atari, Amiga, Unix (many variants), Solaris, DEC, Windows, Windows CE, PocketPC, MS-DOS, Palm OS, QNX, OS/2, Linux, BeOS, EPOC32, AtheOS, OpenVMS, OpenEdition, Macintosh, and Mac OS X.
The Amiga version of Rexx, called ARexx was included with AmigaOS 2 onwards and was popular for scripting as well as application control. Many Amiga applications have "ARexx ports" built into them which allows control of the application via a user defined script.
Several freeware versions of Rexx are available. In 1992, the two most widely-used open-source ports appeared: Ian Collier's REXX/imc for Unix and Anders Christensen's Regina (later adopted by Mark Hessling) for Windows and Linux. BREXX is well-known for WinCE and PocketPC platforms.
In 1996 ANSI published a standard for REXX: ANSI X3.274–1996 “Information Technology – Programming Language REXX”. More than two dozen books on REXX have been published since 1985.
Since the mid-1990s, two newer variants of REXX have appeared:
In 1990, Cathy Dager of SLAC organized the first independent REXX symposium, which led to the forming of the REXX Language Association. Symposiums are held annually.
Rexx marked its 25th anniversary on 20 March 2004, which was celebrated at the REXX Language Association’s 15th International REXX Symposium in Böblingen, Germany, in May 2004.
On October 12, 2004, IBM announced their plan to release their Object Rexx implementation under the Common Public License.
On February 22, 2005, the first public release of ooRexx (Open Object Rexx) was announced.
In plain text, Cowlishaw seems to prefer Rexx, whereas IBM documents and the majority of the web uses REXX. The ANSI standard uses the form preferred by the standardization committee, which has small capitals for the final three letters: REXX. This style is also used on the cover pages of "The Rexx Language" TRL and "The NetRexx Language" written by Michael Cowlishaw.
Originally just "Rex" because the author liked how it sounded, the extra "x" was added to avoid collisions with other products' names.
The DO control structure always begins with a DO and ends with an END.
DO WHILE: do while is true * end
Stepping through a variable:
do i = x to y by z * end
Looping forever until exiting with LEAVE:
do forever if * then leave end
Looping a fixed number of times
do i = x to y by z for a * end
Looping a specified number of times
do x * end
Testing conditions with IF
if * then do * end else do * end
For single instructions, DO and END can also be omitted:
SELECT is REXX's CASE structure, like many other constructs derived from PL/I: select when * then * when * then do * end otherwise * or NOP end
NOP indicates no instruction is to be executed.
The PARSE instruction is particularly powerful; it combines some useful string-handling functions. Its syntax is:
parse * origin template
where origin specifies the source:
and template can be:
upper is optional; it you specify it, data will be converted to upper case.
Examples:
Using a list of variables as template
myVar = "John Smith" parse var MyVar firstName lastName say "First name is:" firstName say "Last name is:" lastName
displays the following
First name is: John Last name is: Smith
Using a delimiter as template:
myVar = "Smith, John" parse var MyVar LastName "," FirstName say "First name is:" firstName say "Last name is:" lastName
also displays the following
First name is: John Last name is: Smith
Using column number delimiters:
myVar = "(202) 123-1234" parse var MyVar 2 AreaCode 5 7 SubNumber say "Area code is:" AreaCode say "Subscriber number is:" SubNumber
displays the following
Area code is: 202 Subscriber number is: 123-1234
A template can use a combination of variables, literal delimiters, and column number delimiters.
do say hello /* => HELLO */ hello = 25 say hello /* => 25 */ hello = "say 5 + 3" say hello /* => say 5 + 3 */ interpret hello /* => 8 */ drop hello say hello /* => HELLO */ end
Unlike many other programming languages, classic REXX has no direct support for arrays of variables addressed by a numerical index. Instead it provides compound variables. A compound variable consists of a stem followed by a tail. A . (dot) is used to join the stem to the tail. If the tails used are numeric, it is easy to produce the same effect as an array.
do i = 1 to 10 stem.i = 10 - i end
Afterwards the following variables with the following values exist: stem.1 = 9, stem.2 = 8, stem.3 = 7...
Unlike arrays, the index for a stem variable is not required to have an integer value. For example, the following code is valid: i = 'Monday' stem.i = 2
In Rexx it is also possible to set a default value for a stem. stem. = 'Unknown' stem.1 = 'USA' stem.44 = 'UK' stem.33 = 'France'
After these assignments the term stem.3 would produce 'Unknown'.
The whole stem can also be erased with the DROP statement. drop stem. This also has the effect of removing any default value set previously.
By convention (and not as part of the language) the compound stem.0 is often used to keep track of how many items are in a stem, for example a procedure to add a word to a list might be coded like this: add_word: procedure expose dictionary. parse arg w n = dictionary.0 + 1 dictionary.n = w dictionary.0 = n return
It is also possible to have multiple elements in the tail of a compound variable. For example: m = 'July' d = 15 y = 2005 day.y.m.d = 'Friday'
Multiple numerical tail elements can be used to provide the effect of a multi-dimensional array.
Features similar to Rexx compound variables are found in many other languages (associative arrays in AWK, hashes in Perl, Vectors in Java, etc). Most of these languages provide an instruction to iterate over all the keys (or tails in Rexx terms) of such a construct, but this is lacking in classic Rexx. Instead it is necessary to keep auxiliary lists of tail values as appropriate. For example in a program to count words the following procedure might be used to record each occurrence of a word. add_word: procedure expose count. word_list parse arg w . count.w = count.w + 1 /* assume count. has been set to 0 */ if count.w = 1 then word_list = word_list w return
and then later do i = 1 to words(word_list) w = word(word_list,i) say w count.w end
At the cost of some opacity it is possible to combine these techniques into a single stem. add_word: procedure expose dictionary. parse arg w . dictionary.w = dictionary.w + 1 if dictionary.w = 1 /* assume dictionary. = 0 */ then do n = dictionary.0+1 dictionary.n = w dictionary.0 = n end return and later do i = 1 to dictionary.0 w = dictionary.i say i w dictionary.w end However Rexx provides no safety net here, so if one of your words happens to be a whole number less than dictionary.0 the above technique will fail mysteriously.
Recent implementations of Rexx, include IBM's Object Rexx and the open source implementations like Regina and OORexx include an new language construct to simplify iteration over the value of a stem. do i over stem. say i '-->' stem.i end
The INTERPRET instruction is very powerful and one of the two reasons why writing REXX compilers isn't trivial (the other reason is REXX's decimal arbitrary precision arithmetic):
/* a touch of LISP */ X = 'square' interpret 'say' X || '(4) ; exit' SQUARE: return arg(1) * arg(1)
This displays 16 and exits. Because anything in REXX are strings, even rational numbers with exponents, and last but not least complete programs, REXX offers to interpret strings as programs.
This feature was used to implement something like function parameters, e.g. passing SIN, COS, etc. to a procedure SIMPSON to calculate integrals.
Note that REXX offers only basic math. functions like ABS, DIGITS, MAX, MIN, SIGN, RANDOM, and a complete set of hex. plus binary conversions with bit operations, anything else like SIN has to be implemented from scratch or using external libraries. The latter typically don't support arbitrary precision.
Later versions (non-classic) support CALL variable constructs, together with the built-in function VALUE this allows to avoid many cases of INTERPRET. This is a classic program:
/* terminated by input "exit" or similar */ do forever ; interpret linein() ; end
A slightly more sophisticated REXX calculator:
X = 'input BYE to quit' do until X = 'BYE' ; interpret 'say' X ; pull X ; end
PULL is a shorthand for parse upper pull like ARG for parse upper arg.
say digits() fuzz() form() /* => 9 0 SCIENTIFIC */ say 999999999 + 1 /* => 1.000000000E+9 */ numeric digits 10 /* only limited by available memory */ say 999999999 + 1 /* => 1000000000 */
say 0.9999999999 = 1 /* => 0 (false) */ numeric fuzz 3 say 0.99999999 = 1 /* => 1 (true) */ say 0.99999999 == 1 /* => 0 (false) */
say 100 * 123456789 /* => 1.23456789E+10 */ numeric form engineering say 100 * 123456789 /* => 12.34567890E+9 */
It is possible in REXX to intercept and deal with errors and other exceptions, using the SIGNAL instruction. There are seven system conditions: ERROR, FAILURE, HALT, NOVALUE, NOTREADY, LOSTDIGITS and SYNTAX. Handling of each can be switched on and off in the source code as desired.
This example will run until stopped by the user: signal on halt; do a = 1 say a do 100000 /* a delay */ end end halt: say "The program was stopped by the user" exit
Virtually all serious REXX programs contain signal on novalue or a similar statement. This disables the "feature", where undefined variables get their own (upper case) name as value. The status of a variable can be checked with the built-in function SYMBOL returning VAR for defined variables.
Function VALUE can be used to get the value of variables without triggering a NOVALUE condition, but its main purpose is to read and set environment variables - similar to POSIX getenv and putenv.
| ERROR | Positive RC from a system command |
|---|---|
| FAILURE | Negative RC for a system command (e.g. command doesn't exist) |
| HALT | Abnormal termination (see above) |
| NOVALUE | An unset variable was referenced (see above) |
| NOTREADY | Input or output error (e.g. read attempts beyond end of file) |
| SYNTAX | Invalid program syntax, or some error condition not covered above |
| LOSTDIGITS | Significant digits are lost (ANSI REXX, not in TRL second edition) |
When a condition is handled by SIGNAL ON, the SIGL and RC system variables can be analyzed to understand the situation. RC contains the REXX error code and SIGL contains the line number where the error arose.
Beginning with REXX version 4 conditions can get names, and there's also a CALL ON construct. That's handy if external functions do not necessarily exist:
CHCP: procedure /* protect SIGNAL settings */ signal on syntax name CHCP.TRAP return SysQueryProcessCodePage() CHCP.TRAP: return 1004 /* windows-1252 on OS/2 */
IBM software | Programming languages | Scripting languages
REXX | REXX | REXX | Restructured Extended Executor | Rexx | REXX | REXX | REXX | REXX