RPG Free Format

Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 18

FREE FOMAT CODING

Version 1.0

Vedala Uma Devi

Each free-form statement begins with an operation code and ends with a semicolon. Here is a list of
the new operation codes:

CTL-OPT for control specs (H)


DCL-F for file specs (F)
DCL-S, DCL-DS, DCL-SUBF, DCL-C, DCL-PR, DCL-PI, DCL-PARM for data specs (D)
DCL-PROC for procedure specs (P)

Example 1. Compiler directives within a declaration statement

DCL-S salary
/IF DEFINED(large_vals)
PACKED(13:3)
/ELSE
PACKED(7:3)
/ENDIF
;

Example 2. Empty control statement

// no keywords
CTL-OPT;

Example 3. Additional control statements

// two keywords
CTL-OPT OPTION(∗SRCSTMT:∗NODEBUGIO)
DFTACTGRP(∗No);

// intermixed free-form and fixed-form


CTL-OPT DATFMT(∗ISO);
H TIMFMT(∗ISO)
CTL-OPT CCSID(∗UCS2 :
1200);
// another intermixed free-form and fixed-form
H DATFMT(∗ISO)
CTL-OPT TIMFMT(∗ISO);
H CCSID(∗UCS2 :
H 1200)

Example 4. Fixed-form and free-form keywords

RPGIV/ILE/Free Format Operation Codes:

D∗ fixed-form declarations
D string S 50A VARYING
D date S D DATFMT(∗MDY)
D obj S O CLASS(∗JAVA:'MyClass')
D ptr S ∗ PROCPTR

// free-form declarations
DCL-S string VARCHAR(50);
DCL-S date DATE(∗MDY);
DCL-S obj OBJECT(∗JAVA:'MyClass');
DCL-S ptr POINTER(∗PROC);

Example 5. Named constants

// without the optional CONST keyword


DCL-C lower_bound -50;
DCL-C max_count 200;
DCL-C start_letter 'A';

// with the optional CONST keyword


DCL-C upper_bound CONST(-50);
DCL-C min_count CONST(200);
DCL-C end_letter CONST('A');

Example 6. Stand-alone fields

DCL-S first_name CHAR(10) INZ('John');


DCL-S last_name VARCHAR(20);
DCL-S index PACKED(6);
DCL-S salary PACKED(8:2);

Example 7. Stand-alone fields with LIKE

// Define using the LIKE keyword


DCL-S cust_index LIKE(index);

//Specify length adjustment with LIKE keyword


DCL-S big_index LIKE(index : +6);

Example 8. Declarations using named constants

DCL-C name_len CONST(10);


DCL-S one CHAR(name_len);
DCL-S two VARCHAR(name_len);
DCL-C digits 10;
DCL-C positions 3;
DCL-S value PACKED(digits:positions);

Example 9. Data structures


// Program described data structure
DCL-DS data_str_1;
emp_name CHAR(10);
first_name CHAR(10);
salary PACKED(8:2);
END-DS;

// Program described data structure


DCL-DS data_str_2;
value VARCHAR(4);
index INT(10);
END-DS data_str_2;

//Unnamed data structure


DCL-DS ∗N;
item VARCHAR(40);
END-DS;

Example 10. Data structure using LIKEREC

DCL-DS custoutput LIKEREC(custrec);

Example 11. Data structure with END-DS

DCL-DS PRT_DS LEN(132) END-DS;

Example 12. Two equivalent data structures (END-DS)

DCL-DS myrecord EXT;


END-DS;

DCL-DS myrecord EXT END-DS;


Example 13. Data structure with the DCL-SUBF keyword

DCL-DS record_one;
buffer CHAR(25);
DCL-SUBF read INT(3);
END-DS;

Example 14. Two equivalent data structures (OVERLAY)

D∗ fixed-form declaration
D myds DS
D subf1 11 15A
D subf2 5P 2 OVERLAY(myds)
D subf3 10A OVERLAY(myds:100)
D subf4 15A OVERLAY(myds:∗NEXT)

//free-form declaration
DCL-DS myds;
subf1 CHAR(5) POS(11);
subf2 PACKED(5:2) POS(1);
subf3 CHAR(10) POS(100);
subf4 CHAR(15);
END-DS;

Example 15. Cosine procedure prototype

DCL-PR cosine FLOAT(8) EXTPROC('cos');


angle FLOAT(8) VALUE;
END-PR;

Example 16. Procedure prototype parameter with the DCL-PARM keyword

DCL-PR proc_one;
buffer CHAR(25) CONST;
DCL-PARM read INT(3) VALUE;
END-PR;

Example 17. File definition statement

DCL-F dspf WORKSTN


EXTDESC('TABF035001');

Some of the more useful opcodes follow, but not all.


