altos/lisp: Make lambda, cond and while all have implicit progns
[fw/altos] / src / lisp / ao_lisp_lambda.c
1 /*
2  * Copyright © 2016 Keith Packard <keithp@keithp.com>
3  *
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; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #define DBG_EVAL 0
19 #include "ao_lisp.h"
20
21 int
22 lambda_size(void *addr)
23 {
24         (void) addr;
25         return sizeof (struct ao_lisp_lambda);
26 }
27
28 void
29 lambda_mark(void *addr)
30 {
31         struct ao_lisp_lambda   *lambda = addr;
32
33         ao_lisp_poly_mark(lambda->code, 0);
34         ao_lisp_poly_mark(lambda->frame, 0);
35 }
36
37 void
38 lambda_move(void *addr)
39 {
40         struct ao_lisp_lambda   *lambda = addr;
41
42         ao_lisp_poly_move(&lambda->code, 0);
43         ao_lisp_poly_move(&lambda->frame, 0);
44 }
45
46 const struct ao_lisp_type ao_lisp_lambda_type = {
47         .size = lambda_size,
48         .mark = lambda_mark,
49         .move = lambda_move,
50         .name = "lambda",
51 };
52
53 void
54 ao_lisp_lambda_print(ao_poly poly)
55 {
56         struct ao_lisp_lambda   *lambda = ao_lisp_poly_lambda(poly);
57         struct ao_lisp_cons     *cons = ao_lisp_poly_cons(lambda->code);
58
59         printf("(");
60         printf("%s", ao_lisp_args_name(lambda->args));
61         while (cons) {
62                 printf(" ");
63                 ao_lisp_poly_print(cons->car);
64                 cons = ao_lisp_poly_cons(cons->cdr);
65         }
66         printf(")");
67 }
68
69 ao_poly
70 ao_lisp_lambda_alloc(struct ao_lisp_cons *code, int args)
71 {
72         ao_lisp_cons_stash(0, code);
73         struct ao_lisp_lambda   *lambda = ao_lisp_alloc(sizeof (struct ao_lisp_lambda));
74         code = ao_lisp_cons_fetch(0);
75         struct ao_lisp_cons     *arg;
76         int                     f;
77
78         if (!lambda)
79                 return AO_LISP_NIL;
80
81         if (!code->cdr)
82                 return ao_lisp_error(AO_LISP_INVALID, "missing parameters to lambda");
83
84         if (!ao_lisp_check_argt(_ao_lisp_atom_lambda, code, 0, AO_LISP_CONS, 1))
85                 return AO_LISP_NIL;
86         f = 0;
87         arg = ao_lisp_poly_cons(ao_lisp_arg(code, 0));
88         while (arg) {
89                 if (ao_lisp_poly_type(arg->car) != AO_LISP_ATOM)
90                         return ao_lisp_error(AO_LISP_INVALID, "formal %d is not an atom", f);
91                 arg = ao_lisp_poly_cons(arg->cdr);
92                 f++;
93         }
94
95         lambda->type = AO_LISP_LAMBDA;
96         lambda->args = args;
97         lambda->code = ao_lisp_cons_poly(code);
98         lambda->frame = ao_lisp_frame_mark(ao_lisp_frame_current);
99         DBGI("build frame: "); DBG_POLY(lambda->frame); DBG("\n");
100         DBG_STACK();
101         return ao_lisp_lambda_poly(lambda);
102 }
103
104 ao_poly
105 ao_lisp_lambda(struct ao_lisp_cons *cons)
106 {
107         return ao_lisp_lambda_alloc(cons, AO_LISP_FUNC_LAMBDA);
108 }
109
110 ao_poly
111 ao_lisp_lexpr(struct ao_lisp_cons *cons)
112 {
113         return ao_lisp_lambda_alloc(cons, AO_LISP_FUNC_LEXPR);
114 }
115
116 ao_poly
117 ao_lisp_nlambda(struct ao_lisp_cons *cons)
118 {
119         return ao_lisp_lambda_alloc(cons, AO_LISP_FUNC_NLAMBDA);
120 }
121
122 ao_poly
123 ao_lisp_macro(struct ao_lisp_cons *cons)
124 {
125         return ao_lisp_lambda_alloc(cons, AO_LISP_FUNC_MACRO);
126 }
127
128 ao_poly
129 ao_lisp_lambda_eval(void)
130 {
131         struct ao_lisp_lambda   *lambda = ao_lisp_poly_lambda(ao_lisp_v);
132         struct ao_lisp_cons     *cons = ao_lisp_poly_cons(ao_lisp_stack->values);
133         struct ao_lisp_cons     *code = ao_lisp_poly_cons(lambda->code);
134         struct ao_lisp_cons     *args = ao_lisp_poly_cons(ao_lisp_arg(code, 0));
135         struct ao_lisp_frame    *next_frame;
136         int                     args_wanted;
137         int                     args_provided;
138         int                     f;
139         struct ao_lisp_cons     *vals;
140
141         DBGI("lambda "); DBG_POLY(ao_lisp_lambda_poly(lambda)); DBG("\n");
142
143         args_wanted = ao_lisp_cons_length(args);
144
145         /* Create a frame to hold the variables
146          */
147         args_provided = ao_lisp_cons_length(cons) - 1;
148         if (lambda->args == AO_LISP_FUNC_LAMBDA) {
149                 if (args_wanted != args_provided)
150                         return ao_lisp_error(AO_LISP_INVALID, "need %d args, got %d", args_wanted, args_provided);
151         } else {
152                 if (args_provided < args_wanted - 1)
153                         return ao_lisp_error(AO_LISP_INVALID, "need at least %d args, got %d", args_wanted, args_provided);
154         }
155
156         next_frame = ao_lisp_frame_new(args_wanted);
157
158         /* Re-fetch all of the values in case something moved */
159         lambda = ao_lisp_poly_lambda(ao_lisp_v);
160         cons = ao_lisp_poly_cons(ao_lisp_stack->values);
161         code = ao_lisp_poly_cons(lambda->code);
162         args = ao_lisp_poly_cons(ao_lisp_arg(code, 0));
163         vals = ao_lisp_poly_cons(cons->cdr);
164
165         next_frame->prev = lambda->frame;
166         ao_lisp_frame_current = next_frame;
167         ao_lisp_stack->frame = ao_lisp_frame_poly(ao_lisp_frame_current);
168
169         switch (lambda->args) {
170         case AO_LISP_FUNC_LAMBDA:
171                 for (f = 0; f < args_wanted; f++) {
172                         DBGI("bind "); DBG_POLY(args->car); DBG(" = "); DBG_POLY(vals->car); DBG("\n");
173                         next_frame->vals[f].atom = args->car;
174                         next_frame->vals[f].val = vals->car;
175                         args = ao_lisp_poly_cons(args->cdr);
176                         vals = ao_lisp_poly_cons(vals->cdr);
177                 }
178                 ao_lisp_cons_free(cons);
179                 cons = NULL;
180                 break;
181         case AO_LISP_FUNC_LEXPR:
182         case AO_LISP_FUNC_NLAMBDA:
183         case AO_LISP_FUNC_MACRO:
184                 for (f = 0; f < args_wanted - 1; f++) {
185                         DBGI("bind "); DBG_POLY(args->car); DBG(" = "); DBG_POLY(vals->car); DBG("\n");
186                         next_frame->vals[f].atom = args->car;
187                         next_frame->vals[f].val = vals->car;
188                         args = ao_lisp_poly_cons(args->cdr);
189                         vals = ao_lisp_poly_cons(vals->cdr);
190                 }
191                 DBGI("bind "); DBG_POLY(args->car); DBG(" = "); DBG_POLY(ao_lisp_cons_poly(vals)); DBG("\n");
192                 next_frame->vals[f].atom = args->car;
193                 next_frame->vals[f].val = ao_lisp_cons_poly(vals);
194                 break;
195         default:
196                 break;
197         }
198         DBGI("eval frame: "); DBG_POLY(ao_lisp_frame_poly(next_frame)); DBG("\n");
199         DBG_STACK();
200         DBGI("eval code: "); DBG_POLY(code->cdr); DBG("\n");
201         return code->cdr;
202 }