Cosmo/docs/operators.md

37 lines
2.3 KiB
Markdown
Raw Permalink Normal View History

2021-01-24 18:28:53 +00:00
# Operators
## Arithmetic
| Operator | Description | Example |
| -------- | ---------------------------- | -------------------------------------- |
| `+` | Adds two numerical values together | `print(2 + 2)` -> `3 ;)` |
| `-` | Subtracts two numerical values together | `print(2 - 1)` -> `1` |
| `*` | Multiplies two numerical values together | `print(3 * 3)` -> `9` |
| `/` | Divides two numerical values together | `print(5 / 2)` -> `2.5` |
| `%` | performs a modulus operator on two numerical values | `print(5 % 2)` -> `1` |
> -> means 'outputs'
## Unary
| Operator | Description | Example |
| -------- | ---------------------------- | -------------------------------------- |
| `!` | "Not" logical operator, flips the logical polarity. | `print(!true)` -> `false` |
| `#` | "Count" calls '__count' metamethod on objects or gives the count of entries in tables | `print(#[1,2,3])` -> `3`, `print(#{__count = function(self) return self.x end, x = 1337})` -> `1337` |
2023-02-11 02:46:05 +00:00
| `++` | Increment operator. | `let i = 0 print(++i .. ", " .. i++ .. ", " .. i)` -> `1, 1, 2` |
| `--` | Decrement operator. | `let i = 0 print(--i .. ", " .. i-- .. ", " .. i)` -> `-1, -1, -2` |
2021-01-25 02:26:54 +00:00
| `( ... )` | Call operator. Arguments should be separated using `,`. | `print("Hello", " ", "world!")` -> `Hello world!` |
2021-01-24 18:28:53 +00:00
> -> means 'outputs'
## Logical
| Operator | Description | Example |
| -------- | ---------------------------- | -------------------------------------- |
| `and` | Logical 'and' operation | `print(true and 1337)` -> `1337` |
| `or` | Logical 'or' operation | `print(nil or false or 1337)` -> `1337` |
| `==` | Logical equality operation | `print(1337 == 1337)` -> `true` |
| `!=` | Logical not equality operation | `print(1337 != 1337)` -> `false` |
| `>=` | Greater than or equals too | `print(1337 >= 2000)` -> `false` |
| `<=` | Less than or equals too | `print(1337 <= 2000)` -> `true` |
| `>` | Greater than | `print(1337 > 2000)` -> `false` |
| `<` | Less than | `print(1337 < 2000)` -> `true` |
> -> means 'outputs'