-
Notifications
You must be signed in to change notification settings - Fork 0
/
Struct.c
66 lines (55 loc) · 1.14 KB
/
Struct.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "Struct.h"
#include <assert.h>
#include <gc.h>
struct Struct {
Symbol tag;
size_t size;
void** fields;
};
Struct* new_struct(Symbol tag, size_t size, void** fields) {
Struct* x = (Struct*)GC_MALLOC(sizeof(Struct));
x->tag = tag;
x->size = size;
x->fields = fields;
return x;
}
Struct* singleton_struct(Symbol tag, void* x) {
return new_struct(tag, 1, (void**)x);
}
Struct* value_struct(Symbol tag, unsigned long x) {
return new_struct(tag, 1, (void**)x);
}
Struct* atomic_struct(Symbol tag) {
return new_struct(tag, 0, NULL);
}
void* singleton_payload(Struct* s) {
assert(s != NULL);
return (void*)s->fields;
}
void* get_field(Struct* s, size_t n) {
assert(s != NULL);
void* x = NULL;
if (s->size > n) {
if (s->size == 1) {
return singleton_payload(s);
}
x = s->fields[n];
}
return x;
}
Symbol get_tag(Struct* s) {
assert(s != NULL);
return s->tag;
}
size_t get_size(Struct* s) {
assert(s != NULL);
return s->size;
}
void** get_fields(Struct* s) {
assert(s != NULL);
return s->fields;
}
unsigned long value_payload(Struct* s) {
assert(s != NULL);
return (unsigned long)s->fields;
}