Variable, Vector & String Definition¶
MathExpr supports the definition of expression local variables, vectors and strings. The definitions must be unique as shadowing is not allowed and object lifetimes are based on scope. Definitions use the following general form:
Variable Definition¶
Variables are of numeric type denoting a single value. They can be explicitly initialised to a value, otherwise they will be defaulted to zero. The following are examples of variable definitions:
Initialise x to zero¶
Initialise y to three¶
Initialise z to the expression¶
Initialise const literal n¶
Vector Definition¶
Vectors are arrays of a common numeric type. The elements in a vector can be explicitly initialised, otherwise they will all be defaulted to zero. The following are examples of vector definitions:
Initialise all values to zero¶
Initialise all values to zero¶
Initialise all values to given value or expression¶
Initialise all values iota style¶
Initialise the first two values, all other elements to zero¶
Initialise the first three (all) values¶
Initialise vector from a vector¶
Initialise vector from a smaller vector¶
Non-initialised vector¶
Error as there are too many initialisers¶
- Error as a vector of size zero is not allowed.
String Definition¶
Strings are sequences comprised of 8-bit characters. They can only be defined with an explicit initialisation value. The following are examples of string variable definitions:
Initialise to a string¶
Initialise to an empty string¶
Initialise to a string expression¶
Initialise to a string range¶
Initialise to another string variable¶
Initialise to another string variable range¶
Initialise to a string expression¶
Initialise to a string expression range¶
Return Value¶
Variable and vector definitions have a return value. In the case of variable definitions, the value to which the variable is initialised will be returned. Where as for vectors, the value of the first element
- shall be returned.
Variable/Vector Assignment¶
The value of a variable can be assigned to a vector and a vector or a vector expression can be assigned to a variable.
Variable To Vector:¶
Every element of the vector is assigned the value of the variable or expression.
Vector To Variable:¶
The variable is assigned the value of the first element of the vector (aka vec[0])
Note 10. During the expression compilation phase, tokens are classified based on the following priorities:
- Reserved keywords or operators (+, -, and, or, etc)
- Base functions (abs, sin, cos, min, max etc)
- Symbol table variables
- Expression local defined variables
- Symbol table functions
- Unknown symbol resolver based variables