OEP

136

Title

Compact IF and expanded WHILE

Summary

Provide alternate forms of IF and WHILE.

Owner

Adam Sampson <ats@offog.org>

Status

Proposed

Date-Proposed

2006-03-14

Keywords

language if while

occam 2 provides the following processes already:

IF
  condition1
    process1
  condition2
    process2
  IF replicator
    condition3
      process3
  ...

Each boolean condition in turn is tested; when one is evaluated to TRUE, the associated process is run and no further guards are tested. If no guards match, the process is equivalent to STOP.

WHILE condition
  process

The boolean condition is tested and, if it evaluates to TRUE, the process is run; this repeats until the condition evaluates to FALSE.

While the general form of the IF process is extremely flexible, it is frequently used to test just one condition -- in which case a TRUE SKIP condition must be given to override the implicit STOP if no conditions match. To provide a more convenient syntax for simple tests like this, we could permit:

IF condition
  process

(This does not collide with the syntax for a replicated IF, since a replicator expression cannot be a valid Boolean expression.)

For symmetry, this suggests that we should also permit an expanded form of WHILE (similar to the loop construct Honeysuckle provides):

WHILE
  condition1
    process1
  condition2
    process2
  WHILE replicator
    process3
  ...

This would loop, running each guard in turn until one of the conditions evaluated to FALSE, at which point no further guards would be tested and the loop would exit. This would provide a convenient syntax for common loops such as the following in which a process must execute before the main condition must be tested:

MOBILE []BYTE s:
WHILE
  TRUE
    read.line (fd, s)
  s <> ""
    process.line (s)

OEP/136 (last edited 2007-09-27 00:59:02 by ats1)