~yerinalexey/sushi

ref: 138cf54b2469ea699b118a9043db1f3ed8953d0e sushi/src/env.c -rw-r--r-- 11.7 KiB
138cf54bAlexey Yerin env: implement do 5 months ago
                                                                                
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
#include <assert.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "env.h"
#include "eval.h"
#include "expr.h"
#include "utils.h"

static bool
is_truthy(struct expression *expr)
{
	if (expr->kind == EXPR_LIST && expr->list != NULL) {
		return true;
	}
	if (expr->kind == EXPR_INTEGER && expr->integer != 0) {
		return true;
	}
	/* TODO: more rules? */
	return false;
}

static struct expression *
env_add(struct location loc, struct list_expression *list,
	struct binding **bindings)
{
	intmax_t res = 0;

	while (list) {
		struct expression *value = eval(list->expr, bindings);
		if (value->kind != EXPR_INTEGER) {
			error(value->loc, "Cannot perform addition on non-integers");
		}
		res += value->integer;

		list = list->next;
	}

	struct expression *new = mkexpression();
	new->kind = EXPR_INTEGER;
	new->integer = res;
	return new;
}

static struct expression *
env_sub(struct location loc, struct list_expression *list,
	struct binding **bindings)
{
	if (!list) {
		error(loc, "Expected at least one argument to subtract");
	}
	struct expression *first = eval(list->expr, bindings);
	list = list->next;
	intmax_t res = first->integer;

	while (list) {
		struct expression *value = eval(list->expr, bindings);
		if (value->kind != EXPR_INTEGER) {
			error(value->loc, "Cannot perform subtraction on non-integers");
		}
		res -= value->integer;

		list = list->next;
	}

	struct expression *new = mkexpression();
	new->kind = EXPR_INTEGER;
	new->integer = res;
	return new;
}

static struct expression *
env_mul(struct location loc, struct list_expression *list,
	struct binding **bindings)
{
	intmax_t res = 1;

	while (list) {
		struct expression *value = eval(list->expr, bindings);
		if (value->kind != EXPR_INTEGER) {
			error(value->loc, "Cannot perform multiplication on non-integers");
		}
		res *= value->integer;

		list = list->next;
	}

	struct expression *new = mkexpression();
	new->kind = EXPR_INTEGER;
	new->integer = res;
	return new;
}

static struct expression *
env_div(struct location loc, struct list_expression *list,
	struct binding **bindings)
{
	if (!list) {
		error(loc, "Expected at least one argument to divide");
	}
	struct expression *first = eval(list->expr, bindings);
	list = list->next;
	intmax_t res = first->integer;

	while (list) {
		struct expression *value = eval(list->expr, bindings);
		if (value->kind != EXPR_INTEGER) {
			error(value->loc, "Cannot perform division on non-integers");
		}
		if (value->integer == 0) {
			error(value->loc, "Cannot divide by zero");
		}
		res /= value->integer;

		list = list->next;
	}

	struct expression *new = mkexpression();
	new->kind = EXPR_INTEGER;
	new->integer = res;
	return new;
}

enum cmp_kind {
	CMP_EQ, /* = */
	CMP_GE, /* >= */
	CMP_GT, /* > */
	CMP_LE, /* <= */
	CMP_LT, /* < */
};

static struct expression *
cmp(struct location loc, struct list_expression *list,
	struct binding **bindings, enum cmp_kind cmp)
{
	if (!list || !list->next) {
		error(loc, "Not enough arguments");
	}
	struct expression *first = eval(list->expr, bindings);
	struct expression *second = eval(list->next->expr, bindings);

	if (first->kind != EXPR_INTEGER || second->kind != EXPR_INTEGER) {
		error(loc, "Cannot compare non-integers");
	}

	bool res;
	switch (cmp) {
	case CMP_EQ:
		res = first->integer == second->integer;
		break;
	case CMP_GE:
		res = first->integer >= second->integer;
		break;
	case CMP_GT:
		res = first->integer > second->integer;
		break;
	case CMP_LE:
		res = first->integer <= second->integer;
		break;
	case CMP_LT:
		res = first->integer < second->integer;
		break;
	}

	struct expression *new = mkexpression();
	new->kind = EXPR_INTEGER;
	new->integer = res ? 1 : 0;
	return new;
}


static struct expression *
env_eq(struct location loc, struct list_expression *list,
	struct binding **bindings)
{
	return cmp(loc, list, bindings, CMP_EQ);
}

