@ enum type

This commit is contained in:
agra
2026-02-14 14:52:39 +02:00
parent fe7efeadb0
commit d61c6488f3
12 changed files with 164 additions and 41 deletions

View File

@@ -291,10 +291,10 @@ word := msg[6..11]; // string → "world"
| `*[N]T` | pointer to array of N T | yes | yes |
| `*[]T` | pointer to slice | yes | yes |
**Address-of**: `&x` returns a pointer to the variable.
**Address-of**: `@x` returns a pointer to the variable.
```sx
v := Vec2.{ 1.0, 2.0 };
ptr := &v; // *Vec2
ptr := @v; // *Vec2
```
**Dereference**: `p.*` loads the value through the pointer.
@@ -307,7 +307,7 @@ copy := ptr.*; // Vec2
set_x :: (p: *Vec2, val: f32) {
p.x = val; // auto-deref: p.*.x = val
}
set_x(&v, 99.0);
set_x(@v, 99.0);
```
**Null**: All pointer types are nullable. `null` is the null pointer literal.
@@ -318,7 +318,7 @@ np : *Vec2 = null;
**Many-pointer**: `[*]T` supports indexing for buffers of unknown size.
```sx
arr : [5]s32 = .[10, 20, 30, 40, 50];
mp : [*]s32 = &arr[0]; // *s32 → [*]s32 implicit
mp : [*]s32 = @arr[0]; // *s32 → [*]s32 implicit
val := mp[2]; // 30
```
@@ -555,6 +555,19 @@ Name :: enum {
Defines a new enum type with the given variants. Trailing comma is allowed.
### Enum Backing Type
An optional backing type can be specified after the `enum` keyword (Jai-style):
```sx
Color :: enum u8 { red; green; blue; }
Status :: enum s16 { ok; error; timeout; }
```
Syntax: `Name :: enum [flags] [type] { ... }`
The backing type must be an integer type (`u8`, `u16`, `u32`, `s8`, `s16`, `s32`, `s64`, etc.). When omitted, the default is `s64`. This is useful for C interop (matching C enum sizes) and memory efficiency.
### Enum Flags
```sx
@@ -565,6 +578,15 @@ Perms :: enum flags {
}
```
Flags can also specify a backing type:
```sx
SDL_InitFlags :: enum flags u32 {
video :: 0x20;
audio :: 0x10;
}
```
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