Control Flow

This topic documents control flow structures in the language.

Simple if Statement

The simple if statement checks the top element on the stack. If this value (<CONDITION>) is truthy (non-zero), the interpreter executes the block inside (<CODE>).

Syntax:

<CONDITION> if
<CODE>
end

Example:

1 if
10 println
end
0 if
20 println
end

In this example, only 10 will be printed, as 1 is truthy and 0 is falsy.

Compound if Statement

The compound if statement checks the top element on the stack. If this value (<CONDITION>) is truthy, the interpreter executes the block inside (<CODE>). Otherwise, the interpreter executes the block after else (<OTHERWISE>).

Syntax:

<CONDITION> if
<CODE>
else
<OTHERWISE>
end

Example:

0 if
10 println
else
20 println
end

In this example, "20" will be printed because the condition is 0 (falsy).

Loops (loop)

The loop keyword creates an infinite loop.

Use break to exit the loop.

Use continue to jump to the next iteration.

Syntax:

loop
<CODE>
end

Example with if and break:

0 loop
dup println
dup 10 = if
break
end
1 +
end

This will print 1, 2, 3, 4, 5, 6, 7, 8, 9 and 10 and then exit the loop.