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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
|
#pragma once
// ------------------------------------------------------------------------
// standard library
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <stdio.h>
#include <wchar.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
typedef wchar_t wchar;
// ----------------------------------------------------------------------------
// dynamic array
typedef struct bufHdr
{
vlong len;
vlong cap;
byte buf[];
} bufHdr;
#define _bufHdr(s) ((bufHdr*)((uint8*)(s)-offsetof(bufHdr, buf)))
#define buflen(s) ((s) ? (_bufHdr(s)->len) : 0)
#define bufcap(s) ((s) ? (_bufHdr(s)->cap) : 0)
#define bufend(s) ((s) + buflen(s))
#define bufsize(s) ((s) ? (buflen(s) * sizeof((s)[0])) : 0)
#define buffree(s) ((s) ? (free(_bufHdr(s)), (s) = nil) : 0)
#define buffit(s, n) ((n) <= bufcap(s) ? 0 : ((s) = bufgrow((s), (n), sizeof(*(s)))))
#define bufresize(s, n) \
do { \
(buffit(s, n)); \
((_bufHdr(s)->len) = (n)); \
} while (0)
#define bufpush(s, ...) (buffit((s), 1 + buflen(s)), (s)[_bufHdr(s)->len++] = (__VA_ARGS__))
#define bufpop(s, i) (_bufpop((s), (i), sizeof(*(s))), (s)[_bufHdr(s)->len])
void* bufgrow(void*, vlong, vlong);
void _bufpop(void*, int, vlong);
// -----------------------------------------------------------------------------
// interfaces
// TODO(nnoll): Think about this idea
/*
typedef struct Iface {
void* impl;
byte fcn[];
} Iface;
*/
// -----------------------------------------------------------------------------
// memory allocation
// TODO(nnoll): Allow for nil iterfaces?
/* allocator interface */
typedef struct mem·Allocator {
void *(*alloc)(void *iface, uint n, ulong size);
void (*free)(void *iface, void *ptr);
} mem·Allocator;
typedef struct mem·Reallocator {
void *(*alloc)(void *iface, uint n, ulong size);
void *(*realloc)(void *iface, void *ptr, uint n, ulong size);
void (*free)(void *iface, void *ptr);
} mem·Reallocator;
/* system implementation */
extern mem·Allocator mem·sys;
extern mem·Reallocator mem·rsys;
/* simple memory arena */
typedef struct mem·Arena mem·Arena;
mem·Arena *mem·makearena(mem·Allocator from, void*);
void *mem·arenaalloc(mem·Arena *A, uint n, ulong size);
void mem·freearena(mem·Arena *A);
/* generalized memxxx functions */
void memset64(void *dst, uint64 val, uintptr size);
// -----------------------------------------------------------------------------
// coroutines
typedef struct Coro Coro;
Coro* coro·make(uintptr stk, uintptr (*func)(Coro*, uintptr));
uintptr coro·yield(Coro *c, uintptr arg);
error coro·free(Coro *c);
// -----------------------------------------------------------------------------
// Strings
typedef byte* string;
/* string helpers */
string str·makecap(const byte *s, vlong len, vlong cap);
string str·makelen(const byte *s, vlong len);
string str·make(const byte *s);
string str·makef(const byte *fmt, ...);
void str·free(string s);
int str·len(const string s);
int str·cap(const string s);
void str·clear(string *s);
void str·grow(string *s, vlong delta);
void str·fit(string *s);
int str·appendlen(string *s, vlong len, const byte *b);
int str·append(string *s, const byte* b);
int str·appendf(string *s, const byte* fmt, ...);
int str·appendbyte(string *s, const byte b);
bool str·equals(const string s, const string t);
int str·find(string s, const byte* substr);
void str·lower(string s);
void str·upper(string s);
int str·read(string s, int size, int n, void *buf);
void str·replace(string s, const byte* from, const byte* to);
string* str·split(string s, const byte* tok);
string str·join(vlong len, byte** fields, const byte* sep);
/*
* UTF-8 functions.
* Perhaps break into own unit
* TODO: Add to(upper|lower|title)
*/
typedef uint32 rune;
/*
* We have to use the preprocessor to ensure
* we have unsigned constants. Unfortunate...
*/
#define UTFmax 4
#define RuneSync 0x80u
#define RuneSelf 0x80u
#define RuneErr 0xFFFDu
#define RuneMax 0x10FFFFu
#define RuneMask 0x1FFFFFu
/* utf8 helpers */
int utf8·fullrune(byte *s, int n);
byte *utf8·findrune(byte *s, long i);
byte *utf8·findrrune(byte* s, long c);
int utf8·bytetorune(rune *r, byte *s);
int utf8·runetobyte(byte *s, rune *r);
int utf8·len(byte *s);
int utf8·runelen(rune r);
int utf8·isletter(rune r);
int utf8·isdigit(rune r);
int utf8·isspace(rune r);
int utf8·istitle(rune r);
// -----------------------------------------------------------------------------
// I/O
typedef FILE Stream;
typedef struct stat io·Stat;
enum SeekPos
{
seek·cur = SEEK_CUR,
seek·set = SEEK_SET,
seek·end = SEEK_END
};
enum
{
ReadOK = R_OK,
WriteOK = W_OK,
ExecOK = X_OK,
};
/* file handling */
Stream *io·open(byte *name, byte *mode);
int io·fd(Stream *s);
error io·stat(Stream *s, io·Stat *buf);
error io·close(Stream *s);
byte io·getbyte(Stream *s);
error io·ungetbyte(Stream *s, byte c);
int io·read(Stream *s, int sz, int n, void *buf);
int io·readln(Stream *s, int n, byte *buf);
error io·putbyte(Stream *s, byte c);
int io·putstring(Stream *s, string str);
int io·write(Stream *s, int sz, int n, void *buf);
int io·flush(Stream *s);
int io·seek(Stream *s, long off, enum SeekPos whence);
/* basic os helpers */
int os·exists(byte *path, int flag);
byte *os·basename(byte *path);
int os·sep(void);
/* io interfaces */
typedef struct io·Reader
{
int (*read)(void*, int sz, int n, void *buf);
} io·Reader;
typedef struct io·LineReader
{
int (*readln)(void*, int n, void *buf);
} io·LineReader;
typedef struct io·Peeker
{
byte (*get)(void*);
error (*unget)(void*, byte);
} io·Peeker;
typedef struct io·FullReader
{
io·Reader;
io·Peeker;
} io·FullReader;
typedef struct io·Writer
{
int (*write)(void*, int sz, int n, void *buf);
} io·Writer;
typedef struct io·Putter
{
error (*put)(void*, byte);
int (*putstr)(void*, string);
} io·Putter;
typedef struct io·FullWriter
{
io·Writer;
io·Putter;
} io·FullWriter;
typedef struct io·ReadWriter
{
io·Reader;
io·Writer;
} io·ReadWriter;
/* buffered i/o */
typedef struct io·Buffer io·Buffer;
enum
{
bufio·size = 2*4096,
bufio·ungets = 8,
bufio·eof = -1,
bufio·err = -2,
bufio·nil = 1 << 0,
bufio·rdr = 1 << 1,
bufio·wtr = 1 << 2,
bufio·end = 1 << 3,
};
struct io·Buffer
{
int state;
int runesize;
void *h;
union {
io·Reader rdr;
io·Writer wtr;
};
vlong size;
byte *beg, *pos, *end;
byte buf[bufio·size + bufio·ungets];
};
error bufio·initreader(io·Buffer *buf, io·Reader rdr, void *h);
void bufio·finireader(io·Buffer *buf);
int bufio·getbyte(io·Buffer *buf);
error bufio·ungetbyte(io·Buffer *buf, byte c);
rune bufio·getrune(io·Buffer *buf);
error bufio·ungetrune(io·Buffer *buf, rune r);
int bufio·read(io·Buffer *buf, int sz, int n, void *out);
// -----------------------------------------------------------------------------
// memory mapped files
typedef struct mmap·Reader
{
vlong len;
union {
byte *buf;
ubyte *ubuf;
};
} mmap·Reader;
mmap·Reader mmap·open(byte *name);
error mmap·close(mmap·Reader rdr);
// -----------------------------------------------------------------------------
// libflate
// NOTE: Experimental!
typedef struct flate·Reader flate·Reader;
typedef struct flate·Writer flate·Writer;
flate·Reader *flate·openreader(io·Reader rdr, void* r, mem·Allocator mem, void* m);
int flate·read(flate·Reader *rdr, int sz, int n, void *buf);
error flate·closereader(flate·Reader *rdr);
flate·Writer *flate·openwriter(io·Writer wtr, void* w, mem·Allocator mem, void* m);
int flate·write(flate·Writer *wtr, int sz, int n, void *buf);
error flate·closewriter(flate·Writer *wtr);
// -----------------------------------------------------------------------------
// libgz
typedef void gz·Stream;
gz·Stream* gz·open(byte *path, byte *mode);
error gz·close(gz·Stream* s);
int gz·read(gz·Stream *s, int sz, int n, void* buf);
int gz·readln(gz·Stream *s, int n, byte *buf);
byte gz·getbyte(gz·Stream *s);
error gz·ungetbyte(gz·Stream *s, byte c);
int gz·write(gz·Stream *s, int sz, int n, void* buf);
error gz·putbyte(gz·Stream *s, byte str);
error gz·putstring(gz·Stream *s, byte *str);
int gz·printf(gz·Stream *s, byte *fmt, ...);
error gz·flush(gz·Stream *s);
vlong gz·seek(gz·Stream *s, long off, enum SeekPos whence);
// -----------------------------------------------------------------------------
// libjson
// NOTE: Experimental!
// -----------------------------------------------------------------------------
// error handling functions
void errorf(byte* fmt, ...);
void verrorf(byte* fmt, va_list args);
void panicf(byte *fmt, ...);
void vpanicf(byte *fmt, va_list args);
// -----------------------------------------------------------------------------
// sorting
void sort·ints(uintptr n, int arr[]);
void sort·int8s(uintptr n, int8 arr[]);
void sort·int16s(uintptr n, int16 arr[]);
void sort·int32s(uintptr n, int32 arr[]);
void sort·int64s(uintptr n, int64 arr[]);
void sort·uints(uintptr n, uint arr[]);
void sort·uint8s(uintptr n, uint8 arr[]);
void sort·uint16s(uintptr n, uint16 arr[]);
void sort·uint32s(uintptr n, uint32 arr[]);
void sort·uint64s(uintptr n, uint64 arr[]);
void sort·floats(uintptr n, float arr[]);
void sort·doubles(uintptr n, double arr[]);
void sort·strings(uintptr n, byte* arr[]);
// -----------------------------------------------------------------------------
// fast random number generation
error rng·init(uint64 seed);
double rng·random();
bool rng·bernoulli(double f);
uint64 rng·randi(int max);
// -----------------------------------------------------------------------------
// variable arguments
/* from plan9 libc */
#define ERRMAX 128 /* max length of error string */
#define SET(x) ((x)=0)
#define USED(x) if(x){}else{}
#ifdef __GNUC__
# if __GNUC__ >= 3
# undef USED
# define USED(x) ((void)(x))
# endif
#endif
extern char *argv0;
#define ARGBEGIN for((argv0?0:(argv0=*argv)),argv++,argc--;\
argv[0] && argv[0][0]=='-' && argv[0][1];\
argc--, argv++) {\
byte *_args, *_argt;\
rune _argc;\
_args = &argv[0][1];\
if(_args[0]=='-' && _args[1]==0){\
argc--; argv++; break;\
}\
_argc = 0;\
while(*_args && (_args += utf8·bytetorune(&_argc, _args)))\
switch(_argc)
#define ARGEND SET(_argt);USED(_argt);USED(_argc);USED(_args);}USED(argv);USED(argc);
#define ARGF() (_argt=_args, _args="",\
(*_argt? _argt: argv[1]? (argc--, *++argv): 0))
#define EARGF(x) (_argt=_args, _args="",\
(*_argt? _argt: argv[1]? (argc--, *++argv): ((x), abort(), (char*)0)))
#define ARGC() _argc
|