Project

General

Profile

Control Flow

This chapter documents the conversion of language statements that are used to control the path or flow of processing through the 4GL code. Examples of this processing is IF THEN ELSE and loops like REPEAT. While some of these statements include block-related processing, the next chapter (entitled Blocks) will provide more detailed coverage of the properties and conversion of blocks. Only the basic control-flow features will be covered in this chapter. Likewise, there are many complex transaction processing features (such as UNDO or database commit/rollback) which are detailed in the Transactions chapter, rather than here. All three topics are highly interrelated, but they have been split up to make the content easier to consume.

It is important to note that the converted code shown in this chapter is highly dependent upon statically importing features from the BlockManager, CompareOps and other important classes in the FWD runtime. For example, most converted programs will have something like this at the top:

import static com.goldencode.p2j.util.BlockManager.*;
import static com.goldencode.p2j.util.CompareOps.*;

These allow the converted code to more compactly reference the BlockManager APIs, comparison operators and other commonly used features. For example, instead of calling BlockManager.doBlock() in the converted code, the result will be simply doBlock(). This greatly improves readability and reduces the amount of code. However, the reader of this chapter must remember that these imports are necessary to make that idiom work.

The descriptions of 4GL behavior in this chapter (what a statement does at runtime, as opposed to how it converts) were checked with the error-handling test suite of #4602: 29,265 small 4GL programs run in batch mode on OpenEdge 11.6, on OpenEdge 12.8 and on FWD, each comparing what it observes with what OpenEdge produced. Where OpenEdge 11.6 and later releases behave differently, both are described and FWD follows the later release (OpenEdge 11.7 and later, checked on 12.8): a FINALLY block runs when a STOP passes through its block, STOP conditions are raised as stop objects which a CATCH block can handle, and the RUN of a missing program raises a StopError. Batch mode means that no statement waits for user input; this matters for RETRY (see Infinite Loop Protection).

This is the list of control-flow features documented in this chapter:

4GL Feature Purpose Java Implementation Supported
BLOCK-LEVEL ON ERROR UNDO, THROW / ROUTINE-LEVEL ON ERROR UNDO, THROW Change the default error action of the blocks of a procedure or class file to UNDO, THROW. Converts to BlockManager.onBlockLevel(Condition.ERROR, Action.THROW) or BlockManager.onRoutineLevel(Condition.ERROR, Action.THROW) as the first statement of the external procedure's body. Yes
CASE Branch to one of an arbitrary number of blocks of code based on the evaluation of a single expression. This converts as either a Java switch statement or as the Java if/else if/else statement. Yes
CATCH Handle an error object raised in the associated block. Converts to a BlockManager.catchError(<error class>, <lambda>) call in the Init part of the associated block. Yes
FINALLY Run code at the end of every execution (iteration) of the associated block, however it ends. Converts to the Fini part of the associated block. Yes
DO Groups statements into a single block, optionally specifying processing block options. Blocks without properties (simple DO blocks) emit using curly braces {}. Loops without properties (simple DO loops that have TO and/or WHILE clauses) emit using the Java while or Java for statements.

Non-simple DO blocks or loops emit as calls to the BlockManager doBlock, doTo, doWhile or doToWhile APIs.
Yes
FOR Groups statements into a block (non-EACH) or an iterating loop (EACH) that reads a record from one or more tables at the start of each block execution. All forms of the FOR block (non-EACH) are converted using BlockManager.forBlock[To][While] APIs.

All forms of the FOR EACH loop are converted using BlockManager.forEach[To][While] APIs.
Yes
IF THEN ELSE Branch to one of two possible blocks of code based on the evaluation of a logical expression. Converted using the Java if () {} else {}. Yes
LEAVE Normally exit the current (or an enclosing) block. Converts to a BlockManager.leave(), if the target is a managed block (a block that is processed by the BlockManager).

Converts to a Java break statement, if the target is a non-managed block and there is no managed block between the LEAVE statement and the targeted non-managed block.

Converts to a combination of a BlockManager.leave(), a BlockManager.deferredLeave() and a Java break statement when the LEAVE targets a non-managed block but there is a managed block between the non-managed and the LEAVE statement.

In the case where the LEAVE is treated by the 4GL as an implicit RETURN, it is converted to BlockManager.returnNormal().
Yes
NEXT Normally iterate the current (or an enclosing) loop. Converts to a BlockManager.next(), if the target is a managed block (a block that is processed by the BlockManager).

Converts to a Java continue statement, if the target is a non-managed block and there is no managed block between the NEXT statement and the targeted non-managed block.

In the case where the NEXT is treated by the 4GL as an implicit LEAVE, it is converted to BlockManager.leave().

In the case where the NEXT is treated by the 4GL as an implicit RETURN, it is converted to BlockManager.returnNormal().
Yes
PAUSE Halt processing of the program until the user presses a key. This converts to LogicalTerminal.pause() or to LogicalTerminal.pauseBeforeHide(). Yes
QUIT Raise the QUIT condition, which by default will cause the current user session to end. This converts to BlockManager.quit(). Yes
REPEAT Declares a block of statements that are processed repeatedly until the block ends based on options, conditions being raised or contained language statements. All forms of the REPEAT loop are converted using BlockManager.repeat[To][While] APIs. Yes
RETRY Detect if the nearest enclosing block is being retried. This converts to TransactionManager.isRetry() (when the result must be a logical) or TransactionManager._isRetry() (when the result needs to be a boolean). Yes
RETURN Exit from the nearest enclosing top-level block. This converts to the BlockManager.return*() family of methods. Yes
RUN Execute an internal or external procedure. Each RUN statement converts to a static InvokeConfig call site (RUN_CALL_SITE_<n>) that holds the target name (or gets it at runtime for RUN VALUE) and the parameter modes. The statement itself calls RUN_CALL_SITE_<n>.clone()...run(...), adding setProcedureHandle() for PERSISTENT SET and setInHandle() for RUN ... IN handle. Internal and external procedures use the same form. Yes
STOP Raise the STOP condition. This converts to BlockManager.stop(). Yes
UNDO Abnormally end processing of the current block (or an enclosing block), roll back the undoable changes of the current transaction or sub-transaction (if one is active) and then execute a programmer-specified control flow action, including THROW of an error object. This converts to the BlockManager.undo*() family of methods (undoThrow() / undoThrowTopLevel() for UNDO, THROW). Yes

Inner Blocks

Progress 4GL supports multiple structures for organizing code. Broadly, these features can be classified as top-level blocks or inner blocks.

A top-level block is a code that is structured in a manner that can be called or invoked. External procedures, internal procedures, user-defined functions and triggers are all top-level blocks.

Inner blocks are structures that group code inside of top-level blocks. Inner blocks can be nested, thay can loop, they can be labeled and through labels (or by implicit control flow behavior) they can be targets of changes in the flow of control of the program.

There are four types of inner block: DO, REPEAT, FOR and EDITING. The first three blocks are documented in this chapter. The EDITING block is special, in that it is really a loop that is nested inside a user-interface statement, rather than a generic flow control block. For details of how EDITING blocks convert, please see the EDITING Block section of the Editing chapter in Part 6.

DO Block/Loop

The following is the 4GL syntax for the DO statement:

[ label : ]
DO
   [ variable = expression1 TO expression2 [ BY k ]]
   [ WHILE expression ]
   [ TRANSACTION ]
   [ other-options ]

This chapter documents the conversion of the above syntax. A summary of support for the other-options is provided in the Block Options section below, but full details of these other options must be found elsewhere in this reference.

The 4GL DO block is special in that, by default, there are no block properties provided by the Progress 4GL runtime. At the programmer's option, many properties (see above) can be added. The result means that the DO block can be lightweight (it will perform better), unless options are added that cause block properties to be attached to the block.

For this reason, the DO block can be converted in several ways. In some cases, when the block doesn't have deep 4GL behavior and is just used to group code together or is used as a looping block, it can be directly converted to compatible Java statements. This is often called a “simple DO block” and it occurs when there are no 4GL properties which need to be duplicated by the runtime processing.

A DO block cannot be emitted as a simple DO block if any of the following scenarios are true:

  • The TRANSACTION keyword is used in the DO block definition.
  • Any ON phrase is included in the DO block definition.
  • A frame phrase (WITH FRAME) is included in the DO block definition.
  • A FOR or PRESELECT phrase is included in the DO block definition.
  • DO loop anomaly: the DO block is a loop (there is a TO and/or WHILE clause) and inside that loop there is use of:
  • the RUN statement; OR
  • the WAIT-FOR statement; OR
  • a call to a user-defined function.

When the simple DO block cannot be used, the DO block will be converted using one of the BlockManager doBlock, doTo, doWhile or doToWhile APIs. Details about all the parameters for these APIs can be find in the DO Block section of the Blocks chapter of this book.

The usage of BlockManager APIs does not presuppose the transaction level of the block being processed. The transaction level will default as appropriate for each block type but can also be explicitly specified. For details, see the Transactions chapter.

It is always safe to emit a DO block (or loop) using the BlockManager APIs. These APIs provide a fully compatible (with the original 4GL) implementation of block processing. That compatibility comes with the cost of a more complex runtime implementation and the performance cost of having multiple layers and delegated block processing. For that reason, where the above scenarios are not present, the resulting code will be emitted as direct Java block processing.

The DO statement is also special because it can be used as both a block (grouping related statements together) and as a loop. By default it is a block, but when the TO or WHILE clauses are used, it becomes a loop.

The DO loop anomaly was found to be necessary at one point in the development of the runtime, but it has not been checked in recent runtime versions. It may be unnecessary in the current implementation. If further testing confirms this is unnecessary, then it will be removed. The resulting block will not have full-transaction or sub-transaction semantics unless the normal transaction level rules are met as specified in the Transactions chapter.

A DO block has no default ON ERROR, ON ENDKEY, ON STOP or ON QUIT processing of its own: unless it has an ON phrase (or a CATCH), a condition raised inside it is handled by the enclosing block. A DO WHILE loop is left after any undo of an iteration, even when its WHILE condition is still true. See Loop Behavior After a Condition.

This section shows how the DO block gets converted to Java statements, including examples of the looping constructs. It does not provide full details on how the WHILE and TO clauses get converted. For this, please see the TO Clause and WHILE Clause sections of this chapter.

Simple DO Block

4GL code:

do:
   message "message 1".
end.

Converted code:

blockLabel0:
{
   message("message 1");
}

Details:

If it is a simple DO block without any specifications (only label is allowed) then it is converted to a plain Java block, annotated by a label.

Simple DO WHILE Loop

4GL code:

do while x < 5:
   x = x + 1.
end.

Converted code:

loopLabel0:
while (_isLessThan(x, 5))
{
   x.assign(plus(x, 1));
}

Details:

If it is a DO WHILE block without any other specifications, it is converted using Java while statement, annotated with a label.

Since this is a direct mapping to the flow control statements of Java, the conditional expression which is evaluated will generally return a logical type. This value will then be “unpacked” into a boolean (often using logical.booleanValue()) that is passed to the Java while. In many places in FWD runtime, there are dual versions of a given method, one which returns a logical and another (usually prefixed by the _ character) which directly returns a boolean. Where possible, the converted code will use these special versions (to avoid the unpacking step) and directly return the boolean. This example of is the replacement less than operator which leads to emitting _isLessThan(x, 5) instead of emitting isLessThan(x, 5).booleanValue().

Simple DO TO Loop

4GL code:

do x = 0 to 10 by 2:
   x = x + 1.
end.

Converted code:

loopLabel0:
for (x.assign(0); _isLessThanOrEqual(x, 10); x.assign(plus(x, 2)))
{
   x.assign(plus(x, 1));
}

Details:

If it is a DO ... TO block without any other specifications, it is converted using Java for statement, annotated with a label.

Simple DO TO WHILE Loop

4GL code:

do x = 0 to 10 while x < 5:
   x = x + 1.
end.

Converted code:

loopLabel1:
for (x.assign(0); _and(_isLessThanOrEqual(x, 10), () -> isLessThan(x, 5)); x.increment())
{
   x.assign(plus(x, 1));
}

Details:

If it is a DO ... TO ... WHILE block without any other specifications, it is converted using Java for statement, annotated with a label. When both WHILE and TO conditions are specified, the for termination expression is the conjunction of the WHILE condition with the loop finishing condition of the TO condition. The test of these logical expressions is conjoined with the AND operator.

Non-Simple DO Block

4GL code:

do transaction:
   message "message 1".
end.

Converted code:

doBlock(TransactionType.FULL, "blockLabel0", new Block((Body) () -> 
{
   message("message 1");
}));

Details:

All other cases (with TRANSACTION option, PRESELECT or ON phrases) are converted using BlockManager.do* functions. In this example, a full transaction is associated with the block, causing the BlockManager APIs to be required.

Non-Simple DO TO Loop

4GL code:

do x = 1 to 10
  on error undo, retry:
   ...
end.

Converted code:

ToClause toClause0 = new ToClause(x, 1, 10);

OnPhrase[] onPhrase0 = new OnPhrase[]
{
   new OnPhrase(Condition.ERROR, Action.RETRY, "loopLabel0")
};

doTo(TransactionType.SUB, "loopLabel0", toClause0, onPhrase0, new Block((Body) () -> 
{
   ...
}));

Details:

This loops 10 times, starting with x as 1 and incrementing x on each iteration, ending with x as 10. When the TO clause is present, the BlockManager.doTo API call is emitted.

Non-Simple DO WHILE Loop

4GL code:

do while x < 5
  on error undo, leave:
   x = x + 1.
end.

Converted code:

LogicalExpression whileClause0 = () -> isLessThan(x, 5);

OnPhrase[] onPhrase0 = new OnPhrase[]
{
   new OnPhrase(Condition.ERROR, Action.LEAVE, "loopLabel0")
};

doWhile(TransactionType.SUB, "loopLabel0", whileClause0, onPhrase0, new Block((Body) () ->
{
   x.assign(plus(x, 1));
}));

Details:

This loops so long as x is less than 5. When the WHILE clause is present, the BlockManager.doWhile API call is emitted.

Non-Simple DO TO WHILE Loop

4GL code:

do y = 1 to 10
  while x < 5
  on error undo, leave:
   x = x + 1.
end.

Converted code:

ToClause toClause0 = new ToClause(y, 1, 10);
LogicalExpression whileClause0 = () -> isLessThan(x, 5);

OnPhrase[] onPhrase0 = new OnPhrase[]
{
   new OnPhrase(Condition.ERROR, Action.LEAVE, "loopLabel0")
};

doToWhile(TransactionType.SUB, "loopLabel0", toClause0, whileClause0, onPhrase0, new Block((Body) () -> 
{
   x.assign(plus(x, 1));
}));

Details:

This loops with two constraints, meaning that it will iterate 10 times, starting with y as 1 and incrementing y on each iteration, ending with y as 10 BUT only so as long as x is less than 5. If x becomes 5 or above before the 10 iterations are complete, then the loop will exit early. The test of these logical expressions is conjoined with the AND operator.

When both the TO and WHILE clauses are present, the BlockManager.doToWhile API call is emitted.

REPEAT Loop

The following is the 4GL syntax for the REPEAT statement:

[ label : ]
REPEAT
   [ variable = expression1 TO expression2 [ BY k ]]
   [ WHILE expression ]
   [ TRANSACTION ]
   [ other-options ]

This chapter documents the conversion of the above syntax. A summary of support for the other-options is provided in the Block Options section below, but full details of these other options must be found elsewhere in this reference.

A REPEAT block is a looping block which will terminate on the condition indicated by the TO or WHILE clauses, until a statement is executed that reroutes control flow out of the loop (e.g. LEAVE, RETURN, UNDO) or until some condition is raised (i.e. the user presses the END key) which causes the loop to end.

By default (with no limiting TO or WHILE clauses), the REPEAT is an infinite loop.

Unlike the DO block, REPEAT loops always have block properties. This means that a REPEAT cannot be directly converted to Java control flow statements since the runtime code must duplicate the block/loop properties of the 4GL. All forms of the REPEAT loop are converted using BlockManager.repeat[To][While] APIs. In cases when either the TO or WHILE clause is present, the clauses are converted following the rules presented in the TO Clause and WHILE Clause sections of this chapter.

By default a REPEAT loop has ON ERROR UNDO, RETRY and ON ENDKEY UNDO, LEAVE (with BLOCK-LEVEL ON ERROR UNDO, THROW the error default becomes UNDO, THROW). When no statement in the loop waits for input, the retry is not performed: a REPEAT ... TO loop goes on with the next iteration, while a REPEAT without TO and a REPEAT WHILE are left. See Loop Behavior After a Condition.

REPEAT Loop

4GL code:

/* simple REPEAT */
repeat:
  ...
end.

Converted code:

/* simple REPEAT */
repeat("loopLabel0", new Block((Body) () -> 
{
   ...
}));

Details:

This simple REPEAT will loop infinitely until a LEAVE statement is encountered or some other condition is raised. When neither the TO nor the WHILE clause is present, the BlockManager.repeat API call is emitted.

REPEAT TO Loop

4GL code:

/* REPEAT with TO */
repeat x = 1 to 10:
  ...
end.

Converted code:

/* REPEAT with TO */
ToClause toClause0 = new ToClause(x, 1, 10);

repeatTo("loopLabel1", toClause0, new Block((Body) () -> 
{
   ...
}));

Details:

This loops 10 times, starting with x as 1 and incrementing x on each iteration, ending with x as 10. When the TO clause is present, the BlockManager.repeatTo API call is emitted.

REPEAT WHILE Loop

4GL code:

/* REPEAT with WHILE */
repeat while y > 0 :
  ...
end.

Converted code:

/* REPEAT with WHILE */
LogicalExpression whileClause0 = () -> isGreaterThan(y, 0);

