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 (char *) "???";
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;
67 return (char *) "???";
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;
82 return (char *) "(unknown)";
87 ao_scheme_builtin_write(ao_poly b, bool write)
89 struct ao_scheme_builtin *builtin = ao_scheme_poly_builtin(b);
91 printf("%s", ao_scheme_builtin_name(builtin->func));
95 ao_scheme_check_argc(ao_poly name, struct ao_scheme_cons *cons, int min, int max)
99 while (cons && argc <= max) {
101 cons = ao_scheme_cons_cdr(cons);
103 if (argc < min || argc > max)
104 return ao_scheme_error(AO_SCHEME_INVALID, "%s: invalid arg count", ao_scheme_poly_atom(name)->name);
105 return _ao_scheme_bool_true;
109 ao_scheme_arg(struct ao_scheme_cons *cons, int argc)
112 return AO_SCHEME_NIL;
115 return AO_SCHEME_NIL;
116 cons = ao_scheme_cons_cdr(cons);
122 ao_scheme_check_argt(ao_poly name, struct ao_scheme_cons *cons, int argc, int type, int nil_ok)
124 ao_poly car = ao_scheme_arg(cons, argc);
126 if ((!car && !nil_ok) || ao_scheme_poly_type(car) != type)
127 return ao_scheme_error(AO_SCHEME_INVALID, "%v: arg %d invalid type %v", name, argc, car);
128 return _ao_scheme_bool_true;
132 ao_scheme_arg_int(ao_poly name, struct ao_scheme_cons *cons, int argc)
134 ao_poly p = ao_scheme_arg(cons, argc);
136 int32_t i = ao_scheme_poly_integer(p, &fail);
139 (void) ao_scheme_error(AO_SCHEME_INVALID, "%v: arg %d invalid type %v", name, argc, p);
144 ao_scheme_do_car(struct ao_scheme_cons *cons)
146 if (!ao_scheme_check_argc(_ao_scheme_atom_car, cons, 1, 1))
147 return AO_SCHEME_NIL;
148 if (!ao_scheme_check_argt(_ao_scheme_atom_car, cons, 0, AO_SCHEME_CONS, 0))
149 return AO_SCHEME_NIL;
150 return ao_scheme_poly_cons(cons->car)->car;
154 ao_scheme_do_cdr(struct ao_scheme_cons *cons)
156 if (!ao_scheme_check_argc(_ao_scheme_atom_cdr, cons, 1, 1))
157 return AO_SCHEME_NIL;
158 if (!ao_scheme_check_argt(_ao_scheme_atom_cdr, cons, 0, AO_SCHEME_CONS, 0))
159 return AO_SCHEME_NIL;
160 return ao_scheme_poly_cons(cons->car)->cdr;
164 ao_scheme_do_cons(struct ao_scheme_cons *cons)
167 if(!ao_scheme_check_argc(_ao_scheme_atom_cons, cons, 2, 2))
168 return AO_SCHEME_NIL;
169 car = ao_scheme_arg(cons, 0);
170 cdr = ao_scheme_arg(cons, 1);
171 return ao_scheme_cons(car, cdr);
175 ao_scheme_do_last(struct ao_scheme_cons *cons)
177 struct ao_scheme_cons *list;
178 if (!ao_scheme_check_argc(_ao_scheme_atom_last, cons, 1, 1))
179 return AO_SCHEME_NIL;
180 if (!ao_scheme_check_argt(_ao_scheme_atom_last, cons, 0, AO_SCHEME_CONS, 1))
181 return AO_SCHEME_NIL;
182 for (list = ao_scheme_poly_cons(ao_scheme_arg(cons, 0));
184 list = ao_scheme_cons_cdr(list))
189 return AO_SCHEME_NIL;
193 ao_scheme_do_length(struct ao_scheme_cons *cons)
195 if (!ao_scheme_check_argc(_ao_scheme_atom_length, cons, 1, 1))
196 return AO_SCHEME_NIL;
197 if (!ao_scheme_check_argt(_ao_scheme_atom_length, cons, 0, AO_SCHEME_CONS, 1))
198 return AO_SCHEME_NIL;
199 return ao_scheme_int_poly(ao_scheme_cons_length(ao_scheme_poly_cons(ao_scheme_arg(cons, 0))));
203 ao_scheme_do_list_copy(struct ao_scheme_cons *cons)
205 struct ao_scheme_cons *new;
207 if (!ao_scheme_check_argc(_ao_scheme_atom_length, cons, 1, 1))
208 return AO_SCHEME_NIL;
209 if (!ao_scheme_check_argt(_ao_scheme_atom_length, cons, 0, AO_SCHEME_CONS, 1))
210 return AO_SCHEME_NIL;
211 new = ao_scheme_cons_copy(ao_scheme_poly_cons(ao_scheme_arg(cons, 0)));
212 return ao_scheme_cons_poly(new);
216 ao_scheme_do_quote(struct ao_scheme_cons *cons)
218 if (!ao_scheme_check_argc(_ao_scheme_atom_quote, cons, 1, 1))
219 return AO_SCHEME_NIL;
220 return ao_scheme_arg(cons, 0);
224 ao_scheme_do_set(struct ao_scheme_cons *cons)
226 if (!ao_scheme_check_argc(_ao_scheme_atom_set, cons, 2, 2))
227 return AO_SCHEME_NIL;
228 if (!ao_scheme_check_argt(_ao_scheme_atom_set, cons, 0, AO_SCHEME_ATOM, 0))
229 return AO_SCHEME_NIL;
231 return ao_scheme_atom_set(ao_scheme_arg(cons, 0), ao_scheme_arg(cons, 1));
235 ao_scheme_do_def(struct ao_scheme_cons *cons)
237 if (!ao_scheme_check_argc(_ao_scheme_atom_def, cons, 2, 2))
238 return AO_SCHEME_NIL;
239 if (!ao_scheme_check_argt(_ao_scheme_atom_def, cons, 0, AO_SCHEME_ATOM, 0))
240 return AO_SCHEME_NIL;
242 return ao_scheme_atom_def(ao_scheme_arg(cons, 0), ao_scheme_arg(cons, 1));
246 ao_scheme_do_setq(struct ao_scheme_cons *cons)
249 if (!ao_scheme_check_argc(_ao_scheme_atom_set21, cons, 2, 2))
250 return AO_SCHEME_NIL;
252 if (ao_scheme_poly_type(name) != AO_SCHEME_ATOM)
253 return ao_scheme_error(AO_SCHEME_INVALID, "set! of non-atom %v", name);
254 if (!ao_scheme_atom_ref(name, NULL))
255 return ao_scheme_error(AO_SCHEME_INVALID, "atom %v not defined", name);
256 return ao_scheme_cons(_ao_scheme_atom_set,
257 ao_scheme_cons(ao_scheme_cons(_ao_scheme_atom_quote,
258 ao_scheme_cons(name, AO_SCHEME_NIL)),
263 ao_scheme_do_cond(struct ao_scheme_cons *cons)
265 ao_scheme_set_cond(cons);
266 return AO_SCHEME_NIL;
270 ao_scheme_do_begin(struct ao_scheme_cons *cons)
272 ao_scheme_stack->state = eval_begin;
273 ao_scheme_stack->sexprs = ao_scheme_cons_poly(cons);
274 return AO_SCHEME_NIL;
278 ao_scheme_do_while(struct ao_scheme_cons *cons)
280 ao_scheme_stack->state = eval_while;
281 ao_scheme_stack->sexprs = ao_scheme_cons_poly(cons);
282 return AO_SCHEME_NIL;
286 ao_scheme_do_write(struct ao_scheme_cons *cons)
288 ao_poly val = AO_SCHEME_NIL;
291 ao_scheme_poly_write(val, true);
292 cons = ao_scheme_cons_cdr(cons);
296 return _ao_scheme_bool_true;
300 ao_scheme_do_display(struct ao_scheme_cons *cons)
302 ao_poly val = AO_SCHEME_NIL;
305 ao_scheme_poly_write(val, false);
306 cons = ao_scheme_cons_cdr(cons);
308 return _ao_scheme_bool_true;
312 ao_scheme_math(struct ao_scheme_cons *orig_cons, enum ao_scheme_builtin_id op)
314 struct ao_scheme_cons *cons;
315 ao_poly ret = AO_SCHEME_NIL;
317 for (cons = orig_cons; cons; cons = ao_scheme_cons_cdr(cons)) {
318 ao_poly car = cons->car;
319 uint8_t rt = ao_scheme_poly_type(ret);
320 uint8_t ct = ao_scheme_poly_type(car);
322 if (cons == orig_cons) {
324 ao_scheme_cons_stash(cons);
325 if (cons->cdr == AO_SCHEME_NIL) {
328 if (ao_scheme_integer_typep(ct))
329 ret = ao_scheme_integer_poly(-ao_scheme_poly_integer(ret, NULL));
330 #ifdef AO_SCHEME_FEATURE_FLOAT
331 else if (ct == AO_SCHEME_FLOAT)
332 ret = ao_scheme_float_get(-ao_scheme_poly_number(ret));
336 if (ao_scheme_poly_integer(ret, NULL) == 1) {
338 #ifdef AO_SCHEME_FEATURE_FLOAT
339 if (ao_scheme_number_typep(ct)) {
340 float v = ao_scheme_poly_number(ret);
341 ret = ao_scheme_float_get(1/v);
344 ret = ao_scheme_integer_poly(0);
352 cons = ao_scheme_cons_fetch();
353 } else if (ao_scheme_integer_typep(rt) && ao_scheme_integer_typep(ct)) {
354 int32_t r = ao_scheme_poly_integer(ret, NULL);
355 int32_t c = ao_scheme_poly_integer(car, NULL);
356 #ifdef AO_SCHEME_FEATURE_FLOAT
364 #ifdef AO_SCHEME_FEATURE_FLOAT
365 if (r < AO_SCHEME_MIN_BIGINT || AO_SCHEME_MAX_BIGINT < r)
374 #ifdef AO_SCHEME_FEATURE_FLOAT
375 t = (int64_t) r * (int64_t) c;
376 if (t < AO_SCHEME_MIN_BIGINT || AO_SCHEME_MAX_BIGINT < t)
384 #ifdef AO_SCHEME_FEATURE_FLOAT
385 if (c != 0 && (r % c) == 0)
393 case builtin_quotient:
395 return ao_scheme_error(AO_SCHEME_DIVIDE_BY_ZERO, "quotient by zero");
396 if (r % c != 0 && (c < 0) != (r < 0))
401 case builtin_remainder:
403 return ao_scheme_error(AO_SCHEME_DIVIDE_BY_ZERO, "remainder by zero");
408 return ao_scheme_error(AO_SCHEME_DIVIDE_BY_ZERO, "modulo by zero");
410 if ((r < 0) != (c < 0))
416 ao_scheme_cons_stash(cons);
417 ret = ao_scheme_integer_poly(r);
418 cons = ao_scheme_cons_fetch();
419 #ifdef AO_SCHEME_FEATURE_FLOAT
420 } else if (ao_scheme_number_typep(rt) && ao_scheme_number_typep(ct)) {
423 r = ao_scheme_poly_number(ret);
424 c = ao_scheme_poly_number(car);
438 case builtin_quotient:
439 case builtin_remainder:
441 return ao_scheme_error(AO_SCHEME_INVALID, "non-integer value in integer divide");
445 ao_scheme_cons_stash(cons);
446 ret = ao_scheme_float_get(r);
447 cons = ao_scheme_cons_fetch();
450 else if (rt == AO_SCHEME_STRING && ct == AO_SCHEME_STRING && op == builtin_plus) {
451 ao_scheme_cons_stash(cons);
452 ret = ao_scheme_string_poly(ao_scheme_string_cat(ao_scheme_poly_string(ret),
453 ao_scheme_poly_string(car)));
454 cons = ao_scheme_cons_fetch();
459 return ao_scheme_error(AO_SCHEME_INVALID, "invalid args");
465 ao_scheme_do_plus(struct ao_scheme_cons *cons)
467 return ao_scheme_math(cons, builtin_plus);
471 ao_scheme_do_minus(struct ao_scheme_cons *cons)
473 return ao_scheme_math(cons, builtin_minus);
477 ao_scheme_do_times(struct ao_scheme_cons *cons)
479 return ao_scheme_math(cons, builtin_times);
483 ao_scheme_do_divide(struct ao_scheme_cons *cons)
485 return ao_scheme_math(cons, builtin_divide);
489 ao_scheme_do_quotient(struct ao_scheme_cons *cons)
491 return ao_scheme_math(cons, builtin_quotient);
495 ao_scheme_do_modulo(struct ao_scheme_cons *cons)
497 return ao_scheme_math(cons, builtin_modulo);
501 ao_scheme_do_remainder(struct ao_scheme_cons *cons)
503 return ao_scheme_math(cons, builtin_remainder);
507 ao_scheme_compare(struct ao_scheme_cons *cons, enum ao_scheme_builtin_id op)
512 return _ao_scheme_bool_true;
515 for (cons = ao_scheme_cons_cdr(cons); cons; cons = ao_scheme_cons_cdr(cons)) {
516 ao_poly right = cons->car;
518 if (op == builtin_equal && left == right) {
521 uint8_t lt = ao_scheme_poly_type(left);
522 uint8_t rt = ao_scheme_poly_type(right);
523 if (ao_scheme_integer_typep(lt) && ao_scheme_integer_typep(rt)) {
524 int32_t l = ao_scheme_poly_integer(left, NULL);
525 int32_t r = ao_scheme_poly_integer(right, NULL);
530 return _ao_scheme_bool_false;
532 case builtin_greater:
534 return _ao_scheme_bool_false;
536 case builtin_less_equal:
538 return _ao_scheme_bool_false;
540 case builtin_greater_equal:
542 return _ao_scheme_bool_false;
546 return _ao_scheme_bool_false;
550 #ifdef AO_SCHEME_FEATURE_FLOAT
551 } else if (ao_scheme_number_typep(lt) && ao_scheme_number_typep(rt)) {
554 l = ao_scheme_poly_number(left);
555 r = ao_scheme_poly_number(right);
560 return _ao_scheme_bool_false;
562 case builtin_greater:
564 return _ao_scheme_bool_false;
566 case builtin_less_equal:
568 return _ao_scheme_bool_false;
570 case builtin_greater_equal:
572 return _ao_scheme_bool_false;
576 return _ao_scheme_bool_false;
580 #endif /* AO_SCHEME_FEATURE_FLOAT */
581 } else if (lt == AO_SCHEME_STRING && rt == AO_SCHEME_STRING) {
582 int c = strcmp(ao_scheme_poly_string(left)->val,
583 ao_scheme_poly_string(right)->val);
587 return _ao_scheme_bool_false;
589 case builtin_greater:
591 return _ao_scheme_bool_false;
593 case builtin_less_equal:
595 return _ao_scheme_bool_false;
597 case builtin_greater_equal:
599 return _ao_scheme_bool_false;
603 return _ao_scheme_bool_false;
609 return _ao_scheme_bool_false;
613 return _ao_scheme_bool_true;
617 ao_scheme_do_equal(struct ao_scheme_cons *cons)
619 return ao_scheme_compare(cons, builtin_equal);
623 ao_scheme_do_less(struct ao_scheme_cons *cons)
625 return ao_scheme_compare(cons, builtin_less);
629 ao_scheme_do_greater(struct ao_scheme_cons *cons)
631 return ao_scheme_compare(cons, builtin_greater);
635 ao_scheme_do_less_equal(struct ao_scheme_cons *cons)
637 return ao_scheme_compare(cons, builtin_less_equal);
641 ao_scheme_do_greater_equal(struct ao_scheme_cons *cons)
643 return ao_scheme_compare(cons, builtin_greater_equal);
647 ao_scheme_do_list_to_string(struct ao_scheme_cons *cons)
649 if (!ao_scheme_check_argc(_ao_scheme_atom_list2d3estring, cons, 1, 1))
650 return AO_SCHEME_NIL;
651 if (!ao_scheme_check_argt(_ao_scheme_atom_list2d3estring, cons, 0, AO_SCHEME_CONS, 1))
652 return AO_SCHEME_NIL;
653 return ao_scheme_string_pack(ao_scheme_poly_cons(ao_scheme_arg(cons, 0)));
657 ao_scheme_do_string_to_list(struct ao_scheme_cons *cons)
659 if (!ao_scheme_check_argc(_ao_scheme_atom_string2d3elist, cons, 1, 1))
660 return AO_SCHEME_NIL;
661 if (!ao_scheme_check_argt(_ao_scheme_atom_string2d3elist, cons, 0, AO_SCHEME_STRING, 0))
662 return AO_SCHEME_NIL;
663 return ao_scheme_string_unpack(ao_scheme_poly_string(ao_scheme_arg(cons, 0)));
667 ao_scheme_do_string_ref(struct ao_scheme_cons *cons)
671 if (!ao_scheme_check_argc(_ao_scheme_atom_string2dref, cons, 2, 2))
672 return AO_SCHEME_NIL;
673 if (!ao_scheme_check_argt(_ao_scheme_atom_string2dref, cons, 0, AO_SCHEME_STRING, 0))
674 return AO_SCHEME_NIL;
675 ref = ao_scheme_arg_int(_ao_scheme_atom_string2dref, cons, 1);
676 if (ao_scheme_exception)
677 return AO_SCHEME_NIL;
678 string = ao_scheme_poly_string(ao_scheme_arg(cons, 0))->val;
679 while (*string && ref) {
684 return ao_scheme_error(AO_SCHEME_INVALID, "%v: string %v ref %v invalid",
685 _ao_scheme_atom_string2dref,
686 ao_scheme_arg(cons, 0),
687 ao_scheme_arg(cons, 1));
688 return ao_scheme_int_poly(*string);
692 ao_scheme_do_string_length(struct ao_scheme_cons *cons)
694 struct ao_scheme_string *string;
696 if (!ao_scheme_check_argc(_ao_scheme_atom_string2dlength, cons, 1, 1))
697 return AO_SCHEME_NIL;
698 if (!ao_scheme_check_argt(_ao_scheme_atom_string2dlength, cons, 0, AO_SCHEME_STRING, 0))
699 return AO_SCHEME_NIL;
700 string = ao_scheme_poly_string(ao_scheme_arg(cons, 0));
701 return ao_scheme_integer_poly(strlen(string->val));
705 ao_scheme_do_string_copy(struct ao_scheme_cons *cons)
707 struct ao_scheme_string *string;
709 if (!ao_scheme_check_argc(_ao_scheme_atom_string2dcopy, cons, 1, 1))
710 return AO_SCHEME_NIL;
711 if (!ao_scheme_check_argt(_ao_scheme_atom_string2dcopy, cons, 0, AO_SCHEME_STRING, 0))
712 return AO_SCHEME_NIL;
713 string = ao_scheme_poly_string(ao_scheme_arg(cons, 0));
714 return ao_scheme_string_poly(ao_scheme_string_copy(string));
718 ao_scheme_do_string_set(struct ao_scheme_cons *cons)
724 if (!ao_scheme_check_argc(_ao_scheme_atom_string2dset21, cons, 3, 3))
725 return AO_SCHEME_NIL;
726 if (!ao_scheme_check_argt(_ao_scheme_atom_string2dset21, cons, 0, AO_SCHEME_STRING, 0))
727 return AO_SCHEME_NIL;
728 string = ao_scheme_poly_string(ao_scheme_arg(cons, 0))->val;
729 ref = ao_scheme_arg_int(_ao_scheme_atom_string2dset21, cons, 1);
730 if (ao_scheme_exception)
731 return AO_SCHEME_NIL;
732 val = ao_scheme_arg_int(_ao_scheme_atom_string2dset21, cons, 2);
733 if (ao_scheme_exception)
734 return AO_SCHEME_NIL;
735 while (*string && ref) {
740 return ao_scheme_error(AO_SCHEME_INVALID, "%v: string %v ref %v invalid",
741 _ao_scheme_atom_string2dset21,
742 ao_scheme_arg(cons, 0),
743 ao_scheme_arg(cons, 1));
745 return ao_scheme_int_poly(*string);
749 ao_scheme_do_flush_output(struct ao_scheme_cons *cons)
751 if (!ao_scheme_check_argc(_ao_scheme_atom_flush2doutput, cons, 0, 0))
752 return AO_SCHEME_NIL;
753 ao_scheme_os_flush();
754 return _ao_scheme_bool_true;
758 ao_scheme_do_led(struct ao_scheme_cons *cons)
761 if (!ao_scheme_check_argc(_ao_scheme_atom_led, cons, 1, 1))
762 return AO_SCHEME_NIL;
763 led = ao_scheme_arg_int(_ao_scheme_atom_led, cons, 0);
764 if (ao_scheme_exception)
765 return AO_SCHEME_NIL;
766 led = ao_scheme_arg(cons, 0);
767 ao_scheme_os_led(ao_scheme_poly_int(led));
772 ao_scheme_do_delay(struct ao_scheme_cons *cons)
776 if (!ao_scheme_check_argc(_ao_scheme_atom_delay, cons, 1, 1))
777 return AO_SCHEME_NIL;
778 delay = ao_scheme_arg_int(_ao_scheme_atom_delay, cons, 0);
779 if (ao_scheme_exception)
780 return AO_SCHEME_NIL;
781 ao_scheme_os_delay(delay);
786 ao_scheme_do_eval(struct ao_scheme_cons *cons)
788 if (!ao_scheme_check_argc(_ao_scheme_atom_eval, cons, 1, 1))
789 return AO_SCHEME_NIL;
790 ao_scheme_stack->state = eval_sexpr;
795 ao_scheme_do_apply(struct ao_scheme_cons *cons)
797 if (!ao_scheme_check_argc(_ao_scheme_atom_apply, cons, 2, INT_MAX))
798 return AO_SCHEME_NIL;
799 ao_scheme_stack->state = eval_apply;
800 return ao_scheme_cons_poly(cons);
804 ao_scheme_do_read(struct ao_scheme_cons *cons)
806 if (!ao_scheme_check_argc(_ao_scheme_atom_read, cons, 0, 0))
807 return AO_SCHEME_NIL;
808 return ao_scheme_read();
812 ao_scheme_do_collect(struct ao_scheme_cons *cons)
816 free = ao_scheme_collect(AO_SCHEME_COLLECT_FULL);
817 return ao_scheme_integer_poly(free);
821 ao_scheme_do_nullp(struct ao_scheme_cons *cons)
823 if (!ao_scheme_check_argc(_ao_scheme_atom_led, cons, 1, 1))
824 return AO_SCHEME_NIL;
825 if (ao_scheme_arg(cons, 0) == AO_SCHEME_NIL)
826 return _ao_scheme_bool_true;
828 return _ao_scheme_bool_false;
832 ao_scheme_do_not(struct ao_scheme_cons *cons)
834 if (!ao_scheme_check_argc(_ao_scheme_atom_led, cons, 1, 1))
835 return AO_SCHEME_NIL;
836 if (ao_scheme_arg(cons, 0) == _ao_scheme_bool_false)
837 return _ao_scheme_bool_true;
839 return _ao_scheme_bool_false;
843 ao_scheme_do_typep(int type, struct ao_scheme_cons *cons)
845 if (!ao_scheme_check_argc(_ao_scheme_atom_led, cons, 1, 1))
846 return AO_SCHEME_NIL;
847 if (ao_scheme_poly_type(ao_scheme_arg(cons, 0)) == type)
848 return _ao_scheme_bool_true;
849 return _ao_scheme_bool_false;
853 ao_scheme_do_pairp(struct ao_scheme_cons *cons)
856 if (!ao_scheme_check_argc(_ao_scheme_atom_led, cons, 1, 1))
857 return AO_SCHEME_NIL;
858 v = ao_scheme_arg(cons, 0);
859 if (ao_scheme_is_pair(v))
860 return _ao_scheme_bool_true;
861 return _ao_scheme_bool_false;
865 ao_scheme_do_integerp(struct ao_scheme_cons *cons)
867 #ifdef AO_SCHEME_FEATURE_BIGINT
868 if (!ao_scheme_check_argc(_ao_scheme_atom_led, cons, 1, 1))
869 return AO_SCHEME_NIL;
870 switch (ao_scheme_poly_type(ao_scheme_arg(cons, 0))) {
872 case AO_SCHEME_BIGINT:
873 return _ao_scheme_bool_true;
875 return _ao_scheme_bool_false;
878 return ao_scheme_do_typep(AO_SCHEME_INT, cons);
883 ao_scheme_do_numberp(struct ao_scheme_cons *cons)
885 #if defined(AO_SCHEME_FEATURE_BIGINT) || defined(AO_SCHEME_FEATURE_FLOAT)
886 if (!ao_scheme_check_argc(_ao_scheme_atom_led, cons, 1, 1))
887 return AO_SCHEME_NIL;
888 switch (ao_scheme_poly_type(ao_scheme_arg(cons, 0))) {
890 #ifdef AO_SCHEME_FEATURE_BIGINT
891 case AO_SCHEME_BIGINT:
893 #ifdef AO_SCHEME_FEATURE_FLOAT
894 case AO_SCHEME_FLOAT:
896 return _ao_scheme_bool_true;
898 return _ao_scheme_bool_false;
901 return ao_scheme_do_integerp(cons);
906 ao_scheme_do_stringp(struct ao_scheme_cons *cons)
908 return ao_scheme_do_typep(AO_SCHEME_STRING, cons);
912 ao_scheme_do_symbolp(struct ao_scheme_cons *cons)
914 return ao_scheme_do_typep(AO_SCHEME_ATOM, cons);
918 ao_scheme_do_booleanp(struct ao_scheme_cons *cons)
920 return ao_scheme_do_typep(AO_SCHEME_BOOL, cons);
924 ao_scheme_do_procedurep(struct ao_scheme_cons *cons)
926 if (!ao_scheme_check_argc(_ao_scheme_atom_led, cons, 1, 1))
927 return AO_SCHEME_NIL;
928 switch (ao_scheme_poly_type(ao_scheme_arg(cons, 0))) {
929 case AO_SCHEME_BUILTIN:
930 case AO_SCHEME_LAMBDA:
931 return _ao_scheme_bool_true;
933 return _ao_scheme_bool_false;
937 /* This one is special -- a list is either nil or
938 * a 'proper' list with only cons cells
941 ao_scheme_do_listp(struct ao_scheme_cons *cons)
944 if (!ao_scheme_check_argc(_ao_scheme_atom_list3f, cons, 1, 1))
945 return AO_SCHEME_NIL;
946 v = ao_scheme_arg(cons, 0);
948 if (v == AO_SCHEME_NIL)
949 return _ao_scheme_bool_true;
950 if (!ao_scheme_is_cons(v))
951 return _ao_scheme_bool_false;
952 v = ao_scheme_poly_cons(v)->cdr;
957 ao_scheme_do_set_car(struct ao_scheme_cons *cons)
959 if (!ao_scheme_check_argc(_ao_scheme_atom_led, cons, 2, 2))
960 return AO_SCHEME_NIL;
961 if (!ao_scheme_check_argt(_ao_scheme_atom_led, cons, 0, AO_SCHEME_CONS, 0))
962 return AO_SCHEME_NIL;
963 return ao_scheme_poly_cons(ao_scheme_arg(cons, 0))->car = ao_scheme_arg(cons, 1);
967 ao_scheme_do_set_cdr(struct ao_scheme_cons *cons)
969 if (!ao_scheme_check_argc(_ao_scheme_atom_led, cons, 2, 2))
970 return AO_SCHEME_NIL;
971 if (!ao_scheme_check_argt(_ao_scheme_atom_led, cons, 0, AO_SCHEME_CONS, 0))
972 return AO_SCHEME_NIL;
973 return ao_scheme_poly_cons(ao_scheme_arg(cons, 0))->cdr = ao_scheme_arg(cons, 1);
977 ao_scheme_do_symbol_to_string(struct ao_scheme_cons *cons)
979 if (!ao_scheme_check_argc(_ao_scheme_atom_led, cons, 1, 1))
980 return AO_SCHEME_NIL;
981 if (!ao_scheme_check_argt(_ao_scheme_atom_led, cons, 0, AO_SCHEME_ATOM, 0))
982 return AO_SCHEME_NIL;
983 return ao_scheme_string_poly(ao_scheme_atom_to_string(ao_scheme_poly_atom(ao_scheme_arg(cons, 0))));
987 ao_scheme_do_string_to_symbol(struct ao_scheme_cons *cons)
989 if (!ao_scheme_check_argc(_ao_scheme_atom_led, cons, 1, 1))
990 return AO_SCHEME_NIL;
991 if (!ao_scheme_check_argt(_ao_scheme_atom_led, cons, 0, AO_SCHEME_STRING, 0))
992 return AO_SCHEME_NIL;
994 return ao_scheme_atom_poly(ao_scheme_string_to_atom(ao_scheme_poly_string(ao_scheme_arg(cons, 0))));;
998 ao_scheme_do_read_char(struct ao_scheme_cons *cons)
1001 if (!ao_scheme_check_argc(_ao_scheme_atom_led, cons, 0, 0))
1002 return AO_SCHEME_NIL;
1004 return ao_scheme_int_poly(c);
1008 ao_scheme_do_write_char(struct ao_scheme_cons *cons)
1010 if (!ao_scheme_check_argc(_ao_scheme_atom_led, cons, 1, 1))
1011 return AO_SCHEME_NIL;
1012 if (!ao_scheme_check_argt(_ao_scheme_atom_led, cons, 0, AO_SCHEME_INT, 0))
1013 return AO_SCHEME_NIL;
1014 putchar(ao_scheme_poly_integer(ao_scheme_arg(cons, 0), NULL));
1015 return _ao_scheme_bool_true;
1019 ao_scheme_do_exit(struct ao_scheme_cons *cons)
1021 if (!ao_scheme_check_argc(_ao_scheme_atom_led, cons, 0, 0))
1022 return AO_SCHEME_NIL;
1023 ao_scheme_exception |= AO_SCHEME_EXIT;
1024 return _ao_scheme_bool_true;
1028 ao_scheme_do_current_jiffy(struct ao_scheme_cons *cons)
1032 if (!ao_scheme_check_argc(_ao_scheme_atom_led, cons, 0, 0))
1033 return AO_SCHEME_NIL;
1034 jiffy = ao_scheme_os_jiffy();
1035 return (ao_scheme_int_poly(jiffy));
1039 ao_scheme_do_current_second(struct ao_scheme_cons *cons)
1043 if (!ao_scheme_check_argc(_ao_scheme_atom_led, cons, 0, 0))
1044 return AO_SCHEME_NIL;
1045 second = ao_scheme_os_jiffy() / AO_SCHEME_JIFFIES_PER_SECOND;
1046 return (ao_scheme_int_poly(second));
1050 ao_scheme_do_jiffies_per_second(struct ao_scheme_cons *cons)
1052 if (!ao_scheme_check_argc(_ao_scheme_atom_led, cons, 0, 0))
1053 return AO_SCHEME_NIL;
1054 return (ao_scheme_int_poly(AO_SCHEME_JIFFIES_PER_SECOND));
1057 #ifdef AO_SCHEME_FEATURE_VECTOR
1060 ao_scheme_do_vector(struct ao_scheme_cons *cons)
1062 return ao_scheme_vector_poly(ao_scheme_list_to_vector(cons));
1066 ao_scheme_do_make_vector(struct ao_scheme_cons *cons)
1070 if (!ao_scheme_check_argc(_ao_scheme_atom_make2dvector, cons, 2, 2))
1071 return AO_SCHEME_NIL;
1072 k = ao_scheme_arg_int(_ao_scheme_atom_make2dvector, cons, 0);
1073 if (ao_scheme_exception)
1074 return AO_SCHEME_NIL;
1075 return ao_scheme_vector_poly(ao_scheme_vector_alloc(k, ao_scheme_arg(cons, 1)));
1079 ao_scheme_do_vector_ref(struct ao_scheme_cons *cons)
1081 if (!ao_scheme_check_argc(_ao_scheme_atom_vector2dref, cons, 2, 2))
1082 return AO_SCHEME_NIL;
1083 if (!ao_scheme_check_argt(_ao_scheme_atom_vector2dref, cons, 0, AO_SCHEME_VECTOR, 0))
1084 return AO_SCHEME_NIL;
1085 return ao_scheme_vector_get(ao_scheme_arg(cons, 0), ao_scheme_arg(cons, 1));
1089 ao_scheme_do_vector_set(struct ao_scheme_cons *cons)
1091 if (!ao_scheme_check_argc(_ao_scheme_atom_vector2dset21, cons, 3, 3))
1092 return AO_SCHEME_NIL;
1093 if (!ao_scheme_check_argt(_ao_scheme_atom_vector2dset21, cons, 0, AO_SCHEME_VECTOR, 0))
1094 return AO_SCHEME_NIL;
1095 return ao_scheme_vector_set(ao_scheme_arg(cons, 0), ao_scheme_arg(cons, 1), ao_scheme_arg(cons, 2));
1099 ao_scheme_do_list_to_vector(struct ao_scheme_cons *cons)
1101 if (!ao_scheme_check_argc(_ao_scheme_atom_list2d3evector, cons, 1, 1))
1102 return AO_SCHEME_NIL;
1103 if (!ao_scheme_check_argt(_ao_scheme_atom_list2d3evector, cons, 0, AO_SCHEME_CONS, 0))
1104 return AO_SCHEME_NIL;
1105 return ao_scheme_vector_poly(ao_scheme_list_to_vector(ao_scheme_poly_cons(ao_scheme_arg(cons, 0))));
1109 ao_scheme_do_vector_to_list(struct ao_scheme_cons *cons)
1111 if (!ao_scheme_check_argc(_ao_scheme_atom_vector2d3elist, cons, 1, 1))
1112 return AO_SCHEME_NIL;
1113 if (!ao_scheme_check_argt(_ao_scheme_atom_vector2d3elist, cons, 0, AO_SCHEME_VECTOR, 0))
1114 return AO_SCHEME_NIL;
1115 return ao_scheme_cons_poly(ao_scheme_vector_to_list(ao_scheme_poly_vector(ao_scheme_arg(cons, 0))));
1119 ao_scheme_do_vector_length(struct ao_scheme_cons *cons)
1121 if (!ao_scheme_check_argc(_ao_scheme_atom_vector2d3elist, cons, 1, 1))
1122 return AO_SCHEME_NIL;
1123 if (!ao_scheme_check_argt(_ao_scheme_atom_vector2d3elist, cons, 0, AO_SCHEME_VECTOR, 0))
1124 return AO_SCHEME_NIL;
1125 return ao_scheme_integer_poly(ao_scheme_poly_vector(ao_scheme_arg(cons, 0))->length);
1129 ao_scheme_do_vectorp(struct ao_scheme_cons *cons)
1131 return ao_scheme_do_typep(AO_SCHEME_VECTOR, cons);
1134 #endif /* AO_SCHEME_FEATURE_VECTOR */
1136 #define AO_SCHEME_BUILTIN_FUNCS
1137 #include "ao_scheme_builtin.h"