-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStructValue.c
More file actions
46 lines (41 loc) · 1.11 KB
/
StructValue.c
File metadata and controls
46 lines (41 loc) · 1.11 KB
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
#include "Pair.h"
#include "StructValue.h"
#include <gc.h>
#include <assert.h>
Struct* quote(Struct* form) {
return singleton_struct(STRUCT_SYMBOL, (void*)form);
}
Struct* structFromNameAndPairs(Symbol tag, Struct* x) {
Struct* s = NULL;
size_t size = 0;
for (Struct* right = x; right != NULL; right = asSecond(right)) {
size++;
}
if (size == 1) {
s = singleton_struct(tag, asFirst(x));
} else {
Struct** payload = (Struct**)GC_MALLOC(sizeof(Struct*)*size);
Struct* p = x;
for (size_t i = 0; i < size; i++) {
payload[i] = asFirst(p);
p = asSecond(p);
}
s = new_struct(tag, size, (void**)payload);
}
return quote(s);
}
Struct* structFromName(Symbol tag) {
return quote(atomic_struct(tag));
}
Struct* dequote(Struct* form) {
return (Struct*)singleton_payload(form);
}
void printStruct(FILE* stream, void(*print)(FILE*, Struct*), Struct* s) {
Struct* x = dequote(s);
fprintf(stream, "[%s", decompressSymbol(get_tag(x)));
for (size_t i = 0; i < get_size(x); i++) {
fprintf(stream, " ");
print(stream, (Struct*)get_field(x, i));
}
fprintf(stream, "]");
}