Search code examples
ibm-midrangerpgle

Converting fixed format RPG to Free format


I have the below pieces of code written in Fixed format RPG in a report generation program. I have tried rewriting this in Free format. The report produced before and after seems to be the same on checking. However please let me know if there are any mistakes in this.

 *                                                 
 *   CONTROL BREAK WITHIN SOLIDS OR PATTERNS       
 *                                                 
C*    WOSRT1        IFEQ      '1'                  
C*    WOBKCL        ANDNE     SVCOLR               

C*    WOSRT1        OREQ      '2'                  
 ****       WOPATT    ANDNESVPATT                  
C*    WOCLT#        ANDNE     SVCLT#               

Equivalent:

If ( (WOSRT1 = '1' and WOBKCL <> SVCOLR) or                          
     (WOSRT1 = '2' and ( WOCLT# <>  SVCLT# or WOCLTH <> SVCLTH )) ); 

Code snippet - 2:

 *   CONTROL BREAK WITHIN SOLIDS OR PATTERNS     
 C*    WOSRT1        IFEQ      '1'                
 C*    WOBKCL        ANDNE     WOBKCLsave         

 C*    WOSRT1        OREQ      '2'                
 C*    WOCLT#        ANDNE     WOCLT#save         

Equivalent:

If ( (WOSRT1 = '1' and WOBKCL <> WOBKCLsave) or    
     (WOSRT1 = '2' and ( WOCLT# <>  WOCLT#save     
                      or WOCLTH <> WOCLTHsave ))); 

The condition WOCLTH <> SVCLTH in the first snippet and WOCLTH <> WOCLTHsave in the second snippet is an additional condition that I needed to add to the Break logic.

When I tried adding this condition in Fixed format (last line), the report records got changed in an unintentional way. Kindly can someone also point out what is wrong with the below?

 *   CONTROL BREAK WITHIN SOLIDS OR PATTERNS
C     WOSRT1        IFEQ      '1'           
C     WOBKCL        ANDNE     WOBKCLsave    

C     WOSRT1        OREQ      '2'           
C     WOCLT#        ANDNE     WOCLT#save    
C     WOCLTH        ORNE      WOCLTHsave    

Solution

  • The free form converter in RDi converts that as:

       IF WOSRT1 = '1'
       AND WOBKCL <> WOBKCLsave
       OR WOSRT1 = '2'
       AND WOCLT# <> WOCLT#save
       OR WOCLTH <> WOCLTHsave;
    

    Since AND has precedence over OR, I'd add parens like so:

       IF (WOSRT1 = '1'
           AND WOBKCL <> WOBKCLsave)
       OR  (WOSRT1 = '2'
            AND WOCLT# <> WOCLT#save)
       OR WOCLTH <> WOCLTHsave;
    

    Thus explaining why you get a difference results than you'd expect from your free-form version.

    Complex IFs are difficult with legacy fixed format IFxx op-codes, why would you bother?

    Even in fixed format, you could use IF with an extended factor 2

     C                   IF        ( (WOSRT1 = '1' and WOBKCL <> SVCOLR)                           
     C                               OR (WOSRT1 = '2'   
     C                                   and ( WOCLT# <>  SVCLT#  
     C                                         or WOCLTH <> SVCLTH   
     C                                       )                       
     C                                  )                             
     C                             )                                   
     C                   endif