forked from billziss-gh/imap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.py
141 lines (124 loc) · 5.14 KB
/
generate.py
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env python3
import re
int_types = {
'char': 's8',
'short': 's16',
'int': 's32',
'long': 'sl32',
'long long': 's64',
'unsigned char': 'u8',
'unsigned short': 'u16',
'unsigned int': 'u32',
'unsigned long': 'ul32',
'unsigned long long': 'u64'}
str_types = {'char*': 'c',
'const char*': 'cc',
'unsigned char*': 'uc',
'const unsigned char*': 'cuc'}
other_types = {'void*': 'v'}
key_types = str_types | int_types | other_types
val_types = int_types | other_types
functions = []
with open("table.h", 'r') as f:
og_lines = [[]]
idx = 0
for line in f:
if re.match(r"^\/\/ [BEGIN|END]+ [HEADER|SOURCE]+$", line):
og_lines.append([])
idx += 1
else:
og_lines[idx].append(line.rstrip())
with open("table.h", 'w') as f:
def printf(msg):
print(msg, file=f)
printf("\n".join(og_lines[0]))
printf("// BEGIN HEADER\n")
for n in ["get", "set"]:
printf(f"#define table_{n}(TABLE, KEY, VALUE) \\")
printf("\t_Generic((KEY), \\")
lines = []
for kk, kv in key_types.items():
lines.append(f"\t\t{kk}: _Generic((VALUE), \\\n" + ", \\\n".join([f"\t\t\t{vk if n == 'set' else vk + '*'}: __table_{n}_{kv}_{vv}" for vk, vv in val_types.items()]))
printf("), \\\n".join(lines) + ") \\")
printf("\t)(TABLE,KEY,VALUE)\n")
for n in ["has", "del"]:
printf(f"#define table_{n}(TABLE, KEY) \\")
printf("\t_Generic((KEY), \\")
printf(", \\\n".join([f"\t\t{kk}: __table_{n}_{kv}" for kk, kv in key_types.items()]) + ") \\")
printf("\t(TABLE,KEY)\n")
for n in ["get", "set"]:
for kk, kv in key_types.items():
for vk, vv in val_types.items():
fn = f"__table_{n}_{kv}_{vv}"
if not fn in functions:
functions.append(fn)
printf(f"int {fn}(table_t *table, {kk} key, {vk if n == 'set' else vk + '*'} val);")
printf("")
for n in ["has", "del"]:
for kk, kv in key_types.items():
fn = f"__table_{n}_{kv}"
if not fn in functions:
functions.append(fn)
printf(f"int __table_{n}_{kv}(table_t *table, {kk} key);")
printf("")
printf("// END HEADER")
printf("\n".join(og_lines[2]))
printf("// BEGIN SOURCE\n")
def print_key(kk):
if kk in str_types:
printf("\tuint64_t k = table->hashfn((void*)key, strlen((const char*)key), table->seed);")
else:
printf("\tuint64_t k = (uint64_t)key;")
for n in ["get", "set"]:
for kk, kv in key_types.items():
for vk, vv in val_types.items():
fn = f"__table_{n}_{kv}_{vv}"
if not fn in functions:
continue
printf(f"int {fn}(table_t *table, {kk} key, {vk if n == 'set' else vk + '*'} val) " + "{")
print_key(kk)
if n == "get":
printf(f"\t{vk} tmp_out = {'NULL' if vk in other_types else '0' };")
printf("\tint result = 1;")
printf("\tif (!unordered_map_has(&table->vmap, k)) {")
printf("\t\tresult = 0;")
printf("\t\tgoto BAIL;")
printf("\t}")
printf("\tuint64_t tmp;")
printf("\tif (!(result = unordered_map_get(&table->vmap, k, &tmp)))")
printf("\t\tgoto BAIL;")
printf(f"\ttmp_out = ({vk})tmp;")
printf("BAIL:")
printf("\tif (val)")
printf("\t\t*val = tmp_out;")
printf("\treturn result;")
else:
if kk in str_types:
printf("\tif (!unordered_map_has(&table->vmap, k)) {")
printf("\t\tconst char *dup = strdup((const char*)key);")
printf("\t\tunordered_map_set(&table->kmap, k, (uint64_t)dup);")
printf("\t}")
printf("\treturn unordered_map_set(&table->vmap, k, (uint64_t)val);")
printf("}\n")
for n in ["has", "del"]:
for kk, kv in key_types.items():
fn = f"__table_{n}_{kv}"
if not fn in functions:
continue
printf(f"int {fn}(table_t *table, {kk} key) " + "{")
print_key(kk)
if n == "has":
printf("\treturn unordered_map_has(&table->vmap, k);")
else:
printf("\tif (!unordered_map_has(&table->vmap, k))")
printf("\t\treturn 0;")
printf("\tif (!unordered_map_del(&table->vmap, k))")
printf("\t\treturn 0;")
printf("\tuint64_t tmp;")
printf("\tif (!unordered_map_get(&table->kmap, k, &tmp))")
printf("\t\treturn 0;")
printf("\tfree((void*)tmp);")
printf("\treturn unordered_map_del(&table->kmap, k);")
printf("}\n")
printf("// END SOURCE\n")
printf("\n".join(og_lines[4]))