value.c 13.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
 * Cherry programming language
 * Copyright (C) 2013 Christoph Mueller
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "cherry/runtime.h"
19
20
#include "cherry/primitives.h"
#include <stdarg.h>
21
#include <stdlib.h>
Chris Müller's avatar
Chris Müller committed
22
#include <string.h>
23
#include <gc.h>
Chris Müller's avatar
Chris Müller committed
24
#include <assert.h>
25
26
27
28
29

// ----------------------------------------------------------------------------
// globals
// ----------------------------------------------------------------------------

30
struct org_cherry_symbollist* 	org_cherry_global_symbollist;
31
struct org_cherry_value*		org_cherry_emptylist;
32
33
struct org_cherry_value*		org_cherry_true;
struct org_cherry_value*		org_cherry_false;
34

35
36
37
38
39
struct org_cherry_value*		org_cherry_symbol_define;
struct org_cherry_value*		org_cherry_symbol_lambda;
struct org_cherry_value*		org_cherry_symbol_if;
struct org_cherry_value*		org_cherry_symbol_quote;

40
41
42
struct org_cherry_value*		org_cherry_symbol_begin;
struct org_cherry_value*		org_cherry_symbol_catch;
struct org_cherry_value*		org_cherry_symbol_finally;
Chris Müller's avatar
Chris Müller committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// ----------------------------------------------------------------------------
// helper methods
// ----------------------------------------------------------------------------


const cy_byte_t*
org_cherry_string_dup(const cy_byte_t* str)
{
	assert(str != 0);

    size_t length = org_cherry_string_size(str);
    cy_byte_t* dest = GC_MALLOC(length);
    memcpy(dest, str, length);
    
	return dest;
}

60
61
62
63
64
65
66
67
68
69
70
71

struct org_cherry_value*
org_cherry_value_dup(struct org_cherry_value* value)
{
	struct org_cherry_value* result = GC_MALLOC(sizeof(struct org_cherry_value));
	
	memcpy(result, value, sizeof(struct org_cherry_value));

	return result;
}


Chris Müller's avatar
Chris Müller committed
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
// ----------------------------------------------------------------------------
// string converter
// ----------------------------------------------------------------------------

struct org_cherry_value*	
org_cherry_fixnum_from_string(const cy_byte_t* str, int base)
{
    cy_fixnum_t value;

    switch(base) {
        case 2:
            value = strtol(str + 2, 0, 2);
            break;
        case 8:
            value = strtol(str + 2, 0, 8);
            break;
        case 16:
            value = strtol(str + 2, 0, 16);
            break;
        default:
            value = strtol(str, 0, 10);
    }

    return org_cherry_fixnum(value);
}

struct org_cherry_value*	
org_cherry_float_from_string(const cy_byte_t* str)
{
    return org_cherry_float(strtod(str, 0));
}

struct org_cherry_value*	
org_cherry_char_from_string(const cy_byte_t* str)
{
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
    if(org_cherry_utf8_compare(str + 1, "newline") == 0)
        return org_cherry_char('\n');
    else if(org_cherry_utf8_compare(str + 1, "null") == 0)
        return org_cherry_char('\0');
    else if(org_cherry_utf8_compare(str + 1, "bell") == 0)
        return org_cherry_char('\a');
    else if(org_cherry_utf8_compare(str + 1, "backspace") == 0)
        return org_cherry_char('\b');
    else if(org_cherry_utf8_compare(str + 1, "formfeed") == 0)
        return org_cherry_char('\f');
    else if(org_cherry_utf8_compare(str + 1, "space") == 0)
        return org_cherry_char(' ');
    else if(org_cherry_utf8_compare(str + 1, "return") == 0)
        return org_cherry_char('\r');
    else if(org_cherry_utf8_compare(str + 1, "tab") == 0)
        return org_cherry_char('\t');
    else if(org_cherry_utf8_compare(str + 1, "vtab") == 0)
        return org_cherry_char('\v');

    // TODO: \uFFFF \UFFFFFF

    return org_cherry_char(org_cherry_utf8_get(str + 1));
Chris Müller's avatar
Chris Müller committed
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
}

struct org_cherry_value*	
org_cherry_symbol_from_string(const cy_byte_t* str)
{
	return org_cherry_symbol(org_cherry_string_dup(str));
}

