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