IBM ^ z/VM Module 6: The REXX Programming Language © 2004 IBM Corporation IBM ^ Module Objectives Data structures IF-THEN-ELSE SELECT LOOPS Data formats An example of formatting numbers and strings are: – FORMAT( ) numerical – SUBSTR( ) string manipulation Input/Output (I/O) functions STREAM( ) function CHARIN, LINEIN, CHAROUT, LINEOUT instructions Parameters To retrieve parameters use: – ARG, PULL, etc. © 2004 IBM Corporation IBM ^ Objectives Describe REXX and how it works with z/VM Describe how to write REXX programs using: Comments Keywords and literal strings Clauses Syntax error messages Explain the use of REXX variables with names, values, and assignments © 2004 IBM Corporation IBM ^ Objectives continued Understand which expressions can be used within a REXX clause: Operators and terms Comparisons (equal, and, or) Functions Learn the control statements for manipulating data flow: IF – THEN ELSE keyword DO LOOPS (repetitive and conditional) Selection © 2004 IBM Corporation IBM ^ Objectives continued Explain arithmetic, text, and conversational expressions for manipulating and gathering data Show how to issue CMS and CP commands within a REXX EXEC Explain the subcommands and macros used in REXX EXECs Introduce REXX subroutines © 2004 IBM Corporation IBM ^ What is REXX? REstructured eXtended eXecutor language A versatile, easy to use, structured programming language A programming language that is easy for both computer professionals and general users to learn and use A compiler can be used to translate REXX source programs into compiled programs © 2004 IBM Corporation IBM ^ Features of REXX Ease of use Free format Interpreted Built-in functions Parsing capabilities Powerful debugger Relationship with z/VM © 2004 IBM Corporation IBM ^ How a Program Works A REXX program is a list of instructions, something like a recipe A computer communicates with users through questions displayed and answers typed in © 2004 IBM Corporation IBM ^ Comments in REXX Programs Comments in programs: •/* . . . */, this is used for descriptions and explanations Comments with special meaning to CMS •To determine you are writing a REXX program the first line must contain /* . . . */ © 2004 IBM Corporation IBM ^ Keywords and Literal Strings Keywords are instructions that describe an action, such as PULL, IF, and SAY. REXX reads each individual clause, then processes it before going on to the next (interpreted language). A literal string is a set of characters bounded by quotation marks. REXX processes a clause containing a variable by substituting the variable name with the stored data. © 2004 IBM Corporation IBM ^ REXX Clauses REXX programs consist of these types of clauses: •Instruction •Assignment •Label •Null •Commands © 2004 IBM Corporation IBM ^ REXX Syntax Errors © 2004 IBM Corporation IBM ^ REXX Variables © 2004 IBM Corporation IBM ^ Names, Values, and Assignments Information stored in a variable is called its value. It is possible to make variable names anything, but a good idea to create meaningful names. An instruction that stores a value in a variable or changes its value is called an assignment. • In formal terms, the syntax might look like this: • symbol = expression © 2004 IBM Corporation IBM ^ Other Assignments The PULL instruction: Pauses the running program to allow the user to enter data Can be used to pull in each piece of data or allow the user to enter multiple amounts of data separated by spaces The ARG instruction: Like PULL, but data items are entered at the command prompt with the program name © 2004 IBM Corporation IBM ^ Assignments and Instructions One way to write this EXEC is: /* SUBMUL1 EXEC */ ARG first second say first “-” second “=” first-second say first “*” second “=” first*second Another way to write this EXEC is: /* SUBMUL2 EXEC */ say “Enter two numbers to multiply and subtract.” pull first second say first “-” second “=” first-second say first “*” second “=” first*second © 2004 IBM Corporation IBM ^ REXX Expressions Operators and terms: Operators include +, -, /, %, *, || Operators manipulate numbers, strings in quotes, variables, results from function calls and evaluated expressions Parentheses: The language processor evaluates the expression inside the parentheses first • The value of 10 * ( 3 || 4 ) is: 340 © 2004 IBM Corporation IBM ^ REXX Expressions (Comparison, True, and False) Comparisons: – > Greater than – = Equal – < Less than TRUE, the computed result is 1 – say 4 < 7 • /* represents a “1”, which means TRUE */ FALSE, the computed result is 0 – say “Chalk” = “Cheese” • /* represents a “0”, which meaning FALSE */ © 2004 IBM Corporation IBM ^ REXX Expressions ( =, &, | ) The equal sign (=) can have two meanings – Can be an assignment if found at the beginning after the symbol – An equal sign anywhere else stands for the comparison operator Use the AND (&) operator to write an expression that is true when everything else is also true Use the OR (|) operator when any part of an expression can be true © 2004 IBM Corporation IBM ^ REXX Functions Function calls can be written anywhere in an expression. The function performs the computation named by the function and returns the result. When the value of the function has been calculated, the result is put back into the expression in place of the function call. – An example is: • say 7 + HALF(6) /* becomes 7 + 3 which says “10” */ © 2004 IBM Corporation IBM ^ Control Statements: IF – THEN © 2004 IBM Corporation IBM ^ Examples and Notes: IF - THEN The THEN instruction may be an assignment, command, or keyword. The NOP instruction can be used when no operations are necessary. An important property of the THEN keyword is that is does not need to start a clause, therefore a semicolon is not needed. Another example is: If answer=‘YES’ then say ‘OK!’; else say ‘Why not?’ © 2004 IBM Corporation IBM ^ Control Statements: ELSE Keyword © 2004 IBM Corporation IBM ^ REXX Loops © 2004 IBM Corporation IBM ^ Repetitive DO Loops © 2004 IBM Corporation IBM ^ Conditional DO Loops Conditional loops continue to be executed as long as some condition is satisfied. The simplest way to code these loops is to use DO FOREVER and LEAVE instructions. © 2004 IBM Corporation IBM ^ Conditional Loops: The Choice There are three kinds of Conditional Loops The decision is made before processing starts – Checking occurs before entering the loop and continues after each iteration. The decision is made after the first pass through the loop and again after every subsequent pass. – Data is requested for the user. The decision is made during each pass. – The decision to leave might depend on information obtained during the loop. © 2004 IBM Corporation IBM ^ The SELECT Instruction © 2004 IBM Corporation IBM ^ A SELECT Instruction SELECT WHEN morning THEN DO say “Take shower” say “Eat breakfast.” say “Get ready for work.” end WHEN afternoon THEN DO until ans=Y say “Did you eat lunch? (Y/N) PARSE UPPER PULL ans end otherwise say “It is in the evening -- get ready for bed!!” end © 2004 IBM Corporation IBM ^ Arithmetic The addition, subtraction and multiplication operations are performed in the usual way. – – – – + Addition - Subtraction * Multiplication ** Power function The result of a % operation is the whole number portion. The remainder is dropped. The result of a // operation is the remainder portion. The whole number is dropped. The result of a / operator is a combination of both operations above. © 2004 IBM Corporation IBM ^ Text - Concatenation © 2004 IBM Corporation IBM ^ Text – String Manipulation SUBSTR() Function: To select a part of a string to use: – WORD = “reveal” – say substr(WORD, 2, 3) /* says “eve” */ LENGTH() Function: To find out the length of a REXX variable: – WORD = "reveal" – say length(WORD) /* says "6" */ © 2004 IBM Corporation IBM ^ Text – String Manipulation continued COPIES(): Produces a number of copies of the string. The arguments are: • The string to be copied • The number of copies required LEFT(): Obtains a string that is padded or truncated on the right RIGHT(): Obtains a string that is padded or truncated on the left © 2004 IBM Corporation IBM ^ Conversations – SAY and PULL The SAY instruction and its expression are computed and the result is displayed as a new line on the screen. The PULL instruction is able to collect an answer that has been displayed by the SAY instruction. The PARSE PULL instruction brings in the data just as it is, without converting the lowercase letters to uppercase. The UPPER instruction translates the value of one or more variables to uppercase. © 2004 IBM Corporation IBM ^ Conversation – Parsing Words PULL can also fetch each word into a different variable Using the period as a place holder in this statement (PULL . . lastname .) means to discard the first two words and assign the third word to lastname. © 2004 IBM Corporation IBM ^ Issuing Commands to CMS and CP The language processor can operate in a number of environments. Use quotes to avoid errors when writing CMS and CP commands within REXX. © 2004 IBM Corporation IBM ^ Issuing Commands – Return Codes More examples: 1) access 591 591 DMSACC113S B(591) not attached or invalid device address Ready (00100); 2) copyfile profile exec a = = b (for luck Invalid parameter LUCK in the option FOR field Ready (00024); 3) erase junk exec File JUNK EXEC A not found Ready (00028) © 2004 IBM Corporation IBM ^ Why Use a Compiler? Advantages of compiling REXX EXECS Source Load can be hidden from end users modules are loaded into memory faster Compile programs using this CMS command: REXXD [source-file-identifier] © 2004 IBM Corporation IBM ^ How to Use the Compiler © 2004 IBM Corporation IBM ^ XEDIT Subcommands and Macros The first word on the command line is assumed to be a subcommand Words that are not subcommands are interpreted as macros © 2004 IBM Corporation IBM ^ Subroutines © 2004 IBM Corporation IBM ^ Conclusion REXX was created as a procedural language that allows programs and algorithms to be written in a clear and structured way. Topics in this module: Comments Clauses Variables Expressions Control statements: – – – – IF THEN ELSE Loops Selection © 2004 IBM Corporation IBM ^ Glossary Clause – a line of code or a statement within a REXX program Parsing – manipulates character strings to let your program read and separate characters, number, and mixed inputs PL/I – was developed as the universal programming language, where definitions were not needed © 2004 IBM Corporation IBM ^ Glossary REXX – REstrutured eXtneded eXecutor language, a versatile, easy to use structured programming language that is an integral part of z/VM. REXXCompiler – translates REXX source programs into compiled programs. (Compiled programs run much faster than interpreted programs.) © 2004 IBM Corporation IBM ^ References z/VM: REXX/VM User’s Guide –Version 3 Release 1.0 SC245962-00 The REXX Language: A Practical Approach to Programming –by Michael Cowlishaw Website: Rexx Language Association © 2004 IBM Corporation