2 * Copyright © 2016 Keith Packard <keithp@keithp.com>
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.
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.
15 #include "ao_scheme.h"
20 builtin_size(void *addr)
23 return sizeof (struct ao_scheme_builtin);
27 builtin_mark(void *addr)
33 builtin_move(void *addr)
38 const struct ao_scheme_type ao_scheme_builtin_type = {
44 #ifdef AO_SCHEME_MAKE_CONST
46 #define AO_SCHEME_BUILTIN_CASENAME
47 #include "ao_scheme_builtin.h"
49 char *ao_scheme_args_name(uint8_t args) {
50 args &= AO_SCHEME_FUNC_MASK;
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 "???";
60 #define AO_SCHEME_BUILTIN_ARRAYNAME
61 #include "ao_scheme_builtin.h"
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;
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,
77 ao_scheme_args_name(uint8_t args)
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;
87 ao_scheme_builtin_write(ao_poly b)
89 struct ao_scheme_builtin *builtin = ao_scheme_poly_builtin(b);
90 printf("%s", ao_scheme_builtin_name(builtin->func));
94 ao_scheme_check_argc(ao_poly name, struct ao_scheme_cons *cons, int min, int max)
98 while (cons && argc <= max) {
100 cons = ao_scheme_cons_cdr(cons);
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;
108 ao_scheme_arg(struct ao_scheme_cons *cons, int argc)
111 return AO_SCHEME_NIL;
114 return AO_SCHEME_NIL;
115 cons = ao_scheme_cons_cdr(cons);
121 ao_scheme_check_argt(ao_poly name, struct ao_scheme_cons *cons, int argc, int type, int nil_ok)
123 ao_poly car = ao_scheme_arg(cons, argc);
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;
131 ao_scheme_arg_int(ao_poly name, struct ao_scheme_cons *cons, int argc)
133 ao_poly p = ao_scheme_arg(cons, argc);
134 int32_t i = ao_scheme_poly_integer(p);
136 if (i == AO_SCHEME_NOT_INTEGER)
137 (void) ao_scheme_error(AO_SCHEME_INVALID, "%v: arg %d invalid type %v", name, argc, p);
142 ao_scheme_do_car(struct ao_scheme_cons *cons)
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;
152 ao_scheme_do_cdr(struct ao_scheme_cons *cons)
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;
162 ao_scheme_do_cons(struct ao_scheme_cons *cons)
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);
173 ao_scheme_do_last(struct ao_scheme_cons *cons)
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));
182 list = ao_scheme_cons_cdr(list))
187 return AO_SCHEME_NIL;
191 ao_scheme_do_length(struct ao_scheme_cons *cons)
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))));
201 ao_scheme_do_list_copy(struct ao_scheme_cons *cons)
203 struct ao_scheme_cons *new;
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);
214 ao_scheme_do_quote(struct ao_scheme_cons *cons)
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);
222 ao_scheme_do_set(struct ao_scheme_cons *cons)
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;
229 return ao_scheme_atom_set(ao_scheme_arg(cons, 0), ao_scheme_arg(cons, 1));
233 ao_scheme_do_def(struct ao_scheme_cons *cons)
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;
240 return ao_scheme_atom_def(ao_scheme_arg(cons, 0), ao_scheme_arg(cons, 1));
244 ao_scheme_do_setq(struct ao_scheme_cons *cons)
247 if (!ao_scheme_check_argc(_ao_scheme_atom_set21, cons, 2, 2))
248 return AO_SCHEME_NIL;
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)),
261 ao_scheme_do_cond(struct ao_scheme_cons *cons)
263 ao_scheme_set_cond(cons);
264 return AO_SCHEME_NIL;
268 ao_scheme_do_begin(struct ao_scheme_cons *cons)
270 ao_scheme_stack->state = eval_begin;
271 ao_scheme_stack->sexprs = ao_scheme_cons_poly(cons);
272 return AO_SCHEME_NIL;
276 ao_scheme_do_while(struct ao_scheme_cons *cons)
278 ao_scheme_stack->state = eval_while;
279 ao_scheme_stack->sexprs = ao_scheme_cons_poly(cons);
280 return AO_SCHEME_NIL;
284 ao_scheme_do_write(struct ao_scheme_cons *cons)
286 ao_poly val = AO_SCHEME_NIL;
289 ao_scheme_poly_write(val);
290 cons = ao_scheme_cons_cdr(cons);
294 return _ao_scheme_bool_true;
298 ao_scheme_do_display(struct ao_scheme_cons *cons)
300 ao_poly val = AO_SCHEME_NIL;
303 ao_scheme_poly_display(val);
304 cons = ao_scheme_cons_cdr(cons);
306 return _ao_scheme_bool_true;
310 ao_scheme_math(struct ao_scheme_cons *orig_cons, enum ao_scheme_builtin_id op)
312 struct ao_scheme_cons *cons = cons;
313 ao_poly ret = AO_SCHEME_NIL;
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);
320 if (cons == orig_cons) {
322 ao_scheme_cons_stash(0, cons);
323 if (cons->cdr == AO_SCHEME_NIL) {
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));
332 if (ao_scheme_integer_typep(ct) && ao_scheme_poly_integer(ret) == 1)
334 else if (ao_scheme_number_typep(ct)) {
335 float v = ao_scheme_poly_number(ret);
336 ret = ao_scheme_float_get(1/v);
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);
353 if (r < AO_SCHEME_MIN_BIGINT || AO_SCHEME_MAX_BIGINT < r)
361 t = (int64_t) r * (int64_t) c;
362 if (t < AO_SCHEME_MIN_BIGINT || AO_SCHEME_MAX_BIGINT < t)
367 if (c != 0 && (r % c) == 0)
372 case builtin_quotient:
374 return ao_scheme_error(AO_SCHEME_DIVIDE_BY_ZERO, "quotient by zero");
375 if (r % c != 0 && (c < 0) != (r < 0))
380 case builtin_remainder:
382 return ao_scheme_error(AO_SCHEME_DIVIDE_BY_ZERO, "remainder by zero");
387 return ao_scheme_error(AO_SCHEME_DIVIDE_BY_ZERO, "modulo by zero");
389 if ((r < 0) != (c < 0))
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)) {
401 r = ao_scheme_poly_number(ret);
402 c = ao_scheme_poly_number(car);
416 case builtin_quotient:
417 case builtin_remainder:
419 return ao_scheme_error(AO_SCHEME_INVALID, "non-integer value in integer divide");
423 ao_scheme_cons_stash(0, cons);
424 ret = ao_scheme_float_get(r);
425 cons = ao_scheme_cons_fetch(0);
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);
436 return ao_scheme_error(AO_SCHEME_INVALID, "invalid args");
442 ao_scheme_do_plus(struct ao_scheme_cons *cons)
444 return ao_scheme_math(cons, builtin_plus);
448 ao_scheme_do_minus(struct ao_scheme_cons *cons)
450 return ao_scheme_math(cons, builtin_minus);
454 ao_scheme_do_times(struct ao_scheme_cons *cons)
456 return ao_scheme_math(cons, builtin_times);
460 ao_scheme_do_divide(struct ao_scheme_cons *cons)
462 return ao_scheme_math(cons, builtin_divide);
466 ao_scheme_do_quotient(struct ao_scheme_cons *cons)
468 return ao_scheme_math(cons, builtin_quotient);
472 ao_scheme_do_modulo(struct ao_scheme_cons *cons)
474 return ao_scheme_math(cons, builtin_modulo);
478 ao_scheme_do_remainder(struct ao_scheme_cons *cons)
480 return ao_scheme_math(cons, builtin_remainder);
484 ao_scheme_compare(struct ao_scheme_cons *cons, enum ao_scheme_builtin_id op)
489 return _ao_scheme_bool_true;
492 for (cons = ao_scheme_cons_cdr(cons); cons; cons = ao_scheme_cons_cdr(cons)) {
493 ao_poly right = cons->car;
495 if (op == builtin_equal) {
497 return _ao_scheme_bool_false;
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);
508 return _ao_scheme_bool_false;
510 case builtin_greater:
512 return _ao_scheme_bool_false;
514 case builtin_less_equal:
516 return _ao_scheme_bool_false;
518 case builtin_greater_equal:
520 return _ao_scheme_bool_false;
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));
531 return _ao_scheme_bool_false;
533 case builtin_greater:
535 return _ao_scheme_bool_false;
537 case builtin_less_equal:
539 return _ao_scheme_bool_false;
541 case builtin_greater_equal:
543 return _ao_scheme_bool_false;
552 return _ao_scheme_bool_true;
556 ao_scheme_do_equal(struct ao_scheme_cons *cons)
558 return ao_scheme_compare(cons, builtin_equal);
562 ao_scheme_do_less(struct ao_scheme_cons *cons)
564 return ao_scheme_compare(cons, builtin_less);
568 ao_scheme_do_greater(struct ao_scheme_cons *cons)
570 return ao_scheme_compare(cons, builtin_greater);
574 ao_scheme_do_less_equal(struct ao_scheme_cons *cons)
576 return ao_scheme_compare(cons, builtin_less_equal);
580 ao_scheme_do_greater_equal(struct ao_scheme_cons *cons)
582 return ao_scheme_compare(cons, builtin_greater_equal);
586 ao_scheme_do_list_to_string(struct ao_scheme_cons *cons)
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)));
596 ao_scheme_do_string_to_list(struct ao_scheme_cons *cons)
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)));
606 ao_scheme_do_string_ref(struct ao_scheme_cons *cons)
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) {
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);
631 ao_scheme_do_string_length(struct ao_scheme_cons *cons)
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));
644 ao_scheme_do_string_copy(struct ao_scheme_cons *cons)
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));
657 ao_scheme_do_string_set(struct ao_scheme_cons *cons)
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) {
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));
684 return ao_scheme_int_poly(*string);
688 ao_scheme_do_flush_output(struct ao_scheme_cons *cons)
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;
697 ao_scheme_do_led(struct ao_scheme_cons *cons)
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));
711 ao_scheme_do_delay(struct ao_scheme_cons *cons)
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);
725 ao_scheme_do_eval(struct ao_scheme_cons *cons)
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;
734 ao_scheme_do_apply(struct ao_scheme_cons *cons)
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);
743 ao_scheme_do_read(struct ao_scheme_cons *cons)
745 if (!ao_scheme_check_argc(_ao_scheme_atom_read, cons, 0, 0))
746 return AO_SCHEME_NIL;
747 return ao_scheme_read();
751 ao_scheme_do_collect(struct ao_scheme_cons *cons)
755 free = ao_scheme_collect(AO_SCHEME_COLLECT_FULL);
756 return ao_scheme_integer_poly(free);
760 ao_scheme_do_nullp(struct ao_scheme_cons *cons)
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;
767 return _ao_scheme_bool_false;
771 ao_scheme_do_not(struct ao_scheme_cons *cons)
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;
778 return _ao_scheme_bool_false;
782 ao_scheme_do_typep(int type, struct ao_scheme_cons *cons)
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;
792 ao_scheme_do_pairp(struct ao_scheme_cons *cons)
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;
804 ao_scheme_do_integerp(struct ao_scheme_cons *cons)
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))) {
810 case AO_SCHEME_BIGINT:
811 return _ao_scheme_bool_true;
813 return _ao_scheme_bool_false;
818 ao_scheme_do_numberp(struct ao_scheme_cons *cons)
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))) {
824 case AO_SCHEME_BIGINT:
825 case AO_SCHEME_FLOAT:
826 return _ao_scheme_bool_true;
828 return _ao_scheme_bool_false;
833 ao_scheme_do_stringp(struct ao_scheme_cons *cons)
835 return ao_scheme_do_typep(AO_SCHEME_STRING, cons);
839 ao_scheme_do_symbolp(struct ao_scheme_cons *cons)
841 return ao_scheme_do_typep(AO_SCHEME_ATOM, cons);
845 ao_scheme_do_booleanp(struct ao_scheme_cons *cons)
847 return ao_scheme_do_typep(AO_SCHEME_BOOL, cons);
851 ao_scheme_do_procedurep(struct ao_scheme_cons *cons)
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;
860 return _ao_scheme_bool_false;
864 /* This one is special -- a list is either nil or
865 * a 'proper' list with only cons cells
868 ao_scheme_do_listp(struct ao_scheme_cons *cons)
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);
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;
884 ao_scheme_do_set_car(struct ao_scheme_cons *cons)
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);
894 ao_scheme_do_set_cdr(struct ao_scheme_cons *cons)
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);
904 ao_scheme_do_symbol_to_string(struct ao_scheme_cons *cons)
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));
914 ao_scheme_do_string_to_symbol(struct ao_scheme_cons *cons)
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;
921 return ao_scheme_atom_poly(ao_scheme_atom_intern(ao_scheme_poly_string(ao_scheme_arg(cons, 0))));
925 ao_scheme_do_read_char(struct ao_scheme_cons *cons)
928 if (!ao_scheme_check_argc(_ao_scheme_atom_led, cons, 0, 0))
929 return AO_SCHEME_NIL;
931 return ao_scheme_int_poly(c);
935 ao_scheme_do_write_char(struct ao_scheme_cons *cons)
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;
946 ao_scheme_do_exit(struct ao_scheme_cons *cons)
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;
955 ao_scheme_do_current_jiffy(struct ao_scheme_cons *cons)
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));
966 ao_scheme_do_current_second(struct ao_scheme_cons *cons)
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));
977 ao_scheme_do_jiffies_per_second(struct ao_scheme_cons *cons)
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));
985 ao_scheme_do_vector(struct ao_scheme_cons *cons)
987 return ao_scheme_vector_poly(ao_scheme_list_to_vector(cons));
991 ao_scheme_do_make_vector(struct ao_scheme_cons *cons)
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)));
1004 ao_scheme_do_vector_ref(struct ao_scheme_cons *cons)
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));
1014 ao_scheme_do_vector_set(struct ao_scheme_cons *cons)
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));
1024 ao_scheme_do_list_to_vector(struct ao_scheme_cons *cons)
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))));
1034 ao_scheme_do_vector_to_list(struct ao_scheme_cons *cons)
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))));
1044 ao_scheme_do_vector_length(struct ao_scheme_cons *cons)
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);
1054 ao_scheme_do_vectorp(struct ao_scheme_cons *cons)
1056 return ao_scheme_do_typep(AO_SCHEME_VECTOR, cons);
1059 #define AO_SCHEME_BUILTIN_FUNCS
1060 #include "ao_scheme_builtin.h"