runtime.c 5.01 KB
Newer Older
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
/*
 * 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 "bootstrap.h"
#include <stdarg.h>
#include <gc.h>

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

struct cherry_symbollist* 	cherry_global_symbollist;
struct cherry_value*		cherry_emptylist;
struct cherry_value*		cherry_true;
struct cherry_value*		cherry_false;
struct cherry_value*		cherry_dot;

struct cherry_value*		cherry_symbol_define;
struct cherry_value*		cherry_symbol_let;
struct cherry_value*		cherry_symbol_lambda;
struct cherry_value*		cherry_symbol_if;
struct cherry_value*		cherry_symbol_quote;
38
struct cherry_value*		cherry_symbol_loop;
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

struct cherry_value*		cherry_symbol_try;
struct cherry_value*		cherry_symbol_begin;
struct cherry_value*		cherry_symbol_catch;


void 
cherry_initialize(struct cherry_value* arguments)
{
	GC_INIT();
	cherry_global_symbollist = cherry_symbollist();

	cherry_true = cherry_value_alloc();
	cherry_true->tag = BOOLEAN;
	cherry_true->boolean_value = 1;

	cherry_false = cherry_value_alloc();
	cherry_false->tag = BOOLEAN;
	cherry_false->boolean_value = 0;

	cherry_dot = cherry_value_alloc();
	cherry_dot->tag = DOT;

	cherry_emptylist = cherry_value_alloc();
	cherry_emptylist->tag = EMPTYLIST;

	cherry_symbol_quote 	= cherry_symbol("quote");
	cherry_symbol_define	= cherry_symbol("define");
	cherry_symbol_let		= cherry_symbol("let");
	cherry_symbol_lambda 	= cherry_symbol("lambda");
	cherry_symbol_if     	= cherry_symbol("if");
70
	cherry_symbol_loop     	= cherry_symbol("loop");
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
	cherry_symbol_begin   	= cherry_symbol("begin");
	cherry_symbol_catch   	= cherry_symbol("catch");
	cherry_symbol_try 		= cherry_symbol("try");
}


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

	env->mapping = cherry_symbollist();
	env->exception_stack = 0; 

#define proc_to_env(ENV, STR, FUN) \
	cherry_env_add(ENV, cherry_symbol(STR), cherry_primitive(FUN))

	proc_to_env(env, "println", cherry_io_println);
	proc_to_env(env, "raise", cherry_core_raise);
	proc_to_env(env, "type", cherry_core_type);
	proc_to_env(env, "+", cherry_core_add);
	proc_to_env(env, "-", cherry_core_sub);
	proc_to_env(env, "*", cherry_core_mul);
	proc_to_env(env, "/", cherry_core_div);
Chris Müller's avatar
Chris Müller committed
95
96
97
98
99
100
101
102
103
	proc_to_env(env, ">", cherry_core_greater);
	proc_to_env(env, "<", cherry_core_less);
	proc_to_env(env, ">=", cherry_core_greater_equal);
	proc_to_env(env, "=<", cherry_core_less_equal);
	proc_to_env(env, "=", cherry_core_equal);
	proc_to_env(env, "not", cherry_core_not);
	proc_to_env(env, "and", cherry_core_and);
	proc_to_env(env, "or", cherry_core_or);
	
104
105
106
107
	proc_to_env(env, "cons", cherry_core_cons);
	proc_to_env(env, "list", cherry_core_list);
	proc_to_env(env, "head", cherry_core_head);
	proc_to_env(env, "tail", cherry_core_tail);
108
109
110
111
112
113
	proc_to_env(env, "null?", cherry_core_is_null);
	proc_to_env(env, "map", cherry_core_map);
	proc_to_env(env, "list->string", cherry_core_list_to_string);
	proc_to_env(env, "list->tuple", cherry_core_list_to_tuple);

	proc_to_env(env, "tuple", cherry_core_tuple);
114
	proc_to_env(env, "length", cherry_core_length);
115
	proc_to_env(env, "nth", cherry_core_nth);
116
117
118
119

    proc_to_env(env, "make-list", cherry_core_make_list);
    proc_to_env(env, "make-tuple", cherry_core_make_tuple);

120
	proc_to_env(env, "string->list", cherry_core_string_to_list);
121
	proc_to_env(env, "string->tuple", cherry_core_string_to_tuple);
122
123
124
	proc_to_env(env, "string->fixnum", cherry_core_string_to_fixnum);
	
    proc_to_env(env, "exit", cherry_system_exit);
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

	return env;
}

static struct cherry_exception*
cherry_exception(void) 
{
	struct cherry_exception* e = GC_MALLOC(sizeof(struct cherry_exception));
	e->data = cherry_emptylist;
	e->next = 0;
	return e;
}


struct cherry_environment*
cherry_env_push_exception_point(struct cherry_environment* env) 
{
	struct cherry_exception* e = cherry_exception();
	e->next = env->exception_stack;

	env->exception_stack = e;

	return env;
}


struct cherry_environment*
cherry_env_pop_exception_point(struct cherry_environment* env)
{
	struct cherry_exception* e = env->exception_stack->next;
	
	env->exception_stack->next = 0;
	env->exception_stack = e;

	return env;
}


void
cherry_env_raise(struct cherry_environment* env, struct cherry_value* e)
{
	env->exception_stack->data = e;
	longjmp(env->exception_stack->jump, 1);
}