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; version 2 of the License.
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.
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.
22 lambda_size(void *addr)
25 return sizeof (struct ao_lisp_lambda);
29 lambda_mark(void *addr)
31 struct ao_lisp_lambda *lambda = addr;
33 ao_lisp_poly_mark(lambda->code, 0);
34 ao_lisp_poly_mark(lambda->frame, 0);
38 lambda_move(void *addr)
40 struct ao_lisp_lambda *lambda = addr;
42 ao_lisp_poly_move(&lambda->code, 0);
43 ao_lisp_poly_move(&lambda->frame, 0);
46 const struct ao_lisp_type ao_lisp_lambda_type = {
54 ao_lisp_lambda_print(ao_poly poly)
56 struct ao_lisp_lambda *lambda = ao_lisp_poly_lambda(poly);
57 struct ao_lisp_cons *cons = ao_lisp_poly_cons(lambda->code);
60 printf("%s", ao_lisp_args_name(lambda->args));
63 ao_lisp_poly_print(cons->car);
64 cons = ao_lisp_poly_cons(cons->cdr);
70 ao_lisp_lambda_alloc(struct ao_lisp_cons *code, int args)
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;
81 if (!ao_lisp_check_argc(_ao_lisp_atom_lambda, code, 2, 2))
83 if (!ao_lisp_check_argt(_ao_lisp_atom_lambda, code, 0, AO_LISP_CONS, 1))
86 arg = ao_lisp_poly_cons(ao_lisp_arg(code, 0));
88 if (ao_lisp_poly_type(arg->car) != AO_LISP_ATOM)
89 return ao_lisp_error(AO_LISP_INVALID, "formal %d is not an atom", f);
90 arg = ao_lisp_poly_cons(arg->cdr);
94 lambda->type = AO_LISP_LAMBDA;
96 lambda->code = ao_lisp_cons_poly(code);
97 lambda->frame = ao_lisp_frame_mark(ao_lisp_frame_current);
98 DBGI("build frame: "); DBG_POLY(lambda->frame); DBG("\n");
100 return ao_lisp_lambda_poly(lambda);
104 ao_lisp_lambda(struct ao_lisp_cons *cons)
106 return ao_lisp_lambda_alloc(cons, AO_LISP_FUNC_LAMBDA);
110 ao_lisp_lexpr(struct ao_lisp_cons *cons)
112 return ao_lisp_lambda_alloc(cons, AO_LISP_FUNC_LEXPR);
116 ao_lisp_nlambda(struct ao_lisp_cons *cons)
118 return ao_lisp_lambda_alloc(cons, AO_LISP_FUNC_NLAMBDA);
122 ao_lisp_macro(struct ao_lisp_cons *cons)
124 return ao_lisp_lambda_alloc(cons, AO_LISP_FUNC_MACRO);
128 ao_lisp_lambda_eval(void)
130 struct ao_lisp_lambda *lambda = ao_lisp_poly_lambda(ao_lisp_v);
131 struct ao_lisp_cons *cons = ao_lisp_poly_cons(ao_lisp_stack->values);
132 struct ao_lisp_cons *code = ao_lisp_poly_cons(lambda->code);
133 struct ao_lisp_cons *args = ao_lisp_poly_cons(ao_lisp_arg(code, 0));
134 struct ao_lisp_frame *next_frame;
138 DBGI("lambda "); DBG_POLY(ao_lisp_lambda_poly(lambda)); DBG("\n");
140 args_wanted = ao_lisp_cons_length(args);
142 /* Create a frame to hold the variables
144 if (lambda->args == AO_LISP_FUNC_LAMBDA)
145 args_provided = ao_lisp_cons_length(cons) - 1;
148 if (args_wanted != args_provided)
149 return ao_lisp_error(AO_LISP_INVALID, "need %d args, not %d", args_wanted, args_provided);
151 next_frame = ao_lisp_frame_new(args_wanted);
153 /* Re-fetch all of the values in case something moved */
154 lambda = ao_lisp_poly_lambda(ao_lisp_v);
155 cons = ao_lisp_poly_cons(ao_lisp_stack->values);
156 code = ao_lisp_poly_cons(lambda->code);
157 args = ao_lisp_poly_cons(ao_lisp_arg(code, 0));
159 switch (lambda->args) {
160 case AO_LISP_FUNC_LAMBDA: {
162 struct ao_lisp_cons *vals = ao_lisp_poly_cons(cons->cdr);
164 for (f = 0; f < args_wanted; f++) {
165 DBGI("bind "); DBG_POLY(args->car); DBG(" = "); DBG_POLY(vals->car); DBG("\n");
166 next_frame->vals[f].atom = args->car;
167 next_frame->vals[f].val = vals->car;
168 args = ao_lisp_poly_cons(args->cdr);
169 vals = ao_lisp_poly_cons(vals->cdr);
171 ao_lisp_cons_free(cons);
174 case AO_LISP_FUNC_LEXPR:
175 case AO_LISP_FUNC_NLAMBDA:
176 case AO_LISP_FUNC_MACRO:
177 DBGI("bind "); DBG_POLY(args->car); DBG(" = "); DBG_POLY(cons->cdr); DBG("\n");
178 next_frame->vals[0].atom = args->car;
179 next_frame->vals[0].val = cons->cdr;
182 next_frame->prev = lambda->frame;
183 DBGI("eval frame: "); DBG_POLY(ao_lisp_frame_poly(next_frame)); DBG("\n");
184 ao_lisp_frame_current = next_frame;
185 ao_lisp_stack->frame = ao_lisp_frame_poly(ao_lisp_frame_current);
187 return ao_lisp_arg(code, 1);