Goal oriented, asynchronous or synchronous

This control structure adds to the Scriptol language goal orientation, it launches a process that runs asynchronously.

It works on global variables or objects. The syntax is as follows:

to ...condition... for duration [, delay]
  ...process...
/to

The compiler also recognizes the syntax in one line:

to ...condition... for duration [, delay] do ...action... 

Condition

The condition is a relational expression that defines the goal. For example:

a >= 100

An action is performed repetitively until the global variable value is 100.

Duration

The duration is the time limit represented a real number, in seconds, during which the processing may be performed. This can be an integer or a real number, but still a literal.
If you do not want time limit, put the & symbol to represent infinity.

Delay

This parameter is optional. It puts a delay between two iterations of the action. It is an integer in milliseconds.
The default is no delay.

Action

The action is a block of statements defined to try to achieve the goal, the block is executed repeatedly like an iteration (but this is not, it is in asynchronous mode).

We can also try to maximize a value. In this case, when the time limit is reached, we ends with the value closest to the given goal.

This works asynchronously and if you set multiple goals in a program, the actions are performed at the same time, therefore it must operate on independent variables.

It's not really concurrency, even though it looks like. The generated JavaScript code is based on the setInterval and setTimeout functions. This control structure is suitable for simulation and control of a robot to command elements or parts and make them tend towards a goal each, independently of one another.

Examples

Example of simple code:

int ai = 50
int bi = 5

to ai >= 100 for &
   ai = ai + bi
   print ai
/to 

Sample of one-line code:

int ci = 100

int addit()
   ci + bi
return ci

to ci >= 1000 for 0.9 do print addit()

If the two examples are placed in the same program, they will run at the same time and intermediate results will be displayed alternately.

If your code works locally with Node.js, it is also possible to use the process and child_process libraries that provide similar features.

Here is the JavaScript code from the scriptol.js library allowing goal orientation:

exports.goal = function(condi, dur, actio) { 
var iter = setInterval(function() {
if(condi()) {
clearInterval(iter);
clearTimeout(limiter);
return;
}
actio();
}, 0);
if(dur == "&") dur = 2147483647;
var limiter=setTimeout(function() { clearInterval(iter); }, dur);
}

This function is called with two functions as arguments, and the duration for third argument. Look at the code generated by the compiler for more information.

Synchronous mode

For synchronous operation, ie for the accomplishment of goals being performed one after another, the syntax is:

to ...condition... while ...duration...
  ...process...
/to

On a single line:

to ...condition... while ...duration... do ...action... 

The reserved word while replaces the word for.

Here is the JavaScript code to the synchronous mode:

exports.goalSync=function(condi, dur, actio) { 
var stopFlag = false;
if(dur == "&") dur = 2147483647;
var limiter=setTimeout(function() { stopFlag = true; }, dur);
while(stopFlag == false) {
if(condi()) {
clearTimeout(limiter); stopFlag=true;
return;
}
actio();
}
}

The use is more general than the asynchronous mode, this control structure can be used in any processing.

You will find examples of goal oriented SVG programs on Scriptol.com.