struct org_cherry_value*	
org_cherry_string_from_string(const cy_byte_t* str)
{
	cy_byte_t* p = GC_MALLOC(org_cherry_string_size(str));
	cy_byte_t* s = p;
	cy_byte_t buffer[7];
	cy_unicode_t c;
144
	int len;
Chris Müller's avatar
Chris Müller committed
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

	while(*str != '\0') {
		if(*str == '\\') {
			switch(*(str + 1)) {
				case 'a': 
					*s = '\a';
					break;
				case 'b':
					*s = '\b';
					break;
				case 'f':
					*s = '\f';
					break;
				case 'n':
					*s = '\n';
					break;
				case 'r':
					*s = '\r';
					break;
				case 't':
					*s = '\t';
					break;
				case 'v':
					*s = '\v';
					break;
				case '0':
					*s = '\0';
					break;
				case '"':
					*s = '"';
					break;
				case 'u':
					memcpy(buffer, str + 2, 4);
					buffer[4] = '\0';
					c = strtol(buffer, 0, 16);
180
181
182
183
184
					len = org_cherry_unicode_to_utf8(buffer, c);
					memcpy(s, buffer, len); 
					str += 6;
					s += len;
					continue;
Chris Müller's avatar
Chris Müller committed
185
186
187
188
				case 'U':
					memcpy(buffer, str + 2, 6);
					buffer[6] = '\0';
					c = strtol(buffer, 0, 16);
189
190
191
192
193
					len = org_cherry_unicode_to_utf8(buffer, c);
					memcpy(s, buffer, len);
					str += 8;
					s += len;
					continue;
Chris Müller's avatar
Chris Müller committed
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
			}
			str++;
		} else {
			*s = *str;
		}

		s++;
		str++;
	}

	*s = '\0';

    return org_cherry_string(p);
}

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

// ----------------------------------------------------------------------------
// cherry object constructors
// ----------------------------------------------------------------------------

struct org_cherry_value*    
org_cherry_value_alloc(void)
{
	struct org_cherry_value* value = GC_MALLOC(sizeof(struct org_cherry_value));

	if(value == 0) {
		fprintf(stderr, "GC: out of memory");
		exit(1);
	}

	return value;
}


struct org_cherry_value* 	
org_cherry_fixnum(cy_fixnum_t value)
{
	struct org_cherry_value* cy_value = org_cherry_value_alloc();
	cy_value->meta.type = CY_FIXNUM;
	cy_value->fixnum_value = value;
	return cy_value;
}


struct org_cherry_value* 	
org_cherry_float(cy_float_t value)
{
	struct org_cherry_value* cy_value = org_cherry_value_alloc();
	cy_value->meta.type = CY_FLOAT;
	cy_value->float_value = value;
	return cy_value;
}


struct org_cherry_value* 	
Chris Müller's avatar
Chris Müller committed
249
org_cherry_string(const cy_byte_t* value)
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
{
	struct org_cherry_value* cy_value = org_cherry_value_alloc();
	cy_value->meta.type = CY_STRING;
	cy_value->string_value = value;
	return cy_value;
}


struct org_cherry_value* 	
org_cherry_char(cy_unicode_t value)
{
	struct org_cherry_value* cy_value = org_cherry_value_alloc();
	cy_value->meta.type = CY_CHAR;
	cy_value->char_value = value;
	return cy_value;
}

struct org_cherry_value*
Chris Müller's avatar
Chris Müller committed
268
org_cherry_symbol(const cy_byte_t* value)
269
{
270
	return org_cherry_symbollist_get(org_cherry_global_symbollist, value);
271
272
}

273
274

struct org_cherry_value*
275
276
277
278
279
280
281
282
283
284
org_cherry_primitive(cy_primitive_t fun)
{
    struct org_cherry_value* proc = GC_MALLOC(sizeof(struct org_cherry_value));
	proc->meta.type = CY_PRIMITIVE;
	proc->fun_value = fun;
	return proc;
}

struct org_cherry_value*
org_cherry_procedure(struct org_cherry_environment* env, struct org_cherry_value* param, struct org_cherry_value* body)
285
286
287
288
289
290
291
292
293
{
    struct org_cherry_procedure* proc = GC_MALLOC(sizeof(struct org_cherry_procedure));
    proc->meta.type = CY_PROCEDURE;
    proc->body = body;
    proc->param = param;
    proc->env = env;
    return TO_VALUE(proc);
}

