Literals are values that you write directly in the script, like 0.8 or 120.There are three kinds of literals:
0.8
120
0.80
100.0
Double
1234
Int
0xA9F
0x8D01
Word64
()
Variables are names that represent values, like foo or bar. They always start witha letter and are followed by zero or more letters or numbers. Examples of validvariables are: x, y, MyVariable, ThisIsAVariable, this2isavariable, Year2018.
foo
bar
x
y
MyVariable
ThisIsAVariable
this2isavariable
Year2018
Types are the kinds of values a script can work with. For instance, there are numbers,times, or booleans. Types are used to classify values and to provide useful informationwhen a script contains a mistake (like adding a boolean to an integer, or passing thewrong parameter to a function). A list of all types available in scripts follow:
Bool
true
false
Unit
EpochTime
Word16
Word32
* -> *
Double -> Int
Like we mentioned earlier (see the Types section), a variable can contain a function. Toapply an argument to a function, simply put in parenthesis the argument right after thefunction variable. For example, if we have a function called f of type Double -> Bool,and a variable x of type Double, the function f can be applied to x simply bytyping f(x). The result will have type Bool.
f
Double -> Bool
f(x)
Warning: this function is only available when using virtual parameters.
There is a special predefined function called input. It is used to get values from theparameters associated to a virtual parameter. Its type is Int -> EpochTime -> Double.The first parameter is the parameter index. If you assigned two parameters to the virtualparameter, the first parameter has index 1, and the second parameter has index 2.The second argument of input is the time to get the value from. For example, input(1,now)will get the current value of the first parameter. If there is no value at the given time,() will be returned.
input
Int -> EpochTime -> Double
input(1,now)
The alias input1 is also available, and it’s equivalent to input(1,now). This alias canbe used when migrating scripts from masks to virtual parameters.
input1
This function gets the latest value from a parameter before a given time. An example:
x := latestBefore(now - days(3), 1);
In this example, x is set to the latest value of the parameter 1 before three days ago.
This function works similarly to input, but only has the parameter index argument.The time time will be automatically set to the time of the latest value in theparameter.
Note: It is equivalent to latestBefore(now).
latestBefore(now)
To assign a value to a variable, the following syntax is used:
<variable> := <expression> ;
Where <variable> is any variable name, and <expression> is any expression involvingvariables and literals. An example:
<variable>
<expression>
x := 2 + sqrt(3) ;
After this statement, the value of x will be the result of evaluating the expression 2 + sqrt(3).Please note the semicolon after the expression in the assignment syntax.
2 + sqrt(3)
To clear the value of a variable, use the following syntax:
<variable> ;
After this statement, the variable will be left undefined.
Using conditionals you can choose to execute one of two options. The syntax goes asfollows:
IF <boolean expression> THEN <code 1> ELSE <code 2> END_IF ;
The ELSE part is optional. This works too:
ELSE
IF <boolean expression> THEN <code> END_IF ;
This way, the code inside the conditional will only be executed if the boolean expression evaluatesto true.
Time loops are a handy tool to execute some code for a sequence of time values. This is the syntax:
WITH <variable> FROM <time expression> TO <time expression> EVERY <time expression> DO <code> END_LOOP ;
This code will do as follows:
FROM
EVERY
TO
An example:
acc := 0; WITH t FROM now - days(5) TO now EVERY minutes(30) DO x := input(1,t); IF not(isUnit(x)) THEN acc := acc + x; END_IF; END_LOOP ;
This time loop will add all values from the input parameter 1 from the last 5 days, every30 minutes. The result will be stored in the acc variable. Sometimes there is no valueat time t, so we need to use isUnit to account for this case.
acc
t
isUnit
Addition (+) and subtraction (-) can be used with Int, Double, and EpochTime.Multiplication and division only with Int and Double. Division always gives a resultof type Double, to account for potential inexact divisions.Exponentiation is done with **. For example, 2 ** 3 = 8.
+
-
**
2 ** 3 = 8
The boolean values are True and False. Lower-cased variants (true and false) are also defined.The function not :: Bool -> Bool gives True for False, and False for True.
True
False
not :: Bool -> Bool
|| :: Bool -> Bool -> Bool
OR
&& :: Bool -> Bool -> Bool
AND
XOR :: Bool -> Bool -> Bool
isSet :: Int -> Int -> Bool
n
0 isSet n = False
MOD :: Int -> Int -> Int
9 MOD 4 = 1
The operator ` :: Double -> Int -> Double truncates a number to the given number of decimals. TRUNC is also accepted.
` :: Double -> Int -> Double
TRUNC
All rounding functions return integers. They accept both Double and Int arguments, but theyleave Int values unmodified. The rounding functions are:
floor
ceiling
round
truncate
All operators work with Int and Double, and only values of the same type can be compared.The available operators are:
==
<>
<
<=
>
>=
The functions max and min have two arguments can be used with values of Int, Double, and EpochTime.They return the smallest (or largest) of its arguments. For example: max(-2,1) = 1, min(-3,2) = -3.
max
min
max(-2,1) = 1
min(-3,2) = -3
The functions abs and negate can be applied to values of type Int or Double. The functionabs returns the absolute value of a number, and the function negate calculates the oppositenumber.
abs
negate
now :: EpochTime
seconds :: Int -> EpochTime
minutes :: Int -> EpochTime
hours :: Int -> EpochTime
weeks: Int -> EpochTime
monthsAgo :: Int -> EpochTime
hour :: EpochTime -> EpochTime
day :: EpochTime -> EpochTime
month :: EpochTime -> EpochTime
year :: EpochTime -> EpochTime
double :: Int -> Double
boolToInt :: Bool -> Int
0
1
intToBool :: Int -> Bool
timeToInt :: EpochTime -> Int
seconds
doubleBits :: Double -> Word64
recip :: Double -> Double
recip(2) = 0.5
sqrt :: Double -> Double
limit :: Double -> Double -> Double -> Double
limit(l,u,x)
l
x <= l
u
x >= u
These functions work with Word16, Word32 and Word64 types. We would refer to them as WordN in the followingfunctions.
WordN
testBit :: WordN -> Int -> Bool
setBit :: WordN -> Int -> WordN
clearBit :: WordN -> Int -> WordN
complementBit :: WordN -> Int -> WordN
complement :: WordN -> WordN
complement(0xF0) = 0x0F
shift :: WordN -> Int -> WordN
shift(0x0F0,4) = 0xF00
shift(0x0F0,-4) = 0x00F
shift(0x0F0,-8) = 0x0
fromWord :: WordN -> Int
.&.
.|.
XOR
Three functions are available: toWord16, toWord32 and toWord64. They all work for all unsigned integer types.If the target type is smaller than the argument type, any excess bits will be truncated. The function toWord64 canalso be used to produce a Word64 from an Int, which will have the same bit representation as the original Int butmight have a different value.
toWord16
toWord32
toWord64
random :: () -> Double
[0,1)
x := 10 * random() ;
random
The function isUnit :: * -> Bool accepts arguments of any type, and returns True when theargument is ().
isUnit :: * -> Bool
pi :: Double
sin :: Double -> Double
cos :: Double -> Double
tan :: Double -> Double
Powered by BetterDocs