c38ba1652c2877eee2ef74a241d901e8d21c68a6
[fw/altos] / src / lisp / ao_lisp_builtin.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, either version 2 of the License, or
7  * (at your option) any later version.
8  *
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.
13  */
14
15 #include "ao_lisp.h"
16
17 static int
18 builtin_size(void *addr)
19 {
20         (void) addr;
21         return sizeof (struct ao_lisp_builtin);
22 }
23
24 static void
25 builtin_mark(void *addr)
26 {
27         (void) addr;
28 }
29
30 static void
31 builtin_move(void *addr)
32 {
33         (void) addr;
34 }
35
36 const struct ao_lisp_type ao_lisp_builtin_type = {
37         .size = builtin_size,
38         .mark = builtin_mark,
39         .move = builtin_move
40 };
41
42 #ifdef AO_LISP_MAKE_CONST
43 char *ao_lisp_builtin_name(enum ao_lisp_builtin_id b) {
44         return "???";
45 }
46 char *ao_lisp_args_name(uint8_t args) {
47         return "???";
48 }
49 #else
50 static const ao_poly builtin_names[] = {
51         [builtin_lambda] = _ao_lisp_atom_lambda,
52         [builtin_lexpr] = _ao_lisp_atom_lexpr,
53         [builtin_nlambda] = _ao_lisp_atom_nlambda,
54         [builtin_macro] = _ao_lisp_atom_macro,
55         [builtin_car] = _ao_lisp_atom_car,
56         [builtin_cdr] = _ao_lisp_atom_cdr,
57         [builtin_cons] = _ao_lisp_atom_cons,
58         [builtin_last] = _ao_lisp_atom_last,
59         [builtin_quote] = _ao_lisp_atom_quote,
60         [builtin_set] = _ao_lisp_atom_set,
61         [builtin_setq] = _ao_lisp_atom_setq,
62         [builtin_cond] = _ao_lisp_atom_cond,
63         [builtin_print] = _ao_lisp_atom_print,
64         [builtin_patom] = _ao_lisp_atom_patom,
65         [builtin_plus] = _ao_lisp_atom_2b,
66         [builtin_minus] = _ao_lisp_atom_2d,
67         [builtin_times] = _ao_lisp_atom_2a,
68         [builtin_divide] = _ao_lisp_atom_2f,
69         [builtin_mod] = _ao_lisp_atom_25,
70         [builtin_equal] = _ao_lisp_atom_3d,
71         [builtin_less] = _ao_lisp_atom_3c,
72         [builtin_greater] = _ao_lisp_atom_3e,
73         [builtin_less_equal] = _ao_lisp_atom_3c3d,
74         [builtin_greater_equal] = _ao_lisp_atom_3e3d,
75 };
76
77 static char *
78 ao_lisp_builtin_name(enum ao_lisp_builtin_id b) {
79         if (0 <= b && b < _builtin_last)
80                 return ao_lisp_poly_atom(builtin_names[b])->name;
81         return "???";
82 }
83
84 static const ao_poly ao_lisp_args_atoms[] = {
85         [AO_LISP_FUNC_LAMBDA] = _ao_lisp_atom_lambda,
86         [AO_LISP_FUNC_LEXPR] = _ao_lisp_atom_lexpr,
87         [AO_LISP_FUNC_NLAMBDA] = _ao_lisp_atom_nlambda,
88         [AO_LISP_FUNC_MACRO] = _ao_lisp_atom_macro,
89 };
90
91 char *
92 ao_lisp_args_name(uint8_t args)
93 {
94         if (args < sizeof ao_lisp_args_atoms / sizeof ao_lisp_args_atoms[0])
95                 return ao_lisp_poly_atom(ao_lisp_args_atoms[args])->name;
96         return "(unknown)";
97 }
98 #endif
99
100 void
101 ao_lisp_builtin_print(ao_poly b)
102 {
103         struct ao_lisp_builtin *builtin = ao_lisp_poly_builtin(b);
104         printf("[builtin %s %s]",
105                ao_lisp_args_name(builtin->args),
106                ao_lisp_builtin_name(builtin->func));
107 }
108
109 ao_poly
110 ao_lisp_check_argc(ao_poly name, struct ao_lisp_cons *cons, int min, int max)
111 {
112         int     argc = 0;
113
114         while (cons && argc <= max) {
115                 argc++;
116                 cons = ao_lisp_poly_cons(cons->cdr);
117         }
118         if (argc < min || argc > max)
119                 return ao_lisp_error(AO_LISP_INVALID, "%s: invalid arg count", ao_lisp_poly_atom(name)->name);
120         return _ao_lisp_atom_t;
121 }
122
123 ao_poly
124 ao_lisp_arg(struct ao_lisp_cons *cons, int argc)
125 {
126         if (!cons)
127                 return AO_LISP_NIL;
128         while (argc--) {
129                 if (!cons)
130                         return AO_LISP_NIL;
131                 cons = ao_lisp_poly_cons(cons->cdr);
132         }
133         return cons->car;
134 }
135
136 ao_poly
137 ao_lisp_check_argt(ao_poly name, struct ao_lisp_cons *cons, int argc, int type, int nil_ok)
138 {
139         ao_poly car = ao_lisp_arg(cons, argc);
140
141         if ((!car && !nil_ok) || ao_lisp_poly_type(car) != type)
142                 return ao_lisp_error(AO_LISP_INVALID, "%s: invalid type for arg %d", ao_lisp_poly_atom(name)->name, argc);
143         return _ao_lisp_atom_t;
144 }
145
146 ao_poly
147 ao_lisp_car(struct ao_lisp_cons *cons)
148 {
149         if (!ao_lisp_check_argc(_ao_lisp_atom_car, cons, 1, 1))
150                 return AO_LISP_NIL;
151         if (!ao_lisp_check_argt(_ao_lisp_atom_car, cons, 0, AO_LISP_CONS, 0))
152                 return AO_LISP_NIL;
153         return ao_lisp_poly_cons(cons->car)->car;
154 }
155
156 ao_poly
157 ao_lisp_cdr(struct ao_lisp_cons *cons)
158 {
159         if (!ao_lisp_check_argc(_ao_lisp_atom_cdr, cons, 1, 1))
160                 return AO_LISP_NIL;
161         if (!ao_lisp_check_argt(_ao_lisp_atom_cdr, cons, 0, AO_LISP_CONS, 0))
162                 return AO_LISP_NIL;
163         return ao_lisp_poly_cons(cons->car)->cdr;
164 }
165
166 ao_poly
167 ao_lisp_cons(struct ao_lisp_cons *cons)
168 {
169         ao_poly car, cdr;
170         if(!ao_lisp_check_argc(_ao_lisp_atom_cons, cons, 2, 2))
171                 return AO_LISP_NIL;
172         if (!ao_lisp_check_argt(_ao_lisp_atom_cons, cons, 1, AO_LISP_CONS, 1))
173                 return AO_LISP_NIL;
174         car = ao_lisp_arg(cons, 0);
175         cdr = ao_lisp_arg(cons, 1);
176         return ao_lisp_cons_poly(ao_lisp_cons_cons(car, ao_lisp_poly_cons(cdr)));
177 }
178
179 ao_poly
180 ao_lisp_last(struct ao_lisp_cons *cons)
181 {
182         ao_poly l;
183         if (!ao_lisp_check_argc(_ao_lisp_atom_last, cons, 1, 1))
184                 return AO_LISP_NIL;
185         if (!ao_lisp_check_argt(_ao_lisp_atom_last, cons, 0, AO_LISP_CONS, 1))
186                 return AO_LISP_NIL;
187         l = ao_lisp_arg(cons, 0);
188         while (l) {
189                 struct ao_lisp_cons *list = ao_lisp_poly_cons(l);
190                 if (!list->cdr)
191                         return list->car;
192                 l = list->cdr;
193         }
194         return AO_LISP_NIL;
195 }
196
197 ao_poly
198 ao_lisp_quote(struct ao_lisp_cons *cons)
199 {
200         if (!ao_lisp_check_argc(_ao_lisp_atom_quote, cons, 1, 1))
201                 return AO_LISP_NIL;
202         return ao_lisp_arg(cons, 0);
203 }
204
205 ao_poly
206 ao_lisp_set(struct ao_lisp_cons *cons)
207 {
208         if (!ao_lisp_check_argc(_ao_lisp_atom_set, cons, 2, 2))
209                 return AO_LISP_NIL;
210         if (!ao_lisp_check_argt(_ao_lisp_atom_set, cons, 0, AO_LISP_ATOM, 0))
211                 return AO_LISP_NIL;
212
213         return ao_lisp_atom_set(ao_lisp_arg(cons, 0), ao_lisp_arg(cons, 1));
214 }
215
216 ao_poly
217 ao_lisp_setq(struct ao_lisp_cons *cons)
218 {
219         struct ao_lisp_cons     *expand = 0;
220         if (!ao_lisp_check_argc(_ao_lisp_atom_setq, cons, 2, 2))
221                 return AO_LISP_NIL;
222         expand = ao_lisp_cons_cons(_ao_lisp_atom_set,
223                                    ao_lisp_cons_cons(ao_lisp_cons_poly(ao_lisp_cons_cons(_ao_lisp_atom_quote,
224                                                                        ao_lisp_cons_cons(cons->car, NULL))),
225                                                      ao_lisp_poly_cons(cons->cdr)));
226         return ao_lisp_cons_poly(expand);
227 }
228
229 ao_poly
230 ao_lisp_cond(struct ao_lisp_cons *cons)
231 {
232         ao_lisp_set_cond(cons);
233         return AO_LISP_NIL;
234 }
235
236 ao_poly
237 ao_lisp_print(struct ao_lisp_cons *cons)
238 {
239         ao_poly val = AO_LISP_NIL;
240         while (cons) {
241                 val = cons->car;
242                 ao_lisp_poly_print(val);
243                 cons = ao_lisp_poly_cons(cons->cdr);
244                 if (cons)
245                         printf(" ");
246         }
247         printf("\n");
248         return val;
249 }
250
251 ao_poly
252 ao_lisp_patom(struct ao_lisp_cons *cons)
253 {
254         ao_poly val = AO_LISP_NIL;
255         while (cons) {
256                 val = cons->car;
257                 ao_lisp_poly_patom(val);
258                 cons = ao_lisp_poly_cons(cons->cdr);
259         }
260         return val;
261 }
262
263 ao_poly
264 ao_lisp_math(struct ao_lisp_cons *cons, enum ao_lisp_builtin_id op)
265 {
266         ao_poly ret = AO_LISP_NIL;
267
268         while (cons) {
269                 ao_poly         car = cons->car;
270                 uint8_t         rt = ao_lisp_poly_type(ret);
271                 uint8_t         ct = ao_lisp_poly_type(car);
272
273                 cons = ao_lisp_poly_cons(cons->cdr);
274
275                 if (rt == AO_LISP_NIL)
276                         ret = car;
277
278                 else if (rt == AO_LISP_INT && ct == AO_LISP_INT) {
279                         int     r = ao_lisp_poly_int(ret);
280                         int     c = ao_lisp_poly_int(car);
281
282                         switch(op) {
283                         case builtin_plus:
284                                 r += c;
285                                 break;
286                         case builtin_minus:
287                                 r -= c;
288                                 break;
289                         case builtin_times:
290                                 r *= c;
291                                 break;
292                         case builtin_divide:
293                                 if (c == 0)
294                                         return ao_lisp_error(AO_LISP_DIVIDE_BY_ZERO, "divide by zero");
295                                 r /= c;
296                                 break;
297                         case builtin_mod:
298                                 if (c == 0)
299                                         return ao_lisp_error(AO_LISP_DIVIDE_BY_ZERO, "mod by zero");
300                                 r %= c;
301                                 break;
302                         default:
303                                 break;
304                         }
305                         ret = ao_lisp_int_poly(r);
306                 }
307
308                 else if (rt == AO_LISP_STRING && ct == AO_LISP_STRING && op == builtin_plus)
309                         ret = ao_lisp_string_poly(ao_lisp_string_cat(ao_lisp_poly_string(ret),
310                                                                      ao_lisp_poly_string(car)));
311                 else
312                         return ao_lisp_error(AO_LISP_INVALID, "invalid args");
313         }
314         return ret;
315 }
316
317 ao_poly
318 ao_lisp_plus(struct ao_lisp_cons *cons)
319 {
320         return ao_lisp_math(cons, builtin_plus);
321 }
322
323 ao_poly
324 ao_lisp_minus(struct ao_lisp_cons *cons)
325 {
326         return ao_lisp_math(cons, builtin_minus);
327 }
328
329 ao_poly
330 ao_lisp_times(struct ao_lisp_cons *cons)
331 {
332         return ao_lisp_math(cons, builtin_times);
333 }
334
335 ao_poly
336 ao_lisp_divide(struct ao_lisp_cons *cons)
337 {
338         return ao_lisp_math(cons, builtin_divide);
339 }
340
341 ao_poly
342 ao_lisp_mod(struct ao_lisp_cons *cons)
343 {
344         return ao_lisp_math(cons, builtin_mod);
345 }
346
347 ao_poly
348 ao_lisp_compare(struct ao_lisp_cons *cons, enum ao_lisp_builtin_id op)
349 {
350         ao_poly left;
351
352         if (!cons)
353                 return _ao_lisp_atom_t;
354
355         left = cons->car;
356         cons = ao_lisp_poly_cons(cons->cdr);
357         while (cons) {
358                 ao_poly right = cons->car;
359
360                 if (op == builtin_equal) {
361                         if (left != right)
362                                 return AO_LISP_NIL;
363                 } else {
364                         uint8_t lt = ao_lisp_poly_type(left);
365                         uint8_t rt = ao_lisp_poly_type(right);
366                         if (lt == AO_LISP_INT && rt == AO_LISP_INT) {
367                                 int l = ao_lisp_poly_int(left);
368                                 int r = ao_lisp_poly_int(right);
369
370                                 switch (op) {
371                                 case builtin_less:
372                                         if (!(l < r))
373                                                 return AO_LISP_NIL;
374                                         break;
375                                 case builtin_greater:
376                                         if (!(l > r))
377                                                 return AO_LISP_NIL;
378                                         break;
379                                 case builtin_less_equal:
380                                         if (!(l <= r))
381                                                 return AO_LISP_NIL;
382                                         break;
383                                 case builtin_greater_equal:
384                                         if (!(l >= r))
385                                                 return AO_LISP_NIL;
386                                         break;
387                                 default:
388                                         break;
389                                 }
390                         } else if (lt == AO_LISP_STRING && rt == AO_LISP_STRING) {
391                                 int c = strcmp(ao_lisp_poly_string(left),
392                                                ao_lisp_poly_string(right));
393                                 switch (op) {
394                                 case builtin_less:
395                                         if (!(c < 0))
396                                                 return AO_LISP_NIL;
397                                         break;
398                                 case builtin_greater:
399                                         if (!(c > 0))
400                                                 return AO_LISP_NIL;
401                                         break;
402                                 case builtin_less_equal:
403                                         if (!(c <= 0))
404                                                 return AO_LISP_NIL;
405                                         break;
406                                 case builtin_greater_equal:
407                                         if (!(c >= 0))
408                                                 return AO_LISP_NIL;
409                                         break;
410                                 default:
411                                         break;
412                                 }
413                         }
414                 }
415                 left = right;
416                 cons = ao_lisp_poly_cons(cons->cdr);
417         }
418         return _ao_lisp_atom_t;
419 }
420
421 ao_poly
422 ao_lisp_equal(struct ao_lisp_cons *cons)
423 {
424         return ao_lisp_compare(cons, builtin_equal);
425 }
426
427 ao_poly
428 ao_lisp_less(struct ao_lisp_cons *cons)
429 {
430         return ao_lisp_compare(cons, builtin_less);
431 }
432
433 ao_poly
434 ao_lisp_greater(struct ao_lisp_cons *cons)
435 {
436         return ao_lisp_compare(cons, builtin_greater);
437 }
438
439 ao_poly
440 ao_lisp_less_equal(struct ao_lisp_cons *cons)
441 {
442         return ao_lisp_compare(cons, builtin_less_equal);
443 }
444
445 ao_poly
446 ao_lisp_greater_equal(struct ao_lisp_cons *cons)
447 {
448         return ao_lisp_compare(cons, builtin_greater_equal);
449 }
450
451 ao_lisp_func_t ao_lisp_builtins[] = {
452         [builtin_lambda] = ao_lisp_lambda,
453         [builtin_lexpr] = ao_lisp_lexpr,
454         [builtin_nlambda] = ao_lisp_nlambda,
455         [builtin_macro] = ao_lisp_macro,
456         [builtin_car] = ao_lisp_car,
457         [builtin_cdr] = ao_lisp_cdr,
458         [builtin_cons] = ao_lisp_cons,
459         [builtin_last] = ao_lisp_last,
460         [builtin_quote] = ao_lisp_quote,
461         [builtin_set] = ao_lisp_set,
462         [builtin_setq] = ao_lisp_setq,
463         [builtin_cond] = ao_lisp_cond,
464         [builtin_print] = ao_lisp_print,
465         [builtin_patom] = ao_lisp_patom,
466         [builtin_plus] = ao_lisp_plus,
467         [builtin_minus] = ao_lisp_minus,
468         [builtin_times] = ao_lisp_times,
469         [builtin_divide] = ao_lisp_divide,
470         [builtin_mod] = ao_lisp_mod,
471         [builtin_equal] = ao_lisp_equal,
472         [builtin_less] = ao_lisp_less,
473         [builtin_greater] = ao_lisp_greater,
474         [builtin_less_equal] = ao_lisp_less_equal,
475         [builtin_greater_equal] = ao_lisp_greater_equal
476 };
477