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: invalid type for arg %d", ao_lisp_poly_atom(name)->name, argc);
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");
230 if (!ao_lisp_atom_ref(name))
231 return ao_lisp_error(AO_LISP_INVALID, "atom not defined");
232 return ao_lisp__cons(_ao_lisp_atom_set,
233 ao_lisp__cons(ao_lisp__cons(_ao_lisp_atom_quote,
234 ao_lisp__cons(name, AO_LISP_NIL)),
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);
336 if (c != 0 && (r % c) == 0)
339 ret = ao_lisp_float_get((float) r / (float) c);
343 case builtin_quotient:
345 return ao_lisp_error(AO_LISP_DIVIDE_BY_ZERO, "quotient by zero");
346 if (r % c != 0 && (c < 0) != (r < 0))
351 case builtin_remainder:
353 return ao_lisp_error(AO_LISP_DIVIDE_BY_ZERO, "remainder by zero");
358 return ao_lisp_error(AO_LISP_DIVIDE_BY_ZERO, "modulo by zero");
360 if ((r < 0) != (c < 0))
366 ret = ao_lisp_integer_poly(r);
367 } else if (ao_lisp_number_typep(rt) && ao_lisp_number_typep(ct)) {
368 float r = ao_lisp_poly_number(ret);
369 float c = ao_lisp_poly_number(car);
384 case builtin_quotient:
386 return ao_lisp_error(AO_LISP_DIVIDE_BY_ZERO, "quotient by zero");
387 if (r % c != 0 && (c < 0) != (r < 0))
392 case builtin_remainder:
394 return ao_lisp_error(AO_LISP_DIVIDE_BY_ZERO, "remainder by zero");
399 return ao_lisp_error(AO_LISP_DIVIDE_BY_ZERO, "modulo by zero");
401 if ((r < 0) != (c < 0))
408 ret = ao_lisp_float_get(r);
411 else if (rt == AO_LISP_STRING && ct == AO_LISP_STRING && op == builtin_plus)
412 ret = ao_lisp_string_poly(ao_lisp_string_cat(ao_lisp_poly_string(ret),
413 ao_lisp_poly_string(car)));
415 return ao_lisp_error(AO_LISP_INVALID, "invalid args");
421 ao_lisp_do_plus(struct ao_lisp_cons *cons)
423 return ao_lisp_math(cons, builtin_plus);
427 ao_lisp_do_minus(struct ao_lisp_cons *cons)
429 return ao_lisp_math(cons, builtin_minus);
433 ao_lisp_do_times(struct ao_lisp_cons *cons)
435 return ao_lisp_math(cons, builtin_times);
439 ao_lisp_do_divide(struct ao_lisp_cons *cons)
441 return ao_lisp_math(cons, builtin_divide);
445 ao_lisp_do_quotient(struct ao_lisp_cons *cons)
447 return ao_lisp_math(cons, builtin_quotient);
451 ao_lisp_do_modulo(struct ao_lisp_cons *cons)
453 return ao_lisp_math(cons, builtin_modulo);
457 ao_lisp_do_remainder(struct ao_lisp_cons *cons)
459 return ao_lisp_math(cons, builtin_remainder);
463 ao_lisp_compare(struct ao_lisp_cons *cons, enum ao_lisp_builtin_id op)
468 return _ao_lisp_bool_true;
471 for (cons = ao_lisp_cons_cdr(cons); cons; cons = ao_lisp_cons_cdr(cons)) {
472 ao_poly right = cons->car;
474 if (op == builtin_equal) {
476 return _ao_lisp_bool_false;
478 uint8_t lt = ao_lisp_poly_type(left);
479 uint8_t rt = ao_lisp_poly_type(right);
480 if (ao_lisp_integer_typep(lt) && ao_lisp_integer_typep(rt)) {
481 int32_t l = ao_lisp_poly_integer(left);
482 int32_t r = ao_lisp_poly_integer(right);
487 return _ao_lisp_bool_false;
489 case builtin_greater:
491 return _ao_lisp_bool_false;
493 case builtin_less_equal:
495 return _ao_lisp_bool_false;
497 case builtin_greater_equal:
499 return _ao_lisp_bool_false;
504 } else if (lt == AO_LISP_STRING && rt == AO_LISP_STRING) {
505 int c = strcmp(ao_lisp_poly_string(left),
506 ao_lisp_poly_string(right));
510 return _ao_lisp_bool_false;
512 case builtin_greater:
514 return _ao_lisp_bool_false;
516 case builtin_less_equal:
518 return _ao_lisp_bool_false;
520 case builtin_greater_equal:
522 return _ao_lisp_bool_false;
531 return _ao_lisp_bool_true;
535 ao_lisp_do_equal(struct ao_lisp_cons *cons)
537 return ao_lisp_compare(cons, builtin_equal);
541 ao_lisp_do_less(struct ao_lisp_cons *cons)
543 return ao_lisp_compare(cons, builtin_less);
547 ao_lisp_do_greater(struct ao_lisp_cons *cons)
549 return ao_lisp_compare(cons, builtin_greater);
553 ao_lisp_do_less_equal(struct ao_lisp_cons *cons)
555 return ao_lisp_compare(cons, builtin_less_equal);
559 ao_lisp_do_greater_equal(struct ao_lisp_cons *cons)
561 return ao_lisp_compare(cons, builtin_greater_equal);
565 ao_lisp_do_list_to_string(struct ao_lisp_cons *cons)
567 if (!ao_lisp_check_argc(_ao_lisp_atom_list2d3estring, cons, 1, 1))
569 if (!ao_lisp_check_argt(_ao_lisp_atom_list2d3estring, cons, 0, AO_LISP_CONS, 1))
571 return ao_lisp_string_pack(ao_lisp_poly_cons(ao_lisp_arg(cons, 0)));
575 ao_lisp_do_string_to_list(struct ao_lisp_cons *cons)
577 if (!ao_lisp_check_argc(_ao_lisp_atom_string2d3elist, cons, 1, 1))
579 if (!ao_lisp_check_argt(_ao_lisp_atom_string2d3elist, cons, 0, AO_LISP_STRING, 0))
581 return ao_lisp_string_unpack(ao_lisp_poly_string(ao_lisp_arg(cons, 0)));
585 ao_lisp_do_flush_output(struct ao_lisp_cons *cons)
587 if (!ao_lisp_check_argc(_ao_lisp_atom_flush2doutput, cons, 0, 0))
590 return _ao_lisp_bool_true;
594 ao_lisp_do_led(struct ao_lisp_cons *cons)
597 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
599 if (!ao_lisp_check_argt(_ao_lisp_atom_led, cons, 0, AO_LISP_INT, 0))
601 led = ao_lisp_arg(cons, 0);
602 ao_lisp_os_led(ao_lisp_poly_int(led));
607 ao_lisp_do_delay(struct ao_lisp_cons *cons)
610 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
612 if (!ao_lisp_check_argt(_ao_lisp_atom_led, cons, 0, AO_LISP_INT, 0))
614 delay = ao_lisp_arg(cons, 0);
615 ao_lisp_os_delay(ao_lisp_poly_int(delay));
620 ao_lisp_do_eval(struct ao_lisp_cons *cons)
622 if (!ao_lisp_check_argc(_ao_lisp_atom_eval, cons, 1, 1))
624 ao_lisp_stack->state = eval_sexpr;
629 ao_lisp_do_apply(struct ao_lisp_cons *cons)
631 if (!ao_lisp_check_argc(_ao_lisp_atom_apply, cons, 2, INT_MAX))
633 ao_lisp_stack->state = eval_apply;
634 return ao_lisp_cons_poly(cons);
638 ao_lisp_do_read(struct ao_lisp_cons *cons)
640 if (!ao_lisp_check_argc(_ao_lisp_atom_read, cons, 0, 0))
642 return ao_lisp_read();
646 ao_lisp_do_collect(struct ao_lisp_cons *cons)
650 free = ao_lisp_collect(AO_LISP_COLLECT_FULL);
651 return ao_lisp_int_poly(free);
655 ao_lisp_do_nullp(struct ao_lisp_cons *cons)
657 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
659 if (ao_lisp_arg(cons, 0) == AO_LISP_NIL)
660 return _ao_lisp_bool_true;
662 return _ao_lisp_bool_false;
666 ao_lisp_do_not(struct ao_lisp_cons *cons)
668 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
670 if (ao_lisp_arg(cons, 0) == _ao_lisp_bool_false)
671 return _ao_lisp_bool_true;
673 return _ao_lisp_bool_false;
677 ao_lisp_do_typep(int type, struct ao_lisp_cons *cons)
679 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
681 if (ao_lisp_poly_type(ao_lisp_arg(cons, 0)) == type)
682 return _ao_lisp_bool_true;
683 return _ao_lisp_bool_false;
687 ao_lisp_do_pairp(struct ao_lisp_cons *cons)
690 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
692 v = ao_lisp_arg(cons, 0);
693 if (v != AO_LISP_NIL && ao_lisp_poly_type(v) == AO_LISP_CONS)
694 return _ao_lisp_bool_true;
695 return _ao_lisp_bool_false;
699 ao_lisp_do_integerp(struct ao_lisp_cons *cons)
701 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
703 switch (ao_lisp_poly_type(ao_lisp_arg(cons, 0))) {
706 return _ao_lisp_bool_true;
708 return _ao_lisp_bool_false;
713 ao_lisp_do_numberp(struct ao_lisp_cons *cons)
715 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
717 switch (ao_lisp_poly_type(ao_lisp_arg(cons, 0))) {
721 return _ao_lisp_bool_true;
723 return _ao_lisp_bool_false;
728 ao_lisp_do_stringp(struct ao_lisp_cons *cons)
730 return ao_lisp_do_typep(AO_LISP_STRING, cons);
734 ao_lisp_do_symbolp(struct ao_lisp_cons *cons)
736 return ao_lisp_do_typep(AO_LISP_ATOM, cons);
740 ao_lisp_do_booleanp(struct ao_lisp_cons *cons)
742 return ao_lisp_do_typep(AO_LISP_BOOL, cons);
746 ao_lisp_do_procedurep(struct ao_lisp_cons *cons)
748 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
750 switch (ao_lisp_poly_type(ao_lisp_arg(cons, 0))) {
751 case AO_LISP_BUILTIN:
753 return _ao_lisp_bool_true;
755 return _ao_lisp_bool_false;
759 /* This one is special -- a list is either nil or
760 * a 'proper' list with only cons cells
763 ao_lisp_do_listp(struct ao_lisp_cons *cons)
766 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
768 v = ao_lisp_arg(cons, 0);
770 if (v == AO_LISP_NIL)
771 return _ao_lisp_bool_true;
772 if (ao_lisp_poly_type(v) != AO_LISP_CONS)
773 return _ao_lisp_bool_false;
774 v = ao_lisp_poly_cons(v)->cdr;
779 ao_lisp_do_set_car(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))->car = ao_lisp_arg(cons, 1);
789 ao_lisp_do_set_cdr(struct ao_lisp_cons *cons)
791 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 2, 2))
793 if (!ao_lisp_check_argt(_ao_lisp_atom_led, cons, 0, AO_LISP_CONS, 0))
795 return ao_lisp_poly_cons(ao_lisp_arg(cons, 0))->cdr = ao_lisp_arg(cons, 1);
799 ao_lisp_do_symbol_to_string(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_ATOM, 0))
805 return ao_lisp_string_poly(ao_lisp_string_copy(ao_lisp_poly_atom(ao_lisp_arg(cons, 0))->name));
809 ao_lisp_do_string_to_symbol(struct ao_lisp_cons *cons)
811 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
813 if (!ao_lisp_check_argt(_ao_lisp_atom_led, cons, 0, AO_LISP_STRING, 0))
816 return ao_lisp_atom_poly(ao_lisp_atom_intern(ao_lisp_poly_string(ao_lisp_arg(cons, 0))));
820 ao_lisp_do_read_char(struct ao_lisp_cons *cons)
823 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 0, 0))
826 return ao_lisp_int_poly(c);
830 ao_lisp_do_write_char(struct ao_lisp_cons *cons)
832 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
834 if (!ao_lisp_check_argt(_ao_lisp_atom_led, cons, 0, AO_LISP_INT, 0))
836 putchar(ao_lisp_poly_integer(ao_lisp_arg(cons, 0)));
837 return _ao_lisp_bool_true;
841 ao_lisp_do_exit(struct ao_lisp_cons *cons)
843 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 0, 0))
845 ao_lisp_exception |= AO_LISP_EXIT;
846 return _ao_lisp_bool_true;
850 ao_lisp_do_current_jiffy(struct ao_lisp_cons *cons)
854 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 0, 0))
856 jiffy = ao_lisp_os_jiffy();
857 return (ao_lisp_int_poly(jiffy));
861 ao_lisp_do_current_second(struct ao_lisp_cons *cons)
865 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 0, 0))
867 second = ao_lisp_os_jiffy() / AO_LISP_JIFFIES_PER_SECOND;
868 return (ao_lisp_int_poly(second));
872 ao_lisp_do_jiffies_per_second(struct ao_lisp_cons *cons)
874 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 0, 0))
876 return (ao_lisp_int_poly(AO_LISP_JIFFIES_PER_SECOND));
879 #define AO_LISP_BUILTIN_FUNCS
880 #include "ao_lisp_builtin.h"