altos/scheme: Stash cons across value allocation in compare
[fw/altos] / src / scheme / ao_scheme_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_scheme.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_scheme_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_scheme_type ao_scheme_builtin_type = {
39         .size = builtin_size,
40         .mark = builtin_mark,
41         .move = builtin_move
42 };
43
44 #ifdef AO_SCHEME_MAKE_CONST
45
46 #define AO_SCHEME_BUILTIN_CASENAME
47 #include "ao_scheme_builtin.h"
48
49 char *ao_scheme_args_name(uint8_t args) {
50         args &= AO_SCHEME_FUNC_MASK;
51         switch (args) {
52         case AO_SCHEME_FUNC_LAMBDA: return ao_scheme_poly_atom(_ao_scheme_atom_lambda)->name;
53         case AO_SCHEME_FUNC_NLAMBDA: return ao_scheme_poly_atom(_ao_scheme_atom_nlambda)->name;
54         case AO_SCHEME_FUNC_MACRO: return ao_scheme_poly_atom(_ao_scheme_atom_macro)->name;
55         default: return "???";
56         }
57 }
58 #else
59
60 #define AO_SCHEME_BUILTIN_ARRAYNAME
61 #include "ao_scheme_builtin.h"
62
63 static char *
64 ao_scheme_builtin_name(enum ao_scheme_builtin_id b) {
65         if (b < _builtin_last)
66                 return ao_scheme_poly_atom(builtin_names[b])->name;
67         return "???";
68 }
69
70 static const ao_poly ao_scheme_args_atoms[] = {
71         [AO_SCHEME_FUNC_LAMBDA] = _ao_scheme_atom_lambda,
72         [AO_SCHEME_FUNC_NLAMBDA] = _ao_scheme_atom_nlambda,
73         [AO_SCHEME_FUNC_MACRO] = _ao_scheme_atom_macro,
74 };
75
76 char *
77 ao_scheme_args_name(uint8_t args)
78 {
79         args &= AO_SCHEME_FUNC_MASK;
80         if (args < sizeof ao_scheme_args_atoms / sizeof ao_scheme_args_atoms[0])
81                 return ao_scheme_poly_atom(ao_scheme_args_atoms[args])->name;
82         return "(unknown)";
83 }
84 #endif
85
86 void
87 ao_scheme_builtin_write(ao_poly b)
88 {
89         struct ao_scheme_builtin *builtin = ao_scheme_poly_builtin(b);
90         printf("%s", ao_scheme_builtin_name(builtin->func));
91 }
92
93 ao_poly
94 ao_scheme_check_argc(ao_poly name, struct ao_scheme_cons *cons, int min, int max)
95 {
96         int     argc = 0;
97
98         while (cons && argc <= max) {
99                 argc++;
100                 cons = ao_scheme_cons_cdr(cons);
101         }
102         if (argc < min || argc > max)
103                 return ao_scheme_error(AO_SCHEME_INVALID, "%s: invalid arg count", ao_scheme_poly_atom(name)->name);
104         return _ao_scheme_bool_true;
105 }
106
107 ao_poly
108 ao_scheme_arg(struct ao_scheme_cons *cons, int argc)
109 {
110         if (!cons)
111                 return AO_SCHEME_NIL;
112         while (argc--) {
113                 if (!cons)
114                         return AO_SCHEME_NIL;
115                 cons = ao_scheme_cons_cdr(cons);
116         }
117         return cons->car;
118 }
119
120 ao_poly
121 ao_scheme_check_argt(ao_poly name, struct ao_scheme_cons *cons, int argc, int type, int nil_ok)
122 {
123         ao_poly car = ao_scheme_arg(cons, argc);
124
125         if ((!car && !nil_ok) || ao_scheme_poly_type(car) != type)
126                 return ao_scheme_error(AO_SCHEME_INVALID, "%v: arg %d invalid type %v", name, argc, car);
127         return _ao_scheme_bool_true;
128 }
129
130 int32_t
131 ao_scheme_arg_int(ao_poly name, struct ao_scheme_cons *cons, int argc)
132 {
133         ao_poly p = ao_scheme_arg(cons, argc);
134         int32_t i = ao_scheme_poly_integer(p);
135
136         if (i == AO_SCHEME_NOT_INTEGER)
137                 (void) ao_scheme_error(AO_SCHEME_INVALID, "%v: arg %d invalid type %v", name, argc, p);
138         return i;
139 }
140
141 ao_poly
142 ao_scheme_do_car(struct ao_scheme_cons *cons)
143 {
144         if (!ao_scheme_check_argc(_ao_scheme_atom_car, cons, 1, 1))
145                 return AO_SCHEME_NIL;
146         if (!ao_scheme_check_argt(_ao_scheme_atom_car, cons, 0, AO_SCHEME_CONS, 0))
147                 return AO_SCHEME_NIL;
148         return ao_scheme_poly_cons(cons->car)->car;
149 }
150
151 ao_poly
152 ao_scheme_do_cdr(struct ao_scheme_cons *cons)
153 {
154         if (!ao_scheme_check_argc(_ao_scheme_atom_cdr, cons, 1, 1))
155                 return AO_SCHEME_NIL;
156         if (!ao_scheme_check_argt(_ao_scheme_atom_cdr, cons, 0, AO_SCHEME_CONS, 0))
157                 return AO_SCHEME_NIL;
158         return ao_scheme_poly_cons(cons->car)->cdr;
159 }
160
161 ao_poly
162 ao_scheme_do_cons(struct ao_scheme_cons *cons)
163 {
164         ao_poly car, cdr;
165         if(!ao_scheme_check_argc(_ao_scheme_atom_cons, cons, 2, 2))
166                 return AO_SCHEME_NIL;
167         car = ao_scheme_arg(cons, 0);
168         cdr = ao_scheme_arg(cons, 1);
169         return ao_scheme__cons(car, cdr);
170 }
171
172 ao_poly
173 ao_scheme_do_last(struct ao_scheme_cons *cons)
174 {
175         struct ao_scheme_cons   *list;
176         if (!ao_scheme_check_argc(_ao_scheme_atom_last, cons, 1, 1))
177                 return AO_SCHEME_NIL;
178         if (!ao_scheme_check_argt(_ao_scheme_atom_last, cons, 0, AO_SCHEME_CONS, 1))
179                 return AO_SCHEME_NIL;
180         for (list = ao_scheme_poly_cons(ao_scheme_arg(cons, 0));
181              list;
182              list = ao_scheme_cons_cdr(list))
183         {
184                 if (!list->cdr)
185                         return list->car;
186         }
187         return AO_SCHEME_NIL;
188 }
189
190 ao_poly
191 ao_scheme_do_length(struct ao_scheme_cons *cons)
192 {
193         if (!ao_scheme_check_argc(_ao_scheme_atom_length, cons, 1, 1))
194                 return AO_SCHEME_NIL;
195         if (!ao_scheme_check_argt(_ao_scheme_atom_length, cons, 0, AO_SCHEME_CONS, 1))
196                 return AO_SCHEME_NIL;
197         return ao_scheme_int_poly(ao_scheme_cons_length(ao_scheme_poly_cons(ao_scheme_arg(cons, 0))));
198 }
199
200 ao_poly
201 ao_scheme_do_list_copy(struct ao_scheme_cons *cons)
202 {
203         struct ao_scheme_cons *new;
204
205         if (!ao_scheme_check_argc(_ao_scheme_atom_length, cons, 1, 1))
206                 return AO_SCHEME_NIL;
207         if (!ao_scheme_check_argt(_ao_scheme_atom_length, cons, 0, AO_SCHEME_CONS, 1))
208                 return AO_SCHEME_NIL;
209         new = ao_scheme_cons_copy(ao_scheme_poly_cons(ao_scheme_arg(cons, 0)));
210         return ao_scheme_cons_poly(new);
211 }
212
213 ao_poly
214 ao_scheme_do_quote(struct ao_scheme_cons *cons)
215 {
216         if (!ao_scheme_check_argc(_ao_scheme_atom_quote, cons, 1, 1))
217                 return AO_SCHEME_NIL;
218         return ao_scheme_arg(cons, 0);
219 }
220
221 ao_poly
222 ao_scheme_do_set(struct ao_scheme_cons *cons)
223 {
224         if (!ao_scheme_check_argc(_ao_scheme_atom_set, cons, 2, 2))
225                 return AO_SCHEME_NIL;
226         if (!ao_scheme_check_argt(_ao_scheme_atom_set, cons, 0, AO_SCHEME_ATOM, 0))
227                 return AO_SCHEME_NIL;
228
229         return ao_scheme_atom_set(ao_scheme_arg(cons, 0), ao_scheme_arg(cons, 1));
230 }
231
232 ao_poly
233 ao_scheme_do_def(struct ao_scheme_cons *cons)
234 {
235         if (!ao_scheme_check_argc(_ao_scheme_atom_def, cons, 2, 2))
236                 return AO_SCHEME_NIL;
237         if (!ao_scheme_check_argt(_ao_scheme_atom_def, cons, 0, AO_SCHEME_ATOM, 0))
238                 return AO_SCHEME_NIL;
239
240         return ao_scheme_atom_def(ao_scheme_arg(cons, 0), ao_scheme_arg(cons, 1));
241 }
242
243 ao_poly
244 ao_scheme_do_setq(struct ao_scheme_cons *cons)
245 {
246         ao_poly name;
247         if (!ao_scheme_check_argc(_ao_scheme_atom_set21, cons, 2, 2))
248                 return AO_SCHEME_NIL;
249         name = cons->car;
250         if (ao_scheme_poly_type(name) != AO_SCHEME_ATOM)
251                 return ao_scheme_error(AO_SCHEME_INVALID, "set! of non-atom %v", name);
252         if (!ao_scheme_atom_ref(name, NULL))
253                 return ao_scheme_error(AO_SCHEME_INVALID, "atom %v not defined", name);
254         return ao_scheme__cons(_ao_scheme_atom_set,
255                              ao_scheme__cons(ao_scheme__cons(_ao_scheme_atom_quote,
256                                                          ao_scheme__cons(name, AO_SCHEME_NIL)),
257                                            cons->cdr));
258 }
259
260 ao_poly
261 ao_scheme_do_cond(struct ao_scheme_cons *cons)
262 {
263         ao_scheme_set_cond(cons);
264         return AO_SCHEME_NIL;
265 }
266
267 ao_poly
268 ao_scheme_do_begin(struct ao_scheme_cons *cons)
269 {
270         ao_scheme_stack->state = eval_begin;
271         ao_scheme_stack->sexprs = ao_scheme_cons_poly(cons);
272         return AO_SCHEME_NIL;
273 }
274
275 ao_poly
276 ao_scheme_do_while(struct ao_scheme_cons *cons)
277 {
278         ao_scheme_stack->state = eval_while;
279         ao_scheme_stack->sexprs = ao_scheme_cons_poly(cons);
280         return AO_SCHEME_NIL;
281 }
282
283 ao_poly
284 ao_scheme_do_write(struct ao_scheme_cons *cons)
285 {
286         ao_poly val = AO_SCHEME_NIL;
287         while (cons) {
288                 val = cons->car;
289                 ao_scheme_poly_write(val);
290                 cons = ao_scheme_cons_cdr(cons);
291                 if (cons)
292                         printf(" ");
293         }
294         return _ao_scheme_bool_true;
295 }
296
297 ao_poly
298 ao_scheme_do_display(struct ao_scheme_cons *cons)
299 {
300         ao_poly val = AO_SCHEME_NIL;
301         while (cons) {
302                 val = cons->car;
303                 ao_scheme_poly_display(val);
304                 cons = ao_scheme_cons_cdr(cons);
305         }
306         return _ao_scheme_bool_true;
307 }
308
309 ao_poly
310 ao_scheme_math(struct ao_scheme_cons *orig_cons, enum ao_scheme_builtin_id op)
311 {
312         struct ao_scheme_cons *cons = cons;
313         ao_poly ret = AO_SCHEME_NIL;
314
315         for (cons = orig_cons; cons; cons = ao_scheme_cons_cdr(cons)) {
316                 ao_poly         car = cons->car;
317                 uint8_t         rt = ao_scheme_poly_type(ret);
318                 uint8_t         ct = ao_scheme_poly_type(car);
319
320                 if (cons == orig_cons) {
321                         ret = car;
322                         ao_scheme_cons_stash(0, cons);
323                         if (cons->cdr == AO_SCHEME_NIL) {
324                                 switch (op) {
325                                 case builtin_minus:
326                                         if (ao_scheme_integer_typep(ct))
327                                                 ret = ao_scheme_integer_poly(-ao_scheme_poly_integer(ret));
328                                         else if (ct == AO_SCHEME_FLOAT)
329                                                 ret = ao_scheme_float_get(-ao_scheme_poly_number(ret));
330                                         break;
331                                 case builtin_divide:
332                                         if (ao_scheme_integer_typep(ct) && ao_scheme_poly_integer(ret) == 1)
333                                                 ;
334                                         else if (ao_scheme_number_typep(ct)) {
335                                                 float   v = ao_scheme_poly_number(ret);
336                                                 ret = ao_scheme_float_get(1/v);
337                                         }
338                                         break;
339                                 default:
340                                         break;
341                                 }
342                         }
343                         cons = ao_scheme_cons_fetch(0);
344                 } else if (ao_scheme_integer_typep(rt) && ao_scheme_integer_typep(ct)) {
345                         int32_t r = ao_scheme_poly_integer(ret);
346                         int32_t c = ao_scheme_poly_integer(car);
347                         int64_t t;
348
349                         switch(op) {
350                         case builtin_plus:
351                                 r += c;
352                         check_overflow:
353                                 if (r < AO_SCHEME_MIN_BIGINT || AO_SCHEME_MAX_BIGINT < r)
354                                         goto inexact;
355                                 break;
356                         case builtin_minus:
357                                 r -= c;
358                                 goto check_overflow;
359                                 break;
360                         case builtin_times:
361                                 t = (int64_t) r * (int64_t) c;
362                                 if (t < AO_SCHEME_MIN_BIGINT || AO_SCHEME_MAX_BIGINT < t)
363                                         goto inexact;
364                                 r = (int32_t) t;
365                                 break;
366                         case builtin_divide:
367                                 if (c != 0 && (r % c) == 0)
368                                         r /= c;
369                                 else
370                                         goto inexact;
371                                 break;
372                         case builtin_quotient:
373                                 if (c == 0)
374                                         return ao_scheme_error(AO_SCHEME_DIVIDE_BY_ZERO, "quotient by zero");
375                                 if (r % c != 0 && (c < 0) != (r < 0))
376                                         r = r / c - 1;
377                                 else
378                                         r = r / c;
379                                 break;
380                         case builtin_remainder:
381                                 if (c == 0)
382                                         return ao_scheme_error(AO_SCHEME_DIVIDE_BY_ZERO, "remainder by zero");
383                                 r %= c;
384                                 break;
385                         case builtin_modulo:
386                                 if (c == 0)
387                                         return ao_scheme_error(AO_SCHEME_DIVIDE_BY_ZERO, "modulo by zero");
388                                 r %= c;
389                                 if ((r < 0) != (c < 0))
390                                         r += c;
391                                 break;
392                         default:
393                                 break;
394                         }
395                         ao_scheme_cons_stash(0, cons);
396                         ret = ao_scheme_integer_poly(r);
397                         cons = ao_scheme_cons_fetch(0);
398                 } else if (ao_scheme_number_typep(rt) && ao_scheme_number_typep(ct)) {
399                         float r, c;
400                 inexact:
401                         r = ao_scheme_poly_number(ret);
402                         c = ao_scheme_poly_number(car);
403                         switch(op) {
404                         case builtin_plus:
405                                 r += c;
406                                 break;
407                         case builtin_minus:
408                                 r -= c;
409                                 break;
410                         case builtin_times:
411                                 r *= c;
412                                 break;
413                         case builtin_divide:
414                                 r /= c;
415                                 break;
416                         case builtin_quotient:
417                         case builtin_remainder:
418                         case builtin_modulo:
419                                 return ao_scheme_error(AO_SCHEME_INVALID, "non-integer value in integer divide");
420                         default:
421                                 break;
422                         }
423                         ao_scheme_cons_stash(0, cons);
424                         ret = ao_scheme_float_get(r);
425                         cons = ao_scheme_cons_fetch(0);
426                 }
427                 else if (rt == AO_SCHEME_STRING && ct == AO_SCHEME_STRING && op == builtin_plus) {
428                         ao_scheme_cons_stash(0, cons);
429                         ret = ao_scheme_string_poly(ao_scheme_string_cat(ao_scheme_poly_string(ret),
430                                                                          ao_scheme_poly_string(car)));
431                         cons = ao_scheme_cons_fetch(0);
432                         if (!ret)
433                                 return ret;
434                 }
435                 else
436                         return ao_scheme_error(AO_SCHEME_INVALID, "invalid args");
437         }
438         return ret;
439 }
440
441 ao_poly
442 ao_scheme_do_plus(struct ao_scheme_cons *cons)
443 {
444         return ao_scheme_math(cons, builtin_plus);
445 }
446
447 ao_poly
448 ao_scheme_do_minus(struct ao_scheme_cons *cons)
449 {
450         return ao_scheme_math(cons, builtin_minus);
451 }
452
453 ao_poly
454 ao_scheme_do_times(struct ao_scheme_cons *cons)
455 {
456         return ao_scheme_math(cons, builtin_times);
457 }
458
459 ao_poly
460 ao_scheme_do_divide(struct ao_scheme_cons *cons)
461 {
462         return ao_scheme_math(cons, builtin_divide);
463 }
464
465 ao_poly
466 ao_scheme_do_quotient(struct ao_scheme_cons *cons)
467 {
468         return ao_scheme_math(cons, builtin_quotient);
469 }
470
471 ao_poly
472 ao_scheme_do_modulo(struct ao_scheme_cons *cons)
473 {
474         return ao_scheme_math(cons, builtin_modulo);
475 }
476
477 ao_poly
478 ao_scheme_do_remainder(struct ao_scheme_cons *cons)
479 {
480         return ao_scheme_math(cons, builtin_remainder);
481 }
482
483 ao_poly
484 ao_scheme_compare(struct ao_scheme_cons *cons, enum ao_scheme_builtin_id op)
485 {
486         ao_poly left;
487
488         if (!cons)
489                 return _ao_scheme_bool_true;
490
491         left = cons->car;
492         for (cons = ao_scheme_cons_cdr(cons); cons; cons = ao_scheme_cons_cdr(cons)) {
493                 ao_poly right = cons->car;
494
495                 if (op == builtin_equal) {
496                         if (left != right)
497                                 return _ao_scheme_bool_false;
498                 } else {
499                         uint8_t lt = ao_scheme_poly_type(left);
500                         uint8_t rt = ao_scheme_poly_type(right);
501                         if (ao_scheme_integer_typep(lt) && ao_scheme_integer_typep(rt)) {
502                                 int32_t l = ao_scheme_poly_integer(left);
503                                 int32_t r = ao_scheme_poly_integer(right);
504
505                                 switch (op) {
506                                 case builtin_less:
507                                         if (!(l < r))
508                                                 return _ao_scheme_bool_false;
509                                         break;
510                                 case builtin_greater:
511                                         if (!(l > r))
512                                                 return _ao_scheme_bool_false;
513                                         break;
514                                 case builtin_less_equal:
515                                         if (!(l <= r))
516                                                 return _ao_scheme_bool_false;
517                                         break;
518                                 case builtin_greater_equal:
519                                         if (!(l >= r))
520                                                 return _ao_scheme_bool_false;
521                                         break;
522                                 default:
523                                         break;
524                                 }
525                         } else if (lt == AO_SCHEME_STRING && rt == AO_SCHEME_STRING) {
526                                 int c = strcmp(ao_scheme_poly_string(left),
527                                                ao_scheme_poly_string(right));
528                                 switch (op) {
529                                 case builtin_less:
530                                         if (!(c < 0))
531                                                 return _ao_scheme_bool_false;
532                                         break;
533                                 case builtin_greater:
534                                         if (!(c > 0))
535                                                 return _ao_scheme_bool_false;
536                                         break;
537                                 case builtin_less_equal:
538                                         if (!(c <= 0))
539                                                 return _ao_scheme_bool_false;
540                                         break;
541                                 case builtin_greater_equal:
542                                         if (!(c >= 0))
543                                                 return _ao_scheme_bool_false;
544                                         break;
545                                 default:
546                                         break;
547                                 }
548                         }
549                 }
550                 left = right;
551         }
552         return _ao_scheme_bool_true;
553 }
554
555 ao_poly
556 ao_scheme_do_equal(struct ao_scheme_cons *cons)
557 {
558         return ao_scheme_compare(cons, builtin_equal);
559 }
560
561 ao_poly
562 ao_scheme_do_less(struct ao_scheme_cons *cons)
563 {
564         return ao_scheme_compare(cons, builtin_less);
565 }
566
567 ao_poly
568 ao_scheme_do_greater(struct ao_scheme_cons *cons)
569 {
570         return ao_scheme_compare(cons, builtin_greater);
571 }
572
573 ao_poly
574 ao_scheme_do_less_equal(struct ao_scheme_cons *cons)
575 {
576         return ao_scheme_compare(cons, builtin_less_equal);
577 }
578
579 ao_poly
580 ao_scheme_do_greater_equal(struct ao_scheme_cons *cons)
581 {
582         return ao_scheme_compare(cons, builtin_greater_equal);
583 }
584
585 ao_poly
586 ao_scheme_do_list_to_string(struct ao_scheme_cons *cons)
587 {
588         if (!ao_scheme_check_argc(_ao_scheme_atom_list2d3estring, cons, 1, 1))
589                 return AO_SCHEME_NIL;
590         if (!ao_scheme_check_argt(_ao_scheme_atom_list2d3estring, cons, 0, AO_SCHEME_CONS, 1))
591                 return AO_SCHEME_NIL;
592         return ao_scheme_string_pack(ao_scheme_poly_cons(ao_scheme_arg(cons, 0)));
593 }
594
595 ao_poly
596 ao_scheme_do_string_to_list(struct ao_scheme_cons *cons)
597 {
598         if (!ao_scheme_check_argc(_ao_scheme_atom_string2d3elist, cons, 1, 1))
599                 return AO_SCHEME_NIL;
600         if (!ao_scheme_check_argt(_ao_scheme_atom_string2d3elist, cons, 0, AO_SCHEME_STRING, 0))
601                 return AO_SCHEME_NIL;
602         return ao_scheme_string_unpack(ao_scheme_poly_string(ao_scheme_arg(cons, 0)));
603 }
604
605 ao_poly
606 ao_scheme_do_string_ref(struct ao_scheme_cons *cons)
607 {
608         char *string;
609         int32_t ref;
610         if (!ao_scheme_check_argc(_ao_scheme_atom_string2dref, cons, 2, 2))
611                 return AO_SCHEME_NIL;
612         if (!ao_scheme_check_argt(_ao_scheme_atom_string2dref, cons, 0, AO_SCHEME_STRING, 0))
613                 return AO_SCHEME_NIL;
614         ref = ao_scheme_arg_int(_ao_scheme_atom_string2dref, cons, 1);
615         if (ref == AO_SCHEME_NOT_INTEGER)
616                 return AO_SCHEME_NIL;
617         string = ao_scheme_poly_string(ao_scheme_arg(cons, 0));
618         while (*string && ref) {
619                 ++string;
620                 --ref;
621         }
622         if (!*string)
623                 return ao_scheme_error(AO_SCHEME_INVALID, "%v: string %v ref %v invalid",
624                                        _ao_scheme_atom_string2dref,
625                                        ao_scheme_arg(cons, 0),
626                                        ao_scheme_arg(cons, 1));
627         return ao_scheme_int_poly(*string);
628 }
629
630 ao_poly
631 ao_scheme_do_string_length(struct ao_scheme_cons *cons)
632 {
633         char *string;
634
635         if (!ao_scheme_check_argc(_ao_scheme_atom_string2dlength, cons, 1, 1))
636                 return AO_SCHEME_NIL;
637         if (!ao_scheme_check_argt(_ao_scheme_atom_string2dlength, cons, 0, AO_SCHEME_STRING, 0))
638                 return AO_SCHEME_NIL;
639         string = ao_scheme_poly_string(ao_scheme_arg(cons, 0));
640         return ao_scheme_integer_poly(strlen(string));
641 }
642
643 ao_poly
644 ao_scheme_do_string_copy(struct ao_scheme_cons *cons)
645 {
646         char *string;
647
648         if (!ao_scheme_check_argc(_ao_scheme_atom_string2dcopy, cons, 1, 1))
649                 return AO_SCHEME_NIL;
650         if (!ao_scheme_check_argt(_ao_scheme_atom_string2dcopy, cons, 0, AO_SCHEME_STRING, 0))
651                 return AO_SCHEME_NIL;
652         string = ao_scheme_poly_string(ao_scheme_arg(cons, 0));
653         return ao_scheme_string_poly(ao_scheme_string_copy(string));
654 }
655
656 ao_poly
657 ao_scheme_do_string_set(struct ao_scheme_cons *cons)
658 {
659         char *string;
660         int32_t ref;
661         int32_t val;
662
663         if (!ao_scheme_check_argc(_ao_scheme_atom_string2dset21, cons, 3, 3))
664                 return AO_SCHEME_NIL;
665         if (!ao_scheme_check_argt(_ao_scheme_atom_string2dset21, cons, 0, AO_SCHEME_STRING, 0))
666                 return AO_SCHEME_NIL;
667         string = ao_scheme_poly_string(ao_scheme_arg(cons, 0));
668         ref = ao_scheme_arg_int(_ao_scheme_atom_string2dset21, cons, 1);
669         if (ref == AO_SCHEME_NOT_INTEGER)
670                 return AO_SCHEME_NIL;
671         val = ao_scheme_arg_int(_ao_scheme_atom_string2dset21, cons, 2);
672         if (val == AO_SCHEME_NOT_INTEGER)
673                 return AO_SCHEME_NIL;
674         while (*string && ref) {
675                 ++string;
676                 --ref;
677         }
678         if (!*string)
679                 return ao_scheme_error(AO_SCHEME_INVALID, "%v: string %v ref %v invalid",
680                                        _ao_scheme_atom_string2dset21,
681                                        ao_scheme_arg(cons, 0),
682                                        ao_scheme_arg(cons, 1));
683         *string = val;
684         return ao_scheme_int_poly(*string);
685 }
686
687 ao_poly
688 ao_scheme_do_flush_output(struct ao_scheme_cons *cons)
689 {
690         if (!ao_scheme_check_argc(_ao_scheme_atom_flush2doutput, cons, 0, 0))
691                 return AO_SCHEME_NIL;
692         ao_scheme_os_flush();
693         return _ao_scheme_bool_true;
694 }
695
696 ao_poly
697 ao_scheme_do_led(struct ao_scheme_cons *cons)
698 {
699         int32_t led;
700         if (!ao_scheme_check_argc(_ao_scheme_atom_led, cons, 1, 1))
701                 return AO_SCHEME_NIL;
702         led = ao_scheme_arg_int(_ao_scheme_atom_led, cons, 0);
703         if (led == AO_SCHEME_NOT_INTEGER)
704                 return AO_SCHEME_NIL;
705         led = ao_scheme_arg(cons, 0);
706         ao_scheme_os_led(ao_scheme_poly_int(led));
707         return led;
708 }
709
710 ao_poly
711 ao_scheme_do_delay(struct ao_scheme_cons *cons)
712 {
713         int32_t delay;
714
715         if (!ao_scheme_check_argc(_ao_scheme_atom_delay, cons, 1, 1))
716                 return AO_SCHEME_NIL;
717         delay = ao_scheme_arg_int(_ao_scheme_atom_delay, cons, 0);
718         if (delay == AO_SCHEME_NOT_INTEGER)
719                 return AO_SCHEME_NIL;
720         ao_scheme_os_delay(delay);
721         return delay;
722 }
723
724 ao_poly
725 ao_scheme_do_eval(struct ao_scheme_cons *cons)
726 {
727         if (!ao_scheme_check_argc(_ao_scheme_atom_eval, cons, 1, 1))
728                 return AO_SCHEME_NIL;
729         ao_scheme_stack->state = eval_sexpr;
730         return cons->car;
731 }
732
733 ao_poly
734 ao_scheme_do_apply(struct ao_scheme_cons *cons)
735 {
736         if (!ao_scheme_check_argc(_ao_scheme_atom_apply, cons, 2, INT_MAX))
737                 return AO_SCHEME_NIL;
738         ao_scheme_stack->state = eval_apply;
739         return ao_scheme_cons_poly(cons);
740 }
741
742 ao_poly
743 ao_scheme_do_read(struct ao_scheme_cons *cons)
744 {
745         if (!ao_scheme_check_argc(_ao_scheme_atom_read, cons, 0, 0))
746                 return AO_SCHEME_NIL;
747         return ao_scheme_read();
748 }
749
750 ao_poly
751 ao_scheme_do_collect(struct ao_scheme_cons *cons)
752 {
753         int     free;
754         (void) cons;
755         free = ao_scheme_collect(AO_SCHEME_COLLECT_FULL);
756         return ao_scheme_integer_poly(free);
757 }
758
759 ao_poly
760 ao_scheme_do_nullp(struct ao_scheme_cons *cons)
761 {
762         if (!ao_scheme_check_argc(_ao_scheme_atom_led, cons, 1, 1))
763                 return AO_SCHEME_NIL;
764         if (ao_scheme_arg(cons, 0) == AO_SCHEME_NIL)
765                 return _ao_scheme_bool_true;
766         else
767                 return _ao_scheme_bool_false;
768 }
769
770 ao_poly
771 ao_scheme_do_not(struct ao_scheme_cons *cons)
772 {
773         if (!ao_scheme_check_argc(_ao_scheme_atom_led, cons, 1, 1))
774                 return AO_SCHEME_NIL;
775         if (ao_scheme_arg(cons, 0) == _ao_scheme_bool_false)
776                 return _ao_scheme_bool_true;
777         else
778                 return _ao_scheme_bool_false;
779 }
780
781 static ao_poly
782 ao_scheme_do_typep(int type, struct ao_scheme_cons *cons)
783 {
784         if (!ao_scheme_check_argc(_ao_scheme_atom_led, cons, 1, 1))
785                 return AO_SCHEME_NIL;
786         if (ao_scheme_poly_type(ao_scheme_arg(cons, 0)) == type)
787                 return _ao_scheme_bool_true;
788         return _ao_scheme_bool_false;
789 }
790
791 ao_poly
792 ao_scheme_do_pairp(struct ao_scheme_cons *cons)
793 {
794         ao_poly v;
795         if (!ao_scheme_check_argc(_ao_scheme_atom_led, cons, 1, 1))
796                 return AO_SCHEME_NIL;
797         v = ao_scheme_arg(cons, 0);
798         if (v != AO_SCHEME_NIL && ao_scheme_poly_type(v) == AO_SCHEME_CONS)
799                 return _ao_scheme_bool_true;
800         return _ao_scheme_bool_false;
801 }
802
803 ao_poly
804 ao_scheme_do_integerp(struct ao_scheme_cons *cons)
805 {
806         if (!ao_scheme_check_argc(_ao_scheme_atom_led, cons, 1, 1))
807                 return AO_SCHEME_NIL;
808         switch (ao_scheme_poly_type(ao_scheme_arg(cons, 0))) {
809         case AO_SCHEME_INT:
810         case AO_SCHEME_BIGINT:
811                 return _ao_scheme_bool_true;
812         default:
813                 return _ao_scheme_bool_false;
814         }
815 }
816
817 ao_poly
818 ao_scheme_do_numberp(struct ao_scheme_cons *cons)
819 {
820         if (!ao_scheme_check_argc(_ao_scheme_atom_led, cons, 1, 1))
821                 return AO_SCHEME_NIL;
822         switch (ao_scheme_poly_type(ao_scheme_arg(cons, 0))) {
823         case AO_SCHEME_INT:
824         case AO_SCHEME_BIGINT:
825         case AO_SCHEME_FLOAT:
826                 return _ao_scheme_bool_true;
827         default:
828                 return _ao_scheme_bool_false;
829         }
830 }
831
832 ao_poly
833 ao_scheme_do_stringp(struct ao_scheme_cons *cons)
834 {
835         return ao_scheme_do_typep(AO_SCHEME_STRING, cons);
836 }
837
838 ao_poly
839 ao_scheme_do_symbolp(struct ao_scheme_cons *cons)
840 {
841         return ao_scheme_do_typep(AO_SCHEME_ATOM, cons);
842 }
843
844 ao_poly
845 ao_scheme_do_booleanp(struct ao_scheme_cons *cons)
846 {
847         return ao_scheme_do_typep(AO_SCHEME_BOOL, cons);
848 }
849
850 ao_poly
851 ao_scheme_do_procedurep(struct ao_scheme_cons *cons)
852 {
853         if (!ao_scheme_check_argc(_ao_scheme_atom_led, cons, 1, 1))
854                 return AO_SCHEME_NIL;
855         switch (ao_scheme_poly_type(ao_scheme_arg(cons, 0))) {
856         case AO_SCHEME_BUILTIN:
857         case AO_SCHEME_LAMBDA:
858                 return _ao_scheme_bool_true;
859         default:
860         return _ao_scheme_bool_false;
861         }
862 }
863
864 /* This one is special -- a list is either nil or
865  * a 'proper' list with only cons cells
866  */
867 ao_poly
868 ao_scheme_do_listp(struct ao_scheme_cons *cons)
869 {
870         ao_poly v;
871         if (!ao_scheme_check_argc(_ao_scheme_atom_list3f, cons, 1, 1))
872                 return AO_SCHEME_NIL;
873         v = ao_scheme_arg(cons, 0);
874         for (;;) {
875                 if (v == AO_SCHEME_NIL)
876                         return _ao_scheme_bool_true;
877                 if (ao_scheme_poly_type(v) != AO_SCHEME_CONS)
878                         return _ao_scheme_bool_false;
879                 v = ao_scheme_poly_cons(v)->cdr;
880         }
881 }
882
883 ao_poly
884 ao_scheme_do_set_car(struct ao_scheme_cons *cons)
885 {
886         if (!ao_scheme_check_argc(_ao_scheme_atom_led, cons, 2, 2))
887                 return AO_SCHEME_NIL;
888         if (!ao_scheme_check_argt(_ao_scheme_atom_led, cons, 0, AO_SCHEME_CONS, 0))
889                 return AO_SCHEME_NIL;
890         return ao_scheme_poly_cons(ao_scheme_arg(cons, 0))->car = ao_scheme_arg(cons, 1);
891 }
892
893 ao_poly
894 ao_scheme_do_set_cdr(struct ao_scheme_cons *cons)
895 {
896         if (!ao_scheme_check_argc(_ao_scheme_atom_led, cons, 2, 2))
897                 return AO_SCHEME_NIL;
898         if (!ao_scheme_check_argt(_ao_scheme_atom_led, cons, 0, AO_SCHEME_CONS, 0))
899                 return AO_SCHEME_NIL;
900         return ao_scheme_poly_cons(ao_scheme_arg(cons, 0))->cdr = ao_scheme_arg(cons, 1);
901 }
902
903 ao_poly
904 ao_scheme_do_symbol_to_string(struct ao_scheme_cons *cons)
905 {
906         if (!ao_scheme_check_argc(_ao_scheme_atom_led, cons, 1, 1))
907                 return AO_SCHEME_NIL;
908         if (!ao_scheme_check_argt(_ao_scheme_atom_led, cons, 0, AO_SCHEME_ATOM, 0))
909                 return AO_SCHEME_NIL;
910         return ao_scheme_string_poly(ao_scheme_string_copy(ao_scheme_poly_atom(ao_scheme_arg(cons, 0))->name));
911 }
912
913 ao_poly
914 ao_scheme_do_string_to_symbol(struct ao_scheme_cons *cons)
915 {
916         if (!ao_scheme_check_argc(_ao_scheme_atom_led, cons, 1, 1))
917                 return AO_SCHEME_NIL;
918         if (!ao_scheme_check_argt(_ao_scheme_atom_led, cons, 0, AO_SCHEME_STRING, 0))
919                 return AO_SCHEME_NIL;
920
921         return ao_scheme_atom_poly(ao_scheme_atom_intern(ao_scheme_poly_string(ao_scheme_arg(cons, 0))));
922 }
923
924 ao_poly
925 ao_scheme_do_read_char(struct ao_scheme_cons *cons)
926 {
927         int     c;
928         if (!ao_scheme_check_argc(_ao_scheme_atom_led, cons, 0, 0))
929                 return AO_SCHEME_NIL;
930         c = getchar();
931         return ao_scheme_int_poly(c);
932 }
933
934 ao_poly
935 ao_scheme_do_write_char(struct ao_scheme_cons *cons)
936 {
937         if (!ao_scheme_check_argc(_ao_scheme_atom_led, cons, 1, 1))
938                 return AO_SCHEME_NIL;
939         if (!ao_scheme_check_argt(_ao_scheme_atom_led, cons, 0, AO_SCHEME_INT, 0))
940                 return AO_SCHEME_NIL;
941         putchar(ao_scheme_poly_integer(ao_scheme_arg(cons, 0)));
942         return _ao_scheme_bool_true;
943 }
944
945 ao_poly
946 ao_scheme_do_exit(struct ao_scheme_cons *cons)
947 {
948         if (!ao_scheme_check_argc(_ao_scheme_atom_led, cons, 0, 0))
949                 return AO_SCHEME_NIL;
950         ao_scheme_exception |= AO_SCHEME_EXIT;
951         return _ao_scheme_bool_true;
952 }
953
954 ao_poly
955 ao_scheme_do_current_jiffy(struct ao_scheme_cons *cons)
956 {
957         int     jiffy;
958
959         if (!ao_scheme_check_argc(_ao_scheme_atom_led, cons, 0, 0))
960                 return AO_SCHEME_NIL;
961         jiffy = ao_scheme_os_jiffy();
962         return (ao_scheme_int_poly(jiffy));
963 }
964
965 ao_poly
966 ao_scheme_do_current_second(struct ao_scheme_cons *cons)
967 {
968         int     second;
969
970         if (!ao_scheme_check_argc(_ao_scheme_atom_led, cons, 0, 0))
971                 return AO_SCHEME_NIL;
972         second = ao_scheme_os_jiffy() / AO_SCHEME_JIFFIES_PER_SECOND;
973         return (ao_scheme_int_poly(second));
974 }
975
976 ao_poly
977 ao_scheme_do_jiffies_per_second(struct ao_scheme_cons *cons)
978 {
979         if (!ao_scheme_check_argc(_ao_scheme_atom_led, cons, 0, 0))
980                 return AO_SCHEME_NIL;
981         return (ao_scheme_int_poly(AO_SCHEME_JIFFIES_PER_SECOND));
982 }
983
984 ao_poly
985 ao_scheme_do_vector(struct ao_scheme_cons *cons)
986 {
987         return ao_scheme_vector_poly(ao_scheme_list_to_vector(cons));
988 }
989
990 ao_poly
991 ao_scheme_do_make_vector(struct ao_scheme_cons *cons)
992 {
993         int32_t k;
994
995         if (!ao_scheme_check_argc(_ao_scheme_atom_make2dvector, cons, 2, 2))
996                 return AO_SCHEME_NIL;
997         k = ao_scheme_arg_int(_ao_scheme_atom_make2dvector, cons, 0);
998         if (k == AO_SCHEME_NOT_INTEGER)
999                 return AO_SCHEME_NIL;
1000         return ao_scheme_vector_poly(ao_scheme_vector_alloc(k, ao_scheme_arg(cons, 1)));
1001 }
1002
1003 ao_poly
1004 ao_scheme_do_vector_ref(struct ao_scheme_cons *cons)
1005 {
1006         if (!ao_scheme_check_argc(_ao_scheme_atom_vector2dref, cons, 2, 2))
1007                 return AO_SCHEME_NIL;
1008         if (!ao_scheme_check_argt(_ao_scheme_atom_vector2dref, cons, 0, AO_SCHEME_VECTOR, 0))
1009                 return AO_SCHEME_NIL;
1010         return ao_scheme_vector_get(ao_scheme_arg(cons, 0), ao_scheme_arg(cons, 1));
1011 }
1012
1013 ao_poly
1014 ao_scheme_do_vector_set(struct ao_scheme_cons *cons)
1015 {
1016         if (!ao_scheme_check_argc(_ao_scheme_atom_vector2dset21, cons, 3, 3))
1017                 return AO_SCHEME_NIL;
1018         if (!ao_scheme_check_argt(_ao_scheme_atom_vector2dset21, cons, 0, AO_SCHEME_VECTOR, 0))
1019                 return AO_SCHEME_NIL;
1020         return ao_scheme_vector_set(ao_scheme_arg(cons, 0), ao_scheme_arg(cons, 1), ao_scheme_arg(cons, 2));
1021 }
1022
1023 ao_poly
1024 ao_scheme_do_list_to_vector(struct ao_scheme_cons *cons)
1025 {
1026         if (!ao_scheme_check_argc(_ao_scheme_atom_list2d3evector, cons, 1, 1))
1027                 return AO_SCHEME_NIL;
1028         if (!ao_scheme_check_argt(_ao_scheme_atom_list2d3evector, cons, 0, AO_SCHEME_CONS, 0))
1029                 return AO_SCHEME_NIL;
1030         return ao_scheme_vector_poly(ao_scheme_list_to_vector(ao_scheme_poly_cons(ao_scheme_arg(cons, 0))));
1031 }
1032
1033 ao_poly
1034 ao_scheme_do_vector_to_list(struct ao_scheme_cons *cons)
1035 {
1036         if (!ao_scheme_check_argc(_ao_scheme_atom_vector2d3elist, cons, 1, 1))
1037                 return AO_SCHEME_NIL;
1038         if (!ao_scheme_check_argt(_ao_scheme_atom_vector2d3elist, cons, 0, AO_SCHEME_VECTOR, 0))
1039                 return AO_SCHEME_NIL;
1040         return ao_scheme_cons_poly(ao_scheme_vector_to_list(ao_scheme_poly_vector(ao_scheme_arg(cons, 0))));
1041 }
1042
1043 ao_poly
1044 ao_scheme_do_vector_length(struct ao_scheme_cons *cons)
1045 {
1046         if (!ao_scheme_check_argc(_ao_scheme_atom_vector2d3elist, cons, 1, 1))
1047                 return AO_SCHEME_NIL;
1048         if (!ao_scheme_check_argt(_ao_scheme_atom_vector2d3elist, cons, 0, AO_SCHEME_VECTOR, 0))
1049                 return AO_SCHEME_NIL;
1050         return ao_scheme_integer_poly(ao_scheme_poly_vector(ao_scheme_arg(cons, 0))->length);
1051 }
1052
1053 ao_poly
1054 ao_scheme_do_vectorp(struct ao_scheme_cons *cons)
1055 {
1056         return ao_scheme_do_typep(AO_SCHEME_VECTOR, cons);
1057 }
1058
1059 #define AO_SCHEME_BUILTIN_FUNCS
1060 #include "ao_scheme_builtin.h"