New control structures in Scriptol

Modern languages ​​can provide more concise and generic structures.

The if then control structure

For conditional processing, the instruction is the keyword if followed by a boolean expression, a list of instructions to be executed or not, depending on condition, and the keyword /if.

Here is the syntax in the Scriptol programming language.

if x = 5
  print "equal"
/if
We introduce an alternative by the keyword else: when the condition is false, another set of instructions must be processed.

if x = 5
  print "equal"
else
  print "different"
/if

When the body of the structure has only one instruction, and is not another structure, the syntax can be reduced tos one line.

if x = 5 print x 
If the instruction is not a command like "print", "break" or "continue" ... etc, the keyword "let" is required.

if x = 5 let y + 1 

composite if

The structure can be extended into a broader construct that is both an "if" structure and a "switch case", which is also more advanced than the equivalent C because we can test any type of variable.

if x 
= 5: print "equal"
> 5: print "more"
else
  print "less"
/if

for

The for loop is common to all languages, and the variation for each that can scan the contents of a list is added to all dynamic languages. But in JavaScript, For each is so inefficient that it is better to use the classic for loop.

The syntax is simplified in Scriptol through instruction in range .

for int x in 0 .. 10
  print
x
/for

If the body of the structure has only one instruction, a simplified one-line syntax can be used:

for int x in 0 .. 10 print x

The control structure can browse for a range, but also a text or a table.

text hello = "hello"
for text t in hello  print t

array a = { 1, 2, 3 }
for dyn x in a print x

while

The C language has created a while loop to execute until a condition is met. The disadvantage is that the C language​ and derivatives (C++, Java, C #) easily lead to infinite loops. Here are two examples.

while(x < 5);
{
printf("%d\n", x);
  x += 1:
}

Perhaps you have noticed immediately, (or maybe not!), but the program is buggy. The programmer, prompted by the habit of putting a semicolon after each statement accidentally added one to a post-condition!

Another example, without semicolon misplaced this time, and equally common. In this program we want to skip a block of statements when x is 5 (break statement), and exit the loop when x is 10 (continue statement).

while(true)
{
printf("%d\n", x); if(x == 10) break;
  if(x == 5) continue;
  ... some instructions ...
  x += 1;
}

Nothing shocking at first glance in this program, but if you look closer, once reached the value 5 in x, the counter will not change ever, because the increment is skipped by the continue statement. So the end of the loop will never be reached.

To increment a variable, Scriptol uses x + 1. This would not work in C or any language derived where the instructions are also expressions that return a value. So this is how looks a while loop in Scriptol.

while x < 5
  print x
  x + 1
/while 

An infinite loop is always possible with this syntax, but to avoid it, a more secure syntax is provided where the loop end is replaced by an instruction to increment the counter.

while true
  print x if x = 10 break
  if x = 5 continue
  ... some instructions ...
let x + 1

Consequently, in Scriptol, the continue statement jumps to the let statement and the condition variable is always incremented.

scan

To browse the contents of one or more arrays, and apply a treatment to each element of (the) table (x), Scriptol added the unique structure scan.
The difference with the for loop is that the latter reads the list items while scan points out them. Scan lets you apply a processing to an arrya and modify it, for allowing only read its contents.

scan a 
   a[] * a[]
   print a[]
/scan
The current element is represented by an empty index. It can be changed and in the example above, we multiply it by itself (and replace the value in the array) before displaying the result.

Example with two arrays:

scan a, b
  print a[] + b[]
/scan

scan with a function

Sometimes it is necessary to apply a function to each element of a list. In JavaScript there is the apply method. When a function requires n arguments, apply replaces the list of n arguments by an array. This remains limited. It would be more interesting to have a function with one argument and that this function is applied repeatedly to each element of an array so that each is in turn taken as the argument of the function. It is what Scriptol allows with scan by.

scan array by function

The function must be defined with one argument. If the function accepts two arguments, we must have two arrays:

scan array1, array2 by function 

And so on for each additional argument.

Complete example with a single argument function, which computes the square of each:

array a = {1,2,3,4 }
 
void fun(number x)
print x * x
return
scan a by fun

An example with two arguments:

array a = {1,2,3,4 } 
array b = {10,20,30,40 } 

void fun(number x, number y)
print x + y
return
scan a, b by fun

This adds the contents of both arrays.

In conclusion, Scriptol has made progress in control structures. These were not included in languages ​​created meanwhile, since 2001.