Sequencer - ChaiScript
Expression Syntax
The Chai script expressions make use of ChaiScript parser to parse and evaluate ChaiScript code.
ChaiScript is a strongly typed declarative language with syntax similar to C++. Important differences from C++ are: Flow-of-control constructs like if, for and while use code enclosed in { … }; they cannot take single statements. There are no post-increment or post-decrement operators (++ and -- after the variable name).
Expressions can take many forms - the last expression result is returned as the boolean value. MyArg1 > MyArg2
It is possible to add debug prints in the expressions. print(Count); Count>5
When comparing non-boolean values with boolean comparisons the values need to be cast to boolean. bool(Count1) || bool(Count2)
Loops and Conditionals require brackets for the code block. if (expression) { print("expression is true"); }
Conditional expressions must have bool type result, that may requre casting of values to bool. if (bool(ArgumentOut)) { print("Good"); }
The expression does not support variable declarations due to performance reasons. The expression is parsed once and then reevaluated each time needed. This makes declaring local variables in the expression not possible as they would be declared anew each time and cause a symbol redeclaration error.
Note: For operators expecting a boolean argument, use bool(MyInt)
to construct a boolean value out of other primitive types.
Mathematical operators supported by ChaiScript
Operator | Description | Priority |
---|---|---|
?: | ternary operator; expects a boolean argument, other values can be used via explicit cast: bool(MyInt) ? true:false | 1 |
|| | logical or; expects boolean arguments, other values can be used via explicit cast: bool(MyInt1) || bool(MyInt2) | 2 |
&& | logical and; expects boolean arguments, other values can be used via explicit cast: bool(MyInt1) && bool(MyInt2) | 3 |
| | bitwise or | 4 |
^ | bitwise xor | 5 |
& | bitwise and | 6 |
!= | not equal | 7 |
== | equal | 7 |
<= | less or equal | 8 |
>= | greater or equal | 8 |
< | less than | 8 |
> | greater than | 8 |
<< | shift left | 9 |
>> | shift right | 9 |
+ | addition | 10 |
- | subtraction | 10 |
* | multiplication | 11 |
/ | division | 11 |
% | modulo | 11 |
++ | prefix increase value | 12 |
-- | prefix decrease value | 12 |
- | prefix minus sign | 12 |
+ | prefix plus sign | 12 |
! | prefix invert; expects a boolean argument, other values can be used via explicit cast: !bool(MyInt1) | 12 |
~ | prefix bitwise invert | 12 |
Note: Lines in multi-line ChaiScript are not allowed to start with an Operator
Note: For functions expecting a float or double type argument use double()
constructor like pow(double(MyInt), 2.0)
; to call with integer numbers.
Mathematical functions supported by ChaiScript for double
and float
types
Function | # of arguments | Explanation |
---|---|---|
sin | 1 | sine function |
cos | 1 | cosine function |
tan | 1 | tangent function |
asin | 1 | arcus sine function |
acos | 1 | arcus cosine function |
atan | 1 | arcus tangens function |
sinh | 1 | hyperbolic sine function |
cosh | 1 | hyperbolic cosine |
tanh | 1 | hyperbolic tangent function |
asinh | 1 | hyperbolic arcus sine function |
acosh | 1 | hyperbolic arcus tangent function |
atanh | 1 | hyperbolic arcus tangent function |
log2 | 1 | logarithm to the base 2 |
log10 | 1 | logarithm to the base 10 |
log | 1 | logarithm to base e (2.71828...) |
exp | 1 | e raised to the power of argument value |
exp2 | 1 | 2 raised to the power of argument value |
pow | 2 | first argument raised to the power of second |
sqrt | 1 | square root of a value |
hypot | 2 | Computes the square root of the sum of the squares of two arguments |
sign | 1 | sign function -1 if < 0; 1 if > 0 |
rint | 1 | round to nearest integer |
fabs | 1 | absolute value |
fmin | 2 | min of arguments |
fmax | 2 | max of arguments |
fdim | 2 | Equivalent to fmax(arg1-arg2, 0) |
fma | 3 | Computes (arg1 * arg2) + arg3 as if to infinite precision and rounded only once to fit the result type |
erf | 1 | Computes the error function of argument |
erfc | 1 | Computes the complementary error functionof arg, that is 1.0-erf(argument) |
ceil | 1 | Computes the smallest integer value not less than the argument |
floor | 1 | Computes the largest integer value not greater than the argument |
fmod | 2 | Computes the floating-point remainder of the division operation the <first argument> / the <second argument> |
trunc | 1 | Computes the nearest integer not greater in magnitude than the argument |
round | 1 | Computes the nearest integer value to the argument (in floating-point format), rounding halfway cases away from zero |
isinf | 1 | Determines if the given floating-point number argument is a positive or negative infinity |
isnan | 1 | Determines if the given floating point number argument is a not-a-number (NaN) value |
signbit | 1 | Determines if the given floating point number argument is negative. |
isnormal | 1 | Determines if the given floating point number argument is normal, i.e. is neither zero, subnormal, infinite, nor NaN |
isfinite | 1 | Determines if the given floating point number argument has finite value i.e. it is normal, subnormal or zero, but not infinite or NaN |
isunordered | 2 | Returns 1 if either argument is NaN, 0 otherwise |
_pi | 0 | the value of PI (3.141592653589793238462643) |
_e | 0 | Euler's number (2.718281828459045235360287) |
Mathematical functions supported by ChaiScript for number types
Function | # of arguments | Explanation |
---|---|---|
bool | 1 | Constructs boolean value out of other primitive types and string; where an empty string is considered false |
min | 2 | Returns the minimum value of two numbers |
max | 2 | Returns the maximum value of two numbers |
odd | 1 | Returns true if the value is odd |
even | 1 | Returns true if the value is even |
abs | 1 | Computes the absolute value of an integer argument. |
String functions supported by ChaiScript
Function | # of arguments | Explanation |
---|---|---|
find | 2 | Returns the position of the second argument string in the first argument string or -1 |
rfind | 2 | Returns the position of the last match of the second argument string in the first argument string or -1 |
ltrim | 1 | Returns left trimmed string of the string argument |
rtrim | 1 | Returns right trimmed string of the string argument |
trim | 1 | Returns trimmed string of the string argument |
replace | 3 | Returns string where in the first argument the occurrences of the second argument are replaced for the third argument |
toLowerCase | 1 | Returns the string argument in lowercase |
toUpperCase | 1 | Returns the string argument in upper case |
See also EventIn, State, RunOperation, and Operation.
Get started with CDP Studio today
Let us help you take your great ideas and turn them into the products your customer will love.