core.c 5.51 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
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
/*
 * 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/primitives.h"

struct org_cherry_value*
org_cherry_core_type(struct org_cherry_environment* env, struct org_cherry_value* args)
{
	if(IS_NULL(args))
		org_cherry_env_raise(env, org_cherry_string("type functions expects an argument"));
	
	switch(HEAD(args)->meta.type) {
		case CY_FIXNUM:
			return org_cherry_symbol("#fixnum");
		case CY_FLOAT:
			return org_cherry_symbol("#float");
		case CY_BOOLEAN:
			return org_cherry_symbol("#boolean");
		case CY_CHAR:
			return org_cherry_symbol("#char");
		case CY_SYMBOL:
			return org_cherry_symbol("#symbol");
		case CY_STRING:
			return org_cherry_symbol("#string");
		case CY_PROCEDURE:
			return org_cherry_symbol("#procedure");
		case CY_PRIMITIVE:
			return org_cherry_symbol("#primitive");
		case CY_PAIR:
			return org_cherry_symbol("#pair");
	}

	return org_cherry_false;
}

struct org_cherry_value*    
org_cherry_core_add(struct org_cherry_environment* env, struct org_cherry_value* args)
{
	struct org_cherry_value* v = org_cherry_fixnum(0);

	while(!IS_NULL(args)) {
		struct org_cherry_value* x = HEAD(args);

		switch(x->meta.type) {
			case CY_FIXNUM:
				if(IS_FIXNUM(v))
					v->fixnum_value += x->fixnum_value;
				else
					v->float_value += x->fixnum_value;
				break;

			case CY_FLOAT:
				if(IS_FIXNUM(v)) {
					v->float_value = v->fixnum_value + x->float_value;
					v->meta.type = CY_FLOAT;
				} else {
					v->float_value += x->float_value;
				}
				break;

			default:
				org_cherry_env_raise(env, org_cherry_string("Addition performed with a non-numerical datatype"));
		}

		args = TAIL(args);
	}

	return v;
}

struct org_cherry_value*    
org_cherry_core_sub(struct org_cherry_environment* env, struct org_cherry_value* args)
{
	if(IS_NULL(args))
		return org_cherry_fixnum(0);
	else if(!IS_FIXNUM(HEAD(args)) && !IS_FLOAT(HEAD(args)))
		org_cherry_env_raise(env, org_cherry_string("First operand in Subtraction is not a numerical datatype"));
	
	struct org_cherry_value* v = org_cherry_value_dup(HEAD(args));
	args = TAIL(args);

	org_cherry_print(stdout, v);
	printf("\n");
	
	while(!IS_NULL(args)) {
		struct org_cherry_value* x = HEAD(args);

		switch(x->meta.type) {
			case CY_FIXNUM:
				if(IS_FIXNUM(v))
					v->fixnum_value -= x->fixnum_value;
				else
					v->float_value -= x->fixnum_value;
				break;

			case CY_FLOAT:
				if(IS_FIXNUM(v)) {
					v->float_value = v->fixnum_value - x->float_value;
					v->meta.type = CY_FLOAT;
				} else {
					v->float_value -= x->float_value;
				}
				break;

			default:
				org_cherry_env_raise(env, org_cherry_string("Subtraction performed with a non-numerical datatype"));
		}

		args = TAIL(args);
	}

	return v;
}

struct org_cherry_value*    
org_cherry_core_mul(struct org_cherry_environment* env, struct org_cherry_value* args)
{
	struct org_cherry_value* v = org_cherry_fixnum(1);

	while(!IS_NULL(args)) {
		struct org_cherry_value* x = HEAD(args);

		switch(x->meta.type) {
			case CY_FIXNUM:
				if(IS_FIXNUM(v))
					v->fixnum_value *= x->fixnum_value;
				else
					v->float_value *= x->fixnum_value;
				break;

			case CY_FLOAT:
				if(IS_FIXNUM(v)) {
					v->float_value = v->fixnum_value * x->float_value;
					v->meta.type = CY_FLOAT;
				} else {
					v->float_value *= x->float_value;
				}
				break;

			default:
				org_cherry_env_raise(env, org_cherry_string("Multiplication performed with a non-numerical datatype"));
		}

		args = TAIL(args);
	}

	return v;
}


struct org_cherry_value*    
org_cherry_core_div(struct org_cherry_environment* env, struct org_cherry_value* args)
{
	if(IS_NULL(args))
		return org_cherry_fixnum(0);
	else if(!IS_FIXNUM(HEAD(args)) && !IS_FLOAT(HEAD(args)))
		org_cherry_env_raise(env, org_cherry_string("First operand in Division is not a numerical datatype"));
	
	struct org_cherry_value* v = org_cherry_value_dup(HEAD(args));
	args = TAIL(args);
	
	while(!IS_NULL(args)) {
		struct org_cherry_value* x = HEAD(args);

		switch(x->meta.type) {
			case CY_FIXNUM:
				if(x->fixnum_value == 0)
					org_cherry_env_raise(env, org_cherry_string("Division by Zero"));

				if(IS_FIXNUM(v))
					v->fixnum_value /= x->fixnum_value;
				else
					v->float_value /= x->fixnum_value;
				break;

			case CY_FLOAT:
				if(x->float_value == 0.0)
					org_cherry_env_raise(env, org_cherry_string("Division by Zero"));

				if(IS_FIXNUM(v)) {
					v->float_value = v->fixnum_value / x->float_value;
					v->meta.type = CY_FLOAT;
				} else {
					v->float_value /= x->float_value;
				}
				break;

			default:
				org_cherry_env_raise(env, org_cherry_string("Subtraction performed with a non-numerical datatype"));
		}

		args = TAIL(args);
	}

	return v;
}

213
214
215
216
217
218
219
struct org_cherry_value*
org_cherry_core_raise(struct org_cherry_environment* env, struct org_cherry_value* args)
{
	if(IS_NULL(args))
		org_cherry_env_raise(env, org_cherry_emptylist);
	else
		org_cherry_env_raise(env, HEAD(args));
220

221
222
	return org_cherry_false;
}
223