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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
|
/*
* Copyright © 2016 Keith Packard <keithp@keithp.com>
*
* 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 2 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.
*/
#include "ao_lisp.h"
#if 0
static int stack_depth;
#define DBG_INDENT() do { int _s; for(_s = 0; _s < stack_depth; _s++) printf(" "); } while(0)
#define DBG_IN() (++stack_depth)
#define DBG_OUT() (--stack_depth)
#define DBG(...) printf(__VA_ARGS__)
#define DBGI(...) do { DBG_INDENT(); DBG(__VA_ARGS__); } while (0)
#define DBG_CONS(a) ao_lisp_cons_print(ao_lisp_cons_poly(a))
#define DBG_POLY(a) ao_lisp_poly_print(a)
#define OFFSET(a) ((a) ? (int) ((uint8_t *) a - ao_lisp_pool) : -1)
#else
#define DBG_INDENT()
#define DBG_IN()
#define DBG_OUT()
#define DBG(...)
#define DBGI(...)
#define DBG_CONS(a)
#define DBG_POLY(a)
#endif
enum eval_state {
eval_sexpr,
eval_val,
eval_formal,
eval_exec,
eval_exec_direct,
eval_cond,
eval_cond_test
};
struct ao_lisp_stack {
ao_poly prev;
uint8_t state;
uint8_t macro;
ao_poly actuals;
ao_poly formals;
ao_poly formals_tail;
ao_poly frame;
};
static struct ao_lisp_stack *
ao_lisp_poly_stack(ao_poly p)
{
return ao_lisp_ref(p);
}
static ao_poly
ao_lisp_stack_poly(struct ao_lisp_stack *stack)
{
return ao_lisp_poly(stack, AO_LISP_OTHER);
}
static int
stack_size(void *addr)
{
(void) addr;
return sizeof (struct ao_lisp_stack);
}
static void
stack_mark(void *addr)
{
struct ao_lisp_stack *stack = addr;
for (;;) {
ao_lisp_poly_mark(stack->actuals);
ao_lisp_poly_mark(stack->formals);
/* no need to mark formals_tail */
ao_lisp_poly_mark(stack->frame);
stack = ao_lisp_poly_stack(stack->prev);
if (ao_lisp_mark_memory(stack, sizeof (struct ao_lisp_stack)))
break;
}
}
static const struct ao_lisp_type ao_lisp_stack_type;
static void
stack_move(void *addr)
{
struct ao_lisp_stack *stack = addr;
while (stack) {
void *prev;
int ret;
(void) ao_lisp_poly_move(&stack->actuals);
(void) ao_lisp_poly_move(&stack->formals);
(void) ao_lisp_poly_move(&stack->formals_tail);
(void) ao_lisp_poly_move(&stack->frame);
prev = ao_lisp_poly_stack(stack->prev);
ret = ao_lisp_move(&ao_lisp_stack_type, &prev);
if (prev != ao_lisp_poly_stack(stack->prev))
stack->prev = ao_lisp_stack_poly(prev);
if (ret);
break;
stack = ao_lisp_poly_stack(stack->prev);
}
}
static const struct ao_lisp_type ao_lisp_stack_type = {
.size = stack_size,
.mark = stack_mark,
.move = stack_move
};
static struct ao_lisp_stack *ao_lisp_stack;
static ao_poly ao_lisp_v;
static uint8_t been_here;
ao_poly
ao_lisp_set_cond(struct ao_lisp_cons *c)
{
ao_lisp_stack->state = eval_cond;
ao_lisp_stack->actuals = ao_lisp_cons_poly(c);
return AO_LISP_NIL;
}
void
ao_lisp_stack_reset(struct ao_lisp_stack *stack)
{
stack->state = eval_sexpr;
stack->macro = 0;
stack->actuals = AO_LISP_NIL;
stack->formals = AO_LISP_NIL;
stack->formals_tail = AO_LISP_NIL;
stack->frame = ao_lisp_frame_poly(ao_lisp_frame_current);
}
struct ao_lisp_stack *
ao_lisp_stack_push(void)
{
struct ao_lisp_stack *stack = ao_lisp_alloc(sizeof (struct ao_lisp_stack));
if (!stack)
return NULL;
stack->prev = ao_lisp_stack_poly(ao_lisp_stack);
ao_lisp_stack = stack;
ao_lisp_stack_reset(stack);
DBGI("stack push\n");
DBG_IN();
return stack;
}
struct ao_lisp_stack *
ao_lisp_stack_pop(void)
{
if (!ao_lisp_stack)
return NULL;
DBG_OUT();
DBGI("stack pop\n");
ao_lisp_stack = ao_lisp_poly_stack(ao_lisp_stack->prev);
if (ao_lisp_stack)
ao_lisp_frame_current = ao_lisp_poly_frame(ao_lisp_stack->frame);
else
ao_lisp_frame_current = NULL;
return ao_lisp_stack;
}
static void
ao_lisp_stack_clear(void)
{
ao_lisp_stack = NULL;
ao_lisp_frame_current = NULL;
}
static ao_poly
func_type(ao_poly func)
{
struct ao_lisp_cons *cons;
struct ao_lisp_cons *args;
int f;
DBGI("func type "); DBG_POLY(func); DBG("\n");
if (func == AO_LISP_NIL)
return ao_lisp_error(AO_LISP_INVALID, "func is nil");
if (ao_lisp_poly_type(func) == AO_LISP_BUILTIN) {
struct ao_lisp_builtin *b = ao_lisp_poly_builtin(func);
return b->args;
} else if (ao_lisp_poly_type(func) == AO_LISP_CONS) {
cons = ao_lisp_poly_cons(func);
if (!ao_lisp_check_argc(_ao_lisp_atom_lambda, cons, 3, 3))
return AO_LISP_NIL;
if (!ao_lisp_check_argt(_ao_lisp_atom_lambda, cons, 0, AO_LISP_ATOM, 0))
return AO_LISP_NIL;
if (!ao_lisp_check_argt(_ao_lisp_atom_lambda, cons, 1, AO_LISP_CONS, 1))
return AO_LISP_NIL;
args = ao_lisp_poly_cons(ao_lisp_arg(cons, 1));
f = 0;
while (args) {
if (ao_lisp_poly_type(args->car) != AO_LISP_ATOM) {
return ao_lisp_error(ao_lisp_arg(cons, 0), "formal %d is not an atom", f);
}
args = ao_lisp_poly_cons(args->cdr);
f++;
}
return ao_lisp_arg(cons, 0);
} else {
ao_lisp_error(AO_LISP_INVALID, "not a func");
abort();
return AO_LISP_NIL;
}
}
static int
ao_lisp_cons_length(struct ao_lisp_cons *cons)
{
int len = 0;
while (cons) {
len++;
cons = ao_lisp_poly_cons(cons->cdr);
}
return len;
}
static ao_poly
ao_lisp_lambda(struct ao_lisp_cons *cons)
{
ao_poly type;
struct ao_lisp_cons *lambda;
struct ao_lisp_cons *args;
struct ao_lisp_frame *next_frame;
int args_wanted;
int args_provided;
lambda = ao_lisp_poly_cons(ao_lisp_arg(cons, 0));
DBGI("lambda "); DBG_CONS(lambda); DBG("\n");
type = ao_lisp_arg(lambda, 0);
args = ao_lisp_poly_cons(ao_lisp_arg(lambda, 1));
args_wanted = ao_lisp_cons_length(args);
/* Create a frame to hold the variables
*/
if (type == _ao_lisp_atom_lambda)
args_provided = ao_lisp_cons_length(cons) - 1;
else
args_provided = 1;
if (args_wanted != args_provided)
return ao_lisp_error(AO_LISP_INVALID, "need %d args, not %d", args_wanted, args_provided);
next_frame = ao_lisp_frame_new(args_wanted);
// DBGI("new frame %d\n", OFFSET(next_frame));
switch (type) {
case _ao_lisp_atom_lambda: {
int f;
struct ao_lisp_cons *vals = ao_lisp_poly_cons(cons->cdr);
for (f = 0; f < args_wanted; f++) {
next_frame->vals[f].atom = args->car;
next_frame->vals[f].val = vals->car;
args = ao_lisp_poly_cons(args->cdr);
vals = ao_lisp_poly_cons(vals->cdr);
}
break;
}
case _ao_lisp_atom_lexpr:
case _ao_lisp_atom_nlambda:
next_frame->vals[0].atom = args->car;
next_frame->vals[0].val = cons->cdr;
break;
case _ao_lisp_atom_macro:
next_frame->vals[0].atom = args->car;
next_frame->vals[0].val = ao_lisp_cons_poly(cons);
break;
}
next_frame->next = ao_lisp_frame_poly(ao_lisp_frame_current);
ao_lisp_frame_current = next_frame;
ao_lisp_stack->frame = ao_lisp_frame_poly(next_frame);
return ao_lisp_arg(lambda, 2);
}
ao_poly
ao_lisp_eval(ao_poly _v)
{
struct ao_lisp_stack *stack;
ao_poly formal;
ao_lisp_v = _v;
if (!been_here) {
been_here = 1;
ao_lisp_root_add(&ao_lisp_stack_type, &ao_lisp_stack);
ao_lisp_root_poly_add(&ao_lisp_v);
}
stack = ao_lisp_stack_push();
for (;;) {
if (ao_lisp_exception)
return AO_LISP_NIL;
switch (stack->state) {
case eval_sexpr:
DBGI("sexpr: "); DBG_POLY(ao_lisp_v); DBG("\n");
switch (ao_lisp_poly_type(ao_lisp_v)) {
case AO_LISP_CONS:
if (ao_lisp_v == AO_LISP_NIL) {
stack->state = eval_exec;
break;
}
stack->actuals = ao_lisp_v;
stack->state = eval_formal;
stack = ao_lisp_stack_push();
ao_lisp_v = ao_lisp_poly_cons(ao_lisp_v)->car;
break;
case AO_LISP_ATOM:
ao_lisp_v = ao_lisp_atom_get(ao_lisp_v);
/* fall through */
case AO_LISP_INT:
case AO_LISP_STRING:
stack->state = eval_val;
break;
}
break;
case eval_val:
DBGI("val: "); DBG_POLY(ao_lisp_v); DBG("\n");
stack = ao_lisp_stack_pop();
if (!stack)
return ao_lisp_v;
DBGI("..state %d\n", stack->state);
break;
case eval_formal:
/* Check what kind of function we've got */
if (!stack->formals) {
switch (func_type(ao_lisp_v)) {
case AO_LISP_LAMBDA:
case _ao_lisp_atom_lambda:
case AO_LISP_LEXPR:
case _ao_lisp_atom_lexpr:
DBGI(".. lambda or lexpr\n");
break;
case AO_LISP_MACRO:
case _ao_lisp_atom_macro:
stack->macro = 1;
case AO_LISP_NLAMBDA:
case _ao_lisp_atom_nlambda:
DBGI(".. nlambda or macro\n");
stack->formals = stack->actuals;
stack->state = eval_exec_direct;
break;
}
if (stack->state == eval_exec_direct)
break;
}
formal = ao_lisp_cons_poly(ao_lisp_cons_cons(ao_lisp_v, NULL));
if (!formal) {
ao_lisp_stack_clear();
return AO_LISP_NIL;
}
if (stack->formals_tail)
ao_lisp_poly_cons(stack->formals_tail)->cdr = formal;
else
stack->formals = formal;
stack->formals_tail = formal;
DBGI("formals now "); DBG_POLY(stack->formals); DBG("\n");
ao_lisp_v = ao_lisp_poly_cons(stack->actuals)->cdr;
stack->state = eval_sexpr;
break;
case eval_exec:
if (!stack->formals) {
ao_lisp_v = AO_LISP_NIL;
stack->state = eval_val;
break;
}
ao_lisp_v = ao_lisp_poly_cons(stack->formals)->car;
case eval_exec_direct:
DBGI("exec: macro %d ", stack->macro); DBG_POLY(ao_lisp_v); DBG(" formals "); DBG_POLY(stack->formals); DBG ("\n");
if (ao_lisp_poly_type(ao_lisp_v) == AO_LISP_BUILTIN) {
struct ao_lisp_builtin *b = ao_lisp_poly_builtin(ao_lisp_v);
struct ao_lisp_cons *f = ao_lisp_poly_cons(ao_lisp_poly_cons(stack->formals)->cdr);
DBGI(".. builtin formals "); DBG_CONS(f); DBG("\n");
if (stack->macro)
stack->state = eval_sexpr;
else
stack->state = eval_val;
stack->macro = 0;
ao_lisp_v = ao_lisp_func(b) (f);
DBGI("builtin result:"); DBG_POLY(ao_lisp_v); DBG ("\n");
if (ao_lisp_exception) {
ao_lisp_stack_clear();
return AO_LISP_NIL;
}
break;
} else {
ao_lisp_v = ao_lisp_lambda(ao_lisp_poly_cons(stack->formals));
ao_lisp_stack_reset(stack);
}
break;
case eval_cond:
DBGI("cond: "); DBG_POLY(stack->actuals); DBG("\n");
if (!stack->actuals) {
ao_lisp_v = AO_LISP_NIL;
stack->state = eval_val;
} else {
ao_lisp_v = ao_lisp_poly_cons(stack->actuals)->car;
if (!ao_lisp_v || ao_lisp_poly_type(ao_lisp_v) != AO_LISP_CONS) {
ao_lisp_error(AO_LISP_INVALID, "invalid cond clause");
goto bail;
}
ao_lisp_v = ao_lisp_poly_cons(ao_lisp_v)->car;
stack->state = eval_cond_test;
stack = ao_lisp_stack_push();
stack->state = eval_sexpr;
}
break;
case eval_cond_test:
DBGI("cond_test "); DBG_POLY(ao_lisp_v); DBG("\n");
if (ao_lisp_v) {
struct ao_lisp_cons *car = ao_lisp_poly_cons(ao_lisp_poly_cons(stack->actuals)->car);
struct ao_lisp_cons *c = ao_lisp_poly_cons(car->cdr);
if (c) {
ao_lisp_v = c->car;
stack->state = eval_sexpr;
} else {
stack->state = eval_val;
}
} else {
stack->actuals = ao_lisp_poly_cons(stack->actuals)->cdr;
stack->state = eval_cond;
}
break;
}
}
bail:
ao_lisp_stack_clear();
return AO_LISP_NIL;
}
|