294
295
296
297
// ----------------------------------------------------------------------------
// cherry list operations
// ----------------------------------------------------------------------------

298
struct org_cherry_value*     
299
300
301
302
303
304
org_cherry_list_cons(struct org_cherry_value* head, struct org_cherry_value* tail)
{
	struct org_cherry_pair* pair = GC_MALLOC(sizeof(struct org_cherry_pair));
	pair->meta.type = CY_PAIR;
	pair->head = head;
	pair->tail = tail;
305
	return TO_VALUE(pair);
306
307
308
309
310
311
312
313
314
315
316
317
318
319
}

struct org_cherry_value*    
org_cherry_list_head(struct org_cherry_pair* pair)
{
	return pair->head;
}

struct org_cherry_value*    
org_cherry_list_tail(struct org_cherry_pair* pair)
{
	return pair->tail;
}

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
struct org_cherry_value*
org_cherry_list(struct org_cherry_value* head, ...)
{
    va_list p;
    va_start(p, head);

    struct org_cherry_value* pair = org_cherry_list_cons(head, org_cherry_emptylist);
	struct org_cherry_value* arg = va_arg(p, struct org_cherry_value*);

    while(0 != arg) {
        struct org_cherry_pair* x = GC_MALLOC(sizeof(struct org_cherry_pair));
        x->meta.type = CY_PAIR;
        x->head = arg;
        x->tail = pair;

        pair = (struct org_cherry_value*) x;
		arg = va_arg(p, struct org_cherry_value*);
    }

    return org_cherry_list_reverse(pair);
}


struct org_cherry_value*
org_cherry_list_reverse(struct org_cherry_value* xs)
{
	struct org_cherry_value* pair = org_cherry_emptylist;

	while(!IS_NULL(xs)) {
		struct org_cherry_pair* x = GC_MALLOC(sizeof(struct org_cherry_pair));
		x->meta.type = CY_PAIR;
		x->head = HEAD(xs);
		x->tail = pair;

		pair = (struct org_cherry_value*) x;

		xs = TAIL(xs);
	}

	return pair;
}

362
363
364
365

void 
org_cherry_initialize(struct org_cherry_pair* arguments)
{
366
	GC_INIT();
367
	org_cherry_global_symbollist = org_cherry_symbollist();
368

369
370
371
	org_cherry_true = org_cherry_value_alloc();
	org_cherry_true->meta.type = CY_BOOLEAN;
	org_cherry_true->boolean_value = 1;
372

373
374
375
	org_cherry_false = org_cherry_value_alloc();
	org_cherry_false->meta.type = CY_BOOLEAN;
	org_cherry_false->boolean_value = 0;
376

377
378
	org_cherry_emptylist = org_cherry_value_alloc();
	org_cherry_emptylist->meta.type = CY_EMPTYLIST;
379

380
381
382
383
384
385
386
387
388
	org_cherry_symbol_quote 	= org_cherry_symbol("quote");
	org_cherry_symbol_define	= org_cherry_symbol("define");
	org_cherry_symbol_lambda 	= org_cherry_symbol("lambda");
	org_cherry_symbol_if     	= org_cherry_symbol("if");
	org_cherry_symbol_begin   	= org_cherry_symbol("begin");
	org_cherry_symbol_catch   	= org_cherry_symbol("catch");
	org_cherry_symbol_finally 	= org_cherry_symbol("finally");


389
}
390

391
392
393
394
395
396
397

struct org_cherry_environment*
org_cherry_environment(void)
{
	struct org_cherry_environment* env = GC_MALLOC(sizeof(struct org_cherry_environment));

	env->mapping = org_cherry_symbollist();
398
	env->exception_stack = 0; 
399

400
401
#define proc_to_env(ENV, STR, FUN) \
	org_cherry_env_add(ENV, org_cherry_symbol(STR), org_cherry_primitive(FUN))
402

403
404
	proc_to_env(env, "println", org_cherry_io_println);
	proc_to_env(env, "raise", org_cherry_core_raise);
405
406
407
408
409
	proc_to_env(env, "type", org_cherry_core_type);
	proc_to_env(env, "+", org_cherry_core_add);
	proc_to_env(env, "-", org_cherry_core_sub);
	proc_to_env(env, "*", org_cherry_core_mul);
	proc_to_env(env, "/", org_cherry_core_div);
410

411
412
413
414
	return env;
}