static struct expression *
env_ge(struct location loc, struct list_expression *list,
	struct binding **bindings)
{
	return cmp(loc, list, bindings, CMP_GE);
}

static struct expression *
env_gt(struct location loc, struct list_expression *list,
	struct binding **bindings)
{
	return cmp(loc, list, bindings, CMP_GT);
}

static struct expression *
env_le(struct location loc, struct list_expression *list,
	struct binding **bindings)
{
	return cmp(loc, list, bindings, CMP_LE);
}

static struct expression *
env_lt(struct location loc, struct list_expression *list,
	struct binding **bindings)
{
	return cmp(loc, list, bindings, CMP_LT);
}

static struct expression *
env_and(struct location loc, struct list_expression *list,
	struct binding **bindings)
{
	bool res = true;

	while (list) {
		struct expression *value = eval(list->expr, bindings);
		if (!is_truthy(value)) {
			res = false;
			break;
		}
		list = list->next;
	}

	struct expression *new = mkexpression();
	new->kind = EXPR_INTEGER;
	new->integer = res ? 1 : 0;
	return new;
}

static struct expression *
env_not(struct location loc, struct list_expression *list,
	struct binding **bindings)
{
	if (!list) {
		error(loc, "Expected an argument");
	}
	struct expression *value = eval(list->expr, bindings);

	struct expression *new = mkexpression();
	new->kind = EXPR_INTEGER;
	new->integer = !is_truthy(value) ? 1 : 0;
	return new;
}

static struct expression *
env_or(struct location loc, struct list_expression *list,
	struct binding **bindings)
{
	bool res = false;

	while (list) {
		struct expression *value = eval(list->expr, bindings);
		if (is_truthy(value)) {
			res = true;
			break;
		}
		list = list->next;
	}

	struct expression *new = mkexpression();
	new->kind = EXPR_INTEGER;
	new->integer = res ? 1 : 0;
	return new;
}

static struct expression *
env_define(struct location loc, struct list_expression *list,
	struct binding **bindings)
{
	if (!list || !list->next) {
		error(loc, "Not enough arguments");
	}

	struct expression *handle = list->expr;
	if (handle->kind != EXPR_SYMBOL) {
		error(loc, "Invalid define; expected a symbol");
	}
	struct expression *value = eval(list->next->expr, bindings);

	if (binding_lookup(*bindings, handle->symbol)) {
		error(loc, "'%s' is already defined", handle->symbol);
	}

	binding_insert(bindings, handle->symbol, value);
	return mknil();
}

static struct expression *
env_set(struct location loc, struct list_expression *list,
	struct binding **bindings)
{
	if (!list || !list->next) {
		error(loc, "Not enough arguments");
	}

	struct expression *handle = list->expr;
	if (handle->kind != EXPR_SYMBOL) {
		error(loc, "Invalid set!; expected a symbol");
	}
	struct expression *value = eval(list->next->expr, bindings);

	struct binding *bind = binding_lookup(*bindings, handle->symbol);
	if (!bind) {
		error(loc, "Undefined symbol '%s'", handle->symbol);
	}
	expression_free(bind->expr);
	bind->expr = value;

	return mknil();
}

static struct expression *
env_if(struct location loc, struct list_expression *list,
	struct binding **bindings)
{
	if (!list || !list->next || !list->next->next) {
		error(loc, "Not enough arguments");
	}

	struct expression *condition = eval(list->expr, bindings);
	struct expression *iftrue = list->next->expr;
	struct expression *iffalse = list->next->next->expr;

	if (is_truthy(condition)) {
		return eval(iftrue, bindings);
	} else {
		return eval(iffalse, bindings);
	}
}

static struct expression *
env_do(struct location loc, struct list_expression *list,
	struct binding **bindings)
{
	while (list) {
		expression_free(eval(list->expr, bindings));
		list = list->next;
	}
	return mknil();
}

static struct expression *
env_cons(struct location loc, struct list_expression *list,
	struct binding **bindings)
{
	if (!list || !list->next) {
		error(loc, "Not enough arguments");
	}

	struct expression *new = mkexpression();
	new->kind = EXPR_PAIR;
	new->pair[0] = eval(list->expr, bindings);
	new->pair[1] = eval(list->next->expr, bindings);
	return new;
}

static struct expression *
env_car(struct location loc, struct list_expression *list,
	struct binding **bindings)
{
	if (!list) {
		error(loc, "Not enough arguments");
	}

