Mathematical operations
The compatibility tables below represent the actions taken by the interpreter when the operation is applied to two values of the given types. anything means that the operation can be applied to any data type that wasn't explicitly listed above in the list (the list is evaluated from top to bottom, so the first matching type is used).
Folding operations
Addidion, subtraction, multiplication, division, and modulo in XMLang accept any number of children.
They are evaluated and the result is calculated by using the popular fold
iterator method.
The first child's value is used as the initial value and stored in an accumulator. Then, each subsequent child's value is used to update the accumulator by applying the operation. The final value of the accumulator is returned as the result.
If no children are provided, the result is null.
If the two data types of an operation are incompatible, an error (Can't {operation} incompatible types: {type1} and {type2}
) is thrown.
Example
<program>
<print>
<add>
<int>1</int>
<float>2.1</float> <!-- 3.1 -->
<int>3</int> <!-- 6.1 -->
<string>a</string> <!-- "6.1a" -->
</add>
</print> <!-- prints 6.1a -->
</program>
<add>
The order of the children does not matter, as addition is commutative.
Compatible types:
- null + anything = the other value
- int + int = the sum of the two integers (int)
- float + float = the sum of the two floats (float)
- int + float = the sum of the two numbers (float)
- bool + bool = the logical OR of the two booleans (bool)
- bool + anything = the boolean converted to the other type + the other value
- string + string = the concatenation of the two strings (string)
- string + int/float = the concatenation of the string and the numer (string)
<sub>
The order of the children matters, as subtraction is not commutative.
Incompatible types:
Compatible types:
- anything - null = the other value
- null - anything = the negation of the other value
- int - int = the difference of the two integers (int)
- float - float = the difference of the two floats (float)
- int - float = the difference of the two numbers (float)
- float - int = the difference of the two numbers (float)
- bool - bool = the difference of the two booleans converted to int (int)
- bool - anything = the boolean converted to the other type - the other value
- anything - bool = the other value - the boolean converted to the other type
<mul>
The order of the children does not matter, as multiplication is commutative.
Incompabile types:
Compatible types:
- null * anything = null
- int * int = the product of the two integers (int)
- float * float = the product of the two floats (float)
- int * float = the product of the two numbers (float)
- bool * bool = the logical AND of the two booleans (bool)
- bool * anything = if the boolean is
true
, the other value is returned, otherwise null is returned - string * int (positive) = the string repeated int times (string)
- string * int (negative) = the string reversed, and then repeated int times (string)
- string * float (positive) = the string repeated [float converted to int] times (string)
- string * float (negative) = the string reversed, and then repeated [float converted to int] times (string)
<div>
The order of the children matters, as division is not commutative.
Division by zero will throw the error Division by zero is not allowed
.
Incompatible types:
Compatible types:
- null / null = null
- int / int = the quotient of the two integers (int)
- float / float = the quotient of the two floats (float)
- int / float = the quotient of the two numbers (float)
- float / int = the quotient of the two numbers (float)
- bool / bool = the quotient of the two booleans converted to int (int)
- bool / anything = the boolean converted to the other type / the other value
- anything / bool = the other value / the boolean converted to the other type
<mod>
The order of the children matters, as modulo is not commutative.
Modulo by zero will throw the error Division by zero is not allowed
.
Incompatible types:
Compatible types:
- null % null = null
- int % int = the remainder of the two integers (int)
- float % float = the remainder of the two floats (float)
- int % float = the remainder of the two numbers (float)
- float % int = the remainder of the two numbers (float)
- bool % bool = the remainder of the two booleans converted to int (int)
- bool % anything = the boolean converted to the other type % the other value
- anything % bool = the other value % the boolean converted to the other type
Unary operations
Arithmetic negation and absolute value in XMLang accept a single child.
If the data type of an operation is incompatible, an error (Can't {operation} incompatible type: {type}
) is thrown.
<neg>
The <neg>
element is used to compute the additive inverse of a value.
Incompatible types:
Compatible types:
<abs>
The <abs>
element is used to calculate the absolute value of a number.
Incompatible types:
Compatible types: