Skip to content

Control flow

Zezombye edited this page Oct 13, 2020 · 6 revisions

If, Elif, Else

An if/elif/else statement is simply represented by the following structure:

if A == 1:
    B = 2
elif A == 2:
    B = 3
elif A == 3:
    B = 4
else:
    B = 5

Loops

While loop

A while loop is represented by the following structure:

while A == 1:
    B = 2

It compiles to the workshop's While instruction.

Do/While loop

A do/while loop is represented by the following structure:

do:
    B = 2
while A == 1

It compiles to the workshop's Loop or Loop If instruction. As this instruction always goes to the beginning of the rule, the do: statement must always be at the beginning of the rule (excluding other do: statements).

Note that the while statement has no ending colon.

For loop

A for loop is represented by the following structure:

for i in range(1, 5, 2):
    B.append(i)
#B = [1,3,5]

Note that the range(start, stop, step) function, that can only be used here, has other forms:

for i in range(1,5) -> for i in range(1,5,1)
for i in range(5) -> for i in range(0,5,1)

As such, you can use the range(start, stop) and range(stop) signatures.

The RULE_CONDITION pseudo-value

An if or do... while statement can use the RULE_CONDITION pseudo-value, that represents all rule conditions.

It compiles to Loop/Abort if condition is true/false. As such, it can only be used in the following cases:

  • if RULE_CONDITION:
        continue
    
  • if not RULE_CONDITION:
        continue
    
  • if RULE_CONDITION:
        return
    
  • if not RULE_CONDITION:
        return
    
  • do:
        #...
    while RULE_CONDITION
    
  • do:
        #...
    while not RULE_CONDITION
    

Gotos

Although not recommended to use due to the now available control flow statements, gotos can be used in conjunction with a label.

For example:

if A == 4:
   goto lbl_0
B = 5
lbl_0:
C = 6

Labels are declared on their own line, and must include a colon at the end (but no additional indentation).

Due to the limitations of the workshop, labels must be in the same rule as the goto instruction, and cannot be before it.

Additionally, dynamic gotos can be specified using the special keyword loc:

goto loc+A

This is however not recommended and can lead to very confusing code; you should consider using switches instead.

These statements are compiled to Skip or Skip If instructions.

OverPy guarantees a fixed amount of instructions:

  • Each logical line counts for one instruction, except for the switch statement which counts for 2 instructions.
  • Each unindent counts for one instruction.

This is true regardless of optimizations.