summaryrefslogtreecommitdiff
path: root/sheep/core.c
blob: a9719a95acb21477ba7f23e2003ba2f89929d198 (plain)
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/*
 * sheep/core.c
 *
 * Copyright (c) 2009 Johannes Weiner <hannes@cmpxchg.org>
 */
#include <sheep/function.h>
#include <sheep/foreign.h>
#include <sheep/compile.h>
#include <sheep/module.h>
#include <sheep/parse.h>
#include <sheep/code.h>
#include <sheep/list.h>
#include <sheep/name.h>
#include <sheep/type.h>
#include <sheep/util.h>
#include <sheep/map.h>
#include <sheep/gc.h>
#include <sheep/vm.h>
#include <stdarg.h>
#include <string.h>
#include <stdio.h>

#include <sheep/core.h>

/* (quote expr) */
static int compile_quote(struct sheep_compile *compile,
			 struct sheep_function *function,
			 struct sheep_context *context,
			 struct sheep_list *args)
{
	unsigned int slot;
	sheep_t expr;

	if (sheep_parse(compile, args, "e", &expr))
		return -1;

	slot = sheep_vm_constant(compile->vm, expr);
	sheep_emit(&function->code, SHEEP_GLOBAL, slot);
	return 0;
}

static int tailposition(struct sheep_context *context, struct sheep_list *block)
{
	struct sheep_list *next;

	next = sheep_list(block->tail);
	if (next->head)
		return 0;

	if (context->flags & SHEEP_CONTEXT_FUNCTION)
		return 1;

	if (!context->parent)
		return 0;

	if (context->parent->flags & SHEEP_CONTEXT_TAILFORM)
		return 1;

	return 0;
}

static int do_compile_forms(struct sheep_compile *compile,
			    struct sheep_function *function,
			    struct sheep_context *context,
			    struct sheep_list *args)
{
	for (;;) {
		sheep_t value = args->head;

		if (tailposition(context, args))
			context->flags |= SHEEP_CONTEXT_TAILFORM;

		if (sheep_compile_object(compile, function, context, value))
			return -1;

		args = sheep_list(args->tail);
		if (!args->head)
			break;
		/*
		 * The last value is the value of the whole block,
		 * drop everything in between to keep the stack
		 * balanced.
		 */
		sheep_emit(&function->code, SHEEP_DROP, 0);
	}
	return 0;
}

static int do_compile_block(struct sheep_compile *compile,
			    struct sheep_function *function,
			    struct sheep_context *parent,
			    struct sheep_map *env,
			    struct sheep_list *args,
			    int function_body)
{
	struct sheep_context context = {
		.env = env,
		.parent = parent,
	};

	if (function_body)
		context.flags |= SHEEP_CONTEXT_FUNCTION;

	return do_compile_forms(compile, function, &context, args);
}

/* (block expr*) */
static int compile_block(struct sheep_compile *compile,
			 struct sheep_function *function,
			 struct sheep_context *context,
			 struct sheep_list *args)
{
	SHEEP_DEFINE_MAP(env);
	int ret;

	/* Just make sure the block is not empty */
	if (sheep_parse(compile, args, "R", &args))
		return -1;

	ret = do_compile_block(compile, function, context, &env, args, 0);

	sheep_map_drain(&env);
	return ret;
}

/* (with (name value) expr*) */
static int compile_with(struct sheep_compile *compile,
			struct sheep_function *function,
			struct sheep_context *context,
			struct sheep_list *args)
{
	struct sheep_list *binding, *body;
	SHEEP_DEFINE_MAP(env);
	struct sheep_context block = {
		.env = &env,
		.parent = context,
	};
	unsigned int slot;
	const char *name;
	sheep_t value;
	int ret;

	if (sheep_parse(compile, args, "lR", &binding, &body))
		return -1;

	if (__sheep_parse(compile, args, binding, "se", &name, &value))
		return -1;

	if (sheep_compile_object(compile, function, &block, value))
		return -1;

	slot = sheep_function_local(function);
	sheep_emit(&function->code, SHEEP_SET_LOCAL, slot);
	sheep_map_set(&env, name, (void *)(unsigned long)slot);

	ret = do_compile_forms(compile, function, &block, body);

	sheep_map_drain(&env);
	return ret;
}

static void compile_set_return(struct sheep_compile *compile,
			       struct sheep_function *function,
			       struct sheep_context *context,
			       const char *name)
{
	unsigned int slot;

	sheep_emit(&function->code, SHEEP_DUP, 0);
	if (context->parent) {
		slot = sheep_function_local(function);
		sheep_emit(&function->code, SHEEP_SET_LOCAL, slot);
	} else {
		slot = sheep_vm_global(compile->vm);
		sheep_emit(&function->code, SHEEP_SET_GLOBAL, slot);
	}
	sheep_map_set(context->env, name, (void *)(unsigned long)slot);
}

