-
Notifications
You must be signed in to change notification settings - Fork 0
/
lis.c
352 lines (298 loc) · 10.2 KB
/
lis.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
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
#include <stdio.h>
#include <stdlib.h>
#include <readline/readline.h>
#include <readline/history.h>
#include "mpc/mpc.h"
#include "types.h"
#define LASSERT(args, cond, err, msg) \
if (!(cond)) { lval_del(args); return new_lval_err(err, msg); }
LValue *lval_get(LValue *v, int i);
LValue *lval_take(LValue *v, int idx);
LValue *lval_only(LValue *v, int idx);
LValue *lval_eval(LValue *v);
LValue *lval_add(LValue *lvals, LValue *lval);
void lval_print(LValue *v, int println);
LValue *eval_op(LValue *op, LValue *lvals);
LValue *builtin_list(LValue *lvals);
LValue *builtin_head(LValue *lvals);
LValue *builtin_tail(LValue *lvals);
LValue *builtin_join(LValue *lvals);
LValue *builtin_eval(LValue *lvals);
LValue *builtin(LValue *op, LValue *lvals) {
switch (op->value.l_sym.sym) {
case LPLUS:
case LMINUS:
case LMUL:
case LDIV:
return eval_op(op, lvals);
// Only eval_op uses op, delete it here;
lval_del(op);
case LLIST:
return builtin_list(lvals);
case LHEAD:
return builtin_head(lvals);
case LTAIL:
return builtin_tail(lvals);
case LJOIN:
return builtin_join(lvals);
case LEVAL:
return builtin_eval(lvals);
}
}
LValue *_eval_op(LValue *op, LValue *a, LValue *b) {
switch(op->value.l_sym.sym) {
case LPLUS: return lval_plus(a, b); break;
case LMINUS: return lval_minus(a, b); break;
case LMUL: return lval_mul(a, b); break;
case LDIV: return lval_div(a, b); break;
default: return new_lval_err(LERR_BAD_OP, op->value.l_sym.repr);
}
}
// Cleanup
LValue *builtin_head(LValue *lvals) {
int count = lvals->len - lvals->start;
LASSERT(lvals, count == 1, LERR_OTHER, "Function 'head' passed too many arguments");
LASSERT(lvals, lvals->value.lval[lvals->start]->type == LVAL_QEXPR, LERR_OTHER, "Function 'head' passed incorrect types")
int child_count = lvals->value.lval[lvals->start]->len -
lvals->value.lval[lvals->start]->start ;
LASSERT(lvals, child_count != 0, LERR_OTHER, "Function 'head' passed empty q-expr")
LValue *ret = lval_take(lvals, 0); // ( '(...) ) -> '(...)
return lval_only(ret, ret->start); // '( x1, x2 ... xn ) -> '(x1)
}
// Doesn't cleanup
LValue *builtin_tail(LValue *lvals) {
int count = lvals->len - lvals->start;
LASSERT(lvals, count == 1, LERR_OTHER, "Function 'tail' passed too many arguments");
LASSERT(lvals, lvals->value.lval[lvals->start]->type == LVAL_QEXPR, LERR_OTHER, "Function 'tail' passed incorrect types");
int child_count = lvals->value.lval[lvals->start]->len -
lvals->value.lval[lvals->start]->start ;
LASSERT(lvals, child_count != 0, LERR_OTHER, "Function 'tail' passed empty q-expr")
LValue *ret = lval_take(lvals, 0); // ( '(...) ) -> '(...)
lval_get(ret, 0); // increment
return ret;
}
LValue *builtin_list(LValue *v) {
v->type = LVAL_QEXPR;
return v;
}
LValue *builtin_eval(LValue *lvals) {
int count = lvals->len - lvals->start;
LASSERT(lvals, count == 1, LERR_OTHER, "Function 'eval' passed too many arguments");
LASSERT(lvals, lvals->value.lval[lvals->start]->type == LVAL_QEXPR, LERR_OTHER, "Function 'eval' passed incorrect types");
LValue *ret = lval_take(lvals, 0); // ( '(...) ) -> '(...)
ret->type = LVAL_SEXPR;
return lval_eval(ret);
}
LValue *builtin_join(LValue *lvals) {
for (int i=lvals->start; i<lvals->len; i++) {
LASSERT(lvals, lvals->value.lval[i]->type == LVAL_QEXPR,
LERR_OTHER, "Function 'join' passed incorrect type");
}
LValue *ret = lval_get(lvals, 0);
while (lvals->start < lvals->len) {
LValue *b = lval_get(lvals, 0);
while (b->start < b->len) {
lval_add(ret, lval_get(b, 0));
}
lval_free(b);
}
lval_free(lvals);
return ret;
}
LValue *eval_op(LValue *op, LValue *lvals) {
int count = lvals->len - lvals->start;
if (op->value.l_sym.sym == LMINUS && count == 1) {
return lval_neg(lvals->value.lval[lvals->start]);
}
LValue *first_arg = lval_get(lvals, 0);
LValue *ret = _eval_op(op, first_arg, lval_get(lvals, 0));
if (ret->type == LVAL_ERR) {
return ret;
}
for (int i=lvals->start; i<lvals->len; i++) {
LValue *res = _eval_op(op, ret, lval_get(lvals, 0));
lval_del(ret);
if (res->type == LVAL_ERR) {
return res;
}
ret = res;
res = NULL;
}
return ret;
}
LValue *lval_read_num(mpc_ast_t *t) {
errno = 0;
long x = strtol(t->contents, NULL, 10);
return errno == ERANGE ? new_lval_err(LERR_BAD_NUM, t->contents)
: new_lval_int(new_l_integer(x));
}
LValue *lval_add(LValue *lvals, LValue *lval) {
lvals->len++;
if (lvals->len >= lvals->size) {
lvals->size = lvals->size * 2;
lvals->value.lval = realloc(lvals->value.lval, sizeof(LValue*) * lvals->size);
}
lvals->value.lval[lvals->len-1] = lval;
return lvals;
}
LValue *lval_read(mpc_ast_t *t) {
if (strstr(t->tag, "num")) return lval_read_num(t);
if (strstr(t->tag, "sym")) return new_lval_sym(t->contents);
LValue *lval = NULL;
if (strcmp(t->tag, ">") == 0) lval = new_lval_sexpr();
if (strstr(t->tag, "sexpr")) lval = new_lval_sexpr();
if (strstr(t->tag, "qexpr")) lval = new_lval_qexpr();
for (int i = 0; i < t->children_num; i++) {
if (strcmp(t->children[i]->contents, "'") == 0) continue;
if (strcmp(t->children[i]->contents, "(") == 0) continue;
if (strcmp(t->children[i]->contents, ")") == 0) continue;
// TODO: ???
if (strcmp(t->children[i]->tag, "regex") == 0) continue;
lval = lval_add(lval, lval_read(t->children[i]));
}
return lval;
}
//LValue *eval(mpc_ast_t* t) {
// if (strstr(t->tag, "num")) {
// errno = 0;
// long x = strtol(t->contents, NULL, 10);
// return errno == ERANGE ? new_lval_err(LERR_BAD_NUM)
// : new_lval_int(new_l_integer(x));
// }
//
// char *op = t->children[1]->contents;
//
// LValue *x = eval(t->children[2]);
// for (int i = 3; strstr(t->children[i]->tag, "expr"); i++) {
// x = eval_op(op, x, eval(t->children[i]));
// }
//
// return x;
//}
//
LValue *lval_only(LValue *v, int idx){
for (int i = 0; i < idx; i++){
lval_del(v->value.lval[i]);
}
for (int i = idx+1; i < v->len; i++){
lval_del(v->value.lval[i]);
}
LValue *only = v->value.lval[idx];
v->len = 1;
v->size = 1;
v->start = 0;
free(v->value.lval);
v->value.lval = malloc(sizeof(LValue*));
v->value.lval[0] = only;
return v;
}
LValue *lval_take(LValue *v, int idx) {
idx = idx + v->start;
for (int i = 0; i < idx; i++){
lval_del(v->value.lval[i]);
}
for (int i = idx+1; i < v->len; i++){
lval_del(v->value.lval[i]);
}
LValue *ret = v->value.lval[idx];
free(v);
return ret;
}
LValue *lval_get(LValue *v, int i) {
if (i == 0) {
return v->value.lval[v->start++];
}
exit(2);
}
LValue *eval_sexpr(LValue *v);
LValue *lval_eval(LValue *v) {
if (v->type == LVAL_SEXPR) return eval_sexpr(v);
return v;
}
LValue *eval_sexpr(LValue *v) {
for (int i=v->start; i<v->len; i++) {
v->value.lval[i] = lval_eval(v->value.lval[i]);
if (v->value.lval[i]->type == LVAL_ERR) {
return lval_take(v, i);
}
}
if (v->len == 0) return v;
if (v->len == 1) return lval_take(v, 0);
LValue *f = lval_get(v, 0);
if (f->type != LVAL_SYM) {
// lval_del(f);
lval_del(v);
return new_lval_err(LERR_OTHER, "Symbol not found. S-expr must start with symbol");
}
LValue *ret = builtin(f, v);
return ret;
}
void lval_print(LValue *v, int println);
void lval_expr_print(LValue *v, char open, char close) {
putchar(open);
for (int i = v->start; i < v->len; i++) {
lval_print(v->value.lval[i], 0);
if (i != (v->len-1)) {
putchar(' ');
}
}
putchar(close);
}
void lval_print(LValue *v, int println) {
switch (v->type) {
case LVAL_INT: print_l_integer(&v->value.l_int); break;
case LVAL_ERR: print_l_err(&v->value.l_err); break;
case LVAL_SYM: print_l_sym(&v->value.l_sym); break;
case LVAL_QEXPR: putchar('\'');
case LVAL_SEXPR: lval_expr_print(v, '(', ')'); break;
}
if (println) putchar('\n');
}
int main(int argc, char** argv) {
mpc_parser_t *Number = mpc_new("num");
mpc_parser_t *Symbol = mpc_new("sym");
mpc_parser_t *Expression = mpc_new("expr");
mpc_parser_t *SExpression = mpc_new("sexpr");
mpc_parser_t *QExpression = mpc_new("qexpr");
mpc_parser_t *L = mpc_new("l");
mpca_lang(MPCA_LANG_DEFAULT,
" \
num : /-?[0-9]+/ ; \
sym : \"head\" | \"tail\" | \"join\" | \
\"list\" | \"eval\" | \
'+' | '-' | '*' | '/' ; \
sexpr : '(' <expr>+ ')' ; \
qexpr : '\'' '(' <expr>+ ')' ; \
expr : <num> | <sym> | <sexpr> | <qexpr> ; \
l : /^/ <expr>+ /$/ ; \
",
Number, Symbol, Expression, QExpression, SExpression, L);
puts("lis version 0.4.0\n");
puts("press ctrl+c to exit\n");
while(1) {
char* input = readline("lis> ");
add_history(input);
printf("< ");
if ( ! input ) {
printf("exiting\n");
exit(0);
}
mpc_result_t r;
if (mpc_parse("<stdin>", input, L, &r)) {
// mpc_ast_print(r.output);
// LValue *res = eval(r.output);
LValue *res = lval_read(r.output);
lval_print(res, 1);
res = lval_eval(res);
lval_print(res, 1);
lval_del(res);
mpc_ast_delete(r.output);
} else {
mpc_err_print(r.error);
mpc_err_delete(r.error);
}
free(input);
}
mpc_cleanup(5, Number, Symbol, Expression, SExpression, QExpression, L);
return 0;
}