runtime.c 4.56 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
/*
 * 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"
#include "cherry/primitives.h"
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <gc.h>

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

struct org_cherry_symbollist* 	org_cherry_global_symbollist;
struct org_cherry_value*		org_cherry_emptylist;
struct org_cherry_value*		org_cherry_true;
struct org_cherry_value*		org_cherry_false;
34
struct org_cherry_value*		org_cherry_dot;
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

struct org_cherry_value*		org_cherry_symbol_define;
struct org_cherry_value*		org_cherry_symbol_let;
struct org_cherry_value*		org_cherry_symbol_lambda;
struct org_cherry_value*		org_cherry_symbol_if;
struct org_cherry_value*		org_cherry_symbol_quote;

struct org_cherry_value*		org_cherry_symbol_try;
struct org_cherry_value*		org_cherry_symbol_begin;
struct org_cherry_value*		org_cherry_symbol_catch;


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

	org_cherry_true = org_cherry_value_alloc();
	org_cherry_true->meta.type = CY_BOOLEAN;
	org_cherry_true->boolean_value = 1;

	org_cherry_false = org_cherry_value_alloc();
	org_cherry_false->meta.type = CY_BOOLEAN;
	org_cherry_false->boolean_value = 0;

61
62
63
	org_cherry_dot = org_cherry_value_alloc();
	org_cherry_dot->meta.type = CY_DOT;

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
	org_cherry_emptylist = org_cherry_value_alloc();
	org_cherry_emptylist->meta.type = CY_EMPTYLIST;

	org_cherry_symbol_quote 	= org_cherry_symbol("quote");
	org_cherry_symbol_define	= org_cherry_symbol("define");
	org_cherry_symbol_let		= org_cherry_symbol("let");
	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_try 		= org_cherry_symbol("try");
}


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();
	env->exception_stack = 0; 

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

	proc_to_env(env, "println", org_cherry_io_println);
	proc_to_env(env, "raise", org_cherry_core_raise);
	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);
96
	proc_to_env(env, "cons", org_cherry_core_cons);
97
98
99
	proc_to_env(env, "list", org_cherry_core_list);
	proc_to_env(env, "head", org_cherry_core_head);
	proc_to_env(env, "tail", org_cherry_core_tail);
100
	proc_to_env(env, "length", org_cherry_core_length);
101
	proc_to_env(env, "tuple", org_cherry_core_tuple);
102
	proc_to_env(env, "string->list", org_cherry_core_string_to_list);
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
	proc_to_env(env, "exit", org_cherry_system_exit);

	return env;
}

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


struct org_cherry_environment*
org_cherry_env_push_exception_point(struct org_cherry_environment* env) 
{
	struct org_cherry_exception* e = org_cherry_exception();
	e->next = env->exception_stack;

	env->exception_stack = e;

	return env;
}


struct org_cherry_environment*
org_cherry_env_pop_exception_point(struct org_cherry_environment* env)
{
	struct org_cherry_exception* e = env->exception_stack->next;
	
	env->exception_stack->next = 0;
	env->exception_stack = e;

	return env;
}


void
org_cherry_env_raise(struct org_cherry_environment* env, struct org_cherry_value* e)
{
	env->exception_stack->data = e;
	longjmp(env->exception_stack->jump, 1);
}