/* (variable name expr) */
static int compile_variable(struct sheep_compile *compile,
			    struct sheep_function *function,
			    struct sheep_context *context,
			    struct sheep_list *args)
{
	SHEEP_DEFINE_MAP(env);
	struct sheep_context block = {
		.env = &env,
		.parent = context,
	};
	const char *name;
	sheep_t value;
	int ret = -1;

	if (sheep_parse(compile, args, "se", &name, &value))
		goto out;

	if (sheep_compile_object(compile, function, &block, value))
		goto out;
	compile_set_return(compile, function, context, name);
	ret = 0;
out:
	sheep_map_drain(&env);
	return ret;
}

/* (function name? (arg*) expr*) */
static int compile_function(struct sheep_compile *compile,
			    struct sheep_function *function,
			    struct sheep_context *context,
			    struct sheep_list *args)
{
	struct sheep_list *parms, *body;
	struct sheep_function *childfun;
	SHEEP_DEFINE_MAP(env);
	unsigned int cslot;
	sheep_t maybe_name;
	const char *name;
	sheep_t sheep;
	int ret = -1;

	maybe_name = sheep_list(args->tail)->head;
	if (maybe_name && sheep_type(maybe_name) == &sheep_name_type) {
		if (sheep_parse(compile, args, "slR", &name, &parms, &body))
			return -1;
	} else {
		if (sheep_parse(compile, args, "lR", &parms, &body))
			return -1;
		name = NULL;
	}

	sheep = sheep_make_function(compile->vm, name);
	childfun = sheep_data(sheep);

	while (parms->head) {
		struct sheep_list *rest;
		unsigned int slot;
		const char *parm;

		if (__sheep_parse(compile, args, parms, "sr", &parm, &rest))
			goto out;

		slot = sheep_function_local(childfun);
		if (sheep_map_set(&env, parm, (void *)(unsigned long)slot)) {
			sheep_parser_error(compile, parms->head,
					"duplicate function parameter");
			goto out;
		}

		childfun->nr_parms++;
		parms = rest;
	}

	cslot = sheep_vm_constant(compile->vm, sheep);
	sheep_emit(&function->code, SHEEP_CLOSURE, cslot);
	if (name)
		compile_set_return(compile, function, context, name);

	sheep_protect(compile->vm, sheep);
	ret = do_compile_block(compile, childfun, context, &env, body, 1);
	sheep_unprotect(compile->vm, sheep);
	if (ret) {
		/* Do not leave the dead slot bound... */
		if (name)
			sheep_map_del(context->env, name);
		goto out;
	}
	sheep_code_finalize(&childfun->code);
	if (childfun->foreign)
		sheep_foreign_propagate(function, childfun);
out:
	sheep_map_drain(&env);
	return ret;
}

/* (type name slotnames*) */
static int compile_type(struct sheep_compile *compile,
			struct sheep_function *function,
			struct sheep_context *context,
			struct sheep_list *args)
{
	const char **slotnames = NULL;
	unsigned int nr_slots = 0;
	struct sheep_list *names;
	unsigned int slot;
	const char *name;
	sheep_t class;

	if (sheep_parse(compile, args, "sR", &name, &names))
		return -1;

	do {
		const char *slotname;

		if (__sheep_parse(compile, args, names, "sr", &slotname, &names))
			goto err;

		slotnames = sheep_realloc(slotnames, sizeof(char *) * ++nr_slots);
		slotnames[nr_slots - 1] = sheep_strdup(slotname);
	} while (names->head);

	class = sheep_make_typeclass(compile->vm, name, slotnames, nr_slots);

	slot = sheep_vm_constant(compile->vm, class);
	sheep_emit(&function->code, SHEEP_GLOBAL, slot);
	compile_set_return(compile, function, context, name);

	return 0;
err:
	while (nr_slots--)
		sheep_free(slotnames[nr_slots]);
	sheep_free(slotnames);
	return -1;
}

static int do_compile_chain(struct sheep_compile *compile,
			    struct sheep_function *function,
			    struct sheep_context *context,
			    struct sheep_list *args,
			    enum sheep_opcode endbranch)
{
	SHEEP_DEFINE_MAP(env);
	struct sheep_context block = {
		.env = &env,
		.parent = context,
	};
	struct sheep_list *args2;
	unsigned long Lend;
	int ret = -1;

	if (sheep_parse(compile, args, "R", &args2))
		return -1;

