altos/lisp: split set/def. Add def support to lambdas
[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 #include <limits.h>
17 #include <math.h>
18
19 static int
20 builtin_size(void *addr)
21 {
22         (void) addr;
23         return sizeof (struct ao_lisp_builtin);
24 }
25
26 static void
27 builtin_mark(void *addr)
28 {
29         (void) addr;
30 }
31
32 static void
33 builtin_move(void *addr)
34 {
35         (void) addr;
36 }
37
38 const struct ao_lisp_type ao_lisp_builtin_type = {
39         .size = builtin_size,
40         .mark = builtin_mark,
41         .move = builtin_move
42 };
43
44 #ifdef AO_LISP_MAKE_CONST
45
46 #define AO_LISP_BUILTIN_CASENAME
47 #include "ao_lisp_builtin.h"
48
49 char *ao_lisp_args_name(uint8_t args) {
50         args &= AO_LISP_FUNC_MASK;
51         switch (args) {
52         case AO_LISP_FUNC_LAMBDA: return ao_lisp_poly_atom(_ao_lisp_atom_lambda)->name;
53         case AO_LISP_FUNC_LEXPR: return ao_lisp_poly_atom(_ao_lisp_atom_lexpr)->name;
54         case AO_LISP_FUNC_NLAMBDA: return ao_lisp_poly_atom(_ao_lisp_atom_nlambda)->name;
55         case AO_LISP_FUNC_MACRO: return ao_lisp_poly_atom(_ao_lisp_atom_macro)->name;
56         default: return "???";
57         }
58 }
59 #else
60
61 #define AO_LISP_BUILTIN_ARRAYNAME
62 #include "ao_lisp_builtin.h"
63
64 static char *
65 ao_lisp_builtin_name(enum ao_lisp_builtin_id b) {
66         if (b < _builtin_last)
67                 return ao_lisp_poly_atom(builtin_names[b])->name;
68         return "???";
69 }
70
71 static const ao_poly ao_lisp_args_atoms[] = {
72         [AO_LISP_FUNC_LAMBDA] = _ao_lisp_atom_lambda,
73         [AO_LISP_FUNC_LEXPR] = _ao_lisp_atom_lexpr,
74         [AO_LISP_FUNC_NLAMBDA] = _ao_lisp_atom_nlambda,
75         [AO_LISP_FUNC_MACRO] = _ao_lisp_atom_macro,
76 };
77
78 char *
79 ao_lisp_args_name(uint8_t args)
80 {
81         args &= AO_LISP_FUNC_MASK;
82         if (args < sizeof ao_lisp_args_atoms / sizeof ao_lisp_args_atoms[0])
83                 return ao_lisp_poly_atom(ao_lisp_args_atoms[args])->name;
84         return "(unknown)";
85 }
86 #endif
87
88 void
89 ao_lisp_builtin_write(ao_poly b)
90 {
91         struct ao_lisp_builtin *builtin = ao_lisp_poly_builtin(b);
92         printf("%s", ao_lisp_builtin_name(builtin->func));
93 }
94
95 ao_poly
96 ao_lisp_check_argc(ao_poly name, struct ao_lisp_cons *cons, int min, int max)
97 {
98         int     argc = 0;
99
100         while (cons && argc <= max) {
101                 argc++;
102                 cons = ao_lisp_cons_cdr(cons);
103         }
104         if (argc < min || argc > max)
105                 return ao_lisp_error(AO_LISP_INVALID, "%s: invalid arg count", ao_lisp_poly_atom(name)->name);
106         return _ao_lisp_bool_true;
107 }
108
109 ao_poly
110 ao_lisp_arg(struct ao_lisp_cons *cons, int argc)
111 {
112         if (!cons)
113                 return AO_LISP_NIL;
114         while (argc--) {
115                 if (!cons)
116                         return AO_LISP_NIL;
117                 cons = ao_lisp_cons_cdr(cons);
118         }
119         return cons->car;
120 }
121
122 ao_poly
123 ao_lisp_check_argt(ao_poly name, struct ao_lisp_cons *cons, int argc, int type, int nil_ok)
124 {
125         ao_poly car = ao_lisp_arg(cons, argc);
126
127         if ((!car && !nil_ok) || ao_lisp_poly_type(car) != type)
128                 return ao_lisp_error(AO_LISP_INVALID, "%s: invalid type for arg %d", ao_lisp_poly_atom(name)->name, argc);
129         return _ao_lisp_bool_true;
130 }
131
132 ao_poly
133 ao_lisp_do_car(struct ao_lisp_cons *cons)
134 {
135         if (!ao_lisp_check_argc(_ao_lisp_atom_car, cons, 1, 1))
136                 return AO_LISP_NIL;
137         if (!ao_lisp_check_argt(_ao_lisp_atom_car, cons, 0, AO_LISP_CONS, 0))
138                 return AO_LISP_NIL;
139         return ao_lisp_poly_cons(cons->car)->car;
140 }
141
142 ao_poly
143 ao_lisp_do_cdr(struct ao_lisp_cons *cons)
144 {
145         if (!ao_lisp_check_argc(_ao_lisp_atom_cdr, cons, 1, 1))
146                 return AO_LISP_NIL;
147         if (!ao_lisp_check_argt(_ao_lisp_atom_cdr, cons, 0, AO_LISP_CONS, 0))
148                 return AO_LISP_NIL;
149         return ao_lisp_poly_cons(cons->car)->cdr;
150 }
151
152 ao_poly
153 ao_lisp_do_cons(struct ao_lisp_cons *cons)
154 {
155         ao_poly car, cdr;
156         if(!ao_lisp_check_argc(_ao_lisp_atom_cons, cons, 2, 2))
157                 return AO_LISP_NIL;
158         car = ao_lisp_arg(cons, 0);
159         cdr = ao_lisp_arg(cons, 1);
160         return ao_lisp__cons(car, cdr);
161 }
162
163 ao_poly
164 ao_lisp_do_last(struct ao_lisp_cons *cons)
165 {
166         struct ao_lisp_cons     *list;
167         if (!ao_lisp_check_argc(_ao_lisp_atom_last, cons, 1, 1))
168                 return AO_LISP_NIL;
169         if (!ao_lisp_check_argt(_ao_lisp_atom_last, cons, 0, AO_LISP_CONS, 1))
170                 return AO_LISP_NIL;
171         for (list = ao_lisp_poly_cons(ao_lisp_arg(cons, 0));
172              list;
173              list = ao_lisp_cons_cdr(list))
174         {
175                 if (!list->cdr)
176                         return list->car;
177         }
178         return AO_LISP_NIL;
179 }
180
181 ao_poly
182 ao_lisp_do_length(struct ao_lisp_cons *cons)
183 {
184         if (!ao_lisp_check_argc(_ao_lisp_atom_length, cons, 1, 1))
185                 return AO_LISP_NIL;
186         if (!ao_lisp_check_argt(_ao_lisp_atom_length, cons, 0, AO_LISP_CONS, 1))
187                 return AO_LISP_NIL;
188         return ao_lisp_int_poly(ao_lisp_cons_length(ao_lisp_poly_cons(ao_lisp_arg(cons, 0))));
189 }
190
191 ao_poly
192 ao_lisp_do_quote(struct ao_lisp_cons *cons)
193 {
194         if (!ao_lisp_check_argc(_ao_lisp_atom_quote, cons, 1, 1))
195                 return AO_LISP_NIL;
196         return ao_lisp_arg(cons, 0);
197 }
198
199 ao_poly
200 ao_lisp_do_set(struct ao_lisp_cons *cons)
201 {
202         if (!ao_lisp_check_argc(_ao_lisp_atom_set, cons, 2, 2))
203                 return AO_LISP_NIL;
204         if (!ao_lisp_check_argt(_ao_lisp_atom_set, cons, 0, AO_LISP_ATOM, 0))
205                 return AO_LISP_NIL;
206
207         return ao_lisp_atom_set(ao_lisp_arg(cons, 0), ao_lisp_arg(cons, 1));
208 }
209
210 ao_poly
211 ao_lisp_do_def(struct ao_lisp_cons *cons)
212 {
213         if (!ao_lisp_check_argc(_ao_lisp_atom_def, cons, 2, 2))
214                 return AO_LISP_NIL;
215         if (!ao_lisp_check_argt(_ao_lisp_atom_def, cons, 0, AO_LISP_ATOM, 0))
216                 return AO_LISP_NIL;
217
218         return ao_lisp_atom_def(ao_lisp_arg(cons, 0), ao_lisp_arg(cons, 1));
219 }
220
221 ao_poly
222 ao_lisp_do_setq(struct ao_lisp_cons *cons)
223 {
224         ao_poly name;
225         if (!ao_lisp_check_argc(_ao_lisp_atom_set21, cons, 2, 2))
226                 return AO_LISP_NIL;
227         name = cons->car;
228         if (ao_lisp_poly_type(name) != AO_LISP_ATOM)
229                 return ao_lisp_error(AO_LISP_INVALID, "set! of non-atom");
230         if (!ao_lisp_atom_ref(name))
231                 return ao_lisp_error(AO_LISP_INVALID, "atom not defined");
232         return ao_lisp__cons(_ao_lisp_atom_set,
233                              ao_lisp__cons(ao_lisp__cons(_ao_lisp_atom_quote,
234                                                          ao_lisp__cons(name, AO_LISP_NIL)),
235                                            cons->cdr));
236 }
237
238 ao_poly
239 ao_lisp_do_cond(struct ao_lisp_cons *cons)
240 {
241         ao_lisp_set_cond(cons);
242         return AO_LISP_NIL;
243 }
244
245 ao_poly
246 ao_lisp_do_begin(struct ao_lisp_cons *cons)
247 {
248         ao_lisp_stack->state = eval_begin;
249         ao_lisp_stack->sexprs = ao_lisp_cons_poly(cons);
250         return AO_LISP_NIL;
251 }
252
253 ao_poly
254 ao_lisp_do_while(struct ao_lisp_cons *cons)
255 {
256         ao_lisp_stack->state = eval_while;
257         ao_lisp_stack->sexprs = ao_lisp_cons_poly(cons);
258         return AO_LISP_NIL;
259 }
260
261 ao_poly
262 ao_lisp_do_write(struct ao_lisp_cons *cons)
263 {
264         ao_poly val = AO_LISP_NIL;
265         while (cons) {
266                 val = cons->car;
267                 ao_lisp_poly_write(val);
268                 cons = ao_lisp_cons_cdr(cons);
269                 if (cons)
270                         printf(" ");
271         }
272         printf("\n");
273         return _ao_lisp_bool_true;
274 }
275
276 ao_poly
277 ao_lisp_do_display(struct ao_lisp_cons *cons)
278 {
279         ao_poly val = AO_LISP_NIL;
280         while (cons) {
281                 val = cons->car;
282                 ao_lisp_poly_display(val);
283                 cons = ao_lisp_cons_cdr(cons);
284         }
285         return _ao_lisp_bool_true;
286 }
287
288 ao_poly
289 ao_lisp_math(struct ao_lisp_cons *orig_cons, enum ao_lisp_builtin_id op)
290 {
291         struct ao_lisp_cons *cons = cons;
292         ao_poly ret = AO_LISP_NIL;
293
294         for (cons = orig_cons; cons; cons = ao_lisp_cons_cdr(cons)) {
295                 ao_poly         car = cons->car;
296                 uint8_t         rt = ao_lisp_poly_type(ret);
297                 uint8_t         ct = ao_lisp_poly_type(car);
298
299                 if (cons == orig_cons) {
300                         ret = car;
301                         if (cons->cdr == AO_LISP_NIL) {
302                                 switch (op) {
303                                 case builtin_minus:
304                                         if (ao_lisp_integer_typep(ct))
305                                                 ret = ao_lisp_integer_poly(-ao_lisp_poly_integer(ret));
306                                         else if (ct == AO_LISP_FLOAT)
307                                                 ret = ao_lisp_float_get(-ao_lisp_poly_number(ret));
308                                         break;
309                                 case builtin_divide:
310                                         if (ao_lisp_integer_typep(ct) && ao_lisp_poly_integer(ret) == 1)
311                                                 ;
312                                         else if (ao_lisp_number_typep(ct)) {
313                                                 float   v = ao_lisp_poly_number(ret);
314                                                 ret = ao_lisp_float_get(1/v);
315                                         }
316                                         break;
317                                 default:
318                                         break;
319                                 }
320                         }
321                 } else if (ao_lisp_integer_typep(rt) && ao_lisp_integer_typep(ct)) {
322                         int32_t r = ao_lisp_poly_integer(ret);
323                         int32_t c = ao_lisp_poly_integer(car);
324
325                         switch(op) {
326                         case builtin_plus:
327                                 r += c;
328                                 break;
329                         case builtin_minus:
330                                 r -= c;
331                                 break;
332                         case builtin_times:
333                                 r *= c;
334                                 break;
335                         case builtin_divide:
336                                 if (c != 0 && (r % c) == 0)
337                                         r /= c;
338                                 else {
339                                         ret = ao_lisp_float_get((float) r / (float) c);
340                                         continue;
341                                 }
342                                 break;
343                         case builtin_quotient:
344                                 if (c == 0)
345                                         return ao_lisp_error(AO_LISP_DIVIDE_BY_ZERO, "quotient by zero");
346                                 if (r % c != 0 && (c < 0) != (r < 0))
347                                         r = r / c - 1;
348                                 else
349                                         r = r / c;
350                                 break;
351                         case builtin_remainder:
352                                 if (c == 0)
353                                         return ao_lisp_error(AO_LISP_DIVIDE_BY_ZERO, "remainder by zero");
354                                 r %= c;
355                                 break;
356                         case builtin_modulo:
357                                 if (c == 0)
358                                         return ao_lisp_error(AO_LISP_DIVIDE_BY_ZERO, "modulo by zero");
359                                 r %= c;
360                                 if ((r < 0) != (c < 0))
361                                         r += c;
362                                 break;
363                         default:
364                                 break;
365                         }
366                         ret = ao_lisp_integer_poly(r);
367                 } else if (ao_lisp_number_typep(rt) && ao_lisp_number_typep(ct)) {
368                         float r = ao_lisp_poly_number(ret);
369                         float c = ao_lisp_poly_number(car);
370                         switch(op) {
371                         case builtin_plus:
372                                 r += c;
373                                 break;
374                         case builtin_minus:
375                                 r -= c;
376                                 break;
377                         case builtin_times:
378                                 r *= c;
379                                 break;
380                         case builtin_divide:
381                                 r /= c;
382                                 break;
383 #if 0
384                         case builtin_quotient:
385                                 if (c == 0)
386                                         return ao_lisp_error(AO_LISP_DIVIDE_BY_ZERO, "quotient by zero");
387                                 if (r % c != 0 && (c < 0) != (r < 0))
388                                         r = r / c - 1;
389                                 else
390                                         r = r / c;
391                                 break;
392                         case builtin_remainder:
393                                 if (c == 0)
394                                         return ao_lisp_error(AO_LISP_DIVIDE_BY_ZERO, "remainder by zero");
395                                 r %= c;
396                                 break;
397                         case builtin_modulo:
398                                 if (c == 0)
399                                         return ao_lisp_error(AO_LISP_DIVIDE_BY_ZERO, "modulo by zero");
400                                 r %= c;
401                                 if ((r < 0) != (c < 0))
402                                         r += c;
403                                 break;
404 #endif
405                         default:
406                                 break;
407                         }
408                         ret = ao_lisp_float_get(r);
409                 }
410
411                 else if (rt == AO_LISP_STRING && ct == AO_LISP_STRING && op == builtin_plus)
412                         ret = ao_lisp_string_poly(ao_lisp_string_cat(ao_lisp_poly_string(ret),
413                                                                      ao_lisp_poly_string(car)));
414                 else
415                         return ao_lisp_error(AO_LISP_INVALID, "invalid args");
416         }
417         return ret;
418 }
419
420 ao_poly
421 ao_lisp_do_plus(struct ao_lisp_cons *cons)
422 {
423         return ao_lisp_math(cons, builtin_plus);
424 }
425
426 ao_poly
427 ao_lisp_do_minus(struct ao_lisp_cons *cons)
428 {
429         return ao_lisp_math(cons, builtin_minus);
430 }
431
432 ao_poly
433 ao_lisp_do_times(struct ao_lisp_cons *cons)
434 {
435         return ao_lisp_math(cons, builtin_times);
436 }
437
438 ao_poly
439 ao_lisp_do_divide(struct ao_lisp_cons *cons)
440 {
441         return ao_lisp_math(cons, builtin_divide);
442 }
443
444 ao_poly
445 ao_lisp_do_quotient(struct ao_lisp_cons *cons)
446 {
447         return ao_lisp_math(cons, builtin_quotient);
448 }
449
450 ao_poly
451 ao_lisp_do_modulo(struct ao_lisp_cons *cons)
452 {
453         return ao_lisp_math(cons, builtin_modulo);
454 }
455
456 ao_poly
457 ao_lisp_do_remainder(struct ao_lisp_cons *cons)
458 {
459         return ao_lisp_math(cons, builtin_remainder);
460 }
461
462 ao_poly
463 ao_lisp_compare(struct ao_lisp_cons *cons, enum ao_lisp_builtin_id op)
464 {
465         ao_poly left;
466
467         if (!cons)
468                 return _ao_lisp_bool_true;
469
470         left = cons->car;
471         for (cons = ao_lisp_cons_cdr(cons); cons; cons = ao_lisp_cons_cdr(cons)) {
472                 ao_poly right = cons->car;
473
474                 if (op == builtin_equal) {
475                         if (left != right)
476                                 return _ao_lisp_bool_false;
477                 } else {
478                         uint8_t lt = ao_lisp_poly_type(left);
479                         uint8_t rt = ao_lisp_poly_type(right);
480                         if (ao_lisp_integer_typep(lt) && ao_lisp_integer_typep(rt)) {
481                                 int32_t l = ao_lisp_poly_integer(left);
482                                 int32_t r = ao_lisp_poly_integer(right);
483
484                                 switch (op) {
485                                 case builtin_less:
486                                         if (!(l < r))
487                                                 return _ao_lisp_bool_false;
488                                         break;
489                                 case builtin_greater:
490                                         if (!(l > r))
491                                                 return _ao_lisp_bool_false;
492                                         break;
493                                 case builtin_less_equal:
494                                         if (!(l <= r))
495                                                 return _ao_lisp_bool_false;
496                                         break;
497                                 case builtin_greater_equal:
498                                         if (!(l >= r))
499                                                 return _ao_lisp_bool_false;
500                                         break;
501                                 default:
502                                         break;
503                                 }
504                         } else if (lt == AO_LISP_STRING && rt == AO_LISP_STRING) {
505                                 int c = strcmp(ao_lisp_poly_string(left),
506                                                ao_lisp_poly_string(right));
507                                 switch (op) {
508                                 case builtin_less:
509                                         if (!(c < 0))
510                                                 return _ao_lisp_bool_false;
511                                         break;
512                                 case builtin_greater:
513                                         if (!(c > 0))
514                                                 return _ao_lisp_bool_false;
515                                         break;
516                                 case builtin_less_equal:
517                                         if (!(c <= 0))
518                                                 return _ao_lisp_bool_false;
519                                         break;
520                                 case builtin_greater_equal:
521                                         if (!(c >= 0))
522                                                 return _ao_lisp_bool_false;
523                                         break;
524                                 default:
525                                         break;
526                                 }
527                         }
528                 }
529                 left = right;
530         }
531         return _ao_lisp_bool_true;
532 }
533
534 ao_poly
535 ao_lisp_do_equal(struct ao_lisp_cons *cons)
536 {
537         return ao_lisp_compare(cons, builtin_equal);
538 }
539
540 ao_poly
541 ao_lisp_do_less(struct ao_lisp_cons *cons)
542 {
543         return ao_lisp_compare(cons, builtin_less);
544 }
545
546 ao_poly
547 ao_lisp_do_greater(struct ao_lisp_cons *cons)
548 {
549         return ao_lisp_compare(cons, builtin_greater);
550 }
551
552 ao_poly
553 ao_lisp_do_less_equal(struct ao_lisp_cons *cons)
554 {
555         return ao_lisp_compare(cons, builtin_less_equal);
556 }
557
558 ao_poly
559 ao_lisp_do_greater_equal(struct ao_lisp_cons *cons)
560 {
561         return ao_lisp_compare(cons, builtin_greater_equal);
562 }
563
564 ao_poly
565 ao_lisp_do_list_to_string(struct ao_lisp_cons *cons)
566 {
567         if (!ao_lisp_check_argc(_ao_lisp_atom_list2d3estring, cons, 1, 1))
568                 return AO_LISP_NIL;
569         if (!ao_lisp_check_argt(_ao_lisp_atom_list2d3estring, cons, 0, AO_LISP_CONS, 1))
570                 return AO_LISP_NIL;
571         return ao_lisp_string_pack(ao_lisp_poly_cons(ao_lisp_arg(cons, 0)));
572 }
573
574 ao_poly
575 ao_lisp_do_string_to_list(struct ao_lisp_cons *cons)
576 {
577         if (!ao_lisp_check_argc(_ao_lisp_atom_string2d3elist, cons, 1, 1))
578                 return AO_LISP_NIL;
579         if (!ao_lisp_check_argt(_ao_lisp_atom_string2d3elist, cons, 0, AO_LISP_STRING, 0))
580                 return AO_LISP_NIL;
581         return ao_lisp_string_unpack(ao_lisp_poly_string(ao_lisp_arg(cons, 0)));
582 }
583
584 ao_poly
585 ao_lisp_do_flush_output(struct ao_lisp_cons *cons)
586 {
587         if (!ao_lisp_check_argc(_ao_lisp_atom_flush2doutput, cons, 0, 0))
588                 return AO_LISP_NIL;
589         ao_lisp_os_flush();
590         return _ao_lisp_bool_true;
591 }
592
593 ao_poly
594 ao_lisp_do_led(struct ao_lisp_cons *cons)
595 {
596         ao_poly led;
597         if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
598                 return AO_LISP_NIL;
599         if (!ao_lisp_check_argt(_ao_lisp_atom_led, cons, 0, AO_LISP_INT, 0))
600                 return AO_LISP_NIL;
601         led = ao_lisp_arg(cons, 0);
602         ao_lisp_os_led(ao_lisp_poly_int(led));
603         return led;
604 }
605
606 ao_poly
607 ao_lisp_do_delay(struct ao_lisp_cons *cons)
608 {
609         ao_poly delay;
610         if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
611                 return AO_LISP_NIL;
612         if (!ao_lisp_check_argt(_ao_lisp_atom_led, cons, 0, AO_LISP_INT, 0))
613                 return AO_LISP_NIL;
614         delay = ao_lisp_arg(cons, 0);
615         ao_lisp_os_delay(ao_lisp_poly_int(delay));
616         return delay;
617 }
618
619 ao_poly
620 ao_lisp_do_eval(struct ao_lisp_cons *cons)
621 {
622         if (!ao_lisp_check_argc(_ao_lisp_atom_eval, cons, 1, 1))
623                 return AO_LISP_NIL;
624         ao_lisp_stack->state = eval_sexpr;
625         return cons->car;
626 }
627
628 ao_poly
629 ao_lisp_do_apply(struct ao_lisp_cons *cons)
630 {
631         if (!ao_lisp_check_argc(_ao_lisp_atom_apply, cons, 2, INT_MAX))
632                 return AO_LISP_NIL;
633         ao_lisp_stack->state = eval_apply;
634         return ao_lisp_cons_poly(cons);
635 }
636
637 ao_poly
638 ao_lisp_do_read(struct ao_lisp_cons *cons)
639 {
640         if (!ao_lisp_check_argc(_ao_lisp_atom_read, cons, 0, 0))
641                 return AO_LISP_NIL;
642         return ao_lisp_read();
643 }
644
645 ao_poly
646 ao_lisp_do_collect(struct ao_lisp_cons *cons)
647 {
648         int     free;
649         (void) cons;
650         free = ao_lisp_collect(AO_LISP_COLLECT_FULL);
651         return ao_lisp_int_poly(free);
652 }
653
654 ao_poly
655 ao_lisp_do_nullp(struct ao_lisp_cons *cons)
656 {
657         if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
658                 return AO_LISP_NIL;
659         if (ao_lisp_arg(cons, 0) == AO_LISP_NIL)
660                 return _ao_lisp_bool_true;
661         else
662                 return _ao_lisp_bool_false;
663 }
664
665 ao_poly
666 ao_lisp_do_not(struct ao_lisp_cons *cons)
667 {
668         if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
669                 return AO_LISP_NIL;
670         if (ao_lisp_arg(cons, 0) == _ao_lisp_bool_false)
671                 return _ao_lisp_bool_true;
672         else
673                 return _ao_lisp_bool_false;
674 }
675
676 static ao_poly
677 ao_lisp_do_typep(int type, struct ao_lisp_cons *cons)
678 {
679         if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
680                 return AO_LISP_NIL;
681         if (ao_lisp_poly_type(ao_lisp_arg(cons, 0)) == type)
682                 return _ao_lisp_bool_true;
683         return _ao_lisp_bool_false;
684 }
685
686 ao_poly
687 ao_lisp_do_pairp(struct ao_lisp_cons *cons)
688 {
689         ao_poly v;
690         if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
691                 return AO_LISP_NIL;
692         v = ao_lisp_arg(cons, 0);
693         if (v != AO_LISP_NIL && ao_lisp_poly_type(v) == AO_LISP_CONS)
694                 return _ao_lisp_bool_true;
695         return _ao_lisp_bool_false;
696 }
697
698 ao_poly
699 ao_lisp_do_integerp(struct ao_lisp_cons *cons)
700 {
701         if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
702                 return AO_LISP_NIL;
703         switch (ao_lisp_poly_type(ao_lisp_arg(cons, 0))) {
704         case AO_LISP_INT:
705         case AO_LISP_BIGINT:
706                 return _ao_lisp_bool_true;
707         default:
708                 return _ao_lisp_bool_false;
709         }
710 }
711
712 ao_poly
713 ao_lisp_do_numberp(struct ao_lisp_cons *cons)
714 {
715         if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
716                 return AO_LISP_NIL;
717         switch (ao_lisp_poly_type(ao_lisp_arg(cons, 0))) {
718         case AO_LISP_INT:
719         case AO_LISP_BIGINT:
720         case AO_LISP_FLOAT:
721                 return _ao_lisp_bool_true;
722         default:
723                 return _ao_lisp_bool_false;
724         }
725 }
726
727 ao_poly
728 ao_lisp_do_stringp(struct ao_lisp_cons *cons)
729 {
730         return ao_lisp_do_typep(AO_LISP_STRING, cons);
731 }
732
733 ao_poly
734 ao_lisp_do_symbolp(struct ao_lisp_cons *cons)
735 {
736         return ao_lisp_do_typep(AO_LISP_ATOM, cons);
737 }
738
739 ao_poly
740 ao_lisp_do_booleanp(struct ao_lisp_cons *cons)
741 {
742         return ao_lisp_do_typep(AO_LISP_BOOL, cons);
743 }
744
745 ao_poly
746 ao_lisp_do_procedurep(struct ao_lisp_cons *cons)
747 {
748         if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
749                 return AO_LISP_NIL;
750         switch (ao_lisp_poly_type(ao_lisp_arg(cons, 0))) {
751         case AO_LISP_BUILTIN:
752         case AO_LISP_LAMBDA:
753                 return _ao_lisp_bool_true;
754         default:
755         return _ao_lisp_bool_false;
756         }
757 }
758
759 /* This one is special -- a list is either nil or
760  * a 'proper' list with only cons cells
761  */
762 ao_poly
763 ao_lisp_do_listp(struct ao_lisp_cons *cons)
764 {
765         ao_poly v;
766         if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
767                 return AO_LISP_NIL;
768         v = ao_lisp_arg(cons, 0);
769         for (;;) {
770                 if (v == AO_LISP_NIL)
771                         return _ao_lisp_bool_true;
772                 if (ao_lisp_poly_type(v) != AO_LISP_CONS)
773                         return _ao_lisp_bool_false;
774                 v = ao_lisp_poly_cons(v)->cdr;
775         }
776 }
777
778 ao_poly
779 ao_lisp_do_set_car(struct ao_lisp_cons *cons)
780 {
781         if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 2, 2))
782                 return AO_LISP_NIL;
783         if (!ao_lisp_check_argt(_ao_lisp_atom_led, cons, 0, AO_LISP_CONS, 0))
784                 return AO_LISP_NIL;
785         return ao_lisp_poly_cons(ao_lisp_arg(cons, 0))->car = ao_lisp_arg(cons, 1);
786 }
787
788 ao_poly
789 ao_lisp_do_set_cdr(struct ao_lisp_cons *cons)
790 {
791         if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 2, 2))
792                 return AO_LISP_NIL;
793         if (!ao_lisp_check_argt(_ao_lisp_atom_led, cons, 0, AO_LISP_CONS, 0))
794                 return AO_LISP_NIL;
795         return ao_lisp_poly_cons(ao_lisp_arg(cons, 0))->cdr = ao_lisp_arg(cons, 1);
796 }
797
798 ao_poly
799 ao_lisp_do_symbol_to_string(struct ao_lisp_cons *cons)
800 {
801         if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
802                 return AO_LISP_NIL;
803         if (!ao_lisp_check_argt(_ao_lisp_atom_led, cons, 0, AO_LISP_ATOM, 0))
804                 return AO_LISP_NIL;
805         return ao_lisp_string_poly(ao_lisp_string_copy(ao_lisp_poly_atom(ao_lisp_arg(cons, 0))->name));
806 }
807
808 ao_poly
809 ao_lisp_do_string_to_symbol(struct ao_lisp_cons *cons)
810 {
811         if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
812                 return AO_LISP_NIL;
813         if (!ao_lisp_check_argt(_ao_lisp_atom_led, cons, 0, AO_LISP_STRING, 0))
814                 return AO_LISP_NIL;
815
816         return ao_lisp_atom_poly(ao_lisp_atom_intern(ao_lisp_poly_string(ao_lisp_arg(cons, 0))));
817 }
818
819 ao_poly
820 ao_lisp_do_read_char(struct ao_lisp_cons *cons)
821 {
822         int     c;
823         if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 0, 0))
824                 return AO_LISP_NIL;
825         c = getchar();
826         return ao_lisp_int_poly(c);
827 }
828
829 ao_poly
830 ao_lisp_do_write_char(struct ao_lisp_cons *cons)
831 {
832         if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
833                 return AO_LISP_NIL;
834         if (!ao_lisp_check_argt(_ao_lisp_atom_led, cons, 0, AO_LISP_INT, 0))
835                 return AO_LISP_NIL;
836         putchar(ao_lisp_poly_integer(ao_lisp_arg(cons, 0)));
837         return _ao_lisp_bool_true;
838 }
839
840 ao_poly
841 ao_lisp_do_exit(struct ao_lisp_cons *cons)
842 {
843         if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 0, 0))
844                 return AO_LISP_NIL;
845         ao_lisp_exception |= AO_LISP_EXIT;
846         return _ao_lisp_bool_true;
847 }
848
849 ao_poly
850 ao_lisp_do_current_jiffy(struct ao_lisp_cons *cons)
851 {
852         int     jiffy;
853
854         if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 0, 0))
855                 return AO_LISP_NIL;
856         jiffy = ao_lisp_os_jiffy();
857         return (ao_lisp_int_poly(jiffy));
858 }
859
860 ao_poly
861 ao_lisp_do_current_second(struct ao_lisp_cons *cons)
862 {
863         int     second;
864
865         if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 0, 0))
866                 return AO_LISP_NIL;
867         second = ao_lisp_os_jiffy() / AO_LISP_JIFFIES_PER_SECOND;
868         return (ao_lisp_int_poly(second));
869 }
870
871 ao_poly
872 ao_lisp_do_jiffies_per_second(struct ao_lisp_cons *cons)
873 {
874         if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 0, 0))
875                 return AO_LISP_NIL;
876         return (ao_lisp_int_poly(AO_LISP_JIFFIES_PER_SECOND));
877 }
878
879 #define AO_LISP_BUILTIN_FUNCS
880 #include "ao_lisp_builtin.h"