Note: Some free format examples only work with V5R1 and higher.
Note: In free format end a line of code with a semicolon (;), all
code within structures are indented, and start a free format program with /FREE
and end the program with /END-FREE

Fixed Format Free Format

READ

/FREE

EXCEPT Heading EXCEPT Heading;

READ Rickfile READ Rickfile;

DOW NOT %EOF DOW NOT %EOF;

EXCEPT Detail EXCEPT Detail;

READ Rickfile READ Rickfile;

ENDDO ENDDO;

/END-FREE
----------------------------------------------------------------------------------------------------------------------
READE

Note: Some free format examples only work with V5R1 and higher.
Note: For free format always use the actual name of the file and not the record format name.

/FREE

KEY SETLL Rickfile SETLL KEY Rickfile;

IF %EQUAL IF %EQUAL;

KEY READE Rickfile READE KEY Rickfile;

DOW NOT %EOF(Rickfile) DOW NOT %EOF(Rickfile);

EXCEPT Detail EXCEPT Detail;

READ Rickfile READ Rickfile;

ENDDO ENDDO;

ELSE ELSE;

EXCEPT NoDetail EXCEPT NoDetail;

ENDIF ENDIF;
/END-FREE
----------------------------------------------------------------------------------------------------------------------
READP

Note: Some free format examples only work with V5R1 and higher.
Note: For free format always use the actual name of the file and not the record format name.
Note: In free format the EVAL operation is not required sometimes.

/FREE

KEY SETGT Rickfile SETGT KEY Rickfile;

READP Rickfile READP KEY Rickfile;

IF NOT %EOF(Rickfile) IF NOT %EOF(Rickfile);

EVAL Data# = Data# + 1 Data# = Data# + 1;

ELSE ELSE;

EXSR Error EXSR Error;

ENDIF ENDIF;

/END-FREE
----------------------------------------------------------------------------------------------------------------------

CHAIN

Note: Some free format examples only work with V5R1 and higher.
Note: For free format always use the actual name of the file and not the record format name.

/FREE

KEY CHAIN Rickfile CHAIN KEY Rickfile;

IF %FOUND(Rickfile) IF %FOUND(Rickfile);

EXSR CustFound EXSR CustFound;

ELSE ELSE;

EXSR NoCust EXSR Error;

ENDIF ENDIF;

/END-FREE
----------------------------------------------------------------------------------------------------------------------

Fixed Format Free Format

EVAL

Note: Some free format examples only work with V5R1 and higher.

Note: In free format the EVAL operation is not required sometimes.

Multiple Examples follow:

/FREE

EVAL Total = A * (B – 1) Total = A * (B – 1);

EVAL Total = Total +1 Total = Total + 1;

Total += 1; //Short for


previous example

EVAL Total = Total – Count Total = Total – Count;

//Name = ‘ Jimmie’ //Name = ‘ Jimmie’

/END-FREE

----------------------------------------------------------------------------------------------------------------------

Fixed Format Free Format

ELSEIF

Note: Instead of Nesting Ifs use an ELSEIF or a SELECT/ENDSL below.

In the last stage of the ELSEIF, the ‘ELSE’ part is optional.

/FREE

IF Age >= 20 and Sex = ‘F’ IF Age >= 20 and Sex = ‘F’;

EVAL Code = 5 Code = 5;

ELSEIF Age >= 20 and Sex = ‘M’ ELSEIF Age >= 20 and Sex =‘M’;
EVAL Code = 4 Code = 4;

ELSEIF Age >= 30 and Sex = ‘F’ ELSEIF Age >= 30 and Sex = ‘F’;

EVAL Code = 10 Code = 10;

ELSEIF Age >= 30 and Sex = ‘M’ ELSEIF Age >= 30 and Sex =‘M’;

EVAL Code = 9 Code = 9;

ELSE ELSE

EVAL Code = 20 Code = 20;

ENDIF ENDIF;

/END-FREE
----------------------------------------------------------------------------------------------------------------------

SELECT/ENDSL

Note: Instead of Nesting Ifs use a SELECT/ENDSL or an ELSEIF above.

In the last stage of the SELECT, the ‘OTHER’ part is optional.

/FREE

SELECT SELECT;

WHEN Age >= 20 and Sex = ‘F’ WHEN Age >= 20 and Sex = ‘F’;

EVAL Code = 5 Code = 5;

WHEN Age >= 20 and Sex = ‘M’ WHEN Age >= 20 and Sex =‘M’;

EVAL Code = 4 Code = 4;

WHEN Age >= 30 and Sex = ‘F’ WHEN Age >= 30 and Sex = ‘F’;

EVAL Code = 10 Code = 10;

WHEN Age >= 20 and Sex = ‘M’ WHEN Age >= 30 and Sex =‘M’;

EVAL Code = 9 Code = 9;

