-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.h
57 lines (48 loc) · 1.04 KB
/
data.h
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
#pragma once
// NOTE: for efficiency, we should use a hashmap as table
struct table {
unsigned n_entries;
struct table_entry* entries; // owned
};
struct vector {
unsigned n_values;
struct value* values; // owned
};
enum value_type {
value_type_string,
value_type_vector,
value_type_table,
};
struct value {
enum value_type type;
union {
const char* string; // owned
struct vector vector;
struct table table;
};
};
struct table_entry {
const char* name; // owned
struct value value; // owned
};
#include <stdlib.h>
void destroy_value(const value* val) {
switch(val->type) {
case value_type_string:
free((void*) val->string);
break;
case value_type_vector:
for(size_t i = 0u; i < val->vector.n_values; ++i) {
destroy_value(&val->vector.values[i]);
}
free(val->vector.values);
break;
case value_type_table:
for(size_t i = 0u; i < val->table.n_entries; ++i) {
destroy_value(&val->table.entries[i].value);
free((void*) val->table.entries[i].name);
}
free(val->vector.values);
break;
}
}