415
416
417
418
419
420
421
// ----------------------------------------------------------------------------
// Print
// ----------------------------------------------------------------------------

static void
print_pair(FILE* out, struct org_cherry_pair* value)
{
422
423
	struct org_cherry_value* head = HEAD(value);
	struct org_cherry_value* tail = TAIL(value);
424
425
426
427
428

	org_cherry_print(out, head);
	
	if(IS_PAIR(tail)) {
		fprintf(out, " ");
429
		print_pair(out, (struct org_cherry_pair*) tail);
430
431
432
433
434
435
436
437
438
	} else if(IS_NULL(tail)) {
		return;
	} else {
		fprintf(out, " . ");
		org_cherry_print(out, tail);
	}
}

void
439
org_cherry_print(FILE* out, struct org_cherry_value* value)
440
{
Chris Müller's avatar
Chris Müller committed
441
	const cy_byte_t* p;
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461

	switch(value->meta.type) {
		case CY_EMPTYLIST:
			fprintf(out, "()");
			break;
		case CY_BOOLEAN:
			fprintf(out, (value->boolean_value) ? "true" : "false");
			break;
		case CY_SYMBOL:
			fprintf(out, "%s", value->symbol_value);
			break;
		case CY_FIXNUM:
			fprintf(out, "%ld", value->fixnum_value);
			break;
		case CY_FLOAT:
			fprintf(out, "%lf", value->float_value);
			break;
		case CY_CHAR:
			fprintf(out, "\\");
			switch(value->char_value) {
Chris Müller's avatar
Chris Müller committed
462
463
464
465
466
467
468
469
470
471
472
473
				case '\0':
					fprintf(out, "null");
					break;
				case '\a':
					fprintf(out, "bell");
					break;
				case '\b':
					fprintf(out, "backspace");
					break;
				case '\f':
					fprintf(out, "formfeed");
					break;
474
475
				case '\n': 
					fprintf(out, "newline");
476
					break;
477
478
479
				case ' ':
					fprintf(out, "space");
					break;
Chris Müller's avatar
Chris Müller committed
480
481
482
483
484
485
486
487
488
				case '\r':
					fprintf(out, "return");
					break;
				case '\t':
					fprintf(out, "tab");
					break;
				case '\v':
					fprintf(out, "vtab");
					break;
489
490
491
492
493
494
495
496
497
498
				default:
					fprintf(out, "%c", value->char_value);
			}
			break;

		case CY_STRING:
			p = value->string_value;
			fprintf(out, "\"");
			while(*p != '\0') {
				switch(*p) {
Chris Müller's avatar
Chris Müller committed
499
500
501
502
503
504
505
506
507
					case '\a':
						fprintf(out, "\\a");
						break;
					case '\b':
						fprintf(out, "\\b");
						break;
					case '\f':
						fprintf(out, "\\f");
						break;
508
509
510
					case '\n': 
						fprintf(out, "\\n");
						break;
Chris Müller's avatar
Chris Müller committed
511
512
513
514
515
					case '\r':
						fprintf(out, "\\r");
						break;
					case '\t':
						fprintf(out, "\\t");
516
						break;
Chris Müller's avatar
Chris Müller committed
517
518
519
					case '\v':
						fprintf(out, "\v");
					case '"':
520
521
						fprintf(out, "\\\"");
						break;
Chris Müller's avatar
Chris Müller committed
522
523
524
					case '\\':
						fprintf(out, "\\");
						break;
525
526
527
528
529
530
531
					default:
						fprintf(out, "%c", *p);
				}
				p++;
			}
			fprintf(out, "\"");
			break;
532
533
534
		case CY_PRIMITIVE:
			fprintf(out, "#primitive-procedure");
			break;
535
536
537
        case CY_PROCEDURE:
            fprintf(out, "#lambda-procedure");
            break;
538
539
		case CY_PAIR:
			fprintf(out, "(");
540
			print_pair(out, (struct org_cherry_pair*) value);
541
542
543
544
545
546
547
			fprintf(out, ")");
			break;
		default:
			fprintf(stderr, "cannot write unknown type\n");
			break;
	}
}