2 * Copyright © 2016 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
20 * Plan: walk actuals, construct formals
22 * stack > save > actuals > actual_1
24 * formals . > actual_2
27 static struct ao_lisp_cons *stack;
28 static struct ao_lisp_cons *actuals;
29 static struct ao_lisp_cons *formals;
30 static struct ao_lisp_cons *formals_tail;
31 static uint8_t been_here;
34 #define DBG(...) printf(__VA_ARGS__)
35 #define DBG_CONS(a) ao_lisp_cons_print(a)
36 #define DBG_POLY(a) ao_lisp_poly_print(a)
44 ao_lisp_eval(ao_poly v)
46 struct ao_lisp_cons *formal;
51 ao_lisp_root_add(&ao_lisp_cons_type, &stack);
52 ao_lisp_root_add(&ao_lisp_cons_type, &actuals);
53 ao_lisp_root_add(&ao_lisp_cons_type, &formals);
54 ao_lisp_root_add(&ao_lisp_cons_type, &formals_tail);
63 /* Build stack frames for each list */
64 while (ao_lisp_poly_type(v) == AO_LISP_CONS) {
68 /* Push existing frame on the stack */
70 struct ao_lisp_cons *frame;
72 frame = ao_lisp_cons_cons(ao_lisp_cons_poly(actuals), formals);
73 stack = ao_lisp_cons_cons(ao_lisp_cons_poly(frame), stack);
75 actuals = ao_lisp_poly_cons(v);
80 DBG("start: stack"); DBG_CONS(stack); DBG("\n");
81 DBG("start: actuals"); DBG_CONS(actuals); DBG("\n");
82 DBG("start: formals"); DBG_CONS(formals); DBG("\n");
85 /* Evaluate primitive types */
87 DBG ("actual: "); DBG_POLY(v); DBG("\n");
89 switch (ao_lisp_poly_type(v)) {
94 v = ao_lisp_poly_atom(v)->val;
102 DBG("add formal: "); DBG_POLY(v); DBG("\n");
104 if (formals == NULL) {
105 if (ao_lisp_poly_type(v) == AO_LISP_BUILTIN) {
106 struct ao_lisp_builtin *b = ao_lisp_poly_builtin(v);
108 case AO_LISP_NLAMBDA:
109 v = ao_lisp_func(b)(ao_lisp_poly_cons(actuals->cdr));
113 v = ao_lisp_func(b)(ao_lisp_poly_cons(actuals->cdr));
114 DBG("macro "); DBG_POLY(ao_lisp_cons_poly(actuals));
115 DBG(" -> "); DBG_POLY(v);
117 if (ao_lisp_poly_type(v) != AO_LISP_CONS) {
118 ao_lisp_exception |= AO_LISP_INVALID;
122 /* Reset frame to the new list */
123 actuals = ao_lisp_poly_cons(v);
130 formal = ao_lisp_cons_cons(v, NULL);
132 formals_tail->cdr = ao_lisp_cons_poly(formal);
135 formals_tail = formal;
136 actuals = ao_lisp_poly_cons(actuals->cdr);
145 /* Process all of the arguments */
153 /* Evaluate the resulting list */
154 if (ao_lisp_poly_type(v) == AO_LISP_BUILTIN) {
155 struct ao_lisp_builtin *b = ao_lisp_poly_builtin(v);
157 v = ao_lisp_func(b) (ao_lisp_poly_cons(formals->cdr));
165 ao_lisp_exception |= AO_LISP_INVALID;
167 if (ao_lisp_exception)
171 struct ao_lisp_cons *frame;
173 /* Pop the previous frame off the stack */
174 frame = ao_lisp_poly_cons(stack->car);
175 actuals = ao_lisp_poly_cons(frame->car);
176 formals = ao_lisp_poly_cons(frame->cdr);
179 /* Recompute the tail of the formals list */
181 for (formal = formals; formal->cdr != AO_LISP_NIL; formal = ao_lisp_poly_cons(formal->cdr));
182 formals_tail = formal;
185 stack = ao_lisp_poly_cons(stack->cdr);
186 DBG("stack pop: stack"); DBG_CONS(stack); DBG("\n");
187 DBG("stack pop: actuals"); DBG_CONS(actuals); DBG("\n");
188 DBG("stack pop: formals"); DBG_CONS(formals); DBG("\n");