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