Home>>Primitives

Primitives

Primitives are procedures that are executed in control network. These primitives appears on arrows that are between two states. When program moves one state to other, these primitives are executed. CNP Compiler is integrated to Pascal compiler Lazarus. So Pascal programming language is used to write primitives.There are some important variables to set and get direction of the execution. Here is a basic primitive code;

{&P} procedure Print(word:string); begin writeln("Word is ", word); end;

"{&P}" string indicates this procedure is a primitive, so it can be available for control network. There could be other helping functions, and they can used in primitives. Helping functions are not displayed in list of available primitives.

"Print" primitive takes a string argument and prints it.

Movement Direction Control

In CNP primitives are implemented differently in forward and backward execution. This is one of powerful features of CNP. Controlling movement from primitives is achieved with FORW and FAILURE variables. In CNP program these variables are automatically defined, so you just call them when it is appropriate.


FORW variable has a boolean value and it shows us direction of the current execution. It is changed by the system. If FORW is equal to true it means execution is forward, otherwise execution is backward. Here is a code how FORW variable is used;

{&P} procedure Increase(var num:integer); begin if FORW then begin num := num + 1; writeln("Forward execution, num is ", num); end else begin num := num -1 writeln("Backward execution, num is", num); end; end;

"Increase" primitive takes num parameter with type integer. "var" keyword is used to pass parameters with references, so we can change its value. In forward execution primitive increases value of num by 1 and prints it, in backward execution decreases num by 1 and prints it.

In general, primitive do its job normally in forward execution, in backward execution it retrieves what it's done in forward execution


FAILURE variable also has boolean value, but it is changed by programmer. If it set to true, it means failure is occurred and backward execution will be started. Here is a code how FAILURE variable is used;

{&P} procedure IncreaseTo10(var num:integer); begin if FORW then begin num := num + 1; if num = 10 then FAILURE := true else writeln("Forward execution, num is ", num); end else begin num := num -1 writeln("Backward execution, num is", num); end; end;

"IncreaseTo10" primitive takes num parameter as integer. It increases num by 1 up to 10. If num is equal to 10 FAILURE variable set to true and backward execution started. Setting FAILURE to true does not terminates forward execution immediately. Forward execution is terminated after forward code implemented completely.

Home>>Primitives