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 ao_lisp_eval(ao_lisp_poly v)
36 struct ao_lisp_cons *formal;
41 ao_lisp_root_add(&ao_lisp_cons_type, &stack);
42 ao_lisp_root_add(&ao_lisp_cons_type, &actuals);
43 ao_lisp_root_add(&ao_lisp_cons_type, &formals);
44 ao_lisp_root_add(&ao_lisp_cons_type, &formals_tail);
52 /* Build stack frames for each list */
53 while (ao_lisp_poly_type(v) == AO_LISP_CONS) {
57 /* Push existing frame on the stack */
59 struct ao_lisp_cons *frame;
61 frame = ao_lisp_cons(ao_lisp_cons_poly(actuals), formals);
62 stack = ao_lisp_cons(ao_lisp_cons_poly(frame), stack);
64 actuals = ao_lisp_poly_cons(v);
69 printf("start: stack"); ao_lisp_cons_print(stack); printf("\n");
70 printf("start: actuals"); ao_lisp_cons_print(actuals); printf("\n");
71 printf("start: formals"); ao_lisp_cons_print(formals); printf("\n");
74 /* Evaluate primitive types */
76 switch (ao_lisp_poly_type(v)) {
81 v = ao_lisp_poly_atom(v)->val;
86 printf("add formal: "); ao_lisp_poly_print(v); printf("\n");
88 formal = ao_lisp_cons(v, NULL);
90 formals_tail->cdr = formal;
93 formals_tail = formal;
94 actuals = actuals->cdr;
97 ao_lisp_cons_print(formals);
100 ao_lisp_cons_print(actuals);
103 /* Process all of the arguments */
106 printf ("actual: "); ao_lisp_poly_print(v); printf("\n");
112 /* Evaluate the resulting list */
113 if (ao_lisp_poly_type(v) == AO_LISP_BUILTIN) {
114 struct ao_lisp_builtin *b = ao_lisp_poly_builtin(v);
116 v = b->func(formals->cdr);
119 ao_lisp_cons_print(formals);
121 ao_lisp_poly_print(v);
124 printf ("invalid eval\n");
128 struct ao_lisp_cons *frame;
130 /* Pop the previous frame off the stack */
131 frame = ao_lisp_poly_cons(stack->car);
132 actuals = ao_lisp_poly_cons(frame->car);
133 formals = frame->cdr;
135 /* Recompute the tail of the formals list */
136 for (formal = formals; formal->cdr != NULL; formal = formal->cdr);
137 formals_tail = formal;
140 printf("stack pop: stack"); ao_lisp_cons_print(stack); printf("\n");
141 printf("stack pop: actuals"); ao_lisp_cons_print(actuals); printf("\n");
142 printf("stack pop: formals"); ao_lisp_cons_print(formals); printf("\n");
144 printf("done func\n");