repeatWhile("loopLabel1", whileClause0, new Block((Body) () -> 
{
   ...
});

Details:

This loops so long as y is a positive number. When the WHILE clause is present, the BlockManager.repeatWhile API call is emitted.

REPEAT TO WHILE Loop

4GL code:

/* REPEAT with TO and WHILE */
repeat x = 1 to 10 while y > 0 :
  ...
end.

Converted code:

/* REPEAT with TO and WHILE */
ToClause toClause0 = new ToClause(x, 1, 10);
LogicalExpression whileClause0 = () -> isGreaterThan(y, 0);

repeatToWhile("loopLabel1", toClause0, whileClause0, new Block((Body) () -> 
{
   ...
});

Details:

This loops with two constraints, meaning that it will iterate 10 times, starting with x as 1 and incrementing x on each iteration, ending with x as 10 BUT only so as long as y is a positive number. If y becomes 0 or negative before the 10 iterations are complete, then the loop will exit early. The test of these logical expressions is conjoined with the AND operator.

When both the TO and WHILE clauses are present, the BlockManager.repeatToWhile API call is emitted.

REPEAT PRESELECT Loop

4GL code:

/* REPEAT PRESELECT */
repeat preselect each book:
  ...
end.

Converted code:

/* REPEAT PRESELECT */
PreselectQuery query0 = new PreselectQuery();

repeat("loopLabel2", new Block((Init) () -> 
{
   RecordBuffer.openScope(book);
   query0.initialize(book, ((String) null), null, "book.bookId asc");
}, 
(Body) () -> 
{
   ...
}));

Details:

When queries are involved, the conversion of the REPEAT block will still use the BlockManager.repeat API call. As detailed in Part 5 of this book, the database processing is added, but there are no differences in the BlockManager API usage. Details on the use of the init() method of the Block class can be found in the Blocks chapter.

FOR Block/Loop

The following is the 4GL syntax for the FOR statement:

[ label : ]
FOR
   { [EACH | FIRST | LAST] record-phrase } [, [ EACH | FIRST | LAST ] record-phrase ] ...
   [ variable = expression1 TO expression2 [ BY k ]]
   [ WHILE expression ]
   [ TRANSACTION ]
   [ other-options ]

This chapter documents the conversion of the above syntax. A summary of support for the other-options is provided in the Block Options section below, but full details of these other options must be found elsewhere in this reference.

The FOR block is the most common statement for iterating through records (from one or more tables) which satisfy a certain condition. As with the DO and REPEAT statements, the TO and WHILE clauses, if used, get converted following the rules presented in the TO Clause and WHILE Clause sections of this chapter.

From the control flow perspective, the FOR block will either execute its body at most once (for the FOR, FOR FIRST or FOR LAST cases) or will execute it for every record returned by a FOR EACH query. This means that the FOR can be either a block or a loop. Where the DO loop iteration is driven by the use of TO or WHILE clauses, the looping in a FOR statement is driven by the database query that is associated with the statement. The use of TO or WHILE clauses can limit the looping behavior, but such usage will not create a loop where the FOR would normally be a block.

FOR Unique Block

4GL code:

for book where book-id = 0:
   ...
end.

Converted code:

RandomAccessQuery query0 = new RandomAccessQuery();

forBlock("blockLabel0", new Block((Init) () -> 
{
   RecordBuffer.openScope(book);
   query0.initialize(book, "book.bookId = 0", null, "book.bookId asc");
}, 
(Body) () -> 
{
   query0.unique();
   ...
}));

Details:

A FOR statement which has no FIRST, LAST or EACH qualifier before the record phrase is a block (not a loop) in which the record phrase must identify one and only one record in the referenced table. This is referred to as a FOR unique block. In this case, any code inside the block will execute zero or one times, depending on whether the record is found. An error is raised if more than one record matches the criteria.

The conversion processing will insert the query initialization into the init() method and in the absence of a TO or WHILE clause, the unique() method call on the query (which will return the record or raise an error) will be emitted as the first statement in the body() method. This is required to fully duplicate the 4GL condition handling and flow control behavior. Please see the Blocks chapter for full details.

When neither the TO or WHILE clauses are present, the BlockManager.forBlock API call is emitted.

FOR Unique TO Block

4GL code:

for book where book-id = 0
    x = 1 to 5:
   ...
end.

Converted code:

RandomAccessQuery query2 = new RandomAccessQuery();

ToClause toClause4 = new ToClause(x, 1, 5);

forBlockTo("blockLabel3", toClause4, new Block((Init) () -> 
{
   RecordBuffer.openScope(book);
   query2.initialize(book, "book.bookId = 0", null, "book.bookId asc");
}, 
(Enter) () -> 
{
   query2.unique();
}, 
(Body) () -> 
{
   ...
}));

Details:

A FOR statement which has no FIRST, LAST or EACH qualifier before the record phrase is a block (not a loop) in which the record phrase must identify one and only one record in the referenced table. This is referred to as a FOR unique block. In this case, any code inside the block will execute zero or one times, depending on whether the record is found. An error is raised if more than one record matches the criteria.

The conversion processing will insert the query initialization into the init() method and with the presence of a TO clause, the unique() method call on the query (which will return the record or raise an error) will be emitted in the enter() method. This is required to fully duplicate the 4GL condition handling and flow control behavior. Please see the Blocks chapter for full details.

The presence of the TO clause DOES NOT make this a loop. Instead, when the block is executed, if a record is found, then x would be set to 1 and it would never be incremented.

When the TO clause is present, the BlockManager.forBlockTo API call is emitted.

FOR Unique WHILE Block

4GL code:

for book where book-id = 0
    while y < 10:
   ...
end.

Converted code:

RandomAccessQuery query0 = new RandomAccessQuery();

LogicalOp whileClause0 = () -> isLessThan(y, 10);

forBlockWhile("blockLabel0", whileClause0, new Block((Init) () -> 
{
   RecordBuffer.openScope(book);
   query0.initialize(book, "book.bookId = 0", null, "book.bookId asc");
}, 
(Enter) () -> 
{
   query0.unique();
}, 
(Body) () -> 
{
   ...
}));

Details:

A FOR statement which has no FIRST, LAST or EACH qualifier before the record phrase is a block (not a loop) in which the record phrase must identify one and only one record in the referenced table. This is referred to as a FOR unique block. In this case, any code inside the block will execute zero or one times, depending on whether the record is found. An error is raised if more than one record matches the criteria.

The conversion processing will insert the query initialization into the init() method and with the presence of a WHILE clause, the unique() method call on the query (which will return the record or raise an error) will be emitted in the enter() method. This is required to fully duplicate the 4GL condition handling and flow control behavior. Please see the Blocks chapter for full details.

The presence of the WHILE clause DOES NOT make this a loop. Instead, before the block is executed, if a record is found, then y would be compared to 10 and if not less than that, the block would exit without executing.

When WHILE clause is present, the BlockManager.forBlockWhile API call is emitted.

FOR Unique TO WHILE Block

4GL code:

for book where book-id = 0
    x = 1 to 5
    while y < 10:
   ...
end.

Converted code:

RandomAccessQuery query0 = new RandomAccessQuery();

ToClause toClause0 = new ToClause(x, 1, 5);
LogicalOp whileClause0 = () -> isLessThan(y, 10);

forBlockToWhile("blockLabel0", toClause0, whileClause0, new Block((Init) () -> 
{
   RecordBuffer.openScope(book);
   query0.initialize(book, "book.bookId = 0", null, "book.bookId asc");
}, 
(Enter) () -> 
{
   query0.unique();
}, 
(Body) () -> 
{
   ...
}));

Details:

A FOR statement which has no FIRST, LAST or EACH qualifier before the record phrase is a block (not a loop) in which the record phrase must identify one and only one record in the referenced table. This is referred to as a FOR unique block. In this case, any code inside the block will execute zero or one times, depending on whether the record is found. An error is raised if more than one record matches the criteria.

The conversion processing will insert the query initialization into the init() method and with the presence of both TO and WHILE clauses, the unique() method call on the query (which will return the record or raise an error) will be emitted in the enter() method. This is required to fully duplicate the 4GL condition handling and flow control behavior. Please see the Blocks chapter for full details.

The presence of the TO and WHILE clauses DOES NOT make this a loop. Instead, before the block is executed, if a record is found, then y would be compared to 10 and if not less than that, the block would exit without executing. At the same time (after a record is found but before the block is executed), x would be set to 1 and it would never be incremented.

When both the TO and WHILE clauses are present, the BlockManager.forBlockToWhile API call is emitted.

FOR FIRST/LAST Block

4GL code:

for first book:
   ...
end.

Converted code:

RandomAccessQuery query0 = new RandomAccessQuery();

forBlock("blockLabel0", new Block((Init) () -> 
{
   RecordBuffer.openScope(book);
   query0.initialize(book, ((String) null), null, "book.bookId asc");
}, 
(Body) () -> 
{
   query0.first();
   ...
}));

Details:

A FOR statement which has a FIRST or LAST qualifier before the record phrase is a block (not a loop) in which the record phrase always identifies (at most) one and only one record in the referenced table. The WHERE clause can refine the criteria but it is also valid to just request the first or last record for a table. In this case, any code inside the block will execute zero or one times, depending on whether the record is found.

The conversion processing will insert the query initialization into the init() method and in the absence of a TO or WHILE clause, either the first() or last() method call on the query (which will return the record or raise an error) will be emitted as the first statement in the body() method. Other than the query retrieval method that is called, there is no other difference between a FOR FIRST and a FOR LAST. The placement of the query retrieval method call is required to fully duplicate the 4GL condition handling and flow control behavior. Please see the Blocks chapter for full details.

When neither the TO or WHILE clauses are present, the BlockManager.forBlock API call is emitted.

FOR FIRST/LAST TO Block

4GL code:

for first book
  x = 1 to 10:
   ...
end.

Converted code:

RandomAccessQuery query0 = new RandomAccessQuery();

ToClause toClause0 = new ToClause(x, 1, 10);

forBlockTo("blockLabel0", toClause0, new Block((Init) () -> 
{
   RecordBuffer.openScope(book);
   query0.initialize(book, ((String) null), null, "book.bookId asc");
}, 
(Enter) () -> 
{
   query0.first();
}, 
(Body) () -> 
{
   ...
}));

Details:

A FOR statement which has a FIRST or LAST qualifier before the record phrase is a block (not a loop) in which the record phrase always identifies (at most) one and only one record in the referenced table. The WHERE clause can refine the criteria but it is also valid to just request the first or last record for a table. In this case, any code inside the block will execute zero or one times, depending on whether the record is found.

The conversion processing will insert the query initialization into the init() method and with the presence of a TO clause, either the first() or last() method call on the query (which will return the record or raise an error) will be emitted in the enter() method. Other than the query retrieval method that is called, there is no other difference between a FOR FIRST TO and a FOR LAST TO. The placement of the query retrieval method call is required to fully duplicate the 4GL condition handling and flow control behavior. Please see the Blocks chapter for full details.

The presence of the TO clause DOES NOT make this a loop. Instead, when the block is executed, if a record is found, then x would be set to 1 and it would never be incremented.

When a TO clause is present, the BlockManager.forBlockTo API call is emitted.

FOR FIRST/LAST WHILE Block

4GL code:

for last book
  while y > 0 :
   ...
end.

Converted code:

RandomAccessQuery query0 = new RandomAccessQuery();

LogicalOp whileClause0 = () -> isGreaterThan(y, 0);

forBlockWhile("blockLabel0", whileClause0, new Block((Init) () -> 
{
   RecordBuffer.openScope(book);
   query0.initialize(book, ((String) null), null, "book.bookId asc");
}, 
(Enter) () -> 
{
   query0.last();
}, 
(Body) () -> 
{
   ...
}));

Details:

A FOR statement which has a FIRST or LAST qualifier before the record phrase is a block (not a loop) in which the record phrase always identifies (at most) one and only one record in the referenced table. The WHERE clause can refine the criteria but it is also valid to just request the first or last record for a table. In this case, any code inside the block will execute zero or one times, depending on whether the record is found.

The conversion processing will insert the query initialization into the init() method and with the presence of a WHILE clause, either the first() or last() method call on the query (which will return the record or raise an error) will be emitted in the enter() method. Other than the query retrieval method that is called, there is no other difference between a FOR FIRST WHILE and a FOR LAST WHILE. The placement of the query retrieval method call is required to fully duplicate the 4GL condition handling and flow control behavior. Please see the Blocks chapter for full details.

The presence of the WHILE clause DOES NOT make this a loop. Instead, before the block is executed, if a record is found, then y would be compared to 0 and if not greater than that, the block would exit without executing.

When a WHILE clause is present, the BlockManager.forBlockWhile API call is emitted.

FOR FIRST/LAST TO WHILE Block

4GL code:

for last book
  x = 1 to 10
  while y > 0:
   ...
end.

Converted code:

RandomAccessQuery query0 = new RandomAccessQuery();

ToClause toClause0 = new ToClause(x, 1, 10);
LogicalOp whileClause0 = () -> isGreaterThan(y, 0);

forBlockToWhile("blockLabel0", toClause0, whileClause0, new Block((Init) () -> 
{
   RecordBuffer.openScope(book);
   query0.initialize(book, ((String) null), null, "book.bookId asc");
}, 
(Enter) () -> 
{
   query0.last();
}, 
(Body) () -> 
{
   ...
}));

Details:

A FOR statement which has a FIRST or LAST qualifier before the record phrase is a block (not a loop) in which the record phrase always identifies (at most) one and only one record in the referenced table. The WHERE clause can refine the criteria but it is also valid to just request the first or last record for a table. In this case, any code inside the block will execute zero or one times, depending on whether the record is found.

The conversion processing will insert the query initialization into the init() method and with the presence of both the TO and WHILE clauses, either the first() or last() method call on the query (which will return the record or raise an error) will be emitted in the enter() method. Other than the query retrieval method that is called, there is no other difference between a FOR FIRST TO WHILE and a FOR LAST TO WHILE. The placement of the query retrieval method call is required to fully duplicate the 4GL condition handling and flow control behavior. Please see the Blocks chapter for full details.

The presence of the TO and WHILE clauses DOES NOT make this a loop. Instead, before the block is executed, if a record is found, then y would be compared to 0 and if not greater than that, the block would exit without executing. At the same time (after a record is found but before the block is executed), x would be set to 1 and it would never be incremented.

When both the TO and WHILE clauses are present, the BlockManager.forBlockToWhile API call is emitted.

FOR FIRST/LAST Multi-Table Block

4GL code:

for first book, last person:
   ...
end.

Converted code:

CompoundQuery query0 = new CompoundQuery();

forBlock("blockLabel0", new Block((Init) () -> 
{
   RecordBuffer.openScope(book, person);
   query0.initialize(false, false);
   query0.addComponent(new RandomAccessQuery().initialize(book, ((String) null), null, 
                                                          "book.bookId asc"), 
                                                          QueryConstants.FIRST);
   query0.addComponent(new RandomAccessQuery().initialize(person, ((String) null), null, 
                                                          "person.siteId asc, person.empNum asc"), 
                                                          QueryConstants.LAST);
}, 
(Body) () -> 
{
   query0.iterate();
   ...
}));

Details:

A FOR statement which references multiple tables where none of the qualifiers before the record phrase is an EACH, is a block (not a loop) in which each record phrase always identifies (at most) one and only one record in the referenced table. In the above example, this is a FOR FIRST table1, LAST table2 statement. The number of record phrases can be arbitrarily large. So long as there is no EACH qualifier, then this is a block that executes (at most) one time. The WHERE clause can refine the criteria but it is also valid to just request the first or last record for a table. In this case, any code inside the block will execute zero or one times, depending on whether the records are found.

The conversion processing will insert the query initialization into the init() method and in the absence of a TO or WHILE clause, the iterate() method call on the query (which will return the records or raise an error) will be emitted as the first statement in the body() method. The result of the single iterate() method call is that each referenced buffer will have a record in it if all of the record phrases successfully identified a record. Although the retrieval method is called iterate(), that method is only ever called once in the FOR block case. The placement of the query retrieval method call is required to fully duplicate the 4GL condition handling and flow control behavior. Please see the Blocks chapter for full details.

When neither the TO or WHILE clauses are present, the BlockManager.forBlock API call is emitted.

From a block management and control flow perspective, the multi-table case is no different than a regular FOR FIRST/LAST. The conversion process does emit the database processing differently, but the block management is unaware of this fact and the control flow does not change based on this.

FOR EACH Loop

4GL code:

for each book:
   ...
end.

Converted code:

AdaptiveQuery query0 = new AdaptiveQuery();

forEach("loopLabel0", new Block((Init) () -> 
{
   RecordBuffer.openScope(book);
   query0.initialize(book, ((String) null), null, "book.bookId asc");
}, 
(Body) () -> 
{
   query0.next();
   ...
}));

Details:

A FOR statement which has an EACH qualifier before the record phrase is a loop in which the record phrase identifies zero or more records in the referenced table. The WHERE clause refines the criteria and the BY clause largely determines the order the records are returned. Any code inside the block will execute once for every record that is found, except if conditions are raised or flow control statements are executed that cause the loop to be exited.

The conversion processing will insert the query initialization into the init() method and in the absence of a TO or WHILE clause, the next() method call on the query (which will return the record or raise an error) will be emitted as the first statement in the body() method. The placement of the query retrieval method call is required to fully duplicate the 4GL condition handling and flow control behavior. Please see the Blocks chapter for full details.

When neither the TO or WHILE clauses are present, the BlockManager.forEach API call is emitted.

FOR EACH TO Loop

4GL code:

for each book
  x = 1 to 10:
   ...
end.

Converted code:

AdaptiveQuery query0 = new AdaptiveQuery();

ToClause toClause0 = new ToClause(x, 1, 10);

forEachTo("loopLabel0", toClause0, new Block((Init) () -> 
{
   RecordBuffer.openScope(book);
   query0.initialize(book, ((String) null), null, "book.bookId asc");
}, 
(Enter) () -> 
{
   query0.next();
}, 
(Body) () -> 
{
   ...
}));

Details:

A FOR statement which has an EACH qualifier before the record phrase is a loop in which the record phrase identifies zero or more records in the referenced table. The WHERE clause refines the criteria and the BY clause largely determines the order the records are returned. Any code inside the block will execute once for every record that is found, except if conditions are raised or flow control statements are executed that cause the loop to be exited.

The conversion processing will insert the query initialization into the init() method and in the presence of a TO clause, the next() method call on the query (which will return the record or raise an error) will be emitted in the enter() method. The placement of the query retrieval method call is required to fully duplicate the 4GL condition handling and flow control behavior. Please see the Blocks chapter for full details.

The presence of the TO clause limits the loop processing. Each time the loop is entered (the first time and then after each iteration), if a record is found, then x would increment from 1 to 10. After the 10th iteration, the loop will exit, even if there are more records. Likewise, if there are less than 10 records, the loop will exit before the TO clause can complete.

When the TO clause is present, the BlockManager.forEachTo API call is emitted.

FOR EACH WHILE Loop

4GL code:

for each book
  while y > 0:
   ...
end.

Converted code:

AdaptiveQuery query0 = new AdaptiveQuery();

LogicalOp whileClause0 = () -> isGreaterThan(y, 0);

forEachWhile("loopLabel0", whileClause0, new Block((Init) () -> 
{
   RecordBuffer.openScope(book);
   query0.initialize(book, ((String) null), null, "book.bookId asc");
}, 
(Enter) () -> 
{
   query0.next();
}, 
(Body) () -> 
{
   ...
}));

Details:

A FOR statement which has an EACH qualifier before the record phrase is a loop in which the record phrase identifies zero or more records in the referenced table. The WHERE clause refines the criteria and the BY clause largely determines the order the records are returned. Any code inside the block will execute once for every record that is found, except if conditions are raised or flow control statements are executed that cause the loop to be exited.

The conversion processing will insert the query initialization into the init() method and in the presence of a WHILE clause, the next() method call on the query (which will return the record or raise an error) will be emitted in the enter() method. The placement of the query retrieval method call is required to fully duplicate the 4GL condition handling and flow control behavior. Please see the Blocks chapter for full details.

The presence of the WHILE clause limits the loop processing. Each time the loop is entered (the first time and then after each iteration), if a record is found, then y is compared to 0 and the loop will exit if it is not greater than that value. The first time the expression evaluates false, the loop will exit, even if there are more records. Likewise, if the records run out before the WHILE clause evaluates false, the loop will still exit.

When the WHILE clause is present, the BlockManager.forEachWhile API call is emitted.

FOR EACH TO WHILE Loop

4GL code:

for each book
  x = 1 to 10
  while y > 0:
   ...
end.

Converted code:

AdaptiveQuery query0 = new AdaptiveQuery();

ToClause toClause0 = new ToClause(x, 1, 10);
LogicalOp whileClause0 = () -> isGreaterThan(y, 0);

forEachToWhile("loopLabel0", toClause0, whileClause0, new Block((Init) () -> 
{
   RecordBuffer.openScope(book);
   query0.initialize(book, ((String) null), null, "book.bookId asc");
}, 
(Enter) () -> 
{
   query0.next();
}, 
(Body) () -> 
{
   ...
}));

Details:

A FOR statement which has an EACH qualifier before the record phrase is a loop in which the record phrase identifies zero or more records in the referenced table. The WHERE clause refines the criteria and the BY clause largely determines the order the records are returned. Any code inside the block will execute once for every record that is found, except if conditions are raised or flow control statements are executed that cause the loop to be exited.

The conversion processing will insert the query initialization into the init() method and in the presence of both the TO and WHILE clauses, the next() method call on the query (which will return the record or raise an error) will be emitted in the enter() method. The placement of the query retrieval method call is required to fully duplicate the 4GL condition handling and flow control behavior. Please see the Blocks chapter for full details.

The presence of the TO clause limits the loop processing. Each time the loop is entered (the first time and then after each iteration), if a record is found, then x would increment from 1 to 10. After the 10th iteration, the loop will exit, even if there are more records. Likewise, if there are less than 10 records, the loop will exit before the TO clause can complete.

The presence of the WHILE clause limits the loop processing. Each time the loop is entered (the first time and then after each iteration), if a record is found, then y is compared to 0 and the loop will exit if it is not greater than that value. The first time the expression evaluates false, the loop will exit, even if there are more records. Likewise, if the records run out before the WHILE clause evaluates false, the loop will still exit.

The evaluation of the TO and WHILE clauses is done using the AND operator, which means that both limits must be met for the loop to continue.

When both the TO and WHILE clauses are present, the BlockManager.forEachToWhile API call is emitted.

FOR EACH Multi-Table Loop

4GL code:

for each person, last address:
   ...
end.

Converted code:

CompoundQuery query0 = new CompoundQuery();

forEach("loopLabel0", new Block((Init) () -> 
{
   RecordBuffer.openScope(person, address);
   query0.initialize(false, false);
   query0.addComponent(new AdaptiveQuery().initialize(person, ((String) null), null, 
                                                      "person.siteId asc, person.empNum asc"));
   query0.addComponent(new RandomAccessQuery().initialize(address, ((String) null), null, 
                                                          "address.addrId asc"), 
                                                          QueryConstants.LAST);
}, 
(Body) () -> 
{
   query0.iterate();
   ...
}));

Details:

A FOR statement which references multiple tables where at least one of the qualifiers before the record phrase is an EACH, is a loop in which each record phrase identifies zero or more records in the referenced tables. In the above example, this is a FOR EACH table1, LAST table2 statement. The number of record phrases can be arbitrarily large. The WHERE clause refines the criteria and the BY clause largely determines the order the records are returned. Any code inside the loop will execute so long as the records are being retrieved, except if conditions are raised or flow control statements are executed that cause the loop to be exited.

The conversion processing will insert the query initialization into the init() method and in the absence of a TO or WHILE clause, the iterate() method call on the query (which will return the records or raise an error) will be emitted as the first statement in the body() method. The result of the single iterate() method call is that each referenced buffer will have a record in it if all of the record phrases successfully identified a record. Every time the loop is entered (the first time in and on every iteration), the iterate() method will be called and the loop body will process so long as records are retrieved. The placement of the query retrieval method call is required to fully duplicate the 4GL condition handling and flow control behavior. Please see the Blocks chapter for full details.

When neither the TO or WHILE clauses are present, the BlockManager.forEach API call is emitted.

From a block management and control flow perspective, the multi-table case is no different than a regular FOR EACH. The conversion process does emit the database processing differently, but the block management is unaware of this fact and the control flow does not change based on this.

Labels

Inner blocks (all forms of DO, REPEAT, FOR and EDITING) can be labeled and that label can then be referenced in language statements that explicitly change the flow of control such as UNDO, LEAVE or NEXT statements and in ON phrases. In 4GL labels are specified before a block, as in: label: <BLOCK>. If a block is converted using a Java statement then a standard Java label is used, as in: label: [statement] { ... }.

Example 1:

label1:
do:
   message "message 1".
end.

label2:
do while true:
   message "message 2".
   leave.
end.

Converted code:

label1:
{
   message("message 1");
}

label2:
while (true)
{
   message("message 2");
   break label2;
}

Details:

The first block is converted to a simple Java block, to which the label is associated (label1 in this case).

The second block converts to a while statement, which also has an associated label (label2 in this case). The LEAVE statement, though unlabeled in the 4GL is explicitly matched to the same label that would be chosen at runtime in the 4GL. For details on how this implicit label reference is resolved, please see the section entitled Determining the Target and Meaning of UNDO, LEAVE, NEXT and RETRY in the Blocks chapter.

Example 2:

some-label:
repeat:
   message "message 1".
   leave.
end.

Converted code:

repeat("someLabel", new Block((Body) () -> 
{
   message("message 1");
   leave("someLabel");
}));

Details:

When a block is converted to an API call provided by the BlockManager, then the label is specified as one of the parameters of this API.

The label name gets converted following the same conversion rules as the 4GL variable names. For details about how they get converted, please see the Naming section of the Other Customization chapter from the FWD Conversion Handbook.

During conversion, each block which doesn't have set an explicit label will have an implicit label added. Depending on the type of block (looping or not), the implicit label names will follow these rules:

Block Type Implicit Label Name Details
Looping block:
     • DO [ TO ] [ WHILE ]
     • FOR EACH
     • REPEAT
loopLabel<index> All looping blocks without an explicit label will be implicitly labeled. The <index> starts from zero and counts, from top to bottom in the procedure file, all looping blocks without an explicit label.
The DO block is considered a looping block only if it contains at least one of the TO or WHILE clauses.
A non-looping block:
     • DO
     • FOR
     • FOR FIRST
     • FOR LAST
blockLabel<index> All non-top level blocks which do not loop will be implicitly labeled. The <index> starts from zero and counts, from top to bottom in the procedure file, all non- top level and non-looping blocks without an explicit label.
EDITING block editingLabel<index> All EDITING blocks without an explicit label will be implicitly labeled. The <index> starts from zero and counts, from top to bottom in the procedure file, all editing blocks without an explicit label.

Example 3:

do:
   x = x + 1.
end.

Converted code:

blockLabel0:
{
   x.assign(plus(x, 1));
}

Details:

A simple DO without any of the TO or WHILE clauses is implicitly labeled using the blockLabel<index> rule.

Example 4:

repeat:
   x = x + 1.
   leave.
end.

Converted code:

repeat("loopLabel0", new Block((Body) () -> 
{
   x.assign(plus(x, 1));
   leave("loopLabel0");
}));

Details:

A non-labeled REPEAT block will be implicitly labeled using the loopLabel<index> rule, as this is a looping block.

TO Clause

The TO clause adds a looping variable to a DO, FOR or REPEAT block. If this variable reaches its specified boundary value, the block exits. Its syntax is:

variable = expression1 TO expression2 [ BY k ]

where the 4GL parameters are:

  • variable - identifies the name of a variable whose value you are changing in a loop. It can be of an integer, decimal or date type.
  • expression1 - an expression which is evaluated on the first iteration of the loop and initializes the looping variable. The type of the expression must be assignment compatible with the variable.
  • expression2 - expression which represents the boundary value for the variable. It gets evaluated on each iteration. When the variable exceeds the value of this expression (or is less than it if the step k is negative), the loop ends. Since expression1 is compared to expression2 at the start of the first iteration of the block, the block can be executed 0 times. The type of the expression must be comparable with the variable.
  • k - loop step, the amount to add to the variable after each iteration. This must be a constant. It can be either positive or negative. The default value is 1 (if the variable is of date type that means 1 day).

For the simple DO ... TO ... block, the TO is converted using a for Java loop.

When non-simple DO blocks are being converted, the TO clause is converted using a ToClause object which is passed to a the BlockManager API.

for Java Loop

When a simple DO block has a TO clause, it gets converted using the Java for loop, which will have the following format:

<label>:
for (<variable>.assign(<expression1>);
     _is<Less/Greater>ThanOrEqual(<variable>, <expression2>);
     <increment_logic>)
{
   ...
}

where <increment_logic> can be:

  • <variable>.increment() is emitted in cases when the BY option is not specified or is set to 1.
  • <variable>.decrement() is emitted in cases when the BY option is specified or is set to -1.
  • <variable>.assign(plus(<variable>, <k>)) is emitted in cases when the BY option is specified and is set to a value which is not 1 or -1.
  • <variable>.assign(plusDays(<variable>, <k>)) is emitted in cases when a date variable is used and the BY option is specified with a value which is not 1 or -1.

Example 4:

/* example with constants */
do x = 1 to 10 by 5:
   ...
end.

Converted code:

/* example with constants */
for (x.assign(1); _isLessThanOrEqual(x, 10); x.assign(plus(x, 5)))
{
   ...
}

Details:

As constants are used for an integer variable, the Java's for statement will have as clauses the converted code which will set the initial value for the loop variable (x.assign(1)), the expression for the termination value (_isLessThanOrEqual(x, 10)) and the increment logic (x.assign(plus(x, 5))).

The use of _isLessThanOrEqual() is required because the increment value is positive.

Example 5:

/* example with expressions */
do x = y + 4.4 to (y + z) / 1.5 by -1:
   ...
end.

Converted code:

/* example with expressions */
for (x.assign(plus(y, decimal.fromLiteral("4.4"))); _isGreaterThanOrEqual(x, divide(plus(y, z), 1.5)); x.decrement())
{
   ...
}

Details:

For complex expression cases the converted code is the same, except that the initial value and the end value will be determined by evaluating the corresponding expression.

The use of _isGreaterThanOrEqual() is required because the increment value is negative.

Example 6:

/* example with dates */
do dat = 01/01/2012 to date2 by -2:
   ...
end.

Converted code:

/* example with dates */
for (dat.assign(date.fromLiteral("01/01/2012"));
     _isGreaterThanOrEqual(dat, date2);
     dat.assign(date.plusDays(dat, -2)))
{
   ...
}

Details:

When date values are used and the step is not 1 or -1, the date.plusDays is used to change the loop variable after each iteration.

The use of _isGreaterThanOrEqual() is required because the increment value is negative.

ToClause

If a block is converted to a block function provided by the BlockManager class, a ToClause helper object is created. It is passed to as a parameter to the BlockManager.doTo[While], BlockManager.forBlockTo[While], BlockManager.forEachTo[While] or BlockManager.repeatTo[While] APIs, as the to parameter .

The ToClause constructor takes the following parameters, each corresponding to 4GL parameters:

  • variable - variable of integer, decimal or date data type.
  • expression1 - arbitrary expression (e.g. plus(x, 1)) or constant value.
  • expression2 - constant, variable or arbitrary expression. If it is a constant or a variable then it is directly passed to the ToClause constructor. Expressions are defined in a more complicated way. Because, unlike the expression1, which is evaluated once and before the first iteration, expression2 should be re-evaluated on each iteration and therefore it is represented, depending on the type of the looping variable, by IntegerExpression, DecimalExpression or DateExpression. Instances of these classes allow the expression evaluation to be deferred and to be delegated to the BlockManager. The expression is defined by overriding the execute() function of the appropriate *Expression class. This allows the expression to access the latest version of the business logic's state (e.g. fields or other variables), as this state may be modified during the block iteration.
  • k - this is an optional, integer or decimal constant, representing the step used to adjust the variable, after each iteration.

The constructors defined by the ToClause class are a bit wider than it is described in this section, but the converted code fits into the described set of parameters.

In cases when the boundary expression (expression2) is not a constant or logical variable, then it is converted using an anonymous implementation of the DecimalExpression, IntegerExpression or DateExpression classes, following this format:

[Decimal|Expression|Integer]Expression resExpr<index> = new [Decimal|Expression|Integer]Expression()
{
   public <type> execute()
   {
      return <expression2>;
   }
};

where the <index> is a counter for all complex expression encountered associated with a TO clause boundary in the current 4GL program, from top to bottom (starting from 0) and the <type> depends on the type of the looping variable (is either decimal, integer or date).

Each ToClause instance will be named using the toClause<index> format, where <index> is a counter for all ToClause objects emitted in this 4GL program, from top to bottom.

Example 1:

repeat x = 1 to 10 by 5:
   ...
end.

Converted code:

ToClause toClause0 = new ToClause(x, 1, 10, 5);

repeatTo("loopLabel0", toClause0, new Block((Body) () -> 
{
   ...
}));

Details:

Constants are used to specify the initial value, the boundary and the step. So, the ToClause constructor will have as parameters a variable and constants for the initial value, boundary and step.

Example 2:

repeat x = y + 4.4 to (y + z) / 1.5:
   ...
end.

Converted code:

DecimalExpression resExpr0 = new DecimalExpression()
{
   public decimal execute()
   {
      return divide(plus(y, z), 1.5);
   }
};

ToClause toClause0 = new ToClause(x, plus(y, decimal.fromLiteral("4.4")), resExpr0);

repeatTo("loopLabel0", toClause0, new Block((Body) () ->
{
   ...
}));

Details:

As a complex expression is used for the loop termination condition, a DecimalExpression object is constructed, with its execute() method overridden so that it will be evaluated before each iteration; the result of this method will be compared against the variable's current value.

Example 3:

repeat dat = 01/01/2012 to date2 by -2:
   ...
end.

Converted code:

ToClause toClause0 = new ToClause(dat, date.fromLiteral("01/01/2012"), date2, new integer(-2));

repeatTo("loopLabel0", toClause0, new Block((Body) () -> 
{
   ...
}));

Details:

When date values are used, the converted code looks similar as when integer or decimal values are used, except the conversion of the literals will be done as appropriate for the date type.

WHILE Clause

A WHILE clause indicates the condition which allows a DO, FOR or REPEAT loop to continue execution of its next iteration. The loop iterates as long as the condition specified by the expression evaluates to true. The expression can be arbitrary, but should evaluate to a logical value. Its syntax is:

WHILE expression

and it can be converted using:

  • a while Java cycle (for simple DO ... WHILE ... loops);
  • as a part of cycle completion condition with a for Java loop (for simple DO ... TO ... WHILE ... loops);
  • with non-simple blocks, a LogicalExpression object passed to a block API provided by the BlockManager.
WHILE as a while Java Loop

The Java while statement is emitted only in cases when a simple DO block has a WHILE clause. Even when complex expressions are encountered, in this case the 4GL expression will be emitted directly in the converted while loop's expression, instead of using a LogicalExpression delegate.

Since the Java flow control statements cannot interpret an expression of type logical, the result of the expression evaluation must be “unwrapped” to a boolean type using booleanValue() or by using a version of a runtime method that directly returns a boolean instead of a logical.

Example 1:

do while l:
   ...
end.

Converted code:

while ((l).booleanValue())
{
   ...
}

Details:

As the loop condition is a simple variable, the Java's WHILE statement is emitted, with the expression emitted directly as the converted while's clause.

Example 2:

do while x + 5 < y - 2:
   ...
end.

Converted code:

while (_isLessThan(plus(x, 5), minus(y, 2)))
{
   ...
}

Details:

Even with a complex expression, the loop termination condition is emitted directly in the converted WHILE statement. In this case, the method that implements the less than operator has a version that directly returns a boolean instead of a logical.

WHILE as a for Java Loop

In cases when a simple DO statement has both the TO and WHILE clauses, it will be converted using Java for loop, with the converted WHILE expression joined with the converted boundary condition of the TO clause using the _and operator.

Example 3:

do x = 1 to y + 1 while l:
   ...
end.

Converted code:

for (x.assign(1); _and(_isLessThanOrEqual(x, plus(y, 1)), l); x.increment())
{
   ...
}

Details:

In this case, the WHILE condition is represented by a simple variable, which gets joined with the TO termination expression using the _and operator.

Example 4:

do x = y to 10 while x + 5 < y - 2:
   ...
end.

Converted code:

for (x.assign(y);
     _and(_isLessThanOrEqual(x, 10), () -> isLessThan(plus(x, 5), minus(y, 2)));
     x.increment())
{
   ...
}

Details:

Even in cases when the WHILE condition is represented by a complex expression, it still gets joined with the TO termination expression using the _and operator.

WHILE as a LogicalExpression

If a block was converted using a BlockManager API, then a LogicalExpression is created in order to represent the WHILE expression. It is passed to the BlockManager.do[To]While, BlockManager.forBlock[To]While, BlockManager.forEach[To]While or BlockManager.repeat[To]While APIs as the expr parameter.

In cases when the expression is not a constant or a logical variable, then it is converted using an anonymous implementation of the LogicalExpression class, following this format:

LogicalExpression whileClause<index> = new LogicalExpression()
{
   public logical execute()
   {
      return <expression>;
   }
};

where the <index> is a counter for all complex logical expressions encountered in the current 4GL program, from top to bottom (starting from 0) and the <expression> is the converted 4GL WHILE clause, which gets evaluated on first entry and after each loop iteration completes.

In cases where both a ToClause and a WHILE LogicalExpression instance are passed to a BlockManager API, the testing of the ToClause boundary expression and WHILE expression will be combined using the AND operator (_and). This is hidden inside the delegation logic of the BlockManager, but it is important to know so that the behavior of the flow control logic can be predicted.

Example 5:

repeat while l:
   ...
end.

Converted code:

repeatWhile("loopLabel0", () -> l, new Block()
{
   ...
});

Details:

If the target expression is a logical constant or a logical variable, then it is directly passed to the LogicalExpression constructor instead of emitting as an inner class. This is a minor optimization since the variable can be directly de-referenced by the delegate.

Example 6:

repeat while x + 5 < y - 2:
   ...
end.

Converted code:

LogicalOp whileClause0 = () -> isLessThan(plus(x, 5), minus(y, 2));

repeatWhile("loopLabel0", whileClause0, new Block((Body) () -> 
{
   ...
}));

Details:

If the target expression is more complex than a single variable or constant, it is defined by overriding the execute() method of the LogicalExpression anonymous implementation. The whileClause0.execute() method will be invoked on first entry and after each iteration and if it returns a true value, the loop body will be executed.

Block Options

The following is a summary of the support for DO, REPEAT and FOR inner block options. EDITING blocks have no block options in the 4GL or Java.

Option Blocks Description Supported
[BREAK] BY { expression | COLLATE (string , strength [, collation ]) } [DESCENDING] ... FOR Implements a sorting specification for the query and optionally associates break-groups. See the Query Sorting chapter in Part 5. Yes, except for the COLLATE option.
FOR record [ , record ] ... DO, REPEAT Causes a strong buffer scope to be associated with the given block. See the Record Buffer Definition and Scoping chapter in Part 5. Yes
ON { ENDKEY | ERROR | QUIT | STOP } spec all Specifies how the inner block is to react to the given condition. See the Blocks chapter for full details. Yes
preselect-phrase DO, REPEAT Creates a preselect query that can retrieve records from one or more tables and associates it with the given block. See the Queries chapter in Part 5 for more details. Yes
query-tuning-phrase all Provides some control over queries backed by a Progress DataServer. FWD supports all databases with the same converted application code. As a general principle, there is no need to convert DataServer-specific code. At this time no reason has been found to convert this. No
variable = expression1 TO expression2 [BY k] all See the TO Clause section above. Yes
STOP-AFTER expression DO, REPEAT, FOR Raises the STOP condition when the block runs longer than the given number of seconds. See the STOP-AFTER Option section below. Yes
TRANSACTION all Causes the given block to have full transaction support. See the Transactions chapter for more details. Yes
WHILE expression all See the WHILE Clause section above. Yes
WITH frame-phrase all Scopes the given frame to the associated block. For more details, see the Frame Scoping section of the Frames chapter in Part 6. Yes

STOP-AFTER Option

The STOP-AFTER option of a DO, REPEAT or FOR block limits the time the block (including the routines it calls) may run:

DO STOP-AFTER expression:
REPEAT STOP-AFTER expression:
FOR EACH record-phrase STOP-AFTER expression:

expression is the time limit in seconds. When it expires, the STOP condition is raised in the code which is running at that moment. FWD raises it at the next block boundary (the start or the end of a block body, so in a loop at the start of its next iteration).

The option converts to a BlockManager.stopAfter(seconds) call emitted as the first statement of the block's body; the block itself is converted using the BlockManager APIs, since the time-out needs the block to be managed. The timer is cancelled when the block ends.

4GL code:

do stop-after 1:
   repeat:
      k = k + 1.
   end.
end.

Converted code:

doBlock("blockLabel2", new Block((Body) () ->
{
   stopAfter(1);

   repeat("loopLabel1", new Block((Body) () ->
   {
      k.assign(plus(k, 1));
   }));
}));

Behavior (OpenEdge 12.8):

  • The STOP of the time-out is handled like any other STOP: by an ON STOP phrase of the STOP-AFTER block itself, of a block nested in it (the REPEAT loop in the example, if it had ON STOP UNDO, LEAVE), of an enclosing block or of a caller, or by a CATCH block for Progress.Lang.StopAfter (or Progress.Lang.Stop) reached before an ON STOP phrase (see Stop Objects). When a block nested in the STOP-AFTER block handles it, execution goes on after that nested block, inside the STOP-AFTER block.
  • FINALLY blocks of the blocks the STOP leaves run (see FINALLY Block).
  • After the time-out was handled, the STOP-AFTER block can be entered again and its timer fires again. FWD releases the timer when the block ends, also when the time-out was handled by an enclosing block or a caller.
  • OpenEdge timing: when the STOP-AFTER block contains a plain REPEAT loop and nothing inside the block handles the STOP, OpenEdge 12.8 sometimes ends the STOP at the STOP-AFTER block itself (the statement after it runs) and sometimes passes it on, from one run of the same program to the next. FWD always passes it to the enclosing handler, which is what OpenEdge does in most runs and in every deterministic case (DO WHILE, a single long statement, a loop with its own ON STOP phrase).

Loop Behavior After a Condition

This section summarizes what happens to an inner block when a condition (ERROR, ENDKEY, STOP or QUIT) is raised in it, when no statement in the block waits for user input. The conversion does not change with these rules: they are implemented by the BlockManager at runtime. For the details of condition processing, see the Conditions section of the Blocks chapter.

Default handling of an ERROR that is not handled by an ON ERROR phrase or a CATCH:

Block Default level, ROUTINE-LEVEL BLOCK-LEVEL
DO, DO ... TO, DO WHILE without TRANSACTION or ON phrase No handling of its own: the error goes to the enclosing block. UNDO, THROW to the enclosing block.
DO ... TO ... TRANSACTION, REPEAT ... TO, FOR EACH UNDO, RETRY, performed as NEXT: the loop goes on with the next iteration. UNDO, THROW to the enclosing block.
DO WHILE ... TRANSACTION, REPEAT without TO, REPEAT WHILE UNDO, RETRY, performed as LEAVE: the loop is left. UNDO, THROW to the enclosing block.
Routine block (procedure, function, method) The routine ends normally and the caller continues; the caller sees no error. OUTPUT parameters are copied back. The error message is written to the current output destination. The error object reaches the caller unchanged (ROUTINE-LEVEL does the same for the routine block).

Rules that apply to all inner blocks:

  • RETRY is not performed when no statement waits for input. UNDO, RETRY (explicit, from an ON phrase or as the default action) acts as NEXT in a counted loop (DO ... TO, REPEAT ... TO, FOR EACH) and as LEAVE in a single DO block and in a loop without a counter. UNDO label, RETRY label acts as NEXT label on an enclosing counted loop. The iteration is never executed a second time.
  • A loop without a counter is left after any undo of an iteration. DO WHILE, REPEAT WHILE and REPEAT without TO are left after an error (also when a CATCH or an ON ERROR UNDO, NEXT phrase handles it), after UNDO, NEXT and after UNDO, RETRY. This happens even when nothing was rolled back and the WHILE condition is still true; the condition is not evaluated again and the statement after the loop runs. A plain NEXT (without UNDO) continues the loop. Exception (OpenEdge 11.6): after ENDKEY, the phrases ON ENDKEY UNDO, NEXT and ON ENDKEY UNDO, RETRY continue a DO WHILE loop.
  • When a CATCH handles the error, the block's ON ERROR phrase is not applied. After the CATCH ends normally, a counted loop goes on with the next iteration (even with ON ERROR UNDO, LEAVE), and a loop without a counter is left (previous rule).
  • ON ERROR UNDO, THROW at the default level or at ROUTINE-LEVEL throws the error to the enclosing block, whose own handling then applies (for an enclosing REPEAT ... TO: the next iteration of that loop).
  • ON <condition> UNDO, LEAVE / NEXT / LEAVE label / NEXT label behave the same for ERROR, system errors, RETURN ERROR from a called procedure, STOP, QUIT and ENDKEY, at every error level. UNDO, NEXT on a single DO block acts as LEAVE.
  • ON <condition> UNDO, RETURN ends the routine; the caller sees no error and RETURN-VALUE is "". The condition is consumed, also a STOP, QUIT or ENDKEY.
  • ON <condition> UNDO, RETURN ERROR raises a new, empty Progress.Lang.AppError in the caller (0 messages, message number 0, ReturnValue ""). The original error object and the callee's RETURN-VALUE are not passed on. A STOP, QUIT or ENDKEY handled this way also reaches the caller as this ERROR.
  • STOP and QUIT are handled by ON STOP / ON QUIT phrases; the default ON ERROR processing does not handle them, and neither does NO-ERROR. A CATCH block handles a STOP only as a stop object (OpenEdge 11.7 and later with -catchStop, always in OpenEdge 12; see Stop Objects); there is no object for QUIT. Without a matching phrase they leave the routine and reach the caller's phrase.
  • ENDKEY without a matching phrase: REPEAT and FOR EACH have a default ON ENDKEY UNDO, LEAVE; DO blocks have none, so the enclosing block's default applies.

What is undone:

  • An undo restores undoable variables and records only when a transaction is active: a TRANSACTION block, or a transaction started by database updates. Without an active transaction (for example, a block that works only on temp-tables), nothing is undone, not even undoable variables, object properties or undoable temp-table records. Records of NO-UNDO temp-tables are never undone.
  • Inside an enclosing transaction, each iteration of an inner block is a sub-transaction: only the failing iteration is undone, and the work of the other iterations is kept.
  • The failing iteration is undone before its CATCH and FINALLY run. Changes made in a CATCH or FINALLY are kept, even when the handler then raises an error or executes NEXT, LEAVE, UNDO or RETURN ERROR. They are undone only by a DO TRANSACTION inside the handler or by the undo of an enclosing transaction. The TRANSACTION function returns no inside a CATCH or FINALLY, unless an enclosing transaction is active.
  • STOP rolls back the active transaction; QUIT commits it.

BLOCK-LEVEL and ROUTINE-LEVEL Statements

Change the default error action of the blocks in a procedure or class file. 4GL syntax:

BLOCK-LEVEL ON ERROR UNDO, THROW.
ROUTINE-LEVEL ON ERROR UNDO, THROW.

The statement must appear before any executable statement of the file. It converts to a call as the first statement of the external procedure's body:

4GL code:

block-level on error undo, throw.

Converted code:

externalProcedure(Test.this, new Block((Body) () ->
{
   onBlockLevel(Condition.ERROR, Action.THROW);
   ...
}));

ROUTINE-LEVEL ON ERROR UNDO, THROW converts to onRoutineLevel(Condition.ERROR, Action.THROW) in the same place.

Behavior:

  • BLOCK-LEVEL: every block of the file that has no ON ERROR phrase (and every block whose ON phrases are for other conditions) gets ON ERROR UNDO, THROW. This includes REPEAT and FOR EACH loops, which would otherwise retry. An unhandled error therefore leaves the loop and the routine at once and reaches the caller as the original error object.
  • ROUTINE-LEVEL: only the routine blocks (procedure, function, method, constructor, property accessor) throw. Inner loops keep their default handling, so inside a routine with an enclosing REPEAT loop the result is the same as at the default level.
  • Default level (no statement): an UNDO, THROW or a system error that is not handled inside the routine does not reach the caller. The routine ends, the caller goes on with its next statement and sees no error (with NO-ERROR, ERROR-STATUS:ERROR is no). OUTPUT parameters are copied back. The error message is written to the current output destination. The same applies to class members: in a class without a level statement, UNDO, THROW in a constructor ends the constructor and the object is created.
  • Only the callee's file decides. Whether an error leaves a routine depends on the level statement of the file that contains the routine; the level of the caller's file never changes it. This holds for internal and external procedures, persistent and super procedures, functions (also IN handle, IN SUPER, DYNAMIC-FUNCTION), methods, static methods, constructors and property accessors. An internal procedure has the level of its own file.
  • RETURN ERROR and STOP are not affected by the level: they always reach the caller (see RETURN Statement and STOP Statement).

CASE Statement

Branch to one of an arbitrary number of blocks of code based on the evaluation of a single expression. 4GL syntax:

CASE expression :
{ WHEN value1 [ OR WHEN value2 ] ... THEN { block | statement } } ...
[ OTHERWISE { block | statement } ]
END [ CASE ]

Where its parameters are:

  • expression - the expression that determines which branch of code to execute. It can be an arbitrary expression which can yield a value of any data type.
  • value - each value is an expression that evaluates to a possible value for expression. It should evaluate to a value of the data type which is comparable with the data type of the value that expression yields.

The CASE statement can be converted in 2 possible ways.

The Java switch statement is the optimal way to convert CASE since this is directly equivalent. However, due to limitations in the Java language, it can only be used if the expression is of type integer expression and ALL values are integer constants. The conversion rules will use the following format:

switch((<expression>).intValue())
{
   case <value1>:
   [case <value2> :]
   ...
   {
      ...
      break;
   }
   ...
   [ default:
     {
        ...
        break;
     } ]
}

As a general purpose fallback, any CASE statement that cannot be implemented using the Java switch statement will be converted using the Java if/ else if /@ else statement. This will occur if the expression is not of integer type or if at least one value is not a constant. The conversion rules will use the following format:

<expression_type> condition<index> = <expression>;
if (condition<index>.equals(<value1>) [ || condition<index>.equals(<value1>) ] ...)
{
   ...
}
...
[ else
  {
     ...
  } ]

where the <index> is a counter for all CASE statements converted to compatible IF/ELSE Java statements, starting from 0 and from top to bottom in the external procedure. This local condition variable is used so that the expression is only evaluated once (it is faster and avoids unexpected side-effects). The captured valid is referenced in each comparison test.

Multiple WHEN clauses are converted in order and included with the Java || (logical OR operator).

In all cases, the block or statement following a certain WHEN branch will be converted following the same rules as the IF's THEN/ELSE branches get converted: the code will be enclosed in a Java { ... } block, with a Java break statement added last.

Example 1:

case x + 1:
   when 1 or when 2 then
      message "message 1".
   when 3 then do:
      message "message 2".
      message "message 3".
   end.
   otherwise message "message 4".
end.

Converted code:

switch (plus(x, 1)).intValue())
{
   case 1:
   case 2:
   {
      message("message 1");
      break;
   }
   case 3:
   {
      message("message 2");
      message("message 3");
      break;
   }
   default:
   {
      message("message 4");
      break;
   }
}

Details:

As the condition evaluates to an integer value and all WHEN clauses test for integer constants, the statement block is converted to a compatible Java switch statement. For each WHEN clause a break statement is added in the converted code. Where multiple tests result in executing the same code (the when 1 or when 2 test), consecutive case clauses are emitted in the converted code, which choose the same block.

The OTHERWISE branch converts to the default branch for the converted switch statement.

Example 2:

case x * 2:
   when 1 or when y then
      message "message 1".
   when (y + 1) * 2 then do:
      message "message 2".
      message "message 3".
   end.
   otherwise message "message 4".
end.

Converted code:

integer condition0 = multiply(x, 2);
if (condition0.equals(new integer(1)) || condition0.equals(y))
{
   message("message 1");
}
else if (condition0.equals(multiply(plus(y, 1), 2)))
{
   message("message 2");
   message("message 3");
}
else
{
   message("message 4");
}

Details:

In this case even if the condition evaluates to an integer value, the WHEN doesn't test for constants - so, IF/ELSE statements are emitted to handle each WHEN condition. For compound WHEN tests (as in when 1 or when y), the converted if statement's condition will contain both tests, joined with the Java || operator. The OTHERWISE block is emitted as the else block for the last converted if.

Example 3:

case x:
   when 1 then do while x < 5:
      x = x + 1.
   end.
   otherwise repeat:
      x = x + 1.
   end.
end.

Converted example:

switch ((x).intValue())
{
   case 1:
   {
      loopLabel0:
      while (_isLessThan(x, 5))  /* nested block inside CASE */
      {
         x.assign(plus(x, 1));
         break; /* break statement emitted incorrect */
      }
      break;
   }
   default:
   {
      repeat("loopLabel1", new Block()  /* nested block inside DEFAULT */
      {
         public void body()
         {
            x.assign(plus(x, 1));
         }
       });
       break;
   }
}

Details:

Progress 4GL allows the CASE statement nested blocks to be more than simple DO blocks. This is called the “integrated complex block” and it is fully supported. To properly support this feature, the code blocks are enclosed in an extra set of Java curly braces { ... } such that the normal switch statement semantics are maintained.

The WHEN and OTHERWISE branches that lead to integrated complex blocks get converted to BlockManager APIs and are nested inside the enclosing simple Java { ... } block.

The FWD conversion engine has a bug which emits an extra Java break statement in cases when the 4GL CASE statement is converted to a Java SWITCH statement. This bug applies to all WHEN branches, except when the WHEN branch contains a simple DO block. In the converted example above the incorrect break is marked in red. This is considered a bug.

Example 4:

case x:
   when 1 then do:
      x = x + 1.
   end.
end.

Converted code:

switch ((x).intValue())
{
   case 1:
   {
      x.assign(plus(x, 1));
      break;
   }
}

Details:

If a simple DO block is used with a WHEN test, then the statements are placed directly into body of the converted CASE clause, without any intermediate blocks.

CATCH Block

Handles an error object raised in the associated block (the block that contains the CATCH). 4GL syntax:

CATCH error-variable AS [ CLASS ] error-class :
   ...
END [ CATCH ] .

A block can have several CATCH blocks; they must be the last statements of the associated block, before its FINALLY (if any). The associated block must be undoable: OpenEdge rejects a CATCH or FINALLY on a DO block that has neither TRANSACTION nor an ON phrase. The first CATCH whose class matches the error object (in source order) handles it; a CATCH for a subclass must therefore come before the CATCH for its superclass.

The CATCH converts to a BlockManager.catchError() call in the Init part of the associated block, with the error class and a lambda that holds the converted CATCH body. The FINALLY converts to the Fini part of the block.

4GL code:

l2: repeat i = 1 to 100:
   k = k + 1.
   h = h:next-sibling.
   catch ex as Progress.Lang.Error:
      k = k + 1.
      leave.
   end catch.
   finally:
      k = k + 1.
      next.
   end finally.
end.

Converted code:

ToClause toClause0 = new ToClause(i, 1, 100);

repeatTo("l2", toClause0, new Block((Init) () ->
{
   catchError(com.goldencode.p2j.oo.lang.LegacyError.class,
   ex ->
   {
      ex_top.assign(ex);
      k.assign(plus(k, 1));
      leave("");
   });
},
(Body) () ->
{
   k.assign(plus(k, 1));
   h.assign(h.unwrap().getNextSibling());
},
(Fini) () ->
{
   k.assign(plus(k, 1));
   next("l2");
}));

Details:

Each iteration runs the body, then the CATCH (if the body raised an error), then the FINALLY. In this example the FINALLY's NEXT overrides the CATCH's LEAVE, so all 100 iterations run on OpenEdge.

Behavior of the CATCH:

  • The failing iteration is undone before the CATCH runs (if a transaction is active); the CATCH's own changes are kept.
  • A CATCH that ends normally ends the error. A counted loop (DO ... TO, REPEAT ... TO, FOR EACH) goes on with the next iteration; the block's ON ERROR phrase is not applied. A loop without a counter (DO WHILE, REPEAT WHILE, REPEAT without TO) is left, even with NEXT in the CATCH.
  • Unlabelled LEAVE and NEXT in a CATCH act on the associated block. Unlabelled UNDO, LEAVE, UNDO, NEXT and UNDO, RETRY all leave the associated block; the statement after it runs. Only the labelled UNDO, NEXT label (label = associated loop) continues the loop. UNDO label, RETRY label that targets the associated block is rejected by the OpenEdge compiler.
  • LEAVE label, NEXT label and UNDO label, ... to an enclosing block act on that block as usual.
  • An error raised in the CATCH (a statement error, UNDO, THROW, or UNDO, THROW error-variable to rethrow the same object) ends the CATCH, leaves the associated block and goes to the enclosing block, also at the default level. The FINALLY of the associated block runs first.
  • STOP or QUIT in the CATCH goes to the enclosing block's ON STOP / ON QUIT phrase. OpenEdge 11.6 does not run the associated block's FINALLY in this case; OpenEdge 11.7 and later (and FWD) run it for a STOP, not for a QUIT, and a NEXT to the loop in that FINALLY cancels the STOP raised in the CATCH (it does not cancel a STOP raised in the body).
  • RETURN ERROR in the CATCH returns the error to the caller of the routine.
  • A CATCH for Progress.Lang.AppError does not catch a Progress.Lang.SysError; the block's ON ERROR phrase then applies.
  • A CATCH for Progress.Lang.Stop, Progress.Lang.StopAfter, Progress.Lang.UserInterrupt or Progress.Lang.LockConflict handles the corresponding STOP condition when stop objects are enabled; it converts to BlockManager.catchStop(<class>, <lambda>) in the Init part of the block. A CATCH for Progress.Lang.StopError (a SysError) handles the STOP of a RUN of a missing program and converts to catchError() like any error class. See Stop Objects.

FINALLY Block

Runs code at the end of every execution (every iteration) of the associated block. 4GL syntax:

FINALLY :
   ...
END [ FINALLY ] .

The FINALLY must be the last statement of the associated block, after any CATCH blocks. It converts to the Fini part of the associated block (see the example in CATCH Block). A FINALLY of a routine (procedure, function, method) is the Fini part of the routine's block.

Two runtime hooks are emitted by the conversion for blocks with a FINALLY:

  • In a loop which has a FINALLY block, each statement of the loop body (and of the blocks nested in the body, but not of the CATCH or FINALLY blocks) which follows another statement is preceded by a BlockManager.statementEnd() call. An error which is still pending after the FINALLY block continued the loop (see below) is raised there, as the 4GL raises it when the next statement ends.
  • The FINALLY block of a FOR block starts with BlockManager.finallyRecordUnavailable() unless the FINALLY contains AVAILABLE or DELETE of a buffer of the FOR statement (anywhere in it): after an iteration which ended normally, the 4GL makes the record of the FOR buffers unavailable to field references in the FINALLY block (error 91), although AVAILABLE would still report it.

Behavior of the FINALLY:

  • When it runs: at the end of every iteration: after a normal end, NEXT, LEAVE, LEAVE label, NEXT label, RETURN, an error, and after the CATCH. It runs after the iteration's undo, so its own changes are kept. Nested FINALLY blocks run innermost first, before the jump of a NEXT label / LEAVE label to an outer block.
  • STOP and QUIT (OpenEdge 11.6): when a STOP or QUIT raised in the body or in the CATCH leaves the block, the block's FINALLY does not run, unless the block itself has an ON STOP / ON QUIT phrase (then the FINALLY runs and the block ends as after LEAVE).
  • STOP and QUIT (OpenEdge 11.7 and later, FWD): the FINALLY runs once when a STOP passes through the block, and the STOP then goes on; LEAVE, NEXT, RETRY, RETURN and an unlabelled UNDO in that FINALLY do not end the STOP (exception: a NEXT to the loop ends a STOP which was raised in the CATCH block). A QUIT in it replaces the STOP (see below). A RETURN ERROR or an error raised in it replaces the STOP only when a CATCH block handles that error; otherwise the STOP goes on. The FINALLY is still skipped when a QUIT passes through the block.
  • Flow statements in the FINALLY override the pending action of the iteration: NEXT (also NEXT label, UNDO, NEXT label on the associated loop) continues the loop, even after LEAVE in the body or in the CATCH, and after an error under ON ERROR UNDO, LEAVE. LEAVE (also LEAVE label, UNDO, LEAVE label) leaves the associated block, even after NEXT or a handled error. LEAVE label / NEXT label to an enclosing block act on that block.
  • Exceptions to the override: a RETURN ERROR in the body is not overridden by a flow statement, a STOP, a QUIT, a RETURN ERROR or a statement error in the FINALLY; only an UNDO, THROW in the FINALLY replaces it (OpenEdge 12.8): the routine does not return, and the thrown error object is raised in the enclosing block. At BLOCK-LEVEL a body error that is being thrown wins over LEAVE, NEXT and RETURN in the FINALLY. In a loop without a counter, NEXT in the FINALLY does not restart the loop after an undo of the iteration. An error raised in the CATCH (or a thrown error object in a counted loop) is not ended by a NEXT in the FINALLY: the loop goes on, but the error is raised again as soon as the first statement of the next iteration ends, unless that statement calls a procedure, function or method, in which case the error is dropped; when no iteration is left, the error is raised in the enclosing block after the loop.
  • Unlabelled UNDO, LEAVE, UNDO, NEXT and UNDO, RETRY in a FINALLY are ignored: they only end the FINALLY, and the iteration goes on exactly as with an empty FINALLY.
  • RETURN and RETURN "value" in the FINALLY end the routine normally and set RETURN-VALUE, overriding a RETURN "value" of the body. RETURN ERROR in the FINALLY replaces any body error: the caller gets an empty Progress.Lang.AppError (or the AppError with the given ReturnValue for RETURN ERROR "value").
  • An error raised in the FINALLY replaces the body's error, leaves the associated block and goes to the enclosing block; nothing is undone, not even under TRANSACTION. If the enclosing block is the routine block, then at the default level the routine ends and the caller does not see the error; at BLOCK-LEVEL it reaches the caller. An error raised in the FINALLY of a routine block reaches the caller at every level.
  • STOP in the FINALLY (OpenEdge 11.6): when the iteration ends with a pending next iteration (NEXT, UNDO, NEXT, UNDO, RETRY, or an error handled by the default), a STOP in the FINALLY of a counted loop is discarded and the loop runs to the end. Otherwise (and always in DO WHILE) it goes to the enclosing block; the ON STOP phrase of the associated block does not handle it. QUIT in the FINALLY always goes to the enclosing block. When a body error is being thrown, STOP or QUIT in the FINALLY delivers both conditions: the caller's ON STOP / ON QUIT phrase controls the flow, and the error is delivered as well.
  • STOP in the FINALLY (OpenEdge 12.8, FWD): a STOP raised in the FINALLY of a DO ... TO or REPEAT ... TO iteration which goes on with the next one (NEXT, UNDO, NEXT, UNDO, RETRY, or the default retry after an error) is ignored, unless it is raised by the nested FINALLY of a CATCH or FINALLY block; in FOR EACH and in loops without a counter it goes to the enclosing block. QUIT in the FINALLY goes to the enclosing block. When a body error is being thrown, the first handler met on the way out decides: a CATCH block for the error handles the error and the STOP or QUIT is dropped; an ON STOP phrase handles the STOP and the error is lost; an ON QUIT phrase handles the QUIT and the error is delivered after that (when the loop which handled the QUIT ends).
  • QUIT in the FINALLY while a STOP is pending (OpenEdge 12.8, FWD): the STOP goes on and carries the QUIT. Once an ON STOP phrase handles the STOP, the QUIT is raised: after UNDO, LEAVE in the enclosing block, before its next statement; after UNDO, NEXT of a counted loop (DO ... TO, REPEAT ... TO) as soon as the first statement of the next iteration ends, wherever that statement is (also inside a called routine), and the loop handles that QUIT with its own ON QUIT phrase (with UNDO, NEXT the following iteration runs in full again); when no iteration is left, the QUIT is raised in the enclosing block. A routine call does not cancel it. FWD raises it at the first point it can detect (a statement end hook, a routine returning normally, or the end of the iteration body), so in code without a statement end hook it can come one statement later than on OpenEdge.
  • ERROR-STATUS: a NO-ERROR statement in a FINALLY or CATCH sees only its own result, and the caller of a NO-ERROR call sees only the original error, whatever the FINALLY did.

An unlabelled UNDO, LEAVE, UNDO, NEXT or UNDO, RETRY in a FINALLY targets the FINALLY block itself (as in a CATCH) and converts to undoLeave(""), undoNext("") or undoRetry(""); at runtime it only ends the FINALLY, as described above.

IF/THEN/ELSE Statement

This makes the execution of a statement or block of statements conditional. If the value of the expression following an IF or ELSE IF statement is TRUE, then the statements following the corresponding THEN keyword are processed. Otherwise, the statements following the ELSE keyword are processed.

4GL syntax:

IF expression THEN { block | statement }
[ ELSE IF expression THEN { block | statement } ] ...
[ ELSE { block | statement } ]

This statement should not be confused with the IF THEN ELSE built-in function. The IF function can be used within expression because it returns a value. This IF function is the same as the Java ternary operator, condition ? value1 : value2. Since it does not alter the logical flow of the program (any more than do any other sub-expressions), it is not documented in this chapter. For complete details about how it gets converted, please see the Expressions chapter.

The IF statement is the same as the Java if statement, which is a control flow mechanism to conditionally branch. The match is exact, such that 4GL IF statements are completely converted as Java if/@ else statements.

At this time the usage of an ELSE IF idiom is converted as else { if { ... } } rather than the more optimal else if { ... }. This will be improved in a subsequent version.

Since this is a direct mapping to the flow control statements of Java, the conditional expression which is evaluated will generally return a logical type. This value will then be “unpacked” into a boolean (often using logical.booleanValue()) that is passed to the Java if. In many places in FWD runtime, there are dual versions of a given method, one which returns a logical and another (usually prefixed by the _ character) which directly returns a boolean. Where possible, the converted code will use these special versions (to avoid the unpacking step) and directly return the boolean. A good example of this is the replacement AND operator (CompareOps.and() versus CompareOps_and()). This might lead to emitting _and(x, y) Instead of emitting and(x, y).booleanValue().

Example 1:

if x = 0 then message "value is 0".
else if x = 1 then message "value is 1".
else if x = 2 then do:
   message "value".
   message "is 2".
end.
else message "other value".

Converted code:

if (_isEqual(x, 0))
{
   message("value is 0");
}
else
{
   if (_isEqual(x, 1))
   {
      message("value is 1");
   }
   else
   {
      if (_isEqual(x, 2))
      {
         message("value");
         message("is 2");
      }
      else
      {
         message("other value");
      }
   }
}

Details:

This example shows how nested if/then/else statements get converted. The converted code will automatically group and indent the THEN and ELSE branches, even in cases when the branch contains a simple statement, and not a block.

Example 2:

if x = 1 then do while x < 5:
   x = x + 1.
end.

if x = 2 then repeat:
   x = x + 1.
   leave.
end.

Converted example:

if (_isEqual(x, 1))
{
   loopLabel0:
   while (_isLessThan(x, 5))  /* nested block inside IF */
   {
      x.assign(plus(x, 1));
   }
}

if (_isEqual(x, 2))
{
   repeat("loopLabel1", new Block((Body) () -> /* nested block inside IF */
   {
      x.assign(plus(x, 1));
      leave("loopLabel1");
   }));
}

Details:

Progress 4GL allows the THEN and ELSE nested blocks to be more than simple DO blocks. This is called the “integrated complex block” and it is fully supported. To properly support this feature, the code blocks are enclosed in an extra set of Java curly braces { ... } such that the normal if statement semantics are maintained.

The THEN and ELSE branches that lead to integrated complex blocks get converted to BlockManager APIs and are nested inside the enclosing simple Java { ... } block.

Example 3:

if x = 1 then do:
   message "message 1".
   message "message 2".
end.

Converted code:

if (_isEqual(x, 1))
{
   message("message 1");
   message("message 2");
}

Details:

For a simple DO block (the most common case for an IF statement), the statements are placed directly into the if body without any intermediate blocks.

LEAVE Statement

The LEAVE statement is used to exit a block and transfer control to the statement immediately following the targeted block. When it is used with a top-level block it is converted to a RETURN statement.

4GL syntax:

LEAVE [ label ]

To implement it, the conversion process will use the following rules to decide how the handle the statement:

  1. If there is an explicit label, that block is targeted.
  2. If there is no label, then the implicit target block must be found. The nearest enclosing inner block which is NOT a simple DO block (DO loops are OK) will be chosen. One of the following types will be the target:
    • DO { TO | WHILE | TO ... WHILE } (the presence or absence of transaction, error or other properties is ignored in this search, the only criteria that matters is whether the DO is a loop)
    • REPEAT
    • FOR (all forms, not just the EACH loop, including the FOR FIRST/LAST blocks)
    • EDITING
  3. If no inner block is found with the above process, then the LEAVE is considered to be targeting the nearest top-level block (external procedure, internal procedure, user-defined function or trigger). In that case the LEAVE is implicitly treated as a RETURN statement (no labels are required).

During conversion, the LEAVE is converted to:

  1. A BlockManager.leave(String label) function call, if the target is a managed block. A “managed block” is one that is processed by the BlockManager APIs.
  2. A Java break statement, if the target is a non-managed block and there is no managed block between the LEAVE statement and the targeted non-managed block.
  3. A combination of a BlockManager.leave, BlockManager.deferredLeave and a Java break statement is used for cases when the LEAVE targets a non-managed block but there is a managed block between the non-managed and the LEAVE statement. Rather than force all blocks to be converted to managed blocks, it was decided to accept this slightly more complex implementation in the rare cases where this is an issue.
  4. A BlockManager.returnNormal() method call if this it is being silently converted to a RETURN statement.

If LEAVE is supposed to cause exit of a block (i.e. is not treated as RETURN action), it is converted using BlockManager.leave function or, if the block is converted using Java for or while cycle or as a simple { ... } block, it is converted using Java break statement. Even if a block was implicitly targeted by a LEAVE statement, FWD always explicitly specifies in the break statement the label of the converted target block.

Example 1:

/* explicit label */
someLabel:
repeat:
  ...
  if l then leave someLabel.
  ...
end.

/* implicit targeting */
repeat:
  ...
  if l then leave.
  ...
end.

Converted code:

repeat("someLabel", new Block((Body) () -> 
{
   ...
   if ((l).booleanValue())
   {
      leave("someLabel");
   }
   ...
}));

repeat("loopLabel0", new Block((Body) () -> 
{
   ...
   if ((l).booleanValue())
   {
      leave("loopLabel0");
   }
   ...
}));

Details:

Both LEAVE statements target a managed block, the first is explicit and the second is implicit. The conversion process will emit BlockManager.leave calls to handle the block exit. The first case has the matching converted label (someLabel) and the second case has the manufactured label (loopLabel0). In both cases, the “labels” are passed as a String. The BlockManager tracks all block labels as String values.

Example 2:

/* explicit label from nested block */
someLabel:
repeat:
   repeat:
      leave someLabel.
   end.
end.

/* implicit targeting from a nested block */
repeat:
   do on error undo, retry:
      leave.
   end.
end.

Converted code:

repeat("someLabel", new Block((Body) () -> 
{
   repeat("loopLabel0", new Block((Body) () -> 
   {
      leave("someLabel");
   }));
}));

repeat("loopLabel1", new Block((Body) () -> 
{
   OnPhrase[] onPhrase0 = new OnPhrase[]
   {
      new OnPhrase(Condition.ERROR, Action.RETRY, "blockLabel0")
   };

   doBlock(TransactionType.SUB, "blockLabel0", onPhrase0, new Block((Body) () -> 
   {
      leave("loopLabel1");
   }));
}));

Details:

This shows both explicit and implicit targeting of more enclosing blocks by LEAVE statements in nested blocks. The implicit case shows the fact that an implicit LEAVE cannot target non-looping DO blocks, so it targets the nearest enclosing looping block.

Example 3:

/* explicit label */
someLabel:
do while x > 0:
  ...
  if x < 10 then leave someLabel.
end.

/* implicit targeting */
do while x > 0:
  ...
  if x < 10 then leave.
end

Converted code:

/* explicit label */
someLabel:
while (_isGreaterThan(x, 0))
{
   ...
   if (_isLessThan(x, 10))
   {
      break someLabel;
   }
}

/* implicit targeting */
loopLabel0:
while (_isGreaterThan(x, 0))
{
   ...
   if (_isLessThan(x, 10))
   {
      break loopLabel0;
   }
}

Details:

In cases when the LEAVE targets non-managed blocks, the Java break statement is used, unless there is a managed block between the non-managed and the LEAVE statement (see the next example). Although one of the two labels in this example was implicit in the 4GL, they are explicitly specified on the break statement in the converted code.

Just as with the managed blocks cases, it is valid to explicitly and implicitly reference outer blocks from more deeply nested blocks. Those examples are not shown here.

Example 4:

b1:
do:

   b2:
   do for book preselect each book:
      ...
      leave b1.
   end.

end.

Converted code:

b1:
{
   PreselectQuery query0 = new PreselectQuery();

   String[] enclBlocks0 = new String[]
   {
      "b1" 
   };

   doBlock(enclBlocks0, "b0", new Block((Init) () -> 
   {
      RecordBuffer.openScope(book);
      query0.initialize(book, ((String) null), null, "book.bookId asc");
   }, 
   (Body) () -> 
   {
      leave("b0");
   }));

   if (deferredLeave("b10))
      break b0;
}

Details:

In this case, between the LEAVE and the non-managed block there is the interim doBlock managed block. The code in that managed block will be processed on a call-back basis by the BlockManager. To accomplish this, the code is emitted as an anonymous inner class instance. This means that code inside that managed block is not in the same lexical scope as the enclosing non-managed block. The Java break statement cannot “see” the enclosing block, so using that statement is not possible. In this case the LEAVE is converted to a BlockManager.leave call. To make it work inside the BlockManager, the conversion process will pass the labels of all parent, non-managed, blocks to any enclosed managed blocks, via the enclosing parameter (which is available for all block-related APIs in BlockManager - see the Blocks chapter of this book for block-related API details). When the BlockManager.leave is executed, it will cause the managed blocks to exit up to the point at which the enclosing non-managed blocks start, then it will set a flag noting that a deferred leave is in effect. The exit from that last managed block will leave it in code that checks for a deferred leave flag that matches the target block. Once deferredLeave notices that there is a pending break for the block with the received label, it will return true and the business logic will execute the Java break statement.

This case is a bit messy, but it is also rare.

Example 5:

someLabel:
do:
  ...
  if l then leave someLabel.
  ...
end.

Converted code:

someLabel:
{
   ...
   if ((l).booleanValue())
   {
      break someLabel;
   }
   ...
}

Details:

A LEAVE statement explicitly targets the DO block (which gets converted to a Java block); a Java break statement is used to exit it. Implicit targeting of a simple DO block is not possible, but as this example shows, an explicit label is honored even on a non-looping DO.

Example 6:

procedure proc:
   ...
   leave.
end.

Converted code:

@LegacySignature(type = Type.PROCEDURE, name = "proc")
public void proc()
{
   internalProcedure(new Block((Body) () -> 
   {
      returnNormal();
   }));
}

Details:

When LEAVE targets a top-level block it is converted to a RETURN statement. In this case, the BlockManager.returnNormal is used in the converted code. This targeting can only occur using an unlabeled LEAVE. That unlabeled LEAVE can be directly inside the top-level block (not enclosed within any inner blocks) or it can be enclosed only within nested simple DO blocks.

LEAVE in CATCH and FINALLY

An unlabelled LEAVE in a CATCH leaves the associated block of the CATCH (not an enclosing loop); it converts to leave("") inside the catchError() lambda. An unlabelled LEAVE in a FINALLY leaves the associated block and overrides the pending action of the iteration (for example a NEXT in the body or in the CATCH). It does not override a RETURN ERROR in the body, nor (at BLOCK-LEVEL) a body error that is being thrown. See CATCH Block and FINALLY Block.

An unlabelled LEAVE that targets the routine block (directly in a procedure or function body, or in its CATCH) ends the routine normally, as described above.

NEXT Statement

The NEXT statement is used to prematurely end the current iteration of a block and start the next iteration. In cases when it is not used with a looping block, it has the same behavior as a LEAVE or RETURN statement.

NEXT [ label ]

The conversion process uses the following rules to decide how the handle the statement:

1. If there is an explicit label:

  • If a block is a looping block, then the next iteration of the targeted block is invoked.
  • The only non-looping block that can be explicitly targeted by a NEXT statement is a FOR block (in which all record phrases are qualified using default (i.e. unique), FIRST or LAST type). In this case, NEXT is automatically converted to LEAVE.
  • If a NEXT statement explicitly targets a simple DO block (without TO or WHILE), it will cause a 4GL compiler error, so this case doesn't convert.

2. If there is no label, then the implicit target block must be found. This is a 2 phase process.

  • The nearest enclosing looping block type will be the target:
    - DO { TO | WHILE | TO ... WHILE }
    - REPEAT
    - FOR with at least one EACH component
    - EDITING
    If one of these blocks is found, then the NEXT iteration of that block will be invoked.
  • If there is no enclosing looping block between the NEXT statement and the nearest enclosing top-level block, then a search will be made to find the nearest enclosing FOR block with unique, FIRST or LAST components only. If such a block is found, then the NEXT will be statically converted to LEAVE and targeted at that nearest enclosing FOR [ FIRST | LAST ] block.
    Note that the looping blocks will always take precedence over the FOR FIRST/LAST. This means that a more deeply nested FOR FIRST/LAST block will NOT have preference to an outer looping block.

3. If no inner block is found with the above process, then the NEXT is considered to be targeting the nearest top-level block (external procedure, internal procedure, user-defined function or trigger). In this case, the NEXT is converted to a RETURN statement.

The NEXT statement will be converted to either BlockManager.next API calls or to the compatible Java statement, continue. The BlockManager.next(String label) API will be emitted in all cases when the targeted block is managed by BlockManager APIs too. In cases when the targeted block is converted to the for or while compatible Java statement (a simple DO loop), the NEXT statement will be converted to the Java continue [ label ], statement, with its label set to target the appropriate block.

The label in 4GL is optional and if missing it defaults to the nearest block which satisfies the previously presented rules, but this does not apply to the converted code. Regardless of whether or not the label is explicitly specified, the converted code for this statement will always explicitly target the block to which it refers to, using the block's label as the parameter.

Example 1:

/* explicit label */
someLabel:
repeat:
  ...
  if nxt then next someLabel.
  ...
end.

/* implicit targeting */
repeat:
  ...
  if nxt then next.
  ...
end.

Converted code:

/* explicit label */
repeat("someLabel", new Block((Body) () -> 
{
   ...
   if ((nxt).booleanValue())
   {
      next("someLabel");
   }
   ...
}));

/* implicit targeting */
repeat("loopLabel0", new Block((Body) () -> 
{
   ...
   if ((nxt).booleanValue())
   {
      next("loopLabel0");
   }
   ...
}));

Details:

These two examples show that the converted NEXT statement will always explicitly specify the targeted block, regardless if the label is explicitly set or not. Since these NEXT statements are each targeting a managed block, the BlockManager.next() method is called.

Example 2:

/* explicit label */
someLabel:
do while x > 0:
  ...
  if x < 10 then next someLabel.
  ...
end.

/* implicit targeting */
do while x > 0:
  ...
  if x < 10 then next.
  ...
end.

Converted code:

/* explicit label */
someLabel:
while (_isGreaterThan(x, 0))
{
   ...
   if (_isLessThan(x, 10))
   {
      continue someLabel;
   }
   ...
}

/* implicit targeting */
loopLabel0:
while (_isGreaterThan(x, 0))
{
   ...
   if (_isLessThan(x, 10))
   {
      continue loopLabel0;
   }
   ...
}

Details:

In this case, the targeted block is the non-managed Java while statement is used. In the converted code, the NEXT is converted to a Java continue statement which explicitly targets the while loop even if the 4GL code didn't specify it.

Example 3:

/* explicit label */
someLabel:
for first book:
  if book.book-id < 100 then next someLabel.
  ...
end.

/* implicit targeting */
for first book:
  if book.book-id < 100 then next.
  ...
end.

Converted code:

RandomAccessQuery query0 = new RandomAccessQuery();
/* explicit label */
forBlock("someLabel", new Block((Init) () ->
{
   RecordBuffer.openScope(book);
   query0.initialize(book, (String) null, null, "book.bookId asc");
},
(Body) () -> 
{
   query0.first();
   if (_isLessThan(book.getBookId(), 100))
   {
      leave("someLabel");
   }
   ...
}));

RandomAccessQuery query1 = new RandomAccessQuery();
/* implicit targeting */
forBlock("blockLabel0", new Block((Init) () ->
{
   RecordBuffer.openScope(book);
   query1.initialize(book, (String) null, null, "book.bookId asc");
}
(Body) () -> 
{
   query1.first();
   if (_isLessThan(book.getBookId(), 100))
   {
      leave("blockLabel0");
   }
   ...
}));

Details:

When NEXT is targeted (explicitly or implicitly) at a FOR FIRST/LAST block then it is silently converted to act as a LEAVE in the 4GL. The Java version will convert to a BlockManager.leave(). Even if a block was implicitly targeted by a NEXT statement, FWD always specifies the explicit label of the converted target block in the leave().

Example 4:

procedure proc:
   ...
   next.
end.

Converted code:

internalProcedure(new Block((Body) () -> 
{
   returnNormal();
}));

Details:

When NEXT implicitly targets a top-level block, it is is converted to a RETURN statement. BlockManager.returnNormal is used in the converted code.

NEXT in CATCH and FINALLY

An unlabelled NEXT in a CATCH acts on the associated block: a counted loop goes on with its next iteration, but a DO WHILE, REPEAT WHILE or REPEAT without TO is left (see Loop Behavior After a Condition). NEXT label in a CATCH abandons the associated block and starts the next iteration of the labelled enclosing loop.

A NEXT in a FINALLY continues the loop even when the body or the CATCH executed LEAVE, and after an error handled by ON ERROR UNDO, LEAVE. It does not restart a loop without a counter after an undo of the iteration, and it does not end an error raised in the CATCH: the loop runs to the end and the error then reaches the enclosing block.

PAUSE Statement

Suspends processing for a specified number of seconds or until the user presses any key. The type-ahead buffer is cleared. Of great importance to control flow, the PAUSE statement allows user to raise ENDKEY, ERROR or STOP condition (using corresponding keys).

PAUSE [ n ] [ BEFORE-HIDE ] [ MESSAGE message | NO-MESSAGE ] [ IN WINDOW window ]

The 4GL parameters of this statement are:

  • n - a numeric expression specifying the maximum number of seconds suspend processing. Without this option, processing is suspended until the user presses a key. Even if the number of seconds is specified, the user can resume processing immediately by pressing a key.
  • BEFORE-HIDE - makes the statement specify the pause action (message and delay) the user must take whenever frames are hidden automatically.

PAUSE statement is converted to the following APIs:

  • LogicalTerminal.pause - for all cases when BEFORE-HIDE option is not specified;
  • LogicalTerminal.pauseBeforeHide - if BEFORE-HIDE option was specified.

During the PAUSE (either kind), if the user presses a key which is associated with raising a condition (e.g. the END-ERROR key), that condition will be raised. The program's processing will resume, BUT instead of executing the statement that immediately follows the PAUSE, the block/loop will UNDO and otherwise execute the expected processing that occurs for that condition (based on default block properties, ON phrases and so forth).

For full details on the conversion of this statement see the PAUSE Statement section of the Terminal Management chapter of Part 6. The content here is only related to control flow.

QUIT Statement

Raises the QUIT condition. By default, this exits from the application and returns to the operating system. One can change this behavior by including the ON QUIT phrase in an enclosing block's header.

Behavior:

  • QUIT commits the active transaction (unlike STOP, which rolls it back).
  • QUIT is handled only by ON QUIT phrases; the default ON ERROR processing, CATCH blocks and NO-ERROR do not handle it. It leaves routines and reaches the caller's ON QUIT phrase.
  • When QUIT leaves a block that has a FINALLY, the FINALLY does not run, unless the block itself has an ON QUIT phrase (OpenEdge 11.6 and 12.8 alike; unlike STOP, for which later releases run the FINALLY). QUIT in a FINALLY goes to the enclosing block; when a STOP is pending, the STOP goes on carrying the QUIT, which is raised once an ON STOP phrase handled the STOP (see FINALLY Block).
  • The conversion treats the code after a block which contains a QUIT as reachable (like after an UNDO, THROW), since an ON QUIT phrase of an enclosing block can handle the condition.
  • ON QUIT UNDO, RETURN ERROR turns the QUIT into an ERROR (an empty Progress.Lang.AppError) in the caller.

QUIT is converted to the BlockManager.quit() method.

Example:

quit.

Converted code:

quit();

RETRY Function

Built-in function which returns true if the current block is being reprocessed after a previous UNDO, RETRY statement or in response to a condition that would cause a RETRY action.

4GL syntax:

RETRY

Using the RETRY function in a block turns off the default error processing, which results in no infinite loop protection for the block (see the Infinite Loop Protection section of the Blocks chapter for more details).

When no statement in the block waits for user input, a retry is not performed at all: UNDO, RETRY acts as NEXT in a counted loop and as LEAVE in a single DO block, in a loop without a counter and in a CATCH (see Loop Behavior After a Condition). In such blocks the RETRY function does not become true through an undo.

RETRY is converted using TransactionManager.isRetry or TransactionManager._isRetry function. The first version returns a 4GL-compatible logical value and is emitted when it is used together with some other variables or fields, while the second function returns a Java boolean value and is emitted when the function is used in a Java flow of control statement such as while or if. As usual, the underscore prefix means the API returns a Java-style value, and not a 4GL-compatible value.

Example 1:

repeat:
   if retry then
      message "You've entered an invalid value.".

   set x.
   if x < 0 then undo, retry.
end.

Converted code:

repeat("loopLabel0", new Block((Body) () ->
{
   if (TransactionManager._isRetry())
   {
      message("You've entered an invalid value.");
   }

   FrameElement[] elementList0 = new FrameElement[]
   {
      new Element(x, frame0.widgetx())
   };

   frame0.set(elementList0);

   if (_isLessThan(x, 0))
   {
      undoRetry("loopLabel0");
   }
}));

Details:

As the RETRY is being used for control flow branching, the TransactionManager._isRetry() is emitted in the converted code.

RETURN Statement

Exits the nearest top-level block (an internal procedure, external procedure, trigger or user-defined function) and returns to the calling code. When exiting a trigger, this can optionally suppress the default behavior for the current user-interface event.

4GL syntax:

RETURN [ ERROR | NO-APPLY ] [ return-value ]

The 4GL provides 5 forms for the RETURN statement:

  1. RETURN without any specifications is used to returns from a top-level block: an external or internal procedure, user-defined function or trigger block and allows normal processing to continue.
    If this statement is executed from a procedure, then the RETURN-VALUE global character variable is set to the empty string "" which can be read by the caller using the RETURN-VALUE function. A procedure that ends without any RETURN statement leaves RETURN-VALUE unchanged.
    If this statement is executed from a function then an unknown value is returned by this function.
    NEXT and LEAVE statements implicitly targeted to a top-level block are treated as RETURN statements. See the sections related to these statements for more information.
    The conversion rules will emit a BlockManager.returnNormal(), with no parameters.
  2. RETURN <character expression> can be used in order to return from an external or internal procedure and set the RETURN-VALUE global character variable to the value of the given expression. It can be read by the caller using the RETURN-VALUE built-in function. The conversion rules will emit a BlockManager.returnNormal(<character expression>) function call.
  3. RETURN <expression> can be used in order to return a value of one of the basic data types from a user-defined function. This is converted using the BlockManager.returnNormal(<expression>) function.
  4. RETURN ERROR [<character expression>] can be used in an external or internal procedure, a method, a constructor or a property accessor in order to raise an ERROR condition in the caller. The caller receives a Progress.Lang.AppError with no messages (NumMessages 0, GetMessageNum(1) 0, GetMessage(1) ""), whose ReturnValue is the given value (or ""); the caller's RETURN-VALUE is set to the value as well, also when the error is caught or suppressed. With NO-ERROR on the call, ERROR-STATUS:ERROR is yes and NUM-MESSAGES is 0. The result is the same when the RETURN ERROR is executed from a nested block, a CATCH or a FINALLY of the routine. The routine's own CATCH blocks and ON ERROR phrases do not handle it, and no FINALLY statement can cancel it. The error level of the file does not matter. In a user-defined function, RETURN ERROR can also be used, but the error is ignored (not raised): the function is left and the caller sees no error at any error level, also when the function is called IN handle, IN SUPER or through DYNAMIC-FUNCTION; the function returns the unknown value. The conversion process will actually drop any expression passed in the RETURN ERROR from a user-defined function, but even if it did not, the runtime will drop any given value. If coded inside a trigger, a warning will be given during conversion and at runtime an IllegalStateException will be thrown. The Progress 4GL compiler will error on the use of RETURN ERROR from a trigger, so that code does not have to convert. The converted code will include a BlockManager.returnError([<character expression>]) call whenever this statement is encountered.
  5. RETURN ERROR <error object> (for example RETURN ERROR NEW Progress.Lang.AppError("msg", 1)) raises that error object in the caller; the first CATCH (in source order) whose class matches it receives it, so a CATCH for a subclass of AppError gets a subclass object. RETURN-VALUE is set to "", except for an AppError created with the one-argument constructor NEW AppError("value"), which has no messages and ReturnValue "value": then RETURN-VALUE becomes "value", as for RETURN ERROR "value". With NO-ERROR on the call, ERROR-STATUS:ERROR is yes, NUM-MESSAGES is the number of messages in the object and GET-MESSAGE(1) is the message text. This form converts to BlockManager.returnError(<error object>), for example returnError(ObjectOps.newInstance(com.goldencode.p2j.oo.lang.AppError.class, "II", "m", 1)). It does not compile in a LOGICAL function.
  6. RETURN NO-APPLY [<character expression>] can be used in a trigger in order to leave it and suppress the default behavior for the current user-interface event. The NO-APPLY option can also be specified in other block types, but it will be ignored by the conversion rules. It is always converted to a BlockManager.returnConsume([<character expression>]) function call.

Example 1:

procedure proc:
   ...
   if l then return.
   ...
end.

Converted code:

public void proc()
{
   internalProcedure(new Block((Body) () -> 
   {
      if ((l).booleanValue())
      {
         returnNormal();
      }
   }));
}

Details:

Converted using BlockManager.returnNormal function, as it is used to exit an internal procedure. Inside the BlockManager, the RETURN-VALUE will be set to the empty string (using ControlFlowOps.setReturnValue()).

Example 2:

procedure proc:
  ...
  return "O" + "K".
end.

run proc.
message return-value.

Converted code:

proc();
message(ControlFlowOps.getReturnValue());

...

public void proc()
{
   internalProcedure(new Block((Body) () -> 
   {
      returnNormal(concat("O", "K"));
   }));
}

Details:

Inside the BlockManager, the RETURN-VALUE will be set to the concatenated string (using ControlFlowOps.setReturnValue()). The calling code can obtain that value via the RETURN-VALUE function (converted to ControlFlowOps.getReturnValue()).

Example 3:

function func returns integer:
  def var x as integer init 1.
  return 2 * x.
end.

message string(func()).

Converted code:

message(valueOf(func()));

...

return integerFunction(new Block()
{
   integer x_1 = UndoableFactory.integer((long) 1);
   ...
   return function(this, "func", integer.class, new Block((Body) () -> 
   {
      returnNormal(multiply(2, x_1));
   }));
});

Details:

When exiting a user-defined function, the type of the returned expression must match the function's return type. In all cases, the BlockManager.returnNormal is used to set the correct return value for the function.

Example 4:

procedure proc:
   ...
   if l then return error "Some error".
end.

run proc no-error.
if error-status:error then
   message "ERROR: " + return-value.

Converted code:

ErrorManager.silentErrorEnable();
proc();
ErrorManager.silentErrorDisable();

if ((ErrorManager.isError()).booleanValue())
{
   message(concat(new character("ERROR: "), ControlFlowOps.getReturnValue()));
}

...

public void proc()
{
   internalProcedure(new Block((Body) () -> 
   {
      if ((l).booleanValue())
      {
         returnError("Some error");
      }
   }));
}

Details:

The RETURN ERROR is converted using the BlockManager.returnError() function.

Example 5:

form x with frame f1.

on "1" of x do:
  /* do not allow to enter "1" in the x field */
  return no-apply.
end.

update x with frame f1.

Converted code:


FrameElement[] elementList0 = new FrameElement[]
{
   new Element(x, f1Frame.widgetx())
};

f1Frame.update(elementList0);

...

registerTrigger(new EventList("1", f1Frame.widgetx()), Start.this, new Trigger((Body) () -> 
{
   returnConsume();
}));

Details:

When the trigger is exited via a RETURN NO-APPLY call, the conversion rules will emit a BlockManager.returnConsume() function call.

RETURN in a FINALLY Block

A RETURN or RETURN "value" in a FINALLY ends the routine normally and overrides a RETURN "value" executed in the body (RETURN-VALUE gets the FINALLY's value). A RETURN ERROR in a FINALLY discards any error of the body: the caller gets the AppError of the RETURN ERROR. At BLOCK-LEVEL, a body error that is being thrown wins over a plain RETURN in the FINALLY. A RETURN ERROR pending from the body is not replaced by a RETURN ERROR of the FINALLY; only an UNDO, THROW in the FINALLY replaces it (OpenEdge 12.8). A RETURN ERROR in the FINALLY does not replace a pending STOP unless a CATCH block handles its error (OpenEdge 12.8). See FINALLY Block.

RETURN-VALUE Function

Returns the character value saved by the most recent RETURN statement. If the RETURN statement had no expression, this returns the empty string.

4GL syntax:

RETURN-VALUE

Details:

  • A procedure that ends without a RETURN statement leaves RETURN-VALUE unchanged. RETURN, UNDO, RETURN and an ON ... UNDO, RETURN phrase without a value set it to "".
  • RETURN ERROR "value" and UNDO, RETURN ERROR "value" set it to "value", also when the caller catches the error or uses NO-ERROR. RETURN ERROR NEW AppError("value") (one-argument constructor) sets it to "value"; other RETURN ERROR <error object> forms set it to "".
  • When a called procedure does RETURN ERROR "value" and the error is handled inside the routine that called it, RETURN-VALUE is still "value" after that routine ends without its own RETURN.
  • ON ... UNDO, RETURN ERROR does not pass a previously set RETURN-VALUE on to the caller.
  • After PUBLISH, a subscriber's RETURN ERROR "value" sets RETURN-VALUE in the publisher.

Inside the BlockManager, when processing the RETURN statement the RETURN-VALUE will be set to the given string or the empty string. This is done in the runtime code using ControlFlowOps.setReturnValue().

Any calling code can obtain that value via the RETURN-VALUE function which is converted to ControlFlowOps.getReturnValue().

Example 1:

procedure proc:
  ...
  return "O" + "K".
end.

run proc.
message return-value.

Converted code:

ControlFlowOps.invoke("proc");
message(ControlFlowOps.getReturnValue());

...

public void proc()
{
   internalProcedure(new Block((Body) () -> 
   {
      ...
      returnNormal(concat("O", "K"));
   }));
}

Details:

Inside the BlockManager, the RETURN-VALUE will be set to the concatenated string (using ControlFlowOps.setReturnValue()). The calling code can obtain that value via the RETURN-VALUE function (converted to ControlFlowOps.getReturnValue()).

RUN Statement

4GL Form Description Notes
RUN
{ extern-proc-name
| VALUE ( extern-expression )
| path-name<<member-name>>
}
[ PERSISTENT
[ SET proc-handle ] ]
[ ON [ SERVER ]
{ server-handle | session-handle }
[ TRANSACTION DISTINCT ]
[ ASYNCHRONOUS
[ SET async-request-handle ]
[ EVENT-PROCEDURE
event-internal-procedure
[ IN procedure-context ] ]
]
[( parameter [, parameter ] ...) ]
[ preprocessor_argument ] ...
[ NO-ERROR ]
Calls an external Progress procedure. FWD doesn't support asynchronous calls, remote calls and calls to external libraries.
PERSISTENT [ SET proc-handle ] is supported (it converts to persistent() / setProcedureHandle(handle) on the call site).
The following options are not supported:
     • path-name<<member-name>>
     • ON ...
Although the runtime does not support passing preprocessor_argument values to the Java equivalent of the RUN statement, the conversion can be configured to incorporate preprocessor arguments when converting specific external procedures. Since it is done at conversion time, the result is a single static version of the called external procedure. For details, please see the Runtime Preprocessor Arguments section of the FWD Conversion Handbook.
RUN missing-file.p Attempt to call a missing external procedure. FWD provides only conversion support for this case. During conversion, the assumption is that the missing-file.p is a missing external procedure, and for all such cases it will use the BogusClassName as the Java class name in the converted code. The converted code will not compile and will look like:
BogusClassName bogusclassname0 =
   new BogusClassName();
bogusclassname0.execute();

To go further with the conversion process, every RUN statement which converts to a BogusClassName invocation needs to be analyzed and fixed as needed.

At runtime (a RUN VALUE() whose name does not resolve), FWD raises the STOP carrying a Progress.Lang.StopError described in Stop Objects.
RUN
{ intern-proc-name
|VALUE ( intern-expression)
}
[ IN proc-handle ]
[ ASYNCHRONOUS
[ SET async-request-handle ]
[ EVENT-PROCEDURE
event-internal-procedure
[ IN procedure-context ] ]
[ ( parameter [, parameter] ... ) ]
[ NO-ERROR ]
Calls an internal Progress procedure. FWD doesn't support asynchronous calls, remote calls and calls to external libraries.
IN proc-handle is supported (it converts to setInHandle(handle) on the call site), for persistent and super procedures alike.
The following options are not supported:
     • ASYNCHRONOUS ...

The RUN statement is used to execute an external or internal procedure. The control flow is switched to the invoked procedure. Once the called procedure returns, the calling procedure will:

  • execute the statement immediately following the RUN statement OR
  • if a condition leaves the called procedure, handle that condition as if it had been raised by the RUN statement (the condition propagates up the call path until a block which handles it is found or the application exits).

Which conditions leave the called procedure:

  • RETURN ERROR (all forms) always reaches the caller; see RETURN Statement.
  • An UNDO, THROW or a system error that the called procedure does not handle reaches the caller only if the file that contains the called procedure has BLOCK-LEVEL or ROUTINE-LEVEL ON ERROR UNDO, THROW. At the default level the called procedure ends, the caller goes on with the next statement and sees no error. See BLOCK-LEVEL and ROUTINE-LEVEL Statements.
  • STOP always reaches the caller, through every call mechanism (RUN, RUN VALUE, RUN ... IN handle, super procedures, functions, methods, constructors, property accessors and PUBLISH). NO-ERROR does not suppress it. An ON STOP phrase handles it; a CATCH block handles it only as a stop object (see Stop Objects).
  • RUN of a procedure file that does not exist, or that does not compile, raises STOP, not ERROR. NO-ERROR does not suppress it. On OpenEdge 11.7 and later (and in FWD) that STOP carries a Progress.Lang.StopError with error 293: a CATCH block for StopError, SysError or Error reached before an ON STOP phrase handles it (also under NO-ERROR, with ERROR-STATUS unchanged and no message shown), ON ERROR phrases do not stop it on its way, and an ON STOP phrase reached first handles it as a plain STOP. A CATCH for Progress.Lang.Stop does not catch it.
  • PUBLISH never passes an error from a subscriber to the publisher; STOP does pass through it. A subscriber's RETURN ERROR does set ERROR-STATUS:ERROR to yes in the publisher (OpenEdge 12.8 also records the messages of the error object; 11.6 records them only when the PUBLISH block has a CATCH) and RETURN-VALUE to its value.
  • OUTPUT and INPUT-OUTPUT parameters are copied back to the caller when the called procedure ends normally (including the default-level case above, where an error ends it without reaching the caller), but not when an error or STOP reaches the caller.
  • An OUTPUT TO opened inside the called procedure is reverted when the procedure ends with an error, RETURN ERROR or STOP.

When invoking a procedure, its name can be specified either using its name (hard coded in the 4GL source) or using an expression (to be evaluated at runtime), via the VALUE() clause.

Static Procedure Names

Each procedure file (e.g. .p files) is converted to a separate Java business logic class. The converted code of an external procedure will be emitted into the execute method of the corresponding Java class.

A RUN statement converts to a call through a static InvokeConfig call site, declared as a field of the business logic class. The call site holds the target name and the parameter modes; the statement clones it, adds the per-call settings (procedure handle, IN handle) and calls run() with the arguments. The runtime then instantiates the class of the target external procedure (or finds the internal procedure method) and executes it.

Internal procedures are converted to instance methods of the same Java class associated with the external procedure where the 4GL internal procedures were defined. A RUN statement which executes an internal procedure uses the same kind of call site as an external one; the runtime resolves the name to the Java method.

Example 1:

run external-proc.p.
run external-proc.p persistent set h.
run internal-proc in h.

Converted code:

private static final InvokeConfig RUN_CALL_SITE_1 = new InvokeConfig().setTarget("external-proc.p");
private static final InvokeConfig RUN_CALL_SITE_2 = new InvokeConfig().setTarget("external-proc.p").persistent();
private static final InvokeConfig RUN_CALL_SITE_3 = new InvokeConfig().setTarget("internal-proc");
...
RUN_CALL_SITE_1.clone().run();
RUN_CALL_SITE_2.clone().setProcedureHandle(h).run();
RUN_CALL_SITE_3.clone().setInHandle(h).run();

Details:

Invoking an external procedure via its explicit name results in a call through a static call site that names the target. The runtime instantiates the class that corresponds to the target and executes it. PERSISTENT SET and IN handle add the handle to the cloned call site.

Example 2:

[test.p]
procedure internal-proc:
   ...
end.

run internal-proc.

Converted code:

public class Test
{
   public void execute()
   {
      externalProcedure(Start.this, new Block((Body) () -> 
      {
         RUN_CALL_SITE_1.clone().run();     // call of the internal procedure "internal-proc" 
      }));
   }

   @LegacySignature(type = Type.PROCEDURE, name = "internalProc")
   public void internalProc()
   {
      internalProcedure(new Block((Body) () -> 
      {
         returnNormal();
      }));
   }
}

Details:

An internal procedure is converted to an instance method in the class which contains the converted external procedure (from which the internal procedure originated). It is invoked through a call site whose target is the 4GL name of the internal procedure (RUN_CALL_SITE_1 = new InvokeConfig().setTarget("internal-proc")); the runtime maps the name to the method.

Dynamic Procedure Names

It is possible to evaluate the name of the procedure (either external or internal) at runtime by passing an arbitrary expression as the procedure name to the RUN VALUE() statement. This type of call is converted using the ControlFlowOps.invoke function, which takes two parameters:

  • The first parameter is the caller object. The instance of the class which represents the current external procedure is passed as the caller.
  • The second parameter is an array of objects. The first element of the array is the expression of an arbitrary data type which yields the name of the procedure to run. The expression is cast to the character type using toStringMessage() function. The following array elements are the parameters that should be passed to the called procedure (you can read about the parameters in the next section).

When using the VALUE() clause, the conversion rules can't decide which needs to be invoked (an external or an internal procedure), as the name of said procedure is determined at runtime. Therefore, the runtime will first check if the given name is for an external procedure (using the mappings defined in the name_map.xml file which is dynamically loaded from the converted application's jar file when the server starts); if found, it will instantiate a new object of that type (which is for an external procedure class) and will invoke the execute() method using reflection.

If it can't find a Java class mapped to the assumed external procedure name, it will check if the mappings in name_map.xml file to find a Java method mapped to an internal procedure, for the calling external procedure class. If it is found, it will invoke it, passing the given parameters as appropriate. For internal procedure cases, the method is resolved only based on the name, ignoring the parameters - if the given parameters do not match the method's signature, the method invocation will fail.

Reflection is used for lookups and invocations. The SourceNameMapper provides the class and method name mapping from the Progress legacy names to the new Java names. The source for the name mapping data is the above mentioned name_map.xml file, which is an output of the conversion process.

From the ControlFlowOps, the following APIs are emitted during conversion:

ControlFlowOps API Details
invoke(handle target, String name, Object[] param) This method is used to invoke methods of a known signature and name in classes whose type is not known at compile-time. For this purpose Progress uses RUN internal_procedure IN procedure_handle.

The current conversion emits InvokeConfig call sites with setInHandle(handle) for RUN ... IN handle instead of this method.
invoke(Object caller, Object[] param) This method is used to invoke classes whose name is not known at compile-time. For this purpose Progress uses the RUN VALUE(expression) construct. The value of the expression is used to determine the procedure name. The procedure name may refer to an external or internal procedure. Since the procedures are translated into different Java entities (an external procedure is translated into a well known method name in a class and internal procedures are translated into instance methods), the processing takes this into account.

Initially the method assumes that the procedure name belongs to an external procedure file name (which should map to a class name). If found, a default constructor will be used to obtain an instance and the execute method will be invoked on this class. If there is no mapping for such a file name then an internal procedure is assumed. In this case the calling class is searched for the method that maps to this internal procedure. If found, this method will be invoked.
invokeMapped(handle target, BaseDataType name,
Object[] param)
invokeMapped(handle target, String name,
Object[] param)
This method is used to invoke methods of a known signature and name in classes whose type is not known at compile-time. For this purpose Progress uses RUN internal_procedure IN procedure_handle.

The method name given is in the form of a Progress 4GL internal procedure name and must be converted into the matching Java name before the invocation can proceed.

Currently the IN handle clause is not yet supported by conversion rules, so this method is not currently emitted.

Example 3:

/* run external procedure external-proc.p */
proc-name = "external-proc".
run value(proc-name + ".p").

Converted code:

/* run external procedure external-proc.p */
procName.assign("external-proc");
ControlFlowOps.invoke(Test.this, new Object[]
{
   (concat(procName, ".p")).toStringMessage()
});

Details:

Assuming the 4GL program file is named test.p, the first parameter for ControlFlowOps.invoke will be Test.this, to disambiguate between the current instance of the Block class being executed (see the Blocks chapter about how the 4GL blocks are converted) and the instance associated with the external procedure.

Example 4:

[test.p]
/* run internal procedure internal-proc */
proc-name = "internal-proc".
run value(proc-name).

Converted code:

/* run internal procedure internal-proc */
procName.assign("internal-proc");
ControlFlowOps.invoke(Test.this, new Object[]
{
   (procName).toStringMessage()
});

Details:

For the internal procedure case, the converted code looks the same as the external procedure case. It is the runtime which will decide that it needs to invoke an internal procedure and not an external one.

Procedure Parameters

The RUN statement parameter passing syntax to the is the following:

[ INPUT ]

  expression |
  TABLE temp-table-name |
  TABLE-HANDLE temp-table-handle
}

OUTPUT
{
  field |
  variable |
  TABLE temp-table-name
    [ APPEND ] |
  TABLE-HANDLE temp-table-handle
    [ APPEND ] |
  param-name AS data-type
}

INPUT-OUTPUT
{
  field |
  variable |
  TABLE temp-table-name
    [ APPEND ] |
  TABLE-HANDLE temp-table-handle
    [ APPEND ]
}

BUFFER buffer

where:

  • TABLE-HANDLE type is not supported.
  • param-name AS data-type clause is not supported.
  • BUFFER clause is not supported.

During conversion, the INPUT, OUTPUT and INPUT-OUTPUT options are ignored by FWD, as valid 4GL code doesn't allow the RUN parameters and the procedure's parameters to be out-of-sync. Each parameter specified with the RUN statement has an equivalent procedure-level parameter defined using DEFINE PARAMETER statements (please see the Variable Definitions section of the Data Types chapter of this book for details on how these statements get converted).

The conversion process will use only the expression, field, variable and temp-table-name to pass the parameters to the procedure invocation code.

When the evaluation of a parameter expression raises an error, the 4GL reports the additional error 984 ("Error attempting to push run-time parameters onto the stack") after it, once for the whole statement, and the call is not made; the same applies to the arguments of method calls, user-defined functions and DYNAMIC-FUNCTION. The conversion wraps every argument which is not a literal or a plain variable in ErrorManager.arg(() -> expression), which adds the 984 (also to ERROR-STATUS under NO-ERROR).

Note: examples 5 to 7 show the parameter passing of earlier FWD versions. The current conversion passes the same arguments to the call site's run(...) method, wrapping parameters that are not plain INPUT values in parameter objects; for example run r(input-output w). with an extent variable w converts to RUN_CALL_SITE_1.clone().run(new InputOutputExtentParameter<integer>(...)), with setModes("U") on the call site.

Example 5:

def temp-table tt
    field f1 as integer.
def var aDate as date.

run proc.p(5,             /* integer constant */
           "str" + "ing", /* string expression */
           aDate,         /* date variable */
           table tt).     /* table */

Converted code:

TempRecord1 tt = TemporaryBuffer.define(TempRecord1.class, "tt", false);
...
Proc proc0 = new Proc();
proc0.execute(new integer(5),       /* integer constant */
              concat("str", "ing"), /* string expression */
              aDate,                /* date variable */
              tt);                  /* table */

Details:

The parameters of an external procedure with the specified static name are passed to the execute method for the instantiated object which corresponds with the target external procedure.

Example 6:

def temp-table tt
    field f1 as integer.
def var aDate as date.

run internal-proc(5,
                  "str" + "ing",
                  aDate,
                  table tt).

Converted code:

TempRecord1 tt = TemporaryBuffer.define(TempRecord1.class, "tt", false);
...
internalProc(new integer(5),
             concat("str", "ing"),
             aDate,
             tt);

Details:

Parameters of an internal procedure with the specified static name are passed to the Java method which corresponds the target internal procedure.

Example 7:

def temp-table tt
    field f1 as integer.
def var aDate as date.

run value("internal" + "-proc") (5,
                                 "str" + "ing",
                                 aDate,
                                 table tt).

Converted code:

TempRecord1 tt = TemporaryBuffer.define(TempRecord1.class, "tt", false);
...
ControlFlowOps.invoke(Test.this, new Object[]
{
   (concat("internal", "-proc")).toStringMessage(),
   new integer(5),
   concat("str", "ing"),
   aDate,
   tt
});

Details:

Parameters of an external or internal procedure, with the name specified as an expression for the VALUE() clause, are passed using the second parameter (named param) of the ControlFlowOps.invoke function. In the param array, the first element represents the name of the procedure to run and the following elements are the parameters of the executed procedure which are passed to the invoked method using reflection.

Other Options

The NO-ERROR option is converted by wrapping the procedure-calling code in a silent() call, which suppresses the ERROR condition and records it in ERROR-STATUS.

Example 8:

run proc.p no-error.

Converted code:

silent(() -> RUN_CALL_SITE_1.clone().run());

Details:

As the NO-ERROR clause is specified with the RUN statement, the converted code runs the procedure invocation inside silent(). This changes the condition processing in the BlockManager/TransactionManager runtime code, such that the error condition is suppressed.

Behavior of RUN ... NO-ERROR:

  • ERROR-STATUS is written only by statements that have NO-ERROR. An error that is caught by a CATCH, handled by an ON phrase or thrown does not change it, and neither does a RUN without NO-ERROR. A NO-ERROR statement that succeeds resets it to ERROR = no, NUM-MESSAGES = 0.
  • Called procedure does RETURN ERROR or RETURN ERROR "value": ERROR-STATUS:ERROR = yes, NUM-MESSAGES = 0, RETURN-VALUE = "value".
  • Called procedure does RETURN ERROR <error object>: ERROR-STATUS:ERROR = yes, NUM-MESSAGES = number of messages in the object, GET-MESSAGE(1) = the message text (without the number).
  • Called procedure raises UNDO, THROW or a system error that it does not handle: at BLOCK-LEVEL or ROUTINE-LEVEL ERROR-STATUS holds the error (yes, the object's messages); at the default level the error does not reach the caller and ERROR-STATUS:ERROR is no.
  • NO-ERROR does not suppress STOP (also not the STOP raised by RUN of a missing or uncompilable procedure: on OpenEdge 11.7 and later a CATCH for its StopError still receives it, and ERROR-STATUS is left unchanged).
  • Inside a CATCH or FINALLY, a NO-ERROR statement sees only its own result, and after the call the caller sees only the original error.

STOP Statement

Raises the STOP condition. By default, this stops processing of a procedure, backs out the active transaction, and returns to the startup procedure. One can change this behavior by including the ON STOP phrase in an inner block header.

The user can initiate a STOP condition by pressing the STOP key ( which is CTRL-C by default).

Behavior:

  • STOP rolls back the active transaction.
  • STOP is handled by ON STOP phrases. The default ON ERROR processing and NO-ERROR do not handle it. A CATCH block handles it only as a stop object: OpenEdge 11.6 has no -catchStop and no Progress.Lang.Stop class; on OpenEdge 11.7 and later (with -catchStop 1, always in OpenEdge 12) a CATCH for Progress.Lang.Stop reached before an ON STOP phrase handles it (see Stop Objects). It leaves routines through every call mechanism, including PUBLISH, and reaches the caller's ON STOP phrase.
  • RUN of a procedure file that does not exist or does not compile raises STOP; on OpenEdge 11.7 and later it carries a Progress.Lang.StopError (see RUN Statement).
  • When STOP leaves a block that has a FINALLY: OpenEdge 11.6 does not run the FINALLY, unless the block itself has an ON STOP phrase; OpenEdge 11.7 and later (and FWD) run it once and the STOP then goes on, with the flow statements of the FINALLY unable to end it (see FINALLY Block).
  • STOP in the FINALLY of a counted loop is discarded when the iteration ends with a pending next iteration; otherwise it goes to the enclosing block (see FINALLY Block for the 11.6 and 12.8 rules).
  • A STOP-AFTER time-out raises a STOP which is handled the same way; see STOP-AFTER Option.
  • The conversion treats the code after a block which contains a STOP as reachable (like after an UNDO, THROW), since an ON STOP phrase or a CATCH block of an enclosing block can handle the condition.
  • ON STOP UNDO, RETURN ERROR turns the STOP into an ERROR (an empty Progress.Lang.AppError) in the caller.

4GL syntax:

STOP

STOP is converted using the BlockManager.stop() function.

Example:

stop.

Converted code:

stop();

Stop Objects

OpenEdge 11.7 introduced stop objects: a STOP condition is raised together with an instance of a class derived from Progress.Lang.Stop, which a CATCH block can handle. The classes are:

Class Raised by Converted CATCH
Progress.Lang.Stop The STOP statement, and the base class of the next three. catchStop(com.goldencode.p2j.oo.lang.Stop.class, s -> { ... })
Progress.Lang.StopAfter A STOP-AFTER time-out. catchStop(com.goldencode.p2j.oo.lang.StopAfter.class, s -> { ... })
Progress.Lang.UserInterrupt The user pressing the STOP key. catchStop(com.goldencode.p2j.oo.lang.UserInterrupt.class, s -> { ... })
Progress.Lang.LockConflict A lock wait which the user cancelled. catchStop(com.goldencode.p2j.oo.lang.LockConflict.class, s -> { ... })
Progress.Lang.StopError RUN of a program which is not found (error 293). This class derives from Progress.Lang.SysError, not from Progress.Lang.Stop. catchError(com.goldencode.p2j.oo.lang.StopError.class, e -> { ... })

Behavior:

  • A CATCH for a Stop class is used only when stop objects are enabled: on OpenEdge 11.7 by the -catchStop 1 startup parameter, always on OpenEdge 12 (the parameter was removed). In FWD this is the catch-stop startup parameter (a string node catch-stop under server/standard/runtime/default of the directory, or -catchStop on the command line); its default is 0, the OpenEdge 11.7 default. With 0, a STOP or STOP-AFTER reaches only a CATCH of the routine which raised it, not one of a caller.
  • With stop objects enabled, the first of an ON STOP phrase or a matching CATCH block met on the way out handles the STOP; the CATCH ends it like any caught error (the loop goes on or is left as described in CATCH Block). ON ERROR phrases and CATCH blocks for error classes do not handle it, NO-ERROR does not suppress it, and FINALLY blocks run on the way.
  • A StopError is an error object which travels with a STOP: a CATCH for StopError, SysError or Progress.Lang.Error reached before an ON STOP phrase handles it, also under NO-ERROR (then ERROR-STATUS is not changed and no message is shown); it passes ON ERROR phrases; an ON STOP phrase reached first handles it as a plain STOP, and the error message is then shown. A CATCH for Progress.Lang.Stop does not catch it. It does not need stop objects to be enabled. FWD raises the same StopError when an expression of a WHERE clause which the database evaluates on each record fails on a record (see the Queries chapter in Part 5).
  • There is no stop object for QUIT: a QUIT is handled by ON QUIT phrases only.

UNDO Statement

This statement backs out the modifications to database fields, undoable records and undoable variables made during the current iteration of a block (if a transaction is active), and initiates a control-flow action to follow the rollback. Without an active transaction nothing is rolled back: undoable variables, object properties and temp-table records keep their values (see Loop Behavior After a Condition).

4GL syntax:

UNDO [ label1 ]
      [  , LEAVE [ label2 ] |
      , NEXT  [ label2 ] |
      , RETRY [ label1 ] |
      , RETURN [ ERROR or NO-APPLY ] [ return-value ] |
      , THROW error-object-expression
   ]

The UNDO statement is tightly coupled with the condition processing and infinite loop processing implementations. This section will show only how this statement gets converted. For details about these other topics, please see the Conditions and the Infinite Loop Protection sections of the Blocks chapter.

This statement is converted to BlockManager.undo* API calls, depending on how it is used. If the UNDO is used without an explicit action to perform, it will default to LEAVE. To summarize the rules:

  • undoLeave - emitted when the UNDO [ label1 ], LEAVE [ label2 ] or UNDO [ label 1 ] form is used.
  • undoNext - emitted when the UNDO [ label1 ], NEXT [ label2 ] form is used.
  • undoRetry - emitted when the UNDO [ label1 ], RETRY [ label1 ] form is used, and the targeted block is an inner block. The explicitly targeted RETRY block must be the same as the block targeted by the UNDO. Otherwise the Progress 4GL compiler will generate an error.
  • undoRetryTopLevel - emitted when the UNDO [ label1 ], RETRY [ label1 ] or UNDO [ label 1 ] form is used, and the targeted block is a top-level block.
  • undoThrow - emitted when the UNDO, THROW error-object-expression form is used in an inner block (for example undoThrow(e) to rethrow a caught error in a CATCH).
  • undoThrowTopLevel - emitted when the UNDO, THROW error-object-expression form is used directly in the routine block (for example undoThrowTopLevel(ObjectOps.newInstance(com.goldencode.p2j.oo.lang.AppError.class, "II", "m5", 555))).
  • undoReturnNormal - emitted when the UNDO [ label1 ], RETURN [ return-value ] form is used, and the targeted block is not a top-level block.
  • undoReturnNormalTopLevel - emitted when the UNDO [ label1 ], RETURN [ return-value ] form is used, and the targeted block is a top-level block.
  • undoReturnConsume - emitted when the UNDO [ label1 ], RETURN NO-APPLY [ return-value ] form is used, and the targeted block is not a top-level block.
  • undoReturnConsumeTopLevel - emitted when the UNDO [ label1 ], RETURN NO-APPLY [ return-value ] form is used, and the targeted block is a top-level block.
  • undoReturnError - emitted when the UNDO [ label1 ], RETURN ERROR [ return-value ] form is used, and the targeted block is not a top-level block.
  • undoReturnErrorTopLevel - emitted when the UNDO [ label1 ], RETURN ERROR [ return-value ] form is used, and the targeted block is a top-level block.

The next table shows UNDO's different conversion forms. Even if the UNDO statement target is implicitly specified (by leaving out the label(s)), FWD will always emit in the converted code the explicit label(s) of the converted target block(s). This means that FWD must calculate the target block for the UNDO and the other actions when the labels are not explicitly specified. To understand the confusing, undocumented and complex rules for how implicitly targeted blocks are determined for the UNDO statement, please see the section entitled Determining the Target and Meaning of UNDO, LEAVE, NEXT and RETRY in the Blocks chapter.

4GL Form BlockManager API Example
UNDO [ label1 ],
LEAVE [ label2 ]

or

UNDO [ label1 ]
undoLeave()
undoLeave(label1)
undoLeave(label1, label2)
repeat:
  ...
  if l then undo, leave.
end.

Converted code:
repeat("loopLabel0", new Block()
{
   public void body()
   {
      ...
      if ((l).booleanValue())
      {
         undoLeave("loopLabel0");
      }
   }
});

UNDO [ label1 ],
NEXT [ label2 ]
undoNext()
undoNext(label1)
undoNext(label1, label2)
repeat:
  ...
  if l then undo, next.
End.

Converted code:
repeat("loopLabel0", new Block()
{
   public void body()
   {
      ...
      if ((l).booleanValue())
      {
         undoNext("loopLabel0");
      }
   }
});

UNDO [ label1 ],
RETRY [ label1 ]
undoRetry()
undoRetry(label1)
repeat:
  ...
  if l then undo, retry.
End.

Converted code:
repeat("loopLabel0", new Block()
{
   public void body()
   {
      ...
      if ((l).booleanValue())
      {
         undoRetry("loopLabel0");
      }
   }
});

UNDO, RETRY

UNDO is targeted to a top-level block.
undoRetryTopLevel()
if l then undo, retry.

Converted code:
if ((l).booleanValue())
{
   undoRetryTopLevel();
}

UNDO [ label1 ],
RETURN
[ return-value ]

UNDO is targeted to an inner block.
undoReturnNormal()
undoReturnNormal(label1)
undoReturnNormal(label1, returnValue)
undoReturnNormal(returnValue)
repeat:
  ...
  if l then undo, return "message".
End.

Converted code:
repeat("loopLabel0", new Block()
{
   public void body()
   {
      ...
      if ((l).booleanValue())
      {
         undoReturnNormal(
                  "loopLabel0",
                  "message");
      }
   }
});

UNDO, RETURN
[ return-value ]

UNDO is targeted to a top-level block.
undoReturnNormalTopLevel()
undoReturnNormalTopLevel(returnValue)
if l then undo, return "message".

Converted code:
if ((l).booleanValue())
{
   undoReturnNormalTopLevel(
                       "message");
}

UNDO [ label1 ],
RETURN NO-APPLY
[ return-value ]

UNDO is targeted to an inner block.
undoReturnConsume()
undoReturnConsume(label1)
undoReturnConsume(label1, returnValue)
Not properly converted by FWD at this time.
UNDO, RETURN NO-APPLY
[ return-value ]

UNDO is targeted to a top-level block.
undoReturnConsumeTopLevel()
undoReturnConsumeTopLevel(returnValue)
Not properly converted by FWD at this time.
UNDO [ label1 ],
RETURN ERROR
[ return-value ]

UNDO is targeted to an inner block.
undoReturnError()
undoReturnError(label1)
undoReturnError(label1, returnValue)
Not properly converted by FWD at this time.
UNDO, RETURN ERROR
[ return-value ]

UNDO is targeted to a top-level block.
undoReturnErrorTopLevel()
undoReturnErrorTopLevel(returnValue)
undo, return error "v".

Converted code:
undoReturnErrorTopLevel("v");

Behavior of UNDO

  • What is undone: see Loop Behavior After a Condition. In short: only with an active transaction; never NO-UNDO data; the failing iteration is undone before its CATCH and FINALLY run; changes made in a CATCH or FINALLY are not undone by the handler's own flow statements.
  • UNDO, RETRY is not performed when no statement in the block waits for input: it acts as NEXT in a counted loop and as LEAVE in a single DO block and in a loop without a counter. UNDO, NEXT also leaves a loop without a counter (DO WHILE, REPEAT WHILE, REPEAT without TO).
  • UNDO, RETURN [value] ends the routine normally; the caller sees no error and RETURN-VALUE is the value (or ""). UNDO, RETURN ERROR [value] gives the caller the same Progress.Lang.AppError as RETURN ERROR [value].
  • UNDO, THROW error-object raises the error object in the enclosing block; inside a CATCH, UNDO, THROW error-variable rethrows the caught object unchanged to the enclosing block's CATCH. Directly in a routine block, the object reaches the caller only if the routine's file has BLOCK-LEVEL or ROUTINE-LEVEL ON ERROR UNDO, THROW; at the default level the routine ends and the caller sees no error.
  • UNDO in a CATCH: unlabelled UNDO, LEAVE, UNDO, NEXT and UNDO, RETRY all leave the associated block of the CATCH; only UNDO, NEXT label with the label of the associated loop continues it. UNDO label, RETRY label that targets the associated block of a CATCH or FINALLY does not compile.
  • UNDO in a FINALLY: unlabelled UNDO, LEAVE, UNDO, NEXT and UNDO, RETRY only end the FINALLY; the iteration goes on as with an empty FINALLY (they convert to undoLeave(""), undoNext(""), undoRetry(""), targeting the FINALLY block). UNDO, LEAVE label / UNDO, NEXT label act like LEAVE label / NEXT label. An UNDO, THROW in a FINALLY replaces a pending RETURN ERROR of the body (OpenEdge 12.8; see FINALLY Block).

© 2004-2026 Golden Code Development Corporation. ALL RIGHTS RESERVED.