Language

Pliant language controls

A program provided as a finit set of instructions, with no loop, will not do much.

Let's imagine that we want to compute the math function factorial(5) = 1*2*3*4*5

We could do it as:

console "factorial(5) = " 1*2*3*4*5 eol

We could also do it more step by step as:

var Int f := 1
f := f*2
f := f*3
f := f*4
f := f*5
console "factorial(5) = " f eol

For

The new sequence might look heavier at first, but it's just an intermediate level to introduce the loop notion:

var Int f := 1
for (var Int i) 2 5
  f := f*i
console "factorial(5) = " f eol

The 'for' bloc means: 'i' variable will be 2, then 3, then 4, then 5, and each time, we do f := f*i

You can provide a 'step' option to 'for' instruction if you want the 'i' variable step to not be the default value which is one:

var Int f := 1
for (var Int i) 5 2 step -1
  f := f*i
console "factorial(5) = " f eol

In the previous sample, 'i' variable value has been 5, then 4, then 3, then 2.

While

Here are the different ways to do loops in Pliant:

var Int f := 1
var Int i := 2
while i<=5
  f := f*i
  i := i+1
console "factorial(5) = " f eol

Part, but no goto

var Int f := 1
var Int i := 2
part loop
  f := f*i
  i := i+1
  if i<=5
    restart loop
console "factorial(5) = " f eol

Or:

var Int f := 1
var Int i := 2
part loop
  if i>5
    leave loop
  f := f*i
  i := i+1
  restart loop
console "factorial(5) = " f eol

'part' takes an identifier as a parameter. Here, we have choosen 'loop', but it could be any indentifier. The identifier is used by 'leave' and 'restart' instruction to select the right part in case your program contains several nested ones.
'part' can accect a second string parameter used for debugging purpose only. It will be displayed if an only if some error appends while the bloc is executing.

part loop "Computing factorial 5"
  ...

The 'goto' instruction is not provided by standard Pliant libraries, so the following program will not work:

var Int f := 1
var Int i := 2
label loop
f := f*i
i := i+1
if i<=5
  goto loop

Also defining 'label' and 'goto' is a very good exercise for advanced programmers that are learning Pliant meta programming.

If

If is the statement introducing conditional execution:

var Int i := 1
if i=1
  console "one" eol
eif i=2
  console "two" eol
eif i=3
  console "three" eol
else
  console "more than three" eol

Shunt

The next program is equivalent to the previous one:

var Int i := 1
console (shunt i=1 "one" i=2 "two" i=3 "three" "more than three") eol

The weak point of 'shunt' is that it want's the result to be the one of one of it's aguments, so in complex situtations, you might have to add an explicit casting:

var CBool c
var Int16 i
var Int j := shunt c i 12

does not work because 'i' type is 'Int16' and the type of 12 is 'uInt', so 'shunt' is not smart enough to decide that the result type must be 'Int'. You have to write it as:

var Int j := shunt c (cast i Int) 12

so that 'Int' be the type of one of the arguments.