OTHER OTHER;
EVAL Code = 20 Code = 20;

ENDSL ENDSL;

/END-FREE

----------------------------------------------------------------------------------------------------------------------
Fixed Format Free Format

FOR

Note: Controls the number of times a group of operations are processed.

/FREE

EVAL Factor = 1 Factor = 1;

FOR I = 1 to %LEN(Field) FOR I = 1 to %LEN(Field);

EVAL Factor = Factor + 1 Factor = Factor + 1;

ENDFOR ENDFOR;

/END-FREE

----------------------------------------------------------------------------------------------------------------------

%CHAR

Note: Converts the value of the expression from graphic, UCS-2, numeric, date, time,

or timestamp data to type character.

/FREE

EVAL Result = ‘It is ‘ + %CHAR(Time) Result = ‘It is ‘ +%CHAR(Time)

+ ‘ on ‘ + %CHAR(Date) + ‘ on ‘ + %CHAR(Date);

*** Result = ‘It is 12:23:34 on 02/02/1977’

/END-FREE
----------------------------------------------------------------------------------------------------------------------

%EDITC

Note: Returns a character result representing the edited number after applying and edit code.
/FREE

EVAL Sal = ‘The annual salary is ‘ Sal = ‘The annual salary is ‘

+ TRIM(%EDITC(Amt * 12 + TRIM(%EDITC(Amt * 12

:’A’ : *CURSYM)) :’A’: *CURSYM));

*** Sal = ‘The annual salary is $12,000.00’

/END-FREE
----------------------------------------------------------------------------------------------------------------------

ADDDUR

Note: The ADDDUR operation adds the duration specified in factor 2 to a date or time and places the
resulting Date, Time or Timestamp in the result field.

/FREE

BillDate ADDDUR 30:*DAYS DueDate DueDate = BillDate + %DAYS(30);

/END-FREE

----------------------------------------------------------------------------------------------------------------------
SUBDUR

Note: Subtract a duration to establish a new Date, Time or Timestamp.

/FREE

DueDate SUBDUR 30:*DAYS BillDate BillDate = DueDate - %DAYS(30);

/END-FREE
----------------------------------------------------------------------------------------------------------------------

Fixed Format Free Format

EXTRCT

Note: Extracts a portion of a date, time, or timestamp data item.

/FREE

EXTRCT BirthDate:*Y BirthYear BirthYear = %SUBDT(BirthDate:*YEARS);

/END-FREE
----------------------------------------------------------------------------------------------------------------------

%DEC / %DECH / %UNS / %UNSH / %INT / %INTH / %FLOAT

Note: As of V5R2 you can convert character arguments to numeric with all of these functions.

Note: These functions can now be used to support “LEGACY” code.

Note: The sign (+ or -), decimal point (. or ,), are optional.

Note: Invalid numeric data will set %STATUS = 00105.

/FREE

EVAL Chars = ‘-123.56’ Chars = ‘-123.56’;

EVAL Num = %DEC(Chars:5:2) Num = %DEC(Chars:5:2); //Num =


-123.56

EVAL Num = %UNSH(Chars) Num = %UNSH(Chars); //Num =


124

/END-FREE

***D Spec:

D ToDate S 8 0 INZ(20021231)

D WorkDate S D

Fixed Format

EVAL ToDate = 20021231

EVAL WorkDate = %DATE(ToDate:*ISO) //WorkDate = D’2002-12-31’

EVAL WorkDate = WorkDate + %DAYS(30) //Add 30 days to WorkDate

EVAL ToDate = %UNS(%CHAR(WorkDate:*ISO)) //ToDate = 20030130

Free Format

/FREE

ToDate = 20021231;
WorkDate = %DATE(ToDate:*ISO); //WorkDate = D’2002-12-31’

WorkDate = WorkDate + %DAYS(30); //Add 30 days to WorkDate

ToDate = %UNS(%CHAR(WorkDate:*ISO)); //ToDate = 20030130

/END-FREE

----------------------------------------------------------------------------------------------------------------------

Fixed Format Free Format

%TIME / %TIMESTAMP

Note: Converts the value of the expression from character, numeric, or |timestamp data to type time.
The converted value remains unchanged, but |is returned as a time.

Note: Available as of V5R2.

/FREE

EVAL String = ’12:34 PM’ String = ’12:34 PM’;

EVAL Time = %TIME(String,*USA) Time = %TIME(String:*USA);

//Time = t’12.34.00’

/END-FREE
----------------------------------------------------------------------------------------------------------------------

Fixed Format Free Format

%DIFF

Note: Used to find the duration between: two dates, two times, two timestamps, a date and the date

portion of a timestamp, and a time and the time portion of a timestamp.

EVAL DaysLeft = %DIFF(ExamDate:Today:*DAYS)

