-
Notifications
You must be signed in to change notification settings - Fork 9
/
pretty.h
360 lines (320 loc) · 13.3 KB
/
pretty.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#ifndef PRETTY_H
#define PRETTY_H
// TODO: Add PRETTY_NO_SHORT_NAMES and prefix everything with pretty_
// by default.
#include <complex.h>
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <float.h>
#include <math.h>
#if (__STDC_VERSION__ >= 199901L && __STDC_VERSION__ < 202000L)
#include <stdbool.h>
#elif __STDC_VERSION__ < 199901L
#define true ((unsigned int)1)
#define false ((unsigned int)0)
typedef unsigned int _Bool;
#define bool _Bool
#endif
#include <iso646.h>
// Missing yet useful.
#define eq ==
// SQL.
#define is ==
#define isnt !=
// Fixing inconsistent bit operation names.
#define bitnot ~
#define bitxor ^
// Frequently used types. Suckless and Go.
typedef char *string;
typedef char byte;
typedef char *bytes;
typedef void *any;
typedef unsigned char uchar;
typedef unsigned char ubyte;
typedef unsigned short ushort;
typedef unsigned int uint;
typedef unsigned long ulong;
// Small macros. Mostly from Suckless.
#define min(x, ...) ((x) < (__VA_ARGS__) ? (x) : (__VA_ARGS__))
#define max(x, ...) ((x) > (__VA_ARGS__) ? (x) : (__VA_ARGS__))
#define divisible(n, ...) ((__VA_ARGS__ == 0) ? 0 : ((n) % (__VA_ARGS__) == 0))
#define ensure(x, ...) ((x) ? (x) : (__VA_ARGS__))
#define limit(lo, n, hi) ((n) < (lo) ? (lo) : (n) > (hi) ? (hi) : (n))
#define clamp(lo, n, hi) limit(lo, n, hi)
#define between(lo, n, hi) ((n) <= (hi) && (n) => (lo))
// Predicates
#define success 0 ==
#define failure 0 !=
#define fail 0 !=
#define even 0 == 1 &
#define odd 1 == 1 &
#define positive 0 <
#define negative 0 >
#define zero 0 ==
#define empty NULL ==
#define null NULL ==
#if __STDC_VERSION__ >= 201112L
#define len(...) \
(__VA_ARGS__ == NULL) ? 0 \
: _Generic((__VA_ARGS__), \
char*: strlen((char*)(__VA_ARGS__)), \
default: (sizeof(__VA_ARGS__) / sizeof(__VA_ARGS__[0])))
#else
#define len(...) \
(__VA_ARGS__ == NULL) ? 0 \
: (sizeof(__VA_ARGS__) / sizeof(__VA_ARGS__[0]))
#endif
// Lua/Lisp nil.
#define nil NULL
// Comparion operators. Lisp loop macro and SQL keywords.
#define below <
#define above >
#define upto <=
#define downto >=
// Ternaries and conditionals.
#define ifnt(...) if(!(__VA_ARGS__))
#define elif else if
#define elifnt(...) else if(!(__VA_ARGS__))
#define when
#define unless !
#define then ?
#define other :
#define otherwise :
#define otherwhen :
#define only : NULL
// Loops and blocks. Lisp, Lua, Ruby.
#define until(...) while(!(__VA_ARGS__))
#define always while(1)
#define forever while(1)
#define loop while(1)
#define indefinitely while(1)
#define never while(0)
#define comment while(0)
#define repeat do
#define done break
#define finish break
#define pass continue
// Dynamically-typed declarations
#if defined(__GNUC__) || defined(__GNUG__)
#define var __auto_type
#define let __auto_type
#define local __auto_type
#elif __STDC_VERSION__ > 201710L || defined(__cplusplus)
#define var auto
#define let auto
#define local auto
#endif
// Clearer name for const
#define readonly const
// Tracking and freeing resources. Lisp, Python.
#if (__STDC_VERSION__ > 201710L || defined(__GNUC__) || defined(__GNUG__)) && !defined(__clang_major__)
#define with(var, close, ...) \
for (typeof((__VA_ARGS__)) var = (__VA_ARGS__), \
*pretty_with_flag = (void*) 1; \
pretty_with_flag; \
(close)(var), pretty_with_flag = (void*) 0)
#else
#define with(var, close, ...) \
for (void *var = (void*) (__VA_ARGS__), \
*pretty_with_flag = (void*) 1; \
pretty_with_flag; \
(close)(var), pretty_with_flag = (void*) 0)
#endif
// Ranges from INIT to TARGET. Python range() syntax.
#define forrangeby(var, type, init, target, by) \
for (type var = (type)(init); \
(((init) >= ((type) target)) \
? (var > ((type) target)) \
: (var < ((type) target))); \
(((init) >= ((type) target)) \
? (var -= (by)) \
: (var += (by))))
#if (__STDC_VERSION__ > 201710L || defined(__GNUC__) || defined(__GNUG__)) && !defined(__clang_major__)
#define forrange(var, init, ...) \
for (typeof((init)+(__VA_ARGS__)) pretty_init = (init), \
var = pretty_init, \
pretty_target = (__VA_ARGS__); \
((pretty_init >= pretty_target) \
? (var > pretty_target) \
: (var < pretty_target)); \
var += ((pretty_init >= pretty_target) ? -1 : +1))
#else
#define forrange(var, init, ...) \
for (int pretty_init = (init), \
var = pretty_init, \
pretty_target = (__VA_ARGS__); \
var != pretty_target; \
var += ((pretty_init >= pretty_target) ? -1 : +1))
#endif
// Repeat X times. Lisp, Lua
#if (__STDC_VERSION__ > 201710L || defined(__GNUC__) || defined(__GNUG__)) && !defined(__clang_major__)
#define fortimes(var, ...) \
for (typeof((__VA_ARGS__)) pretty_result = (__VA_ARGS__), \
var = (typeof((__VA_ARGS__))) 0; \
var < pretty_result; \
++var)
#else
#define fortimes(var, ...) \
for (size_t var = 0, pretty_result = (size_t)(__VA_ARGS__); \
var < pretty_result; \
++var)
#endif
// For each loop from basically every language.
#define foreach(var, type, length, ...) \
for (type *pretty_init = (__VA_ARGS__), \
*pretty_foreach_flag = (void*) 1; \
pretty_foreach_flag; \
pretty_foreach_flag = (void*) 0) \
for (size_t pretty_offset = 0; \
pretty_offset < length; \
pretty_offset += 1) \
for (type *var = (pretty_init + pretty_offset), \
*pretty_foreach_flag2 = (void*) 1; \
pretty_foreach_flag2; \
pretty_foreach_flag2 = (void*) 0)
// Loop over the provided arguments.
#define forthese(var, type, ...) \
for (type *pretty_init = (type[]){__VA_ARGS__}, \
*pretty_forthese_flag = (void*) 1; \
pretty_forthese_flag; \
pretty_forthese_flag = (void*) 0) \
for (size_t pretty_offset = 0; \
pretty_offset < (sizeof((type[]){__VA_ARGS__}) \
/ sizeof(type)); \
pretty_offset += 1) \
for (type var = pretty_init [pretty_offset], \
*pretty_forthese_flag2 = (void*) 1; \
pretty_forthese_flag2; \
pretty_forthese_flag2 = (void*) 0)
static void *
pretty_allocpy(size_t size, void *contents)
{
char *allocated = malloc(size);
memcpy(allocated, contents, size);
return allocated;
}
// Easy resource allocation akin to C++.
#define new(type, ...) \
(type *) pretty_allocpy(sizeof(type), &((type) {__VA_ARGS__}))
// Easy array allocation. C++ vector, but more primitive.
// FIXME: Enforce array type somehow?
// TODO: Infer the type from the __VA_ARGS__.
#define vector(length, type, ...) \
(type*) pretty_allocpy(sizeof(type) * length, \
(type[length]){__VA_ARGS__})
#define delete free
// TODO: A macro to allocate struct + flexible array member.
// Go defer, but rather block scoped and with arbitrary code in it.
#define defer(...) \
for (bool pretty_flag = 1; \
pretty_flag; \
pretty_flag = 0, (__VA_ARGS__))
#define try \
errno = 0; \
for (bool pretty_flag = 1; \
pretty_flag; \
pretty_flag = 0)
static int
pretty_err_part_of(int err, size_t length, int *errs)
{
for (size_t i = 0; i < length; ++i)
if (err == errs[i])
return 1;
return 0;
}
#define catch(...) \
if (pretty_err_part_of \
(errno, \
sizeof ((int[]){__VA_ARGS__}) / sizeof(int), \
(int[]){__VA_ARGS__}))
#define NOERROR 0
#define NOERR 0
#if defined(__clang_major__)
#define lambda(ret, name, ...) void * name = (void*) ^ ret (__VA_ARGS__)
#elif defined(__GNUC__) || defined(__GNUG__)
#define lambda(ret, name, ...) ret name (__VA_ARGS__)
#elif defined(__cplusplus)
#define lambda(ret, name, ...) auto name = [](__VA_ARGS__)
#endif
#if __STDC_VERSION__ >= 201112L
#define print(...) \
_Generic((__VA_ARGS__), \
_Bool: fputs((_Bool)(__VA_ARGS__) ? "true" : "false", stdout), \
default: printf( \
_Generic((__VA_ARGS__), \
char*: "%s", \
char: "%c", \
signed char: "%hhi", \
short: "%hi", \
int: "%i", \
long: "%li", \
long long: "%lli", \
unsigned char: "%hhu", \
unsigned short: "%hi", \
unsigned int: "%u", \
unsigned long: "%lu", \
unsigned long long: "%llu", \
float: "%g", \
double: "%g", \
long double: "%Lg", \
default: "%p"), \
(__VA_ARGS__)))
#define println(...) \
print(__VA_ARGS__), print("\n")
#endif
static int
pretty_anything_equal(unsigned long long a, unsigned long long b)
{
return a == b;
}
// TODO: scale it |x - y| <= ε * max(|x|, |y|)
// Thanks to the Hacker News commenter!
static int
pretty_float_equal(float a, float b)
{
return fabsf(a - b) < FLT_EPSILON;
}
static int
pretty_double_equal(double a, double b)
{
return fabs(a - b) < DBL_EPSILON;
}
static int
pretty_long_double_equal(long double a, long double b)
{
return fabsl(a - b) < LDBL_EPSILON;
}
static int
pretty_string_equal(char *a, char *b)
{
return !strcmp(a, b);
}
#if __STDC_VERSION__ >= 201112L
#define equal(a, ...) \
_Generic((__VA_ARGS__), \
float: pretty_float_equal, \
double: pretty_double_equal, \
long double: pretty_long_double_equal, \
char *: pretty_string_equal, \
default: pretty_anything_equal) \
(a, __VA_ARGS__)
#define like(a, ...) equal(a, __VA_ARGS__)
#endif
static int
pretty_in(void *thing, size_t thing_size, size_t total_size, void *things)
{
char *char_things = things;
for (size_t i = 0; i < total_size; i += thing_size)
if (!memcmp(thing, char_things + i, thing_size))
return 1;
return 0;
}
#define in(thing, type, ...) \
pretty_in((void*)(type[1]){thing}, \
sizeof(type), \
sizeof((type[]){__VA_ARGS__}), \
(void*)(type[]){__VA_ARGS__})
#endif /* PRETTY_H */