Expressions
Expressions are the basis of Kipper, as every operation in a program is made up of single expressions that perform certain actions, like reading a value, calculating something, calling a function, comparing values…
A simple example for expressions can be a simple calculation, like this:
Notice how in this case we are not assigning the result to anything? This is an important fact of expressions that they are usually independent and do a single task, meaning if we wanted to assign this result to a variable, we would have to use an assignment expression like this:
In this case, we also now created an expression statement (ends in ;
), which wraps an
expression and allows it to be used inside a program. Expression statements will be explained
further in the docs page Statements.
Kipper Operators and Expressions Precedence
The following table shows all valid operators and expressions in the Kipper language, with their respective precedence. The higher the precedence the higher the importance of the item, and the order of evaluation.
Precedence | Operator / Expression | Description | Associativity |
---|---|---|---|
14 | ( ... ) |
Tangled expression - Forced increased precedence | n/a |
13 |
++ ... / -- ... call ... ( ... ) ... [ ... ] ... . ...
|
Prefix increment and decrement (Suffix) Function Call List subscripting Object member accessing |
Left-To-Right |
12 | ... ++ / ... -- |
Postfix increment and decrement |
n/a |
11 |
+ ... / - ... ! ... |
Unary plus and minus Logical NOT |
Right-To-Left |
10 |
... as ...
|
Type conversion | Left-To-Right |
9 |
... * ... ... / ... ... % ... ... ** ...
|
Multiplication Division Modulus Power-To |
|
8 | ... + ... / ... - ... |
Addition and subtraction |
|
7 |
... < ... / ... <= ... ... > ... / ... => ... |
More than / More or Equal to Less than / Less or Equal to |
|
6 | ... == ... / ... != ... |
Relational comparison (Equal / Not equal to) |
|
5 | ... && ... |
Logical AND |
|
4 | ... || ... |
Logical OR |
|
3 | ... ? ... : ... |
Ternary expression |
Right-To-Left |
2 |
... = ... ... += ... / ... -= ... ... *= ... ... /= ... ... %= ...
|
Simple assignment Increment or Decrement assignment Multiplicative assignment Divisional assignment Rest of Division / Remainder assignment |
|
1 | ... , ... |
Comma | Left-To-Right |
How does precedence of operators and expressions affect a program?
The order of precedence is a very vital concept in programming languages, as they define how expressions are going to be evaluated. This directly can affect how your code runs, as certain parts might be evaluated first before others and change the entire course of your program.
A simple example of this are arithmetic expressions + - * / %
, which have per
mathematical conventions their own order of precedence. For example, multiplication and division
signs always come first before plus and minus, unless you explicitly use brackets,
like these ( )
. The same can be done here in Kipper as well, where you can forcefully
increase the order of precedence using brackets:
Forced higher precedence using brackets:
What does associativity mean?
As you should have already seen in the table above, there is an extra column defining the so-called
associativity. What does that mean? It's relatively simple and means whether the following (( )
meaning the expression is read first):
is evaluated as (left-associative - Reads from left to right):
or as (right-associative - reads from right to left):
This is especially important as it can change how certain things are evaluated, and also cause unwanted errors! For example the following would be interpreted as (left-associative):
List of expressions in the Kipper language
This is a concise list of all expressions in the Kipper language that may be used:
Arithmetic Expression
Arithmetic expressions are simple mathematical calculations, where a numeric result is returned after the expression was evaluated. They may also be chained based on their order of precedence, where each item is one by one evaluated.
Syntax
Examples
Relational Expressions (Comparisons)
Relational expressions are comparative expressions that compare two other expressions/values with a
specific operator. They evaluate to true
if the condition of the operator is true, for
example <
(less than) with 1 < 5
, and evaluate to
false
if the condition is false, for example >
(greater than) with
1 > 5
.
Such expressions are essential for conditional expressions that are used in statements such as:
Syntax
Examples
Logical Expressions
Logical expressions combine two or more expressions/conditions and evaluate to a
bool
value (Either true
or false
) based on the specific
operator used.
The NOT operator (!
) is also a logical operator, even though unlike the other operators
it can be only used on a single expression, and can be used to invert the result of a logical
expression.
Syntax
For the logical AND
and OR
:
For the logical NOT
:
Examples
Logical Expressions may be also chained together as long as you want. For example:
You can also combine relational expressions with logical expressions, like this for example:
Conditional Expressions (Ternary conditional)
Conditional expressions are like if-statements with the major difference and advantage of being in-line and allowing evaluation of specific expressions based on a condition.
If the CONDITION
evaluates to true
, the left side of the
:
operator is evaluated and returned, otherwise the right side is evaluated and
returned.
Syntax
Examples
As the ternary operator evaluates to more expressions, you can also chain it like this:
Convert Expression
Conversion expressions convert the value of an expression to a target type using pre-defined built-in conversion functions. Such conversions are essential for using values of different types in the same expression and avoiding type errors.
An important example of this are print
function call expressions, as the
print
function is a built-in function that only allows a string as a parameter.
Therefore, to print out a number you first have to convert it to a string.
Syntax
Allowed conversions
From str
to num
Converts a string to a number, if it meets the following requirements:
- Only contains numeric characters (
0 - 9
) - Is not empty (length > 0)
Otherwise if the string does not meet the above requirements, it will return NaN
(Not a
number). In future releases, though this will throw a ConversionError
.
From num
to str
Converts a number to an identical string representation of the number.
From char
to str
Converts a single character to a string.
From num
to bool
Converts a number to a bool. This evaluates to true
if it's a non-zero value.
From bool
to num
Converts a bool to a number. This evaluates to 1
if it's true
, otherwise
it's 0
.
From bool
to str
Converts a string to a number. This evaluates to "true"
if it's true
,
otherwise it's "false"
.
Function Call Expression
Function call expression are expressions that call and run a specified function and evaluates to the return of the function. In case that the return is not assigned to any variable or used in another expression, then it will be simply discarded.
If the return type of the function is void
, then the
function call expression will not return any value.