/FREE
DaysLeft = %DIFF(ExamDate:Today:*DAYS);

/END-FREE
----------------------------------------------------------------------------------------------------------------------

MONITOR/ENDMON

Note: May be used with V5R1 and later.

Note: Used for error handling routines. ON-ERROR list one or more errors for which it is responsible.

These errors generally correspond to the %STATUS code from 00100 to 09999 or you can use *FILE

for file errors.

/FREE

MONITOR MONITOR;

READ Rickfile READ Rickfile;

DOW NOT %EOF(Rickfile) DOW NOT %EOF(Rickfile);

KEY CHAIN Rickfile2 CHAIN KEY Rickfile2;

EXSR DoSomething EXSR DoSomething;

READ Rickfile READ Rickfile;

ENDDO ENDDO;

ON-ERROR 1211 ON-ERROR 1211;

EXSR NotOpen EXSR NotOpen;

ON-ERROR 1218 ON-ERROR 1218;

EXSR LockedRec EXSR LockedRec;

ON-ERROR *FILE ON-ERROR *FILE;

EXSR FileErr EXSR FileErr;

ON-ERROR 00100 : 00121 ON-ERROR 00100 : 00121;

***Handle string error and array-index error***********************

EXSR STRARY EXSR STRARY;

ON-ERROR ON-ERROR;
***Handle all other errors**************************************

EXSR GenErr EXSR GenErr;

ENDMON ENDMON;

/END-FREE
----------------------------------------------------------------------------------------------------------------------
Fixed Format Free Format

LEAVESR

Note: Used with free format only. Leaves a subroutine.

/FREE

LEAVESR;

/END-FREE
----------------------------------------------------------------------------------------------------------------------

MOVE AND MOVEL

Note: RPGIV and free format provides a smoother process.

OLD RPGIII:

MOVEL FIELD1 FIELDA

MOVE FIELD2 FIELDA

MOVEL FIELDA FIELDB

MOVE FIELD3 FIELDB

NEW RPGIV and free format:

/FREE

EVAL FieldB = Field1 + Field2 + Field3 FieldB = Field1 + Field2 + Field3;

/END-FREE
----------------------------------------------------------------------------------------------------------------------

INDICATOR DATA TYPE


Note: Some free format examples only work with V5R1 and higher.

Note: Some languages refer to this as a Boolean data type. An indicator field is a single-byte field

that can contain only two logical values: ‘1’ or ‘0’. You can also refer to these values using *ON

and *OFF, respectfully. Indicator data is usually used within an RPGIV program to signal a true/false

condition and can be tested on as a true/false condition.

Examples follow:

D Spec:

D IsGood S N INZ(*OFF)

/FREE

EXSR ChkGood EXSR ChkGood;

IF IsGood IF IsGood;

EXSR DoSomething EXSR DoSomthing;

EVAL IsGood = *OFF IsGood = *OFF;

ELSE ELSE;

EXSR GiveErr EXSR GiveErr;

ENDIF ENDIF;

/END-FREE

---------------------------------------------------------------------------------------------------------------------------

Free-form equivalents for keywords that are not supported in free-form definitions

· CLASS - The class information is specified as parameters of the OBJECT keyword.


· DATFMT - The date format is specified as the parameter of the DATE keyword.
· FROMFILE - You must use a fixed-form definition if you want to specify the FROMFILE keyword.
· PACKEVEN- The PACKEVEN keyword is related to the use of from-and-to positions which are not
supported for a free-form subfield definition.
· PROCPTR-The procedure pointer data type is specified as POINTER(*PROC).
· TIMFMT-The time format is specified as the parameter of the TIME keyword.
· TOFILE -You must use a fixed-form definition if you want to specify the TOFILE keyword.
· VARYING- Variable length fields are defined using the VARCHAR, VARGRAPH, and VARUCS2
keywords.

---------------------------------------------------------------------------------------------------------------------------
Old Vs Free Format Logic

Old Vs Free Form Declarations


OPCODE REPLACEMENTS:
Op-codes not supported in Free-form Logic
These fall into 5 main categories:
1) "Old fashioned" Op-codes whose use is discouraged
• e.g. ANDxx, COMPxx, DEFINE, DOxyy, GOTO, and IFxx
2) Those for which new support has been provided
• e.g. ALLOC, CHECK, EXTRCT, LOOKUP, TIME, XLATE, KFLD, KLIST
3) Op-codes with existing (if not identical) expression support
• e.g. ADD, DIV, MULT and SUB
4) Those supported by a combination of enhanced and new support
• e.g. ADDDUR and SUBDUR
5) The ones the compiler writers don't like: MOVE, MOVEL, MOVEA
• We will talk more about Why they don't like it in a few minutes
• EVAL is the primary alternative but does not meet all needs

You might also like