enum, union

This commit is contained in:
agra
2026-02-14 13:17:22 +02:00
parent 4ff828fd1a
commit 025b790411
14 changed files with 537 additions and 245 deletions

View File

@@ -4,7 +4,7 @@ Vec4 :: struct {
}
Complex :: struct {
foo : union {
foo : enum {
S: s32;
B: struct {
val: string;

View File

@@ -71,7 +71,7 @@ main :: () {
// inline generic type
Sx :: (user: $T) -> Type {
return union {
return enum {
counter: s32;
user: T;
};

View File

@@ -1,8 +1,8 @@
#import "modules/std.sx";
Shape :: union {
Shape :: enum {
circle: f32;
rect: s32;
rect: struct { w, h: f32;};
none;
}
@@ -20,19 +20,19 @@ main :: () {
print("none: {}\n", s);
// Reassign with payload
s = .rect(42);
s = .rect(.{4, 2});
print("rect: {}\n", s);
// Explicit prefix construction
sh :Shape = Shape.circle(2.71);
print("sh: {}\n", sh);
// Field access on second union variable
sh2 :Shape = .rect(10);
// Field access on second variable
sh2 :Shape = .rect(.{2,4});
val := sh2.rect;
print("rect val: {}\n", val);
// Match on union
// Match on enum
if sh2 == {
case .circle: print("matched circle\n");
case .rect: print("matched rect\n");

View File

@@ -1,7 +1,7 @@
#import "modules/std.sx";
main :: () {
list := List(s32).{};
list : List(s32) = .{};
append(list, 1);

View File

@@ -153,9 +153,10 @@ main :: () {
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;
uniform mat4 uMVP;
uniform mat4 uMVP;
out vec3 vNormal;
out vec3 vPos;
void main() {
gl_Position = uMVP * vec4(aPos, 1.0);
vNormal = aNormal;

28
examples/30-union.sx Normal file
View File

@@ -0,0 +1,28 @@
#import "modules/std.sx";
Overlay :: union {
f: f32;
i: s32;
}
Vec2 :: union {
data: [2]f32;
struct { x, y: f32; };
}
main :: () {
// Basic union: type punning
o :Overlay = ---;
o.f = 3.14;
print("f={}\n", o.f);
print("i={}\n", o.i);
// Union with anonymous struct: member promotion
v :Vec2 = ---;
v.x = 1.0;
v.y = 2.0;
print("x={}\n", v.x);
print("y={}\n", v.y);
print("data[0]={}\n", v.data[0]);
print("data[1]={}\n", v.data[1]);
}

View File

@@ -161,10 +161,6 @@ struct_to_string :: (s: $T) -> string {
concat(result, "}");
}
enum_to_string :: (e: $T) -> string {
concat(".", field_name(T, cast(s64) e));
}
vector_to_string :: (v: $T) -> string {
result := "[";
i := 0;
@@ -205,7 +201,7 @@ pointer_to_string :: (p: $T) -> string {
}
}
union_to_string :: (u: $T) -> string {
enum_to_string :: (u: $T) -> string {
tag := cast(s64) u;
result := concat(".", field_name(T, tag));
payload := field_value(u, tag);
@@ -230,7 +226,6 @@ any_to_string :: (val: Any) -> string {
case vector: result = vector_to_string(cast(type) val);
case array: result = array_to_string(cast(type) val);
case slice: result = slice_to_string(cast(type) val);
case union: result = union_to_string(cast(type) val);
case pointer: result = pointer_to_string(cast(type) val);
case type: { s : string = xx val; result = s; }
}
@@ -314,7 +309,7 @@ List :: struct ($T: Type) {
cap: s64 = 0;
}
append :: (list: *List($T), item: T) {
append ::(list: *List($T), item: T) {
if list.len >= list.cap {
new_cap := if list.cap == 0 then 4 else list.cap * 2;
new_items : [*]T = xx malloc(new_cap * size_of(T));