Jump to content

The REXX/imc Tutorial

From EDM2
Revision as of 21:21, 7 August 2012 by Martini (talk | contribs)

by Ian Collier

Introductory text

Note: some of the information in this file is system-specific, though most of it pertains to every implementation of Rexx. Lines containing implementation-specific information are flagged with letters in column 1. The letter "I" denotes information for REXX/imc. I have also started adding information about OS/2 Rexx, which will be flagged with "O" in column 1. I hope it is not too confusing to see some sections written twice, once for each system. The file is designed so that if you run it through "egrep '^(x| |$)'|cut -c3-" (where x is the desired letter) then it should still make sense and cut out the flag letters. Doing this will select one of the following lines indicating which system was selected; the characters to the left of this paragraph will make sure it gets deleted when that happens.

I This file describes REXX/imc. 
O This file describes OS/2 Classic Rexx 
O (which is also pretty much compatible with the OS/2 Object Rexx interpreter).
 
I More advanced information can be found in rexx.summary (bare details of each command 
I and builtin function, with a list of differences from standard Rexx) and rexx.ref (technical }
I details of every aspect of this Rexx implementation).
O More information is available in the OS/2 help system.  
O For example, typing "help rexx signal" will give the syntax of the Rexx "signal" instruction.


Creating a Rexx program

Many programmers start by writing a program which displays the message "Hello world!". Here is how to do that in Rexx...

I Write a file called "hello.rexx" containing the following text.  Use any
I text editor or simply `cat' the text into the file.
O Write a file called "hello.cmd" containing the following text.  Use any
O text editor (for example, E).

Note that the text, as with all example text in this guide, starts at the first indented line and ends at the last. The four spaces at the start of each line of text should not be entered.

     /* This program says "Hello world!" */
     say "Hello world!"
 

This program consists of a comment saying what the program does, and an instruction which does it. "say" is the Rexx instruction which displays data on the terminal.

The method of executing a Rexx program varies greatly between implementations.

I Here is how you execute that file using REXX/imc:
I
I     rexx hello
I
I Notes about this command: rexx is the name of the interpreter.  In order
I for this command to work, the interpreter must lie on your current PATH.
I If you require more information about this then you should contact the
I person who installed REXX on your system.
I
I The word "hello" which comes after the command "rexx" is the name of your
I program.  Ordinarily, the interpreter adds ".rexx" to the name you give in
I order to construct the name of the file to execute.
I
O Here is how you execute that file on OS/2:
O
O     hello
O

When you execute your first Rexx program using the method detailed above, you should see the message "Hello world!" appear on the screen.

Doing arithmetic

Rexx has a wide range of arithmetic operators, which can deal with very high precision numbers. Here is an example of a program which does arithmetic.

I Make a file called "arith.rexx" containing the following.
O Make a file called "arith.cmd" containing the following.
     /* This program inputs a number and does some calculations on it */
     pull a
     b=a*a
     c=1/a
     d=3+a
     e=2**(a-1)
     say 'Results are:' a b c d e

I Run it with "rexx arith" and type in a positive integer.
O Run it by typing "arith" and type in a positive integer.

Here is a sample run:

I     rexx arith
O     arith
      5
      Results are: 5 25 0.2 8 16

The results you see are the original number (5), its square (25), its reciprocal (0.2), the number plus three (8) and two to the power of one less than the number (16).

This example illustrates several things:

  • variables: in this example a, b, c, d and e are variables. You can assign a value to a variable by typing its name, "=", and a value, and you can use its value in an expression simply by typing its name.
  • input: by typing "pull a" you tell the interpreter to ask the user for input and to store it in the variable a.
  • arithmetic: the usual symbols (+ - * /) as well as ** (to-power) were used to perform calculations. Parentheses (or "brackets") were used to group together an expression, as they are in mathematics.
  • string expressions: the last line of the program displays the results by saying the string expression
       'Results are:' a b c d e

This has six components: the string constant 'Results are:' and the five variables. These components are attached together with spaces into one string expression, which the "say" command then displays on the terminal. A string constant is any sequence of characters which starts and ends with a quotation mark - that is, either " or ' (it makes no difference which you use, as long as they are both the same).

If you supply the number 6 as input to the program, you should notice that the number 1/6 is given to nine significant figures. You can easily change this. Edit the program and insert before the second line:

     numeric digits 25

If you run this new program you should see that 25 significant figures are produced. In this way you can calculate numbers to whatever accuracy you require, within the limits of the machine.

At this point it seems worth a mention that you can put more than one instruction on a line in a Rexx program. You simply place a semicolon between the instructions, like this:

     /* This program inputs a number and does some calculations on it */
     pull a; b=a*a; c=1/a; d=3+a; e=2**(a-1); say 'Results are:' a b c d e

Needless to say, that example went a bit over the top...

Errors

Suppose you ignored the instructions of the previous section and typed a non-integer such as 3.4 as input to the program. Then you would get an error, because the ** (to-power) operator is only designed to work when the second parameter (that is, the power number) is an integer. You might see this, for example:

I     rexx arith
I     3.4
I         6 +++ e=2**(a-1)
I     Error 26 running arith.rexx, line 6: Invalid whole number
O     arith
O     3.4
O          6 +++   e = 2 ** ( a - 1 );
O     REX0026: Error 26 running D:\ARITH.CMD, line 6: Invalid whole number

Or if you typed zero, you might see the following (because you cannot divide by zero):

I     rexx arith
I     0
I         4 +++ c=1/a
I     Error 42 running arith.rexx, line 4: Arithmetic overflow or underflow
O     arith
O     0
O          4 +++   c = 1 / a;
O     REX0042: Error 42 running D:\ARITH.CMD, line 4: Arithmetic 
O     overflow/underflow

Perhaps most interestingly, if you type a sequence of characters which is not a number, you might see this. It does not complain about the characters you entered, but at the fact that the program tries to use it as a number.

I     rexx arith
I     hello
I         3 +++ b=a*a
I     Error 41 running arith.rexx, line 3: Bad arithmetic conversion
O     arith
O     hello
O          3 +++   b = a * a;
O     REX0041: Error 41 running D:\ARITH.CMD, line 3: Bad arithmetic conversion

In each case, you have generated a "syntax error" (it is classified as a syntax error, even though the problem was not directly related to the program's syntax). What you see is a "traceback" starting with the line which caused the error (no other lines are listed in this traceback, because we have not yet considered any Rexx control structures), and a description of the error. This information should help you to determine where the error occurred and what caused it. More difficult errors can be traced with the "trace" instruction (see later).