	Lend = sheep_code_jump(&function->code);
	for (;;) {
		sheep_t expr;

		if (tailposition(context, args2))
			block.flags |= SHEEP_CONTEXT_TAILFORM;

		if (__sheep_parse(compile, args, args2, "er", &expr, &args2))
			goto out;

		if (sheep_compile_object(compile, function, &block, expr))
			goto out;

		if (!args2->head)
			break;

		sheep_emit(&function->code, endbranch, Lend);
		sheep_emit(&function->code, SHEEP_DROP, 0);
	}

	sheep_code_label(&function->code, Lend);
	ret = 0;
out:
	sheep_map_drain(&env);
	return ret;
}

/* (or one two three*?) */
static int compile_or(struct sheep_compile *compile,
		      struct sheep_function *function,
		      struct sheep_context *context,
		      struct sheep_list *args)
{
	return do_compile_chain(compile, function, context, args, SHEEP_BRT);
}

/* (and one two three*?) */
static int compile_and(struct sheep_compile *compile,
		       struct sheep_function *function,
		       struct sheep_context *context,
		       struct sheep_list *args)
{
	return do_compile_chain(compile, function, context, args, SHEEP_BRF);
}

/* (if cond then else*?) */
static int compile_if(struct sheep_compile *compile,
		      struct sheep_function *function,
		      struct sheep_context *context,
		      struct sheep_list *args)
{
	SHEEP_DEFINE_MAP(env);
	struct sheep_context block = {
		.env = &env,
		.parent = context,
	};
	struct sheep_list *elseform;
	unsigned long Lelse;
	sheep_t cond, then;
	int ret = -1;

	if (sheep_parse(compile, args, "eer", &cond, &then, &elseform))
		return -1;

	Lelse = sheep_code_jump(&function->code);

	if (sheep_compile_object(compile, function, &block, cond))
		goto out;
	sheep_emit(&function->code, SHEEP_BRF, Lelse);

	sheep_emit(&function->code, SHEEP_DROP, 0);
	if (context->flags & SHEEP_CONTEXT_TAILFORM)
		block.flags |= SHEEP_CONTEXT_TAILFORM;
	if (sheep_compile_object(compile, function, &block, then))
		goto out;
	block.flags &= ~SHEEP_CONTEXT_TAILFORM;

	if (elseform->head) {
		unsigned long Lend;

		Lend = sheep_code_jump(&function->code);
		sheep_emit(&function->code, SHEEP_BR, Lend);

		sheep_code_label(&function->code, Lelse);

		sheep_emit(&function->code, SHEEP_DROP, 0);
		if (do_compile_forms(compile, function, &block, elseform))
			goto out;

		sheep_code_label(&function->code, Lend);
	} else
		sheep_code_label(&function->code, Lelse);

	ret = 0;
out:
	sheep_map_drain(&env);
	return ret;
}

/* (set name value) */
static int compile_set(struct sheep_compile *compile,
		       struct sheep_function *function,
		       struct sheep_context *context,
		       struct sheep_list *args)
{
	struct sheep_name *name;
	SHEEP_DEFINE_MAP(env);
	struct sheep_context block = {
		.env = &env,
		.parent = context,
	};
	sheep_t value;
	int ret = -1;

	if (sheep_parse(compile, args, "ne", &name, &value))
		goto out;

	if (sheep_compile_object(compile, function, &block, value))
		goto out;

	ret = sheep_compile_set(compile, function, context,
				sheep_list(args->tail)->head);
out:
	sheep_map_drain(&env);
	return ret;
}

/* (load name) */
static int compile_load(struct sheep_compile *compile,
			struct sheep_function *function,
			struct sheep_context *context,
			struct sheep_list *args)
{
	unsigned int slot;
	const char *name;

	if (sheep_parse(compile, args, "s", &name))
		return -1;

	slot = sheep_vm_key(compile->vm, name);
	sheep_emit(&function->code, SHEEP_LOAD, slot);
	compile_set_return(compile, function, context, name);

	return 0;
}

void sheep_core_init(struct sheep_vm *vm)
{
	sheep_map_set(&vm->specials, "quote", compile_quote);
	sheep_map_set(&vm->specials, "block", compile_block);
	sheep_map_set(&vm->specials, "with", compile_with);
	sheep_map_set(&vm->specials, "variable", compile_variable);
	sheep_map_set(&vm->specials, "function", compile_function);
	sheep_map_set(&vm->specials, "type", compile_type);
	sheep_map_set(&vm->specials, "or", compile_or);
	sheep_map_set(&vm->specials, "and", compile_and);
	sheep_map_set(&vm->specials, "if", compile_if);
	sheep_map_set(&vm->specials, "set", compile_set);
	sheep_map_set(&vm->specials, "load", compile_load);
}

void sheep_core_exit(struct sheep_vm *vm)
{
	sheep_map_drain(&vm->specials);
}