altos/lisp: Add builtin 'collect'
[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         (void) b;
45         return "???";
46 }
47 char *ao_lisp_args_name(uint8_t args) {
48         (void) args;
49         return "???";
50 }
51 #else
52 static const ao_poly builtin_names[] = {
53         [builtin_eval] = _ao_lisp_atom_eval,
54         [builtin_read] = _ao_lisp_atom_read,
55         [builtin_lambda] = _ao_lisp_atom_lambda,
56         [builtin_lexpr] = _ao_lisp_atom_lexpr,
57         [builtin_nlambda] = _ao_lisp_atom_nlambda,
58         [builtin_macro] = _ao_lisp_atom_macro,
59         [builtin_car] = _ao_lisp_atom_car,
60         [builtin_cdr] = _ao_lisp_atom_cdr,
61         [builtin_cons] = _ao_lisp_atom_cons,
62         [builtin_last] = _ao_lisp_atom_last,
63         [builtin_length] = _ao_lisp_atom_length,
64         [builtin_quote] = _ao_lisp_atom_quote,
65         [builtin_set] = _ao_lisp_atom_set,
66         [builtin_setq] = _ao_lisp_atom_setq,
67         [builtin_cond] = _ao_lisp_atom_cond,
68         [builtin_progn] = _ao_lisp_atom_progn,
69         [builtin_while] = _ao_lisp_atom_while,
70         [builtin_print] = _ao_lisp_atom_print,
71         [builtin_patom] = _ao_lisp_atom_patom,
72         [builtin_plus] = _ao_lisp_atom_2b,
73         [builtin_minus] = _ao_lisp_atom_2d,
74         [builtin_times] = _ao_lisp_atom_2a,
75         [builtin_divide] = _ao_lisp_atom_2f,
76         [builtin_mod] = _ao_lisp_atom_25,
77         [builtin_equal] = _ao_lisp_atom_3d,
78         [builtin_less] = _ao_lisp_atom_3c,
79         [builtin_greater] = _ao_lisp_atom_3e,
80         [builtin_less_equal] = _ao_lisp_atom_3c3d,
81         [builtin_greater_equal] = _ao_lisp_atom_3e3d,
82         [builtin_pack] = _ao_lisp_atom_pack,
83         [builtin_unpack] = _ao_lisp_atom_unpack,
84         [builtin_flush] = _ao_lisp_atom_flush,
85         [builtin_delay] = _ao_lisp_atom_delay,
86         [builtin_led] = _ao_lisp_atom_led,
87         [builtin_save] = _ao_lisp_atom_save,
88         [builtin_restore] = _ao_lisp_atom_restore,
89         [builtin_call_cc] = _ao_lisp_atom_call2fcc,
90         [builtin_collect] = _ao_lisp_atom_collect,
91
92 };
93
94 static char *
95 ao_lisp_builtin_name(enum ao_lisp_builtin_id b) {
96         if (b < _builtin_last)
97                 return ao_lisp_poly_atom(builtin_names[b])->name;
98         return "???";
99 }
100
101 static const ao_poly ao_lisp_args_atoms[] = {
102         [AO_LISP_FUNC_LAMBDA] = _ao_lisp_atom_lambda,
103         [AO_LISP_FUNC_LEXPR] = _ao_lisp_atom_lexpr,
104         [AO_LISP_FUNC_NLAMBDA] = _ao_lisp_atom_nlambda,
105         [AO_LISP_FUNC_MACRO] = _ao_lisp_atom_macro,
106 };
107
108 char *
109 ao_lisp_args_name(uint8_t args)
110 {
111         args &= AO_LISP_FUNC_MASK;
112         if (args < sizeof ao_lisp_args_atoms / sizeof ao_lisp_args_atoms[0])
113                 return ao_lisp_poly_atom(ao_lisp_args_atoms[args])->name;
114         return "(unknown)";
115 }
116 #endif
117
118 void
119 ao_lisp_builtin_print(ao_poly b)
120 {
121         struct ao_lisp_builtin *builtin = ao_lisp_poly_builtin(b);
122         printf("%s", ao_lisp_builtin_name(builtin->func));
123 }
124
125 ao_poly
126 ao_lisp_check_argc(ao_poly name, struct ao_lisp_cons *cons, int min, int max)
127 {
128         int     argc = 0;
129
130         while (cons && argc <= max) {
131                 argc++;
132                 cons = ao_lisp_poly_cons(cons->cdr);
133         }
134         if (argc < min || argc > max)
135                 return ao_lisp_error(AO_LISP_INVALID, "%s: invalid arg count", ao_lisp_poly_atom(name)->name);
136         return _ao_lisp_atom_t;
137 }
138
139 ao_poly
140 ao_lisp_arg(struct ao_lisp_cons *cons, int argc)
141 {
142         if (!cons)
143                 return AO_LISP_NIL;
144         while (argc--) {
145                 if (!cons)
146                         return AO_LISP_NIL;
147                 cons = ao_lisp_poly_cons(cons->cdr);
148         }
149         return cons->car;
150 }
151
152 ao_poly
153 ao_lisp_check_argt(ao_poly name, struct ao_lisp_cons *cons, int argc, int type, int nil_ok)
154 {
155         ao_poly car = ao_lisp_arg(cons, argc);
156
157         if ((!car && !nil_ok) || ao_lisp_poly_type(car) != type)
158                 return ao_lisp_error(AO_LISP_INVALID, "%s: invalid type for arg %d", ao_lisp_poly_atom(name)->name, argc);
159         return _ao_lisp_atom_t;
160 }
161
162 ao_poly
163 ao_lisp_car(struct ao_lisp_cons *cons)
164 {
165         if (!ao_lisp_check_argc(_ao_lisp_atom_car, cons, 1, 1))
166                 return AO_LISP_NIL;
167         if (!ao_lisp_check_argt(_ao_lisp_atom_car, cons, 0, AO_LISP_CONS, 0))
168                 return AO_LISP_NIL;
169         return ao_lisp_poly_cons(cons->car)->car;
170 }
171
172 ao_poly
173 ao_lisp_cdr(struct ao_lisp_cons *cons)
174 {
175         if (!ao_lisp_check_argc(_ao_lisp_atom_cdr, cons, 1, 1))
176                 return AO_LISP_NIL;
177         if (!ao_lisp_check_argt(_ao_lisp_atom_cdr, cons, 0, AO_LISP_CONS, 0))
178                 return AO_LISP_NIL;
179         return ao_lisp_poly_cons(cons->car)->cdr;
180 }
181
182 ao_poly
183 ao_lisp_cons(struct ao_lisp_cons *cons)
184 {
185         ao_poly car, cdr;
186         if(!ao_lisp_check_argc(_ao_lisp_atom_cons, cons, 2, 2))
187                 return AO_LISP_NIL;
188         if (!ao_lisp_check_argt(_ao_lisp_atom_cons, cons, 1, AO_LISP_CONS, 1))
189                 return AO_LISP_NIL;
190         car = ao_lisp_arg(cons, 0);
191         cdr = ao_lisp_arg(cons, 1);
192         return ao_lisp_cons_poly(ao_lisp_cons_cons(car, ao_lisp_poly_cons(cdr)));
193 }
194
195 ao_poly
196 ao_lisp_last(struct ao_lisp_cons *cons)
197 {
198         ao_poly l;
199         if (!ao_lisp_check_argc(_ao_lisp_atom_last, cons, 1, 1))
200                 return AO_LISP_NIL;
201         if (!ao_lisp_check_argt(_ao_lisp_atom_last, cons, 0, AO_LISP_CONS, 1))
202                 return AO_LISP_NIL;
203         l = ao_lisp_arg(cons, 0);
204         while (l) {
205                 struct ao_lisp_cons *list = ao_lisp_poly_cons(l);
206                 if (!list->cdr)
207                         return list->car;
208                 l = list->cdr;
209         }
210         return AO_LISP_NIL;
211 }
212
213 ao_poly
214 ao_lisp_length(struct ao_lisp_cons *cons)
215 {
216         if (!ao_lisp_check_argc(_ao_lisp_atom_length, cons, 1, 1))
217                 return AO_LISP_NIL;
218         if (!ao_lisp_check_argt(_ao_lisp_atom_length, cons, 0, AO_LISP_CONS, 1))
219                 return AO_LISP_NIL;
220         return ao_lisp_int_poly(ao_lisp_cons_length(ao_lisp_poly_cons(ao_lisp_arg(cons, 0))));
221 }
222
223 ao_poly
224 ao_lisp_quote(struct ao_lisp_cons *cons)
225 {
226         if (!ao_lisp_check_argc(_ao_lisp_atom_quote, cons, 1, 1))
227                 return AO_LISP_NIL;
228         return ao_lisp_arg(cons, 0);
229 }
230
231 ao_poly
232 ao_lisp_set(struct ao_lisp_cons *cons)
233 {
234         if (!ao_lisp_check_argc(_ao_lisp_atom_set, cons, 2, 2))
235                 return AO_LISP_NIL;
236         if (!ao_lisp_check_argt(_ao_lisp_atom_set, cons, 0, AO_LISP_ATOM, 0))
237                 return AO_LISP_NIL;
238
239         return ao_lisp_atom_set(ao_lisp_arg(cons, 0), ao_lisp_arg(cons, 1));
240 }
241
242 ao_poly
243 ao_lisp_setq(struct ao_lisp_cons *cons)
244 {
245         struct ao_lisp_cons     *expand = 0;
246         if (!ao_lisp_check_argc(_ao_lisp_atom_setq, cons, 2, 2))
247                 return AO_LISP_NIL;
248         expand = ao_lisp_cons_cons(_ao_lisp_atom_set,
249                                    ao_lisp_cons_cons(ao_lisp_cons_poly(ao_lisp_cons_cons(_ao_lisp_atom_quote,
250                                                                        ao_lisp_cons_cons(cons->car, NULL))),
251                                                      ao_lisp_poly_cons(cons->cdr)));
252         return ao_lisp_cons_poly(expand);
253 }
254
255 ao_poly
256 ao_lisp_cond(struct ao_lisp_cons *cons)
257 {
258         ao_lisp_set_cond(cons);
259         return AO_LISP_NIL;
260 }
261
262 ao_poly
263 ao_lisp_progn(struct ao_lisp_cons *cons)
264 {
265         ao_lisp_stack->state = eval_progn;
266         ao_lisp_stack->sexprs = ao_lisp_cons_poly(cons);
267         return AO_LISP_NIL;
268 }
269
270 ao_poly
271 ao_lisp_while(struct ao_lisp_cons *cons)
272 {
273         ao_lisp_stack->state = eval_while;
274         ao_lisp_stack->sexprs = ao_lisp_cons_poly(cons);
275         return AO_LISP_NIL;
276 }
277
278 ao_poly
279 ao_lisp_print(struct ao_lisp_cons *cons)
280 {
281         ao_poly val = AO_LISP_NIL;
282         while (cons) {
283                 val = cons->car;
284                 ao_lisp_poly_print(val);
285                 cons = ao_lisp_poly_cons(cons->cdr);
286                 if (cons)
287                         printf(" ");
288         }
289         printf("\n");
290         return val;
291 }
292
293 ao_poly
294 ao_lisp_patom(struct ao_lisp_cons *cons)
295 {
296         ao_poly val = AO_LISP_NIL;
297         while (cons) {
298                 val = cons->car;
299                 ao_lisp_poly_patom(val);
300                 cons = ao_lisp_poly_cons(cons->cdr);
301         }
302         return val;
303 }
304
305 ao_poly
306 ao_lisp_math(struct ao_lisp_cons *cons, enum ao_lisp_builtin_id op)
307 {
308         ao_poly ret = AO_LISP_NIL;
309
310         while (cons) {
311                 ao_poly         car = cons->car;
312                 uint8_t         rt = ao_lisp_poly_type(ret);
313                 uint8_t         ct = ao_lisp_poly_type(car);
314
315                 cons = ao_lisp_poly_cons(cons->cdr);
316
317                 if (rt == AO_LISP_NIL)
318                         ret = car;
319
320                 else if (rt == AO_LISP_INT && ct == AO_LISP_INT) {
321                         int     r = ao_lisp_poly_int(ret);
322                         int     c = ao_lisp_poly_int(car);
323
324                         switch(op) {
325                         case builtin_plus:
326                                 r += c;
327                                 break;
328                         case builtin_minus:
329                                 r -= c;
330                                 break;
331                         case builtin_times:
332                                 r *= c;
333                                 break;
334                         case builtin_divide:
335                                 if (c == 0)
336                                         return ao_lisp_error(AO_LISP_DIVIDE_BY_ZERO, "divide by zero");
337                                 r /= c;
338                                 break;
339                         case builtin_mod:
340                                 if (c == 0)
341                                         return ao_lisp_error(AO_LISP_DIVIDE_BY_ZERO, "mod by zero");
342                                 r %= c;
343                                 break;
344                         default:
345                                 break;
346                         }
347                         ret = ao_lisp_int_poly(r);
348                 }
349
350                 else if (rt == AO_LISP_STRING && ct == AO_LISP_STRING && op == builtin_plus)
351                         ret = ao_lisp_string_poly(ao_lisp_string_cat(ao_lisp_poly_string(ret),
352                                                                      ao_lisp_poly_string(car)));
353                 else
354                         return ao_lisp_error(AO_LISP_INVALID, "invalid args");
355         }
356         return ret;
357 }
358
359 ao_poly
360 ao_lisp_plus(struct ao_lisp_cons *cons)
361 {
362         return ao_lisp_math(cons, builtin_plus);
363 }
364
365 ao_poly
366 ao_lisp_minus(struct ao_lisp_cons *cons)
367 {
368         return ao_lisp_math(cons, builtin_minus);
369 }
370
371 ao_poly
372 ao_lisp_times(struct ao_lisp_cons *cons)
373 {
374         return ao_lisp_math(cons, builtin_times);
375 }
376
377 ao_poly
378 ao_lisp_divide(struct ao_lisp_cons *cons)
379 {
380         return ao_lisp_math(cons, builtin_divide);
381 }
382
383 ao_poly
384 ao_lisp_mod(struct ao_lisp_cons *cons)
385 {
386         return ao_lisp_math(cons, builtin_mod);
387 }
388
389 ao_poly
390 ao_lisp_compare(struct ao_lisp_cons *cons, enum ao_lisp_builtin_id op)
391 {
392         ao_poly left;
393
394         if (!cons)
395                 return _ao_lisp_atom_t;
396
397         left = cons->car;
398         cons = ao_lisp_poly_cons(cons->cdr);
399         while (cons) {
400                 ao_poly right = cons->car;
401
402                 if (op == builtin_equal) {
403                         if (left != right)
404                                 return AO_LISP_NIL;
405                 } else {
406                         uint8_t lt = ao_lisp_poly_type(left);
407                         uint8_t rt = ao_lisp_poly_type(right);
408                         if (lt == AO_LISP_INT && rt == AO_LISP_INT) {
409                                 int l = ao_lisp_poly_int(left);
410                                 int r = ao_lisp_poly_int(right);
411
412                                 switch (op) {
413                                 case builtin_less:
414                                         if (!(l < r))
415                                                 return AO_LISP_NIL;
416                                         break;
417                                 case builtin_greater:
418                                         if (!(l > r))
419                                                 return AO_LISP_NIL;
420                                         break;
421                                 case builtin_less_equal:
422                                         if (!(l <= r))
423                                                 return AO_LISP_NIL;
424                                         break;
425                                 case builtin_greater_equal:
426                                         if (!(l >= r))
427                                                 return AO_LISP_NIL;
428                                         break;
429                                 default:
430                                         break;
431                                 }
432                         } else if (lt == AO_LISP_STRING && rt == AO_LISP_STRING) {
433                                 int c = strcmp(ao_lisp_poly_string(left),
434                                                ao_lisp_poly_string(right));
435                                 switch (op) {
436                                 case builtin_less:
437                                         if (!(c < 0))
438                                                 return AO_LISP_NIL;
439                                         break;
440                                 case builtin_greater:
441                                         if (!(c > 0))
442                                                 return AO_LISP_NIL;
443                                         break;
444                                 case builtin_less_equal:
445                                         if (!(c <= 0))
446                                                 return AO_LISP_NIL;
447                                         break;
448                                 case builtin_greater_equal:
449                                         if (!(c >= 0))
450                                                 return AO_LISP_NIL;
451                                         break;
452                                 default:
453                                         break;
454                                 }
455                         }
456                 }
457                 left = right;
458                 cons = ao_lisp_poly_cons(cons->cdr);
459         }
460         return _ao_lisp_atom_t;
461 }
462
463 ao_poly
464 ao_lisp_equal(struct ao_lisp_cons *cons)
465 {
466         return ao_lisp_compare(cons, builtin_equal);
467 }
468
469 ao_poly
470 ao_lisp_less(struct ao_lisp_cons *cons)
471 {
472         return ao_lisp_compare(cons, builtin_less);
473 }
474
475 ao_poly
476 ao_lisp_greater(struct ao_lisp_cons *cons)
477 {
478         return ao_lisp_compare(cons, builtin_greater);
479 }
480
481 ao_poly
482 ao_lisp_less_equal(struct ao_lisp_cons *cons)
483 {
484         return ao_lisp_compare(cons, builtin_less_equal);
485 }
486
487 ao_poly
488 ao_lisp_greater_equal(struct ao_lisp_cons *cons)
489 {
490         return ao_lisp_compare(cons, builtin_greater_equal);
491 }
492
493 ao_poly
494 ao_lisp_pack(struct ao_lisp_cons *cons)
495 {
496         if (!ao_lisp_check_argc(_ao_lisp_atom_pack, cons, 1, 1))
497                 return AO_LISP_NIL;
498         if (!ao_lisp_check_argt(_ao_lisp_atom_pack, cons, 0, AO_LISP_CONS, 1))
499                 return AO_LISP_NIL;
500         return ao_lisp_string_pack(ao_lisp_poly_cons(ao_lisp_arg(cons, 0)));
501 }
502
503 ao_poly
504 ao_lisp_unpack(struct ao_lisp_cons *cons)
505 {
506         if (!ao_lisp_check_argc(_ao_lisp_atom_unpack, cons, 1, 1))
507                 return AO_LISP_NIL;
508         if (!ao_lisp_check_argt(_ao_lisp_atom_unpack, cons, 0, AO_LISP_STRING, 0))
509                 return AO_LISP_NIL;
510         return ao_lisp_string_unpack(ao_lisp_poly_string(ao_lisp_arg(cons, 0)));
511 }
512
513 ao_poly
514 ao_lisp_flush(struct ao_lisp_cons *cons)
515 {
516         if (!ao_lisp_check_argc(_ao_lisp_atom_flush, cons, 0, 0))
517                 return AO_LISP_NIL;
518         ao_lisp_os_flush();
519         return _ao_lisp_atom_t;
520 }
521
522 ao_poly
523 ao_lisp_led(struct ao_lisp_cons *cons)
524 {
525         ao_poly led;
526         if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
527                 return AO_LISP_NIL;
528         if (!ao_lisp_check_argt(_ao_lisp_atom_led, cons, 0, AO_LISP_INT, 0))
529                 return AO_LISP_NIL;
530         led = ao_lisp_arg(cons, 0);
531         ao_lisp_os_led(ao_lisp_poly_int(led));
532         return led;
533 }
534
535 ao_poly
536 ao_lisp_delay(struct ao_lisp_cons *cons)
537 {
538         ao_poly delay;
539         if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
540                 return AO_LISP_NIL;
541         if (!ao_lisp_check_argt(_ao_lisp_atom_led, cons, 0, AO_LISP_INT, 0))
542                 return AO_LISP_NIL;
543         delay = ao_lisp_arg(cons, 0);
544         ao_lisp_os_delay(ao_lisp_poly_int(delay));
545         return delay;
546 }
547
548 ao_poly
549 ao_lisp_do_eval(struct ao_lisp_cons *cons)
550 {
551         if (!ao_lisp_check_argc(_ao_lisp_atom_eval, cons, 1, 1))
552                 return AO_LISP_NIL;
553         ao_lisp_stack->state = eval_sexpr;
554         return cons->car;
555 }
556
557 ao_poly
558 ao_lisp_do_read(struct ao_lisp_cons *cons)
559 {
560         if (!ao_lisp_check_argc(_ao_lisp_atom_read, cons, 0, 0))
561                 return AO_LISP_NIL;
562         return ao_lisp_read();
563 }
564
565 ao_poly
566 ao_lisp_do_collect(struct ao_lisp_cons *cons)
567 {
568         int     free;
569         (void) cons;
570         free = ao_lisp_collect(AO_LISP_COLLECT_FULL);
571         return ao_lisp_int_poly(free);
572 }
573
574 const ao_lisp_func_t ao_lisp_builtins[] = {
575         [builtin_eval] = ao_lisp_do_eval,
576         [builtin_read] = ao_lisp_do_read,
577         [builtin_lambda] = ao_lisp_lambda,
578         [builtin_lexpr] = ao_lisp_lexpr,
579         [builtin_nlambda] = ao_lisp_nlambda,
580         [builtin_macro] = ao_lisp_macro,
581         [builtin_car] = ao_lisp_car,
582         [builtin_cdr] = ao_lisp_cdr,
583         [builtin_cons] = ao_lisp_cons,
584         [builtin_last] = ao_lisp_last,
585         [builtin_length] = ao_lisp_length,
586         [builtin_quote] = ao_lisp_quote,
587         [builtin_set] = ao_lisp_set,
588         [builtin_setq] = ao_lisp_setq,
589         [builtin_cond] = ao_lisp_cond,
590         [builtin_progn] = ao_lisp_progn,
591         [builtin_while] = ao_lisp_while,
592         [builtin_print] = ao_lisp_print,
593         [builtin_patom] = ao_lisp_patom,
594         [builtin_plus] = ao_lisp_plus,
595         [builtin_minus] = ao_lisp_minus,
596         [builtin_times] = ao_lisp_times,
597         [builtin_divide] = ao_lisp_divide,
598         [builtin_mod] = ao_lisp_mod,
599         [builtin_equal] = ao_lisp_equal,
600         [builtin_less] = ao_lisp_less,
601         [builtin_greater] = ao_lisp_greater,
602         [builtin_less_equal] = ao_lisp_less_equal,
603         [builtin_greater_equal] = ao_lisp_greater_equal,
604         [builtin_pack] = ao_lisp_pack,
605         [builtin_unpack] = ao_lisp_unpack,
606         [builtin_flush] = ao_lisp_flush,
607         [builtin_led] = ao_lisp_led,
608         [builtin_delay] = ao_lisp_delay,
609         [builtin_save] = ao_lisp_save,
610         [builtin_restore] = ao_lisp_restore,
611         [builtin_call_cc] = ao_lisp_call_cc,
612         [builtin_collect] = ao_lisp_do_collect,
613 };
614