-
Notifications
You must be signed in to change notification settings - Fork 42
/
kitten.h
476 lines (413 loc) · 11.4 KB
/
kitten.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
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
#ifndef KITTEN_H
#define KITTEN_H
#include <assert.h>
#include <float.h>
#include <inttypes.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int k_bool_t;
typedef uint32_t k_char_t;
typedef double k_float_t;
typedef FILE* k_handle_t;
typedef int64_t k_int_t;
typedef uint64_t k_cell_t;
typedef enum KClosedName {
K_CLOSED,
K_RECLOSED
} KClosedName;
typedef enum KType {
K_UNBOXED = 0x00,
K_BOOL = K_UNBOXED + 1,
K_CHAR,
K_FLOAT,
K_HANDLE,
K_INT,
K_UNIT,
K_BOXED = 0x10,
K_ACTIVATION = K_BOXED + 1,
K_CHOICE,
K_OPTION,
K_PAIR,
K_USER,
K_VECTOR,
K_MAX_TYPE
} KType;
struct KActivation;
struct KChoice;
struct KPair;
struct KUser;
struct KVector;
typedef union KData {
k_bool_t as_bool;
k_char_t as_char;
k_int_t as_int;
k_float_t as_float;
k_handle_t as_handle;
k_cell_t* as_refs;
struct KActivation* as_activation;
struct KChoice* as_choice;
struct KOption* as_option;
struct KPair* as_pair;
struct KUser* as_user;
struct KVector* as_vector;
} KData;
typedef struct KObject {
KType type;
KData data;
} KObject;
typedef struct KActivation {
k_cell_t refs;
void* function;
KObject* begin;
KObject* end;
} KActivation;
typedef struct KPair {
k_cell_t refs;
KObject first;
KObject rest;
} KPair;
typedef struct KChoice {
k_cell_t refs;
k_cell_t which;
KObject value;
} KChoice;
typedef struct KOption {
k_cell_t refs;
KObject value;
} KOption;
typedef struct KUser {
k_cell_t refs;
k_cell_t size;
k_cell_t tag;
KObject fields[];
} KUser;
typedef struct KVector {
k_cell_t refs;
KObject* begin;
KObject* end;
KObject* capacity;
} KVector;
typedef struct KCall {
void* address;
int closure;
} KCall;
typedef struct KClosure {
k_cell_t size;
KObject fields[];
} KClosure;
extern KCall* k_call;
extern KClosure** k_closure;
extern KObject* k_data;
extern KObject* k_locals;
// Runtime initialization.
void k_runtime_init(int, char**);
void k_runtime_quit(void);
// Memory and reference counting.
void* k_mem_alloc(size_t, size_t);
void k_mem_free(void*);
void* k_mem_realloc(void*, size_t, size_t);
static inline KObject k_object_retain(const KObject object) {
if (object.type > K_BOXED && object.data.as_refs)
++*object.data.as_refs;
return object;
}
void k_object_release(KObject);
int k_object_unique(KObject);
// Value creation.
KObject k_activation_new(void*, ...);
KObject k_left_new(KObject);
KObject k_pair_new(KObject, KObject);
KObject k_right_new(KObject);
KObject k_some_new(KObject);
KObject k_vector_new(size_t);
// Vector operations.
KObject k_vector(size_t, ...);
KObject k_vector_append(KObject, KObject);
KObject k_vector_get(KObject, k_cell_t);
k_cell_t k_vector_size(KObject);
void k_vector_set(KObject, k_cell_t, KObject);
// Intrinsics.
void k_in_add_vector(void);
void k_in_char_to_int(void);
void k_in_close(void);
void k_in_construct(k_cell_t, size_t);
void k_in_first(void);
void k_in_from_left(void);
void k_in_from_right(void);
void k_in_from_some(void);
void k_in_get(void);
void k_in_get_line(void);
void k_in_init(void);
void k_in_int_to_char(void);
void k_in_left(void);
void k_in_length(void);
void k_in_make_vector(size_t);
void k_in_match(size_t, ...);
void k_in_mod_float(void);
void k_in_pair(void);
void k_in_print(void);
void k_in_rest(void);
void k_in_right(void);
void k_in_set(void);
void k_in_show_float(void);
void k_in_show_int(void);
void k_in_some(void);
void k_in_tail(void);
#define K_ASSERT_IMPOSSIBLE() \
do { \
fprintf(stderr, "the impossible has happened\n"); \
exit(1); \
} while (0)
////////////////////////////////////////////////////////////////////////////////
// Stack manipulation.
static inline void k_closure_drop() {
for (size_t i = 0; i < k_closure[0]->size; ++i)
k_object_release(k_closure[0]->fields[i]);
k_mem_free(k_closure[0]);
++k_closure;
}
static inline void k_data_drop(size_t size) {
while (size--)
k_object_release(*k_data++);
}
static inline void k_locals_drop(size_t size) {
while (size--)
k_object_release(*k_locals++);
}
static inline KObject k_closure_get(const size_t i) {
return k_closure[0]->fields[i];
}
static inline KObject k_locals_get(const size_t i) {
return k_locals[i];
}
static inline void k_closure_push(KClosure* const closure) {
*--k_closure = closure;
}
static inline void k_call_push(const KCall call) {
*--k_call = call;
}
static inline void k_data_push(const KObject object) {
*--k_data = object;
}
static inline KObject k_data_pop() {
return *k_data++;
}
static inline void k_locals_enter(size_t size) {
while (size--)
*--k_locals = k_data_pop();
}
static inline KCall k_call_pop() {
return *k_call++;
}
////////////////////////////////////////////////////////////////////////////////
// Value creation.
static inline KObject k_bool_new(const k_bool_t value) {
return (KObject) { .data = (KData) { .as_bool = !!value }, .type = K_BOOL };
}
static inline KObject k_char_new(const k_char_t value) {
return (KObject) { .data = (KData) { .as_char = value }, .type = K_CHAR };
}
static inline KObject k_float_new(const k_float_t value) {
return (KObject) { .data = (KData) { .as_float = value }, .type = K_FLOAT };
}
static inline KObject k_handle_new(const k_handle_t value) {
return (KObject) { .data = (KData) { .as_handle = value }, .type = K_HANDLE };
}
static inline KObject k_int_new(const k_int_t value) {
return (KObject) { .data = (KData) { .as_int = value }, .type = K_INT };
}
static inline KObject k_none_new() {
return (KObject) { .data = (KData) { .as_option = NULL }, .type = K_OPTION };
}
static inline KObject k_unit_new() {
return (KObject) { .data = (KData) { .as_int = 0 }, .type = K_UNIT };
}
////////////////////////////////////////////////////////////////////////////////
// Intrinsics.
#define K_IN_ACT(LABEL, ...) \
do { \
k_data_push(k_activation_new(&&LABEL, __VA_ARGS__)); \
} while (0)
#define K_IN_CALL(CALL, RETURN) \
do { \
k_call_push(((KCall) { \
.address = &&RETURN, \
.closure = 0, \
})); \
goto CALL; \
} while (0)
#define K_IN_TAIL_CALL(LOCALS, CALL) \
do { \
k_locals_drop(LOCALS); \
goto CALL; \
} while (0)
#define K_IN_RETURN(LOCALS) \
do { \
KCall call = k_call_pop(); \
k_locals_drop(LOCALS); \
if (call.closure) \
k_closure_drop(); \
goto *call.address; \
} while (0)
#define K_IN_APPLY(RETURN) \
do { \
const KObject object = k_data_pop(); \
assert(object.type == K_ACTIVATION); \
const KActivation* const activation = object.data.as_activation; \
const size_t size = activation->end - activation->begin; \
k_closure_push(k_mem_alloc(1, sizeof(k_cell_t) + size * sizeof(KObject))); \
k_closure[0]->size = size; \
for (size_t i = 0; i < size; ++i) \
k_closure[0]->fields[i] = k_object_retain(activation->begin[i]); \
k_call_push(((KCall) { \
.address = &&RETURN, \
.closure = 1 \
})); \
void* const function = activation->function; \
k_object_release(object); \
goto *function; \
} while (0)
#define K_IN_TAIL_APPLY(LOCALS) \
do { \
k_locals_drop(LOCALS); \
const KObject object = k_data_pop(); \
assert(object.type == K_ACTIVATION); \
const KActivation* const activation = object.data.as_activation; \
const size_t size = activation->end - activation->begin; \
k_closure_drop(); \
k_closure_push(k_mem_alloc(1, sizeof(k_cell_t) + size * sizeof(KObject))); \
k_closure[0]->size = size; \
for (size_t i = 0; i < size; ++i) \
k_closure[0]->fields[i] = k_object_retain(activation->begin[i]); \
void* const function = activation->function; \
k_object_release(object); \
goto *function; \
} while (0)
#define CAT_(A, B) A##B
#define CAT(A, B) CAT_(A, B)
#define K_IN_CHOICE(RETURN) \
do { \
const KObject left = k_data_pop(); \
assert(left.type == K_ACTIVATION); \
const KObject choice = k_data_pop(); \
assert(choice.type == K_CHOICE); \
if (choice.data.as_choice->which == 0) { \
k_data_push(k_object_retain(choice.data.as_choice->value)); \
k_object_release(choice); \
k_data_push(left); \
K_IN_APPLY(RETURN); \
} \
} while (0)
#define K_IN_CHOICE_ELSE(RETURN) \
do { \
const KObject right = k_data_pop(); \
assert(right.type == K_ACTIVATION); \
const KObject left = k_data_pop(); \
assert(left.type == K_ACTIVATION); \
const KObject choice = k_data_pop(); \
assert(choice.type == K_CHOICE); \
switch (choice.data.as_choice->which) { \
case 0: \
k_data_push(k_object_retain(choice.data.as_choice->value)); \
k_object_release(choice); \
k_data_push(left); \
break; \
case 1: \
k_data_push(k_object_retain(choice.data.as_choice->value)); \
k_object_release(choice); \
k_data_push(right); \
break; \
default: \
K_ASSERT_IMPOSSIBLE(); \
} \
K_IN_APPLY(RETURN); \
} while (0)
#define K_IN_IF(RETURN) \
do { \
const KObject true = k_data_pop(); \
assert(true.type == K_ACTIVATION); \
const KObject cond = k_data_pop(); \
assert(cond.type == K_BOOL); \
if (cond.data.as_bool) { \
k_data_push(true); \
K_IN_APPLY(RETURN); \
} else { \
k_object_release(true); \
} \
} while (0)
#define K_IN_IF_ELSE(RETURN) \
do { \
const KObject false = k_data_pop(); \
assert(false.type == K_ACTIVATION); \
const KObject true = k_data_pop(); \
assert(true.type == K_ACTIVATION); \
const KObject cond = k_data_pop(); \
assert(cond.type == K_BOOL); \
if (cond.data.as_bool) { \
k_data_push(true); \
k_object_release(false); \
} else { \
k_object_release(true); \
k_data_push(false); \
} \
K_IN_APPLY(RETURN); \
} while (0)
#define K_IN_OPTION(RETURN) \
do { \
const KObject some = k_data_pop(); \
assert(some.type == K_ACTIVATION); \
const KObject option = k_data_pop(); \
assert(option.type == K_OPTION); \
if (option.data.as_option) { \
k_data_push(k_object_retain(option.data.as_option->value)); \
k_object_release(option); \
k_data_push(some); \
K_IN_APPLY(RETURN); \
} else { \
k_object_release(option); \
k_object_release(some); \
} \
} while (0)
#define K_IN_OPTION_ELSE(RETURN) \
do { \
const KObject none = k_data_pop(); \
assert(none.type == K_ACTIVATION); \
const KObject some = k_data_pop(); \
assert(some.type == K_ACTIVATION); \
const KObject option = k_data_pop(); \
assert(option.type == K_OPTION); \
if (option.data.as_option) { \
k_data_push(k_object_retain(option.data.as_option->value)); \
k_object_release(option); \
k_data_push(some); \
k_object_release(none); \
} else { \
k_data_push(none); \
k_object_release(some); \
} \
K_IN_APPLY(RETURN); \
} while (0)
#define K_IN_BINARY(TYPE, OPERATION) \
do { \
const KObject b = k_data_pop(); \
KObject* const a = &k_data[0]; \
assert(a->type == b.type); \
a->data.as_##TYPE = (a->data.as_##TYPE) OPERATION (b.data.as_##TYPE); \
} while (0)
#define K_IN_RELATIONAL(TYPE, OPERATION) \
do { \
const KObject b = k_data_pop(); \
KObject* const a = &k_data[0]; \
assert(a->type == b.type); \
a->type = K_BOOL; \
a->data.as_##TYPE = (a->data.as_##TYPE) OPERATION (b.data.as_##TYPE); \
} while (0)
#define K_IN_UNARY(TYPE, OPERATION) \
do { \
KObject* const a = &k_data[0]; \
a->data.as_##TYPE = OPERATION (a->data.as_##TYPE); \
} while (0)
#endif