-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluation.c
279 lines (218 loc) · 6.88 KB
/
evaluation.c
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
#include <string.h>
#include <stdarg.h>
#include "mpc.h"
#include "lval.h"
#define LASSERT(args, cond, fmt, ...) \
if (!(cond)) { \
lval* err = lval_err(LERR_ERR, fmt, ##__VA_ARGS__); \
lval_del(args); \
return err; \
}
#define LASSERT_TYPE(func, args, index, expect) \
LASSERT(args, args->value.cell[index]->type == expect, \
"Function '%s' passed incorrect type for argument %i. Got %s, Expected %s.", \
func, index, ltype_name(args->value.cell[index]->type), ltype_name(expect))
#define LASSERT_NUM(func, args, num) \
LASSERT(args, args->count == num, \
"Function '%s' passed incorrect number of arguments. Got %i, Expected %i.", \
func, args->count, num)
#define LASSERT_NOT_EMPTY(func, args, index) \
LASSERT(args, args->value.cell[index]->value.qexpr->count != 0, \
"Function '%s' passed {} for argument %i.", func, index)
/* Add builtin function */
/* Convert list to q-expr
* list -> q_expr
* (a b c) -> '(a b c)
*/
lval* builtin_list(lenv* e, lval* a) {
return lval_sexpr_quote(a);
}
/* Get first element from q-expr
* list(q-expr(list)) -> q-expr
* ('(a b c)) -> 'a
*/
lval* builtin_head(lenv* e, lval* a) {
LASSERT_NUM("head", a, 1);
LASSERT_TYPE("head", a, 0, LVAL_QEXPR);
LASSERT_NOT_EMPTY("head", a, 0);
lval* q = lval_list_take(a, 0);
lval* v = lval_qexpr_unquote(q);
lval* x = lval_list_take(v, 0);
return lval_sexpr_quote(x);
}
/* Take tail element from List Q-expression
* list(qexpr(list)) -> qexpr(list)
* ('(a b c)) -> '(b c)
*/
lval* builtin_tail(lenv* e, lval* a) {
LASSERT_NUM("tail", a, 1);
LASSERT_TYPE("tail", a, 0, LVAL_QEXPR);
LASSERT_NOT_EMPTY("tail", a, 0);
lval* q = lval_list_take(a, 0);
lval* v = lval_qexpr_unquote(q);
lval_del(lval_list_pop(v, 0));
return lval_sexpr_quote(v);
}
lval* builtin_def(lenv* e, lval* a) {
LASSERT_TYPE("def", a, 0, LVAL_QEXPR);
/* First argument is symbol list */
lval* syms = lval_qexpr_pop(a->value.cell[0]);
/* Ensure all elements of first list are symbols */
for (int i = 0; i < syms->count; i++) {
LASSERT(a, syms->value.cell[i]->type == LVAL_SYM,
"Function 'def' cannot define non-symbol."
"Got %s, Expected %s.",
ltype_name(syms->value.cell[i]->type), ltype_name(LVAL_SYM));
}
/* Check correct number of symbols and values */
LASSERT(a, syms->count == a->count-1,
"Function 'def' cannot define incorrect "
"number of values to symbols "
"Got %i, Expected %i.",
syms->count, a->count-1);
/* Assign copies of values to symbols */
for (int i = 0; i < syms->count; i++) {
lenv_put(e, syms->value.cell[i], a->value.cell[i+1]);
}
lval_del(a);
return lval_list();
}
/* Forward declaration*/
lval* lval_eval(lenv* e, lval* v);
/* Evalute list in q-expr
* qexpr(list) -> lval
* '(+ 1 2) -> 3
*/
lval* builtin_qexpr_eval(lenv* e, lval* a) {
LASSERT_TYPE("eval", a, 0, LVAL_QEXPR);
lval* x = lval_qexpr_unquote(a);
return lval_eval(e, x);
}
//todo: join what ? Add cons?
/* John two qexpr(list)
* list(qexpr qexpr ...) -> list
* ('(a b c) '(d e)) -> '(a b c d e)
*/
lval* builtin_join(lenv* e, lval* a) {
for (int i = 0; i < a->count; i++) {
LASSERT_TYPE("join", a, i, LVAL_QEXPR);
LASSERT(a, a->value.cell[i]->value.qexpr->type == LVAL_LIST,
"Function 'join' passed incorrect type."
"Got %s, Expected: %s. ",
ltype_name(a->value.cell[i]->value.qexpr->type), ltype_name(LVAL_LIST));
}
lval* q = lval_list_pop(a, 0);
lval* x = lval_qexpr_unquote(q);
while (a->count) {
q = lval_list_pop(a, 0);
x = lval_list_join(x, lval_qexpr_unquote(q));
}
lval_del(a);
return lval_sexpr_quote(x);
}
/* end of add builtin function*/
lval* builtin_op(lenv* e, lval* a, char* op) {
/* Ensure all arguments are numbers */
for (int i = 0; i < a->count; i++) {
if (a->value.cell[i]->type != LVAL_NUM) {
lval_del(a);
return lval_err(LERR_BAD_NUM, "Cannot operate on non-number");
}
}
/* Pop the first element */
lval* x = lval_list_pop(a, 0);
/* If no arguments and sub then perform unary negation */
if ((strcmp(op, "-") == 0) && a->count == 0) {
x->value.num = -x->value.num;
}
/* While there are still elements remaining */
while (a->count > 0) {
/* Pop the next element */
lval* y = lval_list_pop(a, 0);
/* Perform operation */
if (strcmp(op, "+") == 0) { x->value.num += y->value.num; }
if (strcmp(op, "-") == 0) { x->value.num -= y->value.num; }
if (strcmp(op, "*") == 0) { x->value.num *= y->value.num; }
if (strcmp(op, "/") == 0) {
if (y->value.num == 0) {
lval_del(x); lval_del(y);
x = lval_err(LERR_DIV_ZERO, "Divison by zero");
break;
}
x->value.num /= y->value.num;
}
/* Delete element now finished with */
lval_del(y);
}
/* Delete input expression and return result */
lval_del(a);
return x;
}
lval* builtin_add(lenv* e, lval* a) {
return builtin_op(e, a, "+");
}
lval* builtin_sub(lenv* e, lval* a) {
return builtin_op(e, a, "-");
}
lval* builtin_mul(lenv* e, lval* a) {
return builtin_op(e, a, "*");
}
lval* builtin_div(lenv* e, lval* a) {
return builtin_op(e, a, "/");
}
void lenv_add_builtin(lenv* e, char* name, lbuiltin func) {
lval* k = lval_sym(name);
lval* v = lval_fun(func);
lenv_put(e, k, v);
lval_del(k); lval_del(v);
}
void lenv_add_builtins(lenv* e) {
/* Variable Function */
lenv_add_builtin(e, "def", builtin_def);
/* List Functions */
lenv_add_builtin(e, "list", builtin_list);
lenv_add_builtin(e, "head", builtin_head);
lenv_add_builtin(e, "tail", builtin_tail);
lenv_add_builtin(e, "eval", builtin_qexpr_eval);
lenv_add_builtin(e, "join", builtin_join);
/* Mathematical Functions */
lenv_add_builtin(e, "+", builtin_add);
lenv_add_builtin(e, "-", builtin_sub);
lenv_add_builtin(e, "*", builtin_mul);
lenv_add_builtin(e, "/", builtin_div);
}
lval* lval_eval_list(lenv* e, lval* v) {
/* Evaluate Children */
for (int i = 0; i < v->count; i++) {
v->value.cell[i] = lval_eval(e, v->value.cell[i]);
}
/* Error Checking */
for (int i = 0; i < v->count; i++) {
if (v->value.cell[i]->type == LVAL_ERR) { return lval_list_take(v, i); }
}
/* Empty Expression */
if (v->count == 0) { return v; }
/* Single Expression */
if (v->count == 1) { return lval_list_take(v, 0); }
/* Ensure First Element is Function */
lval* f = lval_list_pop(v, 0);
if (f->type != LVAL_FUN) {
lval_del(f); lval_del(v);
return lval_err(LERR_BAD_LIST, "List does not start with symbol");
}
/* Call builtin with operator */
lval* result = f->value.fun(e, v);
lval_del(f);
return result;
}
lval* lval_eval(lenv* e, lval* v) {
if (v->type == LVAL_SYM) {
lval* x = lenv_get(e, v);
lval_del(v);
return x;
}
/* Evaluate List */
if (v->type == LVAL_LIST) { return lval_eval_list(e, v); }
/* All other lval types remain the same */
return v;
}