C import: unions (SDL_Event) and their fields; #defines that are expressions, casts or text; typedef'd structs no function mentions; FLAG_A | FLAG_B fits a u64

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Nexoniarz 2026-10-29 09:23:29 +01:00
commit d0666cf788
7 changed files with 314 additions and 13 deletions

View file

@ -0,0 +1,25 @@
// C unions (SDL_Event-style) and #defines that are expressions, strings or casts
import "cunion.h"
link "cunion.c"
fn main() {
print(EV_MOTION, EV_KEY, FLAG_BIG, FLAGS_BOTH, HALF, GREETING, LONG_NAME)
u64 flags = FLAG_BIG | FLAG_GL
u32 small = EV_KEY
print(flags, small)
Event e
if poll_event(addr(e)) != 0 && e.type == EV_MOTION {
print("motion", e.motion.x, e.motion.y)
}
k := Event(key: Key(type: EV_KEY, key: 32))
print(k.type == EV_KEY, k.key.key)
[]Event queue
queue.push(e)
queue.push(k)
print(queue[1].key.key, queue.len)
Tagged t
t.as.f = 0.5
print(t.as.f)
}

View file

@ -0,0 +1,6 @@
1024 1025 1099511627776 1099511627778 0.5 hi there ab
1099511627778 1025
motion 1.5 -2
true 32
32 2
0.5

7
tests/new/cunion.c Normal file
View file

@ -0,0 +1,7 @@
#include "cunion.h"
int poll_event(Event *e) {
e->motion.type = EV_MOTION;
e->motion.x = 1.5f;
e->motion.y = -2.0f;
return 1;
}

30
tests/new/cunion.h Normal file
View file

@ -0,0 +1,30 @@
// C unions and #define constants that are expressions (tests/new/c_unions_macros.fch)
#include <stdint.h>
typedef struct { uint32_t type; float x, y; } Motion;
typedef struct { uint32_t type; int32_t key; } Key;
typedef union {
uint32_t type;
Motion motion;
Key key;
uint8_t padding[64];
} Event;
typedef struct { int kind; union { int i; float f; } as; } Tagged;
#define EV_MOTION 0x400u
#define EV_KEY (EV_MOTION + 1)
#define FLAG_BIG ((uint64_t)1 << 40)
#define FLAG_GL 0x2ULL
#define FLAGS_BOTH (FLAG_BIG | FLAG_GL)
#define HALF (1.0 / 2)
#define GREETING "hi\tthere"
#define LONG_NAME "a" "b"
#define NOT_A_CONSTANT do { } while (0)
static inline int fill_key(Event *e, int key) {
e->key.type = EV_KEY;
e->key.key = key;
return 1;
}
int poll_event(Event *e);

View file

@ -0,0 +1,5 @@
// expect: Event is a C union: give one field by name
import "../cunion.h"
fn main() {
e := Event(1, 2)
}