Operators
Operators are special symbols used to perform calculations, compare values, and evaluate expressions in TinyPanda.
Arithmetic Operators
TinyPanda supports standard mathematical operators for performing calculations on integers. It correctly respects operator precedence (multiplication and division are evaluated before addition and subtraction).
| Operator | Operation | Example | Result |
|---|---|---|---|
+ | Addition | 5 + 5 | 10 |
- | Subtraction | 10 - 3 | 7 |
* | Multiplication | 4 * 2 | 8 |
/ | Division | 10 / 2 | 5 |
% | Mod | 10 % 3 | 1 |
Zero Division Safety
Attempting to divide or mod by 0 or 0.0 will throw a runtime error instead of crashing the program.
echoln(10 / 0); // Runtime Error: cannot divide by zero
echoln(10.5 % 0); // Runtime Error: cannot divide by zero
- Integer Math: Performing division
/or mod%on twoINToperands always executes integer math and returns aINTvalue.
what(10 % 2); // Returns: INTEGER
- Float Cast: If any operand in the expression is a
FLOAT, the other operand is also casted toFLOATtype, and it always returns aFLOATvalue, even if the final mathematical result is aINTvalue.
what(10 % 2.5); // Returns: FLOAT
what(<ident>) is a builtin function which returns the data type of the ident, learn nore about it here.
Increment and Decrement Operators
TinyPanda supports postfix increment (++) and postfix decrement (--) operators. These operators provide a clean, shortcut syntax to mutate existing numerical variables in place by adding or subtracting by 1.
Only
INTEGERandFLOATcan be incremented or decremented.
bamboo num = 10;
num++;
echoln(num); // Outputs: 11
In TinyPanda, ++ and -- are statements, not expressions. This means they mutate the variable but do not return a value. You cannot assign them to other variables or pass them into function.
bamboo x = i++; // This is a imvalid syntax
Comparison Operators
Comparison operators look at two values and evaluate down to a boolean literal (true or false). These are highly useful for controlling flow inside conditional blocks.
| Operator | Description | Example | Result |
|---|---|---|---|
| == | Equal to | 5 == 5 | true |
| != | Not equal to | 5 != 3 | true |
| > | Greater than | 10 > 2 | true |
| < | Less than | 4 < 1 | false |
| >= | Greater than or equal to | 5 >= 5 | true |
| <= | Less than or equal to | 3 <= 5 | true |
echoln(10 > 5); // Outputs: true
echoln(10 == 5); // Outputs: false
Prefix Operators
Prefix operators are placed before a single value to invert it or change its state. TinyPanda supports the ! (bang/exclamation) operator to invert boolean values.
| Operator | Description | Example | Result |
|---|---|---|---|
! | Invert / NOT | !false | true |
echoln(!"name"); // Outputs: false
echoln(!1); // Outputs: false
echoln(!true); // Outputs: false
echoln(!false); // Outputs: true
You can learn about truthy and falsy values in TinyPanda here.
TinyPanda checks your data type on the left and right side of an operator. Operations or comparisons can only be performed if the data types on both sides matches.
Mixing mismatched types across an operator will instantly halt execution with a runtime error.
// Both of these will throw a runtime error!
iff (10 > "abc") { ... }
bamboo foo = "bar" + 100;