From OpenSCADAWiki
Jump to: navigation, search

1 The user programming language JavaLikeCalc

1.1 Elements of the language

Keywords: if, else, while, for, in, break, continue, return, function, using.
Constants:

  • decimal: digits 0-9 (12, 111, 678);
  • octal: digits 0-7 (012, 011, 076);
  • hexadecimal: digits 0-9, letters a-f or A-F (0x12, 0XAB);
  • real: 345.23, 2.1e5, 3.4E-5, 3e6;
  • boolean: true, false;
  • string: "hello", without going to the next line, however, with the support of direct concatenation of the string constants.

Types of variables:

  • integer: -263 ... 263, EVAL_INT(-9223372036854775807);
  • real: 3.4*10308, EVAL_REAL(-1.79E308);
  • Boolean: false, true, EVAL_BOOL(2);
  • string: a sequence of character-bytes (0...255) of any length, limited by the capacity of the memory and DB storage; EVAL_STR("<EVAL>").

Built-in constants: pi = 3.14159265..., e = 2.71828182..., EVAL_BOOL(2), EVAL_INT(-9223372036854775807), null,EVAL,EVAL_REAL(-1.79E308), EVAL_STR("<EVAL>")
Global attributes of the DAQ parameter (starting from the subsystem "DAQ", as follows {Type of DAQ module}.{Controller}.{Parameter}.{Attribute}).
The functions and parameters of the object model of OpenSCADA.

At.png The EVAL (Error VALue) variants and null are processed specially in conversion one to one depending on used the base type, that is you are free in use only null or EVAL for any cases.

1.2 Operations of the language

The operations supported by the language are presented in the table below. Priority of the operations is reduced from top to bottom. Operations with the same priority are in the same color group.

SymbolDescription
()Call of function.
{}Program blocks.
++Increment (post and pre).
--Decrement (post and pre).
-Unary minus.
!Logical negation.
~Bitwise negation.
*Multiplication.
/Division.
%Remainder of integer division.
+Addition
-Subtraction
<<Bitwise shift left
>>Bitwise shift right
>Greater
>=Greater than or equal to
<Less
<=Less than or equal to
==Equals
!=Unequal
|Bitwise "OR"
&Bitwise "AND"
^Bitwise "Exclusive OR"
&&Boolean "AND"
||Boolean "OR"
?:Conditional operation "i=(i<0)?0:i;"
=Assignment.
+=Assignment with addition.
-=Assignment with subtraction.
*=Assignment with multiplication.
/=Assignment with division.

1.3 Embedded functions of the language

The virtual machine of the language provides the following set of built-in functions general-purpose:

  • double max(double x, double x1) — maximum value of x and x1;
  • double min(double x, double x1) — minimum value of x and x1;
  • string typeof(ElTp vl) — type of the value vl;
  • string tr(string base) — translation to the base message.

To provide high speed work in mathematical calculations, the module provides built-in mathematical functions that are called at the level of the commands of the virtual machine:

  • double sin(double x) — sine x;
  • double cos(double x) — cosine x;
  • double tan(double x) — tangent x;
  • double sinh(double x) — hyperbolic sine of x;
  • double cosh(double x) — hyperbolic cosine of x;
  • double tanh(double x) — hyperbolic tangent of x;
  • double asin(double x) — arcsine of x;
  • double acos(double x) — arc cosine of x;
  • double atan(double x) — arctangent of x;
  • double rand(double x) — random number from 0 to x;
  • double lg(double x) — decimal logarithm of x;
  • double ln(double x) — natural logarithm of x;
  • double exp(double x) — exponent of x;
  • double pow(double x, double x1) — erection of x to the power x1;
  • double sqrt(double x) — the square root of x;
  • double abs(double x) — absolute value of x;
  • double sign(double x) — sign of x;
  • double ceil(double x) — rounding the number x to a greater integer;
  • double floor(double x) — rounding the number x to a smaller integer.

1.4 Operators of the language

The total list of the operators of the language:

  • var — operator of initialization of a variable; specifying a variable without assigning a value sets it to null-EVAL, which allows for one-time initialization of complex data types, such as an object, through the direct comparing and checking by isEVal();
  • if — operator "IF" of the condition;
  • else — operator "ELSE" of the condition;
  • while — definition of the "WHILE" loop;
  • for — definition of the "FOR" loop;
  • in — separator of the "FOR" cycle for object's properties scan;
  • break — interruption of the cycle;
  • continue — continue the cycle from the beginning;
  • function — definition of the internal function;
  • using — allows you to set the visibility space of the external functions of the often used libraries (using Special.FLibSYS;) for the next access only by the name of the function, has no effect for object access;
  • return — interrupt function and return the result that is copied to an attribute marked as return one (return 123;); in the middle of the internal function it is completed with a definite result;
  • new — creating an object, implemented for: the generic object "Object", massif "Array" and regular expressions "RegExp".
  • delete — delete/free of an object or its properties, while: internal variables are set in null-EVAL, external ones are replaced by an empty object, and the properties of the object are cleared.

1.4.1 Conditional operators

The language supports two types of conditions. First — this is the operation of condition for use within the expression, second — a global, based on the conditional operators.

The condition inside an expression is based on the operations '?' and ':'. As an example we'll write the following practical expression:

st_open = (pos >= 100) ? true : false;

Which reads as — if the variable pos greater than or equal to 100, the variable st_open is set to true, otherwise — to false.

The global condition is based on the conditional operators "if" and "else". As an example, we can show the same expression, but recorded in another way:

if(pos > 100) st_open = true; else st_open = false;

1.4.2 Loops

Two types of the loops are supported: while, for and for-in. The syntax of the loops corresponds to the programming languages: C++, Java and JavaScript.

Loop while is written generally as follows: while({condition}) {body of the loop};
Loop for is written as follows: for({pre-initialization};{condition};{post-calculation}) {body of the loop};
Loop for-in is written as follows: for({variable} in {object}) {body of the loop};
Where:

{condition} — expression, determining the condition;
{body of the loop} — the body of the loop of multiple execution;
{pre-initialization} — expression of pre-initialization of variables of the loop;
{post-calculation} — expression of modification of parameters of the loop after next iteration;
{variable} — variable, which will contain object's properties name at scan;
{object} — object for which the properties are scanned.

1.4.3 Internal functions

The language supports definition and call of internal functions. To determine the internal function, the keyword "function" is used and in general, the definition has a syntax: function {fName} ({var1}, {var2}, ... {varN}) { {the function body} }. Defining an internal function inside another is not allowed but it is allowed to call a previously defined one.

Calling an internal function is done in a typical way as a procedure {fName}({var1}, {var2}, ... {varN}); or as a function {vRez} = {fName}({var1}, {var2}, ... {varN});. The call of internal functions is valid only after their declaration is higher!

All defined variables into the main body inaccessible into the internal function and can be pass in as two way arguments of the internal function call or as the main function arguments. All defined variables into the internal function have itself namespace and inaccessible from the main body or any other internal function and can be pass out to the main body as two way arguments, return of the internal function call or the main function arguments. The internal function variables are registered for saving/restoring their context after second and more entry to the function, so they completely support the recursive calls!

Operator "return" into the internal function makes controllable finishing of the function execution and places a pointed variable or an expression result as the internal function call result.

An example of the internal function declaration and using in typical way shown next:

function sum(a, b, c, d) { return a + ((b==null)?0:b) + ((c==null)?0:c) + ((d==null)?0:d); }
rez = sum(1, 2);

1.4.4 Special characters of the string variables

The language supports the following special characters of the string variables:

"\n" — line feed;
"\t" — tabulation symbol;
"\b" — culling;
"\f" — page feed;
"\r" — carriage return;
"\\" — the character itself '\'.
"\041" — the '!' character, written in an octal number;
"\x21" — the '!' character, written in a hex number.