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.
20 builtin_size(void *addr)
23 return sizeof (struct ao_lisp_builtin);
27 builtin_mark(void *addr)
33 builtin_move(void *addr)
38 const struct ao_lisp_type ao_lisp_builtin_type = {
44 #ifdef AO_LISP_MAKE_CONST
46 #define AO_LISP_BUILTIN_CASENAME
47 #include "ao_lisp_builtin.h"
49 char *ao_lisp_args_name(uint8_t args) {
50 args &= AO_LISP_FUNC_MASK;
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 "???";
61 #define AO_LISP_BUILTIN_ARRAYNAME
62 #include "ao_lisp_builtin.h"
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;
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,
79 ao_lisp_args_name(uint8_t args)
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;
89 ao_lisp_builtin_write(ao_poly b)
91 struct ao_lisp_builtin *builtin = ao_lisp_poly_builtin(b);
92 printf("%s", ao_lisp_builtin_name(builtin->func));
96 ao_lisp_check_argc(ao_poly name, struct ao_lisp_cons *cons, int min, int max)
100 while (cons && argc <= max) {
102 cons = ao_lisp_cons_cdr(cons);
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;
110 ao_lisp_arg(struct ao_lisp_cons *cons, int argc)
117 cons = ao_lisp_cons_cdr(cons);
123 ao_lisp_check_argt(ao_poly name, struct ao_lisp_cons *cons, int argc, int type, int nil_ok)
125 ao_poly car = ao_lisp_arg(cons, argc);
127 if ((!car && !nil_ok) || ao_lisp_poly_type(car) != type)
128 return ao_lisp_error(AO_LISP_INVALID, "%s: arg %d invalid type %v", ao_lisp_poly_atom(name)->name, argc, car);
129 return _ao_lisp_bool_true;
133 ao_lisp_do_car(struct ao_lisp_cons *cons)
135 if (!ao_lisp_check_argc(_ao_lisp_atom_car, cons, 1, 1))
137 if (!ao_lisp_check_argt(_ao_lisp_atom_car, cons, 0, AO_LISP_CONS, 0))
139 return ao_lisp_poly_cons(cons->car)->car;
143 ao_lisp_do_cdr(struct ao_lisp_cons *cons)
145 if (!ao_lisp_check_argc(_ao_lisp_atom_cdr, cons, 1, 1))
147 if (!ao_lisp_check_argt(_ao_lisp_atom_cdr, cons, 0, AO_LISP_CONS, 0))
149 return ao_lisp_poly_cons(cons->car)->cdr;
153 ao_lisp_do_cons(struct ao_lisp_cons *cons)
156 if(!ao_lisp_check_argc(_ao_lisp_atom_cons, cons, 2, 2))
158 car = ao_lisp_arg(cons, 0);
159 cdr = ao_lisp_arg(cons, 1);
160 return ao_lisp__cons(car, cdr);
164 ao_lisp_do_last(struct ao_lisp_cons *cons)
166 struct ao_lisp_cons *list;
167 if (!ao_lisp_check_argc(_ao_lisp_atom_last, cons, 1, 1))
169 if (!ao_lisp_check_argt(_ao_lisp_atom_last, cons, 0, AO_LISP_CONS, 1))
171 for (list = ao_lisp_poly_cons(ao_lisp_arg(cons, 0));
173 list = ao_lisp_cons_cdr(list))
182 ao_lisp_do_length(struct ao_lisp_cons *cons)
184 if (!ao_lisp_check_argc(_ao_lisp_atom_length, cons, 1, 1))
186 if (!ao_lisp_check_argt(_ao_lisp_atom_length, cons, 0, AO_LISP_CONS, 1))
188 return ao_lisp_int_poly(ao_lisp_cons_length(ao_lisp_poly_cons(ao_lisp_arg(cons, 0))));
192 ao_lisp_do_quote(struct ao_lisp_cons *cons)
194 if (!ao_lisp_check_argc(_ao_lisp_atom_quote, cons, 1, 1))
196 return ao_lisp_arg(cons, 0);
200 ao_lisp_do_set(struct ao_lisp_cons *cons)
202 if (!ao_lisp_check_argc(_ao_lisp_atom_set, cons, 2, 2))
204 if (!ao_lisp_check_argt(_ao_lisp_atom_set, cons, 0, AO_LISP_ATOM, 0))
207 return ao_lisp_atom_set(ao_lisp_arg(cons, 0), ao_lisp_arg(cons, 1));
211 ao_lisp_do_def(struct ao_lisp_cons *cons)
213 if (!ao_lisp_check_argc(_ao_lisp_atom_def, cons, 2, 2))
215 if (!ao_lisp_check_argt(_ao_lisp_atom_def, cons, 0, AO_LISP_ATOM, 0))
218 return ao_lisp_atom_def(ao_lisp_arg(cons, 0), ao_lisp_arg(cons, 1));
222 ao_lisp_do_setq(struct ao_lisp_cons *cons)
225 if (!ao_lisp_check_argc(_ao_lisp_atom_set21, cons, 2, 2))
228 if (ao_lisp_poly_type(name) != AO_LISP_ATOM)
229 return ao_lisp_error(AO_LISP_INVALID, "set! of non-atom %v", name);
230 if (!ao_lisp_atom_ref(name))
231 return ao_lisp_error(AO_LISP_INVALID, "atom %v not defined", name);
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)),
239 ao_lisp_do_cond(struct ao_lisp_cons *cons)
241 ao_lisp_set_cond(cons);
246 ao_lisp_do_begin(struct ao_lisp_cons *cons)
248 ao_lisp_stack->state = eval_begin;
249 ao_lisp_stack->sexprs = ao_lisp_cons_poly(cons);
254 ao_lisp_do_while(struct ao_lisp_cons *cons)
256 ao_lisp_stack->state = eval_while;
257 ao_lisp_stack->sexprs = ao_lisp_cons_poly(cons);
262 ao_lisp_do_write(struct ao_lisp_cons *cons)
264 ao_poly val = AO_LISP_NIL;
267 ao_lisp_poly_write(val);
268 cons = ao_lisp_cons_cdr(cons);
273 return _ao_lisp_bool_true;
277 ao_lisp_do_display(struct ao_lisp_cons *cons)
279 ao_poly val = AO_LISP_NIL;
282 ao_lisp_poly_display(val);
283 cons = ao_lisp_cons_cdr(cons);
285 return _ao_lisp_bool_true;
289 ao_lisp_math(struct ao_lisp_cons *orig_cons, enum ao_lisp_builtin_id op)
291 struct ao_lisp_cons *cons = cons;
292 ao_poly ret = AO_LISP_NIL;
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);
299 if (cons == orig_cons) {
301 if (cons->cdr == AO_LISP_NIL) {
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));
310 if (ao_lisp_integer_typep(ct) && ao_lisp_poly_integer(ret) == 1)
312 else if (ao_lisp_number_typep(ct)) {
313 float v = ao_lisp_poly_number(ret);
314 ret = ao_lisp_float_get(1/v);
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);
330 if (r < AO_LISP_MIN_BIGINT || AO_LISP_MAX_BIGINT < r)
338 t = (int64_t) r * (int64_t) c;
339 if (t < AO_LISP_MIN_BIGINT || AO_LISP_MAX_BIGINT < t)
344 if (c != 0 && (r % c) == 0)
349 case builtin_quotient:
351 return ao_lisp_error(AO_LISP_DIVIDE_BY_ZERO, "quotient by zero");
352 if (r % c != 0 && (c < 0) != (r < 0))
357 case builtin_remainder:
359 return ao_lisp_error(AO_LISP_DIVIDE_BY_ZERO, "remainder by zero");
364 return ao_lisp_error(AO_LISP_DIVIDE_BY_ZERO, "modulo by zero");
366 if ((r < 0) != (c < 0))
372 ret = ao_lisp_integer_poly(r);
373 } else if (ao_lisp_number_typep(rt) && ao_lisp_number_typep(ct)) {
376 r = ao_lisp_poly_number(ret);
377 c = ao_lisp_poly_number(car);
391 case builtin_quotient:
392 case builtin_remainder:
394 return ao_lisp_error(AO_LISP_INVALID, "non-integer value in integer divide");
398 ret = ao_lisp_float_get(r);
401 else if (rt == AO_LISP_STRING && ct == AO_LISP_STRING && op == builtin_plus)
402 ret = ao_lisp_string_poly(ao_lisp_string_cat(ao_lisp_poly_string(ret),
403 ao_lisp_poly_string(car)));
405 return ao_lisp_error(AO_LISP_INVALID, "invalid args");
411 ao_lisp_do_plus(struct ao_lisp_cons *cons)
413 return ao_lisp_math(cons, builtin_plus);
417 ao_lisp_do_minus(struct ao_lisp_cons *cons)
419 return ao_lisp_math(cons, builtin_minus);
423 ao_lisp_do_times(struct ao_lisp_cons *cons)
425 return ao_lisp_math(cons, builtin_times);
429 ao_lisp_do_divide(struct ao_lisp_cons *cons)
431 return ao_lisp_math(cons, builtin_divide);
435 ao_lisp_do_quotient(struct ao_lisp_cons *cons)
437 return ao_lisp_math(cons, builtin_quotient);
441 ao_lisp_do_modulo(struct ao_lisp_cons *cons)
443 return ao_lisp_math(cons, builtin_modulo);
447 ao_lisp_do_remainder(struct ao_lisp_cons *cons)
449 return ao_lisp_math(cons, builtin_remainder);
453 ao_lisp_compare(struct ao_lisp_cons *cons, enum ao_lisp_builtin_id op)
458 return _ao_lisp_bool_true;
461 for (cons = ao_lisp_cons_cdr(cons); cons; cons = ao_lisp_cons_cdr(cons)) {
462 ao_poly right = cons->car;
464 if (op == builtin_equal) {
466 return _ao_lisp_bool_false;
468 uint8_t lt = ao_lisp_poly_type(left);
469 uint8_t rt = ao_lisp_poly_type(right);
470 if (ao_lisp_integer_typep(lt) && ao_lisp_integer_typep(rt)) {
471 int32_t l = ao_lisp_poly_integer(left);
472 int32_t r = ao_lisp_poly_integer(right);
477 return _ao_lisp_bool_false;
479 case builtin_greater:
481 return _ao_lisp_bool_false;
483 case builtin_less_equal:
485 return _ao_lisp_bool_false;
487 case builtin_greater_equal:
489 return _ao_lisp_bool_false;
494 } else if (lt == AO_LISP_STRING && rt == AO_LISP_STRING) {
495 int c = strcmp(ao_lisp_poly_string(left),
496 ao_lisp_poly_string(right));
500 return _ao_lisp_bool_false;
502 case builtin_greater:
504 return _ao_lisp_bool_false;
506 case builtin_less_equal:
508 return _ao_lisp_bool_false;
510 case builtin_greater_equal:
512 return _ao_lisp_bool_false;
521 return _ao_lisp_bool_true;
525 ao_lisp_do_equal(struct ao_lisp_cons *cons)
527 return ao_lisp_compare(cons, builtin_equal);
531 ao_lisp_do_less(struct ao_lisp_cons *cons)
533 return ao_lisp_compare(cons, builtin_less);
537 ao_lisp_do_greater(struct ao_lisp_cons *cons)
539 return ao_lisp_compare(cons, builtin_greater);
543 ao_lisp_do_less_equal(struct ao_lisp_cons *cons)
545 return ao_lisp_compare(cons, builtin_less_equal);
549 ao_lisp_do_greater_equal(struct ao_lisp_cons *cons)
551 return ao_lisp_compare(cons, builtin_greater_equal);
555 ao_lisp_do_list_to_string(struct ao_lisp_cons *cons)
557 if (!ao_lisp_check_argc(_ao_lisp_atom_list2d3estring, cons, 1, 1))
559 if (!ao_lisp_check_argt(_ao_lisp_atom_list2d3estring, cons, 0, AO_LISP_CONS, 1))
561 return ao_lisp_string_pack(ao_lisp_poly_cons(ao_lisp_arg(cons, 0)));
565 ao_lisp_do_string_to_list(struct ao_lisp_cons *cons)
567 if (!ao_lisp_check_argc(_ao_lisp_atom_string2d3elist, cons, 1, 1))
569 if (!ao_lisp_check_argt(_ao_lisp_atom_string2d3elist, cons, 0, AO_LISP_STRING, 0))
571 return ao_lisp_string_unpack(ao_lisp_poly_string(ao_lisp_arg(cons, 0)));
575 ao_lisp_do_flush_output(struct ao_lisp_cons *cons)
577 if (!ao_lisp_check_argc(_ao_lisp_atom_flush2doutput, cons, 0, 0))
580 return _ao_lisp_bool_true;
584 ao_lisp_do_led(struct ao_lisp_cons *cons)
587 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
589 if (!ao_lisp_check_argt(_ao_lisp_atom_led, cons, 0, AO_LISP_INT, 0))
591 led = ao_lisp_arg(cons, 0);
592 ao_lisp_os_led(ao_lisp_poly_int(led));
597 ao_lisp_do_delay(struct ao_lisp_cons *cons)
600 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
602 if (!ao_lisp_check_argt(_ao_lisp_atom_led, cons, 0, AO_LISP_INT, 0))
604 delay = ao_lisp_arg(cons, 0);
605 ao_lisp_os_delay(ao_lisp_poly_int(delay));
610 ao_lisp_do_eval(struct ao_lisp_cons *cons)
612 if (!ao_lisp_check_argc(_ao_lisp_atom_eval, cons, 1, 1))
614 ao_lisp_stack->state = eval_sexpr;
619 ao_lisp_do_apply(struct ao_lisp_cons *cons)
621 if (!ao_lisp_check_argc(_ao_lisp_atom_apply, cons, 2, INT_MAX))
623 ao_lisp_stack->state = eval_apply;
624 return ao_lisp_cons_poly(cons);
628 ao_lisp_do_read(struct ao_lisp_cons *cons)
630 if (!ao_lisp_check_argc(_ao_lisp_atom_read, cons, 0, 0))
632 return ao_lisp_read();
636 ao_lisp_do_collect(struct ao_lisp_cons *cons)
640 free = ao_lisp_collect(AO_LISP_COLLECT_FULL);
641 return ao_lisp_int_poly(free);
645 ao_lisp_do_nullp(struct ao_lisp_cons *cons)
647 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
649 if (ao_lisp_arg(cons, 0) == AO_LISP_NIL)
650 return _ao_lisp_bool_true;
652 return _ao_lisp_bool_false;
656 ao_lisp_do_not(struct ao_lisp_cons *cons)
658 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
660 if (ao_lisp_arg(cons, 0) == _ao_lisp_bool_false)
661 return _ao_lisp_bool_true;
663 return _ao_lisp_bool_false;
667 ao_lisp_do_typep(int type, struct ao_lisp_cons *cons)
669 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
671 if (ao_lisp_poly_type(ao_lisp_arg(cons, 0)) == type)
672 return _ao_lisp_bool_true;
673 return _ao_lisp_bool_false;
677 ao_lisp_do_pairp(struct ao_lisp_cons *cons)
680 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
682 v = ao_lisp_arg(cons, 0);
683 if (v != AO_LISP_NIL && ao_lisp_poly_type(v) == AO_LISP_CONS)
684 return _ao_lisp_bool_true;
685 return _ao_lisp_bool_false;
689 ao_lisp_do_integerp(struct ao_lisp_cons *cons)
691 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
693 switch (ao_lisp_poly_type(ao_lisp_arg(cons, 0))) {
696 return _ao_lisp_bool_true;
698 return _ao_lisp_bool_false;
703 ao_lisp_do_numberp(struct ao_lisp_cons *cons)
705 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
707 switch (ao_lisp_poly_type(ao_lisp_arg(cons, 0))) {
711 return _ao_lisp_bool_true;
713 return _ao_lisp_bool_false;
718 ao_lisp_do_stringp(struct ao_lisp_cons *cons)
720 return ao_lisp_do_typep(AO_LISP_STRING, cons);
724 ao_lisp_do_symbolp(struct ao_lisp_cons *cons)
726 return ao_lisp_do_typep(AO_LISP_ATOM, cons);
730 ao_lisp_do_booleanp(struct ao_lisp_cons *cons)
732 return ao_lisp_do_typep(AO_LISP_BOOL, cons);
736 ao_lisp_do_procedurep(struct ao_lisp_cons *cons)
738 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
740 switch (ao_lisp_poly_type(ao_lisp_arg(cons, 0))) {
741 case AO_LISP_BUILTIN:
743 return _ao_lisp_bool_true;
745 return _ao_lisp_bool_false;
749 /* This one is special -- a list is either nil or
750 * a 'proper' list with only cons cells
753 ao_lisp_do_listp(struct ao_lisp_cons *cons)
756 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
758 v = ao_lisp_arg(cons, 0);
760 if (v == AO_LISP_NIL)
761 return _ao_lisp_bool_true;
762 if (ao_lisp_poly_type(v) != AO_LISP_CONS)
763 return _ao_lisp_bool_false;
764 v = ao_lisp_poly_cons(v)->cdr;
769 ao_lisp_do_set_car(struct ao_lisp_cons *cons)
771 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 2, 2))
773 if (!ao_lisp_check_argt(_ao_lisp_atom_led, cons, 0, AO_LISP_CONS, 0))
775 return ao_lisp_poly_cons(ao_lisp_arg(cons, 0))->car = ao_lisp_arg(cons, 1);
779 ao_lisp_do_set_cdr(struct ao_lisp_cons *cons)
781 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 2, 2))
783 if (!ao_lisp_check_argt(_ao_lisp_atom_led, cons, 0, AO_LISP_CONS, 0))
785 return ao_lisp_poly_cons(ao_lisp_arg(cons, 0))->cdr = ao_lisp_arg(cons, 1);
789 ao_lisp_do_symbol_to_string(struct ao_lisp_cons *cons)
791 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
793 if (!ao_lisp_check_argt(_ao_lisp_atom_led, cons, 0, AO_LISP_ATOM, 0))
795 return ao_lisp_string_poly(ao_lisp_string_copy(ao_lisp_poly_atom(ao_lisp_arg(cons, 0))->name));
799 ao_lisp_do_string_to_symbol(struct ao_lisp_cons *cons)
801 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
803 if (!ao_lisp_check_argt(_ao_lisp_atom_led, cons, 0, AO_LISP_STRING, 0))
806 return ao_lisp_atom_poly(ao_lisp_atom_intern(ao_lisp_poly_string(ao_lisp_arg(cons, 0))));
810 ao_lisp_do_read_char(struct ao_lisp_cons *cons)
813 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 0, 0))
816 return ao_lisp_int_poly(c);
820 ao_lisp_do_write_char(struct ao_lisp_cons *cons)
822 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
824 if (!ao_lisp_check_argt(_ao_lisp_atom_led, cons, 0, AO_LISP_INT, 0))
826 putchar(ao_lisp_poly_integer(ao_lisp_arg(cons, 0)));
827 return _ao_lisp_bool_true;
831 ao_lisp_do_exit(struct ao_lisp_cons *cons)
833 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 0, 0))
835 ao_lisp_exception |= AO_LISP_EXIT;
836 return _ao_lisp_bool_true;
840 ao_lisp_do_current_jiffy(struct ao_lisp_cons *cons)
844 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 0, 0))
846 jiffy = ao_lisp_os_jiffy();
847 return (ao_lisp_int_poly(jiffy));
851 ao_lisp_do_current_second(struct ao_lisp_cons *cons)
855 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 0, 0))
857 second = ao_lisp_os_jiffy() / AO_LISP_JIFFIES_PER_SECOND;
858 return (ao_lisp_int_poly(second));
862 ao_lisp_do_jiffies_per_second(struct ao_lisp_cons *cons)
864 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 0, 0))
866 return (ao_lisp_int_poly(AO_LISP_JIFFIES_PER_SECOND));
869 #define AO_LISP_BUILTIN_FUNCS
870 #include "ao_lisp_builtin.h"