	struct expression *pair = eval(list->expr, bindings);
	if (pair->kind != EXPR_PAIR) {
		error(loc, "car must be used on a pair");
	}

	return expression_dup(pair->pair[0]);
}

static struct expression *
env_cdr(struct location loc, struct list_expression *list,
	struct binding **bindings)
{
	if (!list) {
		error(loc, "Not enough arguments");
	}

	struct expression *pair = eval(list->expr, bindings);
	if (pair->kind != EXPR_PAIR) {
		error(loc, "cdr must be used on a pair");
	}

	return expression_dup(pair->pair[1]);
}

static struct expression *
env_string_concat(struct location loc, struct list_expression *list,
	struct binding **bindings)
{
	/* TODO: varargs */
	if (!list || !list->next) {
		error(loc, "Not enough arguments");
	}
	struct expression *a = eval(list->expr, bindings);
	struct expression *b = eval(list->next->expr, bindings);
	if (a->kind != EXPR_STRING || b->kind != EXPR_STRING) {
		error(loc, "Cannot concatenate non-strings");
	}
	size_t alen = strlen(a->string);
	size_t blen = strlen(b->string);

	char *new = xmalloc(alen + blen + 1);
	strcat(new, a->string);
	strcat(new, b->string);

	struct expression *expr = mkexpression();
	expr->kind = EXPR_STRING;
	expr->string = new;
	return expr;
}

static struct expression *
env_string_sub(struct location loc, struct list_expression *list,
	struct binding **bindings)
{
	if (!list || !list->next) {
		error(loc, "Not enough arguments");
	}
	struct expression *str = eval(list->expr, bindings);
	if (str->kind != EXPR_STRING) {
		error(loc, "Expected first argument to be a string");
	}
	size_t slen = strlen(str->string);

	struct expression *estart = eval(list->next->expr, bindings);
	if (estart->kind != EXPR_INTEGER) {
		error(loc, "Index must be a number");
	}
	size_t start = estart->integer;
	size_t end = slen - 1;

	if (list->next->next) {
		struct expression *eend = eval(list->next->next->expr, bindings);
		if (eend->kind != EXPR_INTEGER) {
			error(loc, "Index must be a number");
		}
		end = eend->integer;
	}

	if (start > end) {
		error(loc, "Invalid slice");
	}
	if (end > slen) {
		error(loc, "End index outside of string");
	}
	assert(end - start <= slen);

	char *new = xmalloc(end - start + 1);
	strncpy(new, str->string + start, end - start);

	struct expression *expr = mkexpression();
	expr->kind = EXPR_STRING;
	expr->string = new;
	return expr;
}

static struct expression *
env_display(struct location loc, struct list_expression *list,
	struct binding **bindings)
{
	while (list) {
		struct expression *value = eval(list->expr, bindings);
		switch (value->kind) {
		case EXPR_STRING:
			fputs(value->string, stdout);
			break;
		default:
			expression_print(stdout, value);
		}
		list = list->next;
	}
	return mknil();
}

static struct expression *
env_newline(struct location loc, struct list_expression *list,
	struct binding **bindings)
{
	puts("");
	return mknil();
}

struct env_function
environment[] = {
	{ .name = "+", .func = &env_add },
	{ .name = "-", .func = &env_sub },
	{ .name = "*", .func = &env_mul },
	{ .name = "/", .func = &env_div },

	{ .name = "=",  .func = &env_eq },
	{ .name = ">=", .func = &env_ge },
	{ .name = ">",  .func = &env_gt },
	{ .name = "<=", .func = &env_le },
	{ .name = "<",  .func = &env_lt },

	{ .name = "and", .func = &env_and },
	{ .name = "not", .func = &env_not },
	{ .name = "or",  .func = &env_or },

	{ .name = "define", .func = &env_define },
	{ .name = "set!",   .func = &env_set },
	{ .name = "if",     .func = &env_if },
	{ .name = "do",     .func = &env_do },

	{ .name = "cons", .func = &env_cons },
	{ .name = "car",  .func = &env_car },
	{ .name = "cdr",  .func = &env_cdr },

	{ .name = "string-concat", .func = &env_string_concat },
	{ .name = "string-sub",    .func = &env_string_sub },

	{ .name = "display", .func = &env_display },
	{ .name = "newline", .func = &env_newline },

	{ .name = NULL, .func = NULL },
};