Expression are made up exclusively out of certain atomic elements. These elements are string-literals, integer-constants, floating-point constants, variables and objectfields. Throughout the language you can always savely use an objectfield every time a variable is used in a statement. The objectfield just needs to have the right type, in order to pass the typechecking stage of the compiler. You will encounter the following type of atoms in the language:
Integer contants like 123 or -10100
Floating-point constants like 102.00f or -1.0f, please note that floats need to be suffixed with a trailing f to set them apart from integers.
String literals enclosed in double-quotes like "hello world" or ""test 123\n". There are a number of escape sequences that work inside string literials.
Names of variables, which refer to a previously declared variable and will either retrieve values from it if used on the righthand side of an expression or it will be the target of an assignment if found on the lefthand side of an assignment expression.
Classfields are equivalent in their behaviour to plain variables. They can dereference their associated values and can also be used in assignment expressions.
Expressions are made up of these atomic types and operators. Operators will be covered next, since expressions dont make any sense with just operands and no operators.
In the language you will find a few unary arithmetic operators such as the unary minus. Those operators operate on the argument (aka expression) which stands right to them.
The unary - (minus) operator evaluates to the negative of its argument.
The unary ! invert-operator evaluates to 0 if its argument is not zero. If its argument is zero, than this operator will return 1. This are the semantics of a logical-not operation.
The above operators will only work with numeric types. In case it is attempted to use them on different types or user defined-structures, a typewarning is emitted during compilation and compilation is haltet with an error.
There is a full set of binary arithmetic operators in the language. The following will work as expected (and also with proper precedence):
+, -, *, / and % perform the standard arithmetics on the their two arguments and evaluate to the result of that arithmetic operation. The modulo operator % is also defined for floating point values and returns a float-point remainder in this case.
The above operators will only work with numeric types. In case it is attempted to use them on different types or user defined-structures, a typewarning is emitted during compilation and compilation is halted with an error.
In addition to these standard arithmetical operators there are also a whole bunch of logical and bitwise operators. Bitwise operators can be used to do bit-manipulation on integral types, like deleting or setting a single bit.
"<<" - bitwise shift left
">>" - bitwise shift right
"&" - bitwise and
"|" - bitwise inclusive-or
"^" - bitwise exclusive-or
Logical operators are used for checking whether a combination of certains conditions fullfills a certain criteria or not. In butter every value except 0 represents a logical true-condition. Here is a list of the logical operators:
"<" - smaller than
">" - smaller than
">=" - greater or equal
"<=" - smaller or equal
"&&" - logical and
"||" - logical or
"&&&" - logical and with sideeffects
"|||" - logical or with sideeffects
"==" - is equal
"!=" - not is equal
Please note that all logical-and and logical-or expressions are not always fully evaluated. This may have nasty consequences, if the reamining operands of the sequence have sideeffects that are needed. An logical-and expression will only be executed until a false value is encountered. Execution of the expression is then stopped and the remaining expressions and their sideeffects are not evaluated. In case you want all your expressions to be evaluated under all circumstances use the special operators "&&" or "&&&".
In order to assign a value to a variable associated with the user defined identifier one uses the = operator. Please note the big difference between the == equality-operator and the = assignment-operator.
An assignment expression has the following form: myvar = 10. In this example myvar is the identfier chosen as a name for an integer variable. After evaluating this expression myvar will have the value 10. The assignment operator nevertheless returns the assigned value. This is tricky as that means you can use the assignment operator in not so obvious ways like the following: ((myvar = 10 ) < 11). This expression will always evaluate to true, as 10 is certainly always guaranteed to be smaller than 11. A more common thing is using the returned value of an assignment expression to assign the same value to multiple variables:
int i, j, k; i = j = k = 0; |
In addition to plain assignments there are combined assignment/arithmetics operators, which will alter the value of the left-hand side variable of an assignment expression. This is done by applying the operation speciefied by the special assignment operator to the value of the variable. The result of this computation is then assigned to the variable associated with the identifier(s) on the left-hand side of an assignment expressions.
The sequence operator "," can be used for specifying a list of expression that will act like a single expression. All the values the expressions in the sequence are evalued too are disposed, only the computed value of the last expression in an expression sequence is returned.
A example usage of the sequence operator might be for invoking a bunch of functions as a single expression in a compact way.
void main () { do_thing(), do_another_thing(), do_yet_another_thing(); } |
Often you see expression sequences for compact expression of a bunch of assignment-statement.
int i, j; i = 10, j = 20; /* i is set to 10 and j to 20 */ |
The somewhat confusing aspect of expression sequences is that only the last expression's value is returned. The following code thus will assign the value 1001 to the variable i.
int i; i = do_a_function_call(), 1, 2, i * i, (10 + 20) * i, 1001; |