Expressions - dialogos-project/dialogos GitHub Wiki
DialogOS provides syntax for creating complex expressions and operations for use in scripts or other places such as output and condition nodes.
The following sections describe all available types and operators for creating expressions.
Expressions consist mainly of operators and variables. The possibility of using certain operators depends, among other things, on variable types. Also, the type of the evaluated expression depends on the operators used. DialogOS provides the following types:
bool
The type bool
is used to represent the boolean values true or false. Logical operations always result in a value of type bool
.
int
The type int
is used for integer numbers, such as 1, 312, or -21.
real
The type real
is used for real numbers, such as 3.142, -2.8, or 5.0.
string
The type string
stands for character sequences. String must be surrounded with quotation marks at the beginning the end, as in "DialogOS", "Hallo", or the empty string "".
list
The type list
is represents lists of elements. The type of each element is arbitrary. It is also possible to create lists of lists. Lists are created using square brackets around the list values. Elements are seperated from each other with a comma, as in [1,2,3]. The empty list is represented as [ ]. Elements of a list can be accessed (reading and writing) with bracket notation: list[index]
. (However, note that --
/++
/+=
... are not implemented for list elements). Additionally lists can be constructed using the Cons-Operator (::), 0::1::2::[]
(however, only lists can be constructed in this way, not arbitrary pairs).
struct
The type struct
represents a set of elements in which each element has a label (key). Structures are created with parentheses around the elements, and all elements are seperated with a comma, for example { one=1, two=2 }
or { numbers = [1,2,3], letters = [a,b,c]}
. Every element is, for all intents and purposes, an assigment of the form label=value. An empty structure is represented as { }
. An example of nested structures and lists: { eins=1, zwei=2 }
, { numbers={ one=1}, letter="a" }
. Elements of a structure can be accessed (reading/writing) using dot-notation (struct.numbers.one=1
), but elements cannot be added in this way. Elements can also be added and accessed using bracket notation: struct["name"] = "frank"
.
All variables can have the value "undefined" independently from its type.
A simple expression consists of a variable or constant value that can be expressed with one of the supported types listed above, such as int
, real
, bool
, list
, etc. Complex expressions consist of variables or constant values that are connected by operators. Expressions are generally evaluated from left to right, however parentheses may be used to establish precedence in the order of evaluations. Complex expressions can be cascaded to an arbitrary depth. This section lists all operators.
The unary operator - can be used to invert numeric values, whereas the ! operator will invert boolean values (e.g. turning true into false). The result of the operations will be of the same type as the type of its operands.
The following is a list of all binary operators:
+
When the + operator is used with the type real
or int
, the operation will be summation and the result of the expression will be of type real
or int
respectively. If the expression contains both a real
and int
, the result will be of type real
.
When the + operator is used with the type string, it will result in an append operation and the expression will evaluate to the type string
. Furthermore, if the first operand is of type string
, then any left operands, even of type int
or real
, will be appended to the string and the result will be of type string
.
-
The operator - is used for subtraction. It is only for use with int and real types; operands of any other type will lead to a "Type error" when used with this operator. As with the + operator, subtraction of two int types will result in the type int, while subtraction of numbers containing a real will always result in real.
*
The operator * is used for multiplication. As with the arithmetic operators discussed before, it can only be used with operands of type int or real; use of this operator with other types will result in a "Type error".
/
The operator / refers to division. The left operand is divided by the right operand, and operands must be of type int or real as with the other arithmetic operators. Additionally, if the right operand evaluates to zero, the operation will throw a "Division by zero" error. Note that dividing two int values will always result in a (rounded) int value.
%
The operator % refers to modular-division in which the remainder of an integer division is returned. In DialogOS, this operator works with both int and real values. When using real values, the value of the right operand is be rounded before evaluation.
::
The operator :: is used as an append operation in the context of lists. The right operand must be of a list type, but the left operand can be of any type. The operation associates to the right, thus the expression x::y::[] will evaluate to [x,y].
<, >, <=, >=
The operators <, >, <= and >= correspond to the algebraic relations of the same symbols. The operators on the left and right side must be of the same type, either int or real. The result of this operation is always of type bool.
==
The operator == compares two expressions for equality. This operator works with any type as long as the types of both operands are the same. It returns true if the values of both operands are the same, or false otherwise.
!=
The operator != corresponds to the negation of equivalence. It works analoguously to the equivalance operator, but evaluates to true if the value of the operands are different, and false if the values are the same.
&&
The operator && stands for logical conjunction. This may be used to connect two expressions that evaluate to the type bool, returning true when both operands are true, and false otherwise.
||
The operator || corresponds to logical disjunction. It is analogous to &&, but returns false only in the case that both operands evaluate to false, and true if either one or both operands evaluate to true.
The ? :
is the only ternary operator. This operator associates to the right and will first evaluate the expression to the left of ?, which must be a boolean value. If the expression is true, the value of the operand to the left of : will be returned. If the expression evaluates to false, the value of the operand to the right of : will be returned. An example of this operator is: (correctAnswer == userAnswer) ? "That is correct!" : "Sorry, incorrect."
.
In addition to the operators mentioned above, there are the so-called compound assignment operators which have a variable as its left operand and an expression that evaluates to the type of the variable as its right operand. Each of these operators associates to the right:
=
The operator = assigns the value of the right expression to the variable on the left.
+=, -=
The operators += and -= add or substract the value of the right expression to or from the value of the variable on the left side, respectively. The type restrictions of +, -, and = also hold here.
*=, /=, %=
The operators *=, /= and %= assign a value to the variable on the left which corresponds to its current value multiplied, divided or modulo divided by the value of the expression on the right side. The restrictions of the operators *, / and % also hold here.
DialogOS also provides shortcuts and bitwise operators as in the following:
++, --
The prefix and postfix ++ and -- can be used on variables or constants with a real or int value. The ++ operator increments the current value by one, and the -- operator decrements the value by one. These operators are at the same time assigment operators. When used as a prefix, the value will be increased before evaluation in an expression; as a postfix, the expression will be evaluated first and the variable will be incremented afterwards. Thus, if a=0, then a=3+(a++)
will evaluate to a=3, but a=3+(++a)
will evaluate to a=4.
&
The binary operator & stands for the bitwise "And" operator for numbers.
|
The binary operator | stands for the bitwise "Or" operator for numbers.
^
The binary operator ^ stands for the bitwise "Exclusive Or" operator for numbers.
Every function call function_name(parameters)
can be seen as an expression whereas the type of it is the return value of the function. The parameters are defined within the function. A detailed description of function syntax and predefined functions can be found in the chapter on Functions and Scripts.
Furthermore, it is possible to obtain values from a structure by using the expression structure_name.label
when the type of the element is known. For example, using the expression struct.one
with the structure struct={ one=1, two=2 }
will result in the value of "one", namely, 1.
Next page: Patterns