flags
This commit is contained in:
46
specs.md
46
specs.md
@@ -64,6 +64,8 @@ GLSL;
|
||||
| `>` | greater than |
|
||||
| `<=` | less or equal |
|
||||
| `>=` | greater or equal |
|
||||
| `&` | bitwise AND |
|
||||
| `\|` | bitwise OR |
|
||||
| `and` | logical AND (short-circuit) |
|
||||
| `or` | logical OR (short-circuit) |
|
||||
| `+=` | add-assign |
|
||||
@@ -553,6 +555,47 @@ Name :: enum {
|
||||
|
||||
Defines a new enum type with the given variants. Trailing comma is allowed.
|
||||
|
||||
### Enum Flags
|
||||
|
||||
```sx
|
||||
Perms :: enum flags {
|
||||
read; // 1
|
||||
write; // 2
|
||||
execute; // 4
|
||||
}
|
||||
```
|
||||
|
||||
The `flags` modifier assigns auto power-of-2 values (1, 2, 4, 8, ...) instead of sequential indices (0, 1, 2, ...). Flags can be combined with `|` and tested with `&`:
|
||||
|
||||
```sx
|
||||
p :Perms = .read | .write;
|
||||
if p & .execute { ... }
|
||||
print("{}\n", p); // .read | .write
|
||||
```
|
||||
|
||||
Explicit values use `::` syntax (Jai-style):
|
||||
|
||||
```sx
|
||||
WindowFlags :: enum flags {
|
||||
vsync :: 64;
|
||||
resizable :: 4;
|
||||
hidden :: 128;
|
||||
}
|
||||
```
|
||||
|
||||
Restrictions:
|
||||
- Flags enum variants cannot have payloads
|
||||
- `flags` is a contextual identifier, not a keyword
|
||||
|
||||
### Bitwise Operators
|
||||
|
||||
`&` (bitwise AND) and `|` (bitwise OR) work on all integer types, not just flags. They sit at precedence level 3, between comparisons and logical operators.
|
||||
|
||||
```sx
|
||||
x := 0xFF & 0x0F; // 15
|
||||
y := 1 | 2 | 4; // 7
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 4. Expressions
|
||||
@@ -563,9 +606,10 @@ Everything in `sx` is expression-oriented where possible.
|
||||
|
||||
| Prec | Operators | Notes |
|
||||
|------|-----------|-------|
|
||||
| 6 (highest) | `*`, `/` | multiplication, division |
|
||||
| 6 (highest) | `*`, `/`, `%` | multiplication, division, modulo |
|
||||
| 5 | `+`, `-` | addition, subtraction |
|
||||
| 4 | `<`, `<=`, `>`, `>=`, `==`, `!=` | comparisons (chainable) |
|
||||
| 3 | `&`, `\|` | bitwise AND, bitwise OR |
|
||||
| 2 | `and` | logical AND (short-circuit) |
|
||||
| 1 (lowest) | `or` | logical OR (short-circuit) |
|
||||
|
||||
|
||||
Reference in New Issue
Block a user