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.
19 builtin_size(void *addr)
22 return sizeof (struct ao_lisp_builtin);
26 builtin_mark(void *addr)
32 builtin_move(void *addr)
37 const struct ao_lisp_type ao_lisp_builtin_type = {
43 #ifdef AO_LISP_MAKE_CONST
45 #define AO_LISP_BUILTIN_CASENAME
46 #include "ao_lisp_builtin.h"
48 char *ao_lisp_args_name(uint8_t args) {
49 args &= AO_LISP_FUNC_MASK;
51 case AO_LISP_FUNC_LAMBDA: return ao_lisp_poly_atom(_ao_lisp_atom_lambda)->name;
52 case AO_LISP_FUNC_LEXPR: return ao_lisp_poly_atom(_ao_lisp_atom_lexpr)->name;
53 case AO_LISP_FUNC_NLAMBDA: return ao_lisp_poly_atom(_ao_lisp_atom_nlambda)->name;
54 case AO_LISP_FUNC_MACRO: return ao_lisp_poly_atom(_ao_lisp_atom_macro)->name;
55 default: return "???";
60 #define AO_LISP_BUILTIN_ARRAYNAME
61 #include "ao_lisp_builtin.h"
64 ao_lisp_builtin_name(enum ao_lisp_builtin_id b) {
65 if (b < _builtin_last)
66 return ao_lisp_poly_atom(builtin_names[b])->name;
70 static const ao_poly ao_lisp_args_atoms[] = {
71 [AO_LISP_FUNC_LAMBDA] = _ao_lisp_atom_lambda,
72 [AO_LISP_FUNC_LEXPR] = _ao_lisp_atom_lexpr,
73 [AO_LISP_FUNC_NLAMBDA] = _ao_lisp_atom_nlambda,
74 [AO_LISP_FUNC_MACRO] = _ao_lisp_atom_macro,
78 ao_lisp_args_name(uint8_t args)
80 args &= AO_LISP_FUNC_MASK;
81 if (args < sizeof ao_lisp_args_atoms / sizeof ao_lisp_args_atoms[0])
82 return ao_lisp_poly_atom(ao_lisp_args_atoms[args])->name;
88 ao_lisp_builtin_write(ao_poly b)
90 struct ao_lisp_builtin *builtin = ao_lisp_poly_builtin(b);
91 printf("%s", ao_lisp_builtin_name(builtin->func));
95 ao_lisp_check_argc(ao_poly name, struct ao_lisp_cons *cons, int min, int max)
99 while (cons && argc <= max) {
101 cons = ao_lisp_poly_cons(cons->cdr);
103 if (argc < min || argc > max)
104 return ao_lisp_error(AO_LISP_INVALID, "%s: invalid arg count", ao_lisp_poly_atom(name)->name);
105 return _ao_lisp_bool_true;
109 ao_lisp_arg(struct ao_lisp_cons *cons, int argc)
116 cons = ao_lisp_poly_cons(cons->cdr);
122 ao_lisp_check_argt(ao_poly name, struct ao_lisp_cons *cons, int argc, int type, int nil_ok)
124 ao_poly car = ao_lisp_arg(cons, argc);
126 if ((!car && !nil_ok) || ao_lisp_poly_type(car) != type)
127 return ao_lisp_error(AO_LISP_INVALID, "%s: invalid type for arg %d", ao_lisp_poly_atom(name)->name, argc);
128 return _ao_lisp_bool_true;
132 ao_lisp_do_car(struct ao_lisp_cons *cons)
134 if (!ao_lisp_check_argc(_ao_lisp_atom_car, cons, 1, 1))
136 if (!ao_lisp_check_argt(_ao_lisp_atom_car, cons, 0, AO_LISP_CONS, 0))
138 return ao_lisp_poly_cons(cons->car)->car;
142 ao_lisp_do_cdr(struct ao_lisp_cons *cons)
144 if (!ao_lisp_check_argc(_ao_lisp_atom_cdr, cons, 1, 1))
146 if (!ao_lisp_check_argt(_ao_lisp_atom_cdr, cons, 0, AO_LISP_CONS, 0))
148 return ao_lisp_poly_cons(cons->car)->cdr;
152 ao_lisp_do_cons(struct ao_lisp_cons *cons)
155 if(!ao_lisp_check_argc(_ao_lisp_atom_cons, cons, 2, 2))
157 car = ao_lisp_arg(cons, 0);
158 cdr = ao_lisp_arg(cons, 1);
159 return ao_lisp__cons(car, cdr);
163 ao_lisp_do_last(struct ao_lisp_cons *cons)
166 if (!ao_lisp_check_argc(_ao_lisp_atom_last, cons, 1, 1))
168 if (!ao_lisp_check_argt(_ao_lisp_atom_last, cons, 0, AO_LISP_CONS, 1))
170 l = ao_lisp_arg(cons, 0);
172 struct ao_lisp_cons *list = ao_lisp_poly_cons(l);
181 ao_lisp_do_length(struct ao_lisp_cons *cons)
183 if (!ao_lisp_check_argc(_ao_lisp_atom_length, cons, 1, 1))
185 if (!ao_lisp_check_argt(_ao_lisp_atom_length, cons, 0, AO_LISP_CONS, 1))
187 return ao_lisp_int_poly(ao_lisp_cons_length(ao_lisp_poly_cons(ao_lisp_arg(cons, 0))));
191 ao_lisp_do_quote(struct ao_lisp_cons *cons)
193 if (!ao_lisp_check_argc(_ao_lisp_atom_quote, cons, 1, 1))
195 return ao_lisp_arg(cons, 0);
199 ao_lisp_do_set(struct ao_lisp_cons *cons)
201 if (!ao_lisp_check_argc(_ao_lisp_atom_set, cons, 2, 2))
203 if (!ao_lisp_check_argt(_ao_lisp_atom_set, cons, 0, AO_LISP_ATOM, 0))
206 return ao_lisp_atom_set(ao_lisp_arg(cons, 0), ao_lisp_arg(cons, 1));
210 ao_lisp_do_setq(struct ao_lisp_cons *cons)
213 if (!ao_lisp_check_argc(_ao_lisp_atom_set21, cons, 2, 2))
216 if (ao_lisp_poly_type(name) != AO_LISP_ATOM)
217 return ao_lisp_error(AO_LISP_INVALID, "set! of non-atom");
218 if (!ao_lisp_atom_ref(ao_lisp_frame_current, name))
219 return ao_lisp_error(AO_LISP_INVALID, "atom not defined");
220 return ao_lisp__cons(_ao_lisp_atom_set,
221 ao_lisp__cons(ao_lisp__cons(_ao_lisp_atom_quote,
222 ao_lisp__cons(name, AO_LISP_NIL)),
227 ao_lisp_do_cond(struct ao_lisp_cons *cons)
229 ao_lisp_set_cond(cons);
234 ao_lisp_do_progn(struct ao_lisp_cons *cons)
236 ao_lisp_stack->state = eval_progn;
237 ao_lisp_stack->sexprs = ao_lisp_cons_poly(cons);
242 ao_lisp_do_while(struct ao_lisp_cons *cons)
244 ao_lisp_stack->state = eval_while;
245 ao_lisp_stack->sexprs = ao_lisp_cons_poly(cons);
250 ao_lisp_do_write(struct ao_lisp_cons *cons)
252 ao_poly val = AO_LISP_NIL;
255 ao_lisp_poly_write(val);
256 cons = ao_lisp_poly_cons(cons->cdr);
261 return _ao_lisp_bool_true;
265 ao_lisp_do_display(struct ao_lisp_cons *cons)
267 ao_poly val = AO_LISP_NIL;
270 ao_lisp_poly_display(val);
271 cons = ao_lisp_poly_cons(cons->cdr);
273 return _ao_lisp_bool_true;
277 ao_lisp_math(struct ao_lisp_cons *cons, enum ao_lisp_builtin_id op)
279 struct ao_lisp_cons *orig_cons = cons;
280 ao_poly ret = AO_LISP_NIL;
283 ao_poly car = cons->car;
285 uint8_t rt = ao_lisp_poly_type(ret);
286 uint8_t ct = ao_lisp_poly_type(car);
288 if (cons == orig_cons) {
290 if (cons->cdr == AO_LISP_NIL && ct == AO_LISP_INT) {
293 ret = ao_lisp_integer_poly(-ao_lisp_poly_integer(ret));
296 switch (ao_lisp_poly_integer(ret)) {
298 return ao_lisp_error(AO_LISP_DIVIDE_BY_ZERO, "divide by zero");
302 ret = ao_lisp_int_poly(0);
310 } else if (ao_lisp_integer_typep(rt) && ao_lisp_integer_typep(ct)) {
311 int32_t r = ao_lisp_poly_integer(ret);
312 int32_t c = ao_lisp_poly_integer(car);
326 return ao_lisp_error(AO_LISP_DIVIDE_BY_ZERO, "divide by zero");
329 case builtin_quotient:
331 return ao_lisp_error(AO_LISP_DIVIDE_BY_ZERO, "quotient by zero");
332 if (r % c != 0 && (c < 0) != (r < 0))
337 case builtin_remainder:
339 return ao_lisp_error(AO_LISP_DIVIDE_BY_ZERO, "remainder by zero");
344 return ao_lisp_error(AO_LISP_DIVIDE_BY_ZERO, "modulo by zero");
346 if ((r < 0) != (c < 0))
352 ret = ao_lisp_integer_poly(r);
355 else if (rt == AO_LISP_STRING && ct == AO_LISP_STRING && op == builtin_plus)
356 ret = ao_lisp_string_poly(ao_lisp_string_cat(ao_lisp_poly_string(ret),
357 ao_lisp_poly_string(car)));
359 return ao_lisp_error(AO_LISP_INVALID, "invalid args");
362 if (cdr != AO_LISP_NIL && ao_lisp_poly_type(cdr) != AO_LISP_CONS)
363 return ao_lisp_error(AO_LISP_INVALID, "improper list");
364 cons = ao_lisp_poly_cons(cdr);
370 ao_lisp_do_plus(struct ao_lisp_cons *cons)
372 return ao_lisp_math(cons, builtin_plus);
376 ao_lisp_do_minus(struct ao_lisp_cons *cons)
378 return ao_lisp_math(cons, builtin_minus);
382 ao_lisp_do_times(struct ao_lisp_cons *cons)
384 return ao_lisp_math(cons, builtin_times);
388 ao_lisp_do_divide(struct ao_lisp_cons *cons)
390 return ao_lisp_math(cons, builtin_divide);
394 ao_lisp_do_quotient(struct ao_lisp_cons *cons)
396 return ao_lisp_math(cons, builtin_quotient);
400 ao_lisp_do_modulo(struct ao_lisp_cons *cons)
402 return ao_lisp_math(cons, builtin_modulo);
406 ao_lisp_do_remainder(struct ao_lisp_cons *cons)
408 return ao_lisp_math(cons, builtin_remainder);
412 ao_lisp_compare(struct ao_lisp_cons *cons, enum ao_lisp_builtin_id op)
417 return _ao_lisp_bool_true;
420 cons = ao_lisp_poly_cons(cons->cdr);
422 ao_poly right = cons->car;
424 if (op == builtin_equal) {
426 return _ao_lisp_bool_false;
428 uint8_t lt = ao_lisp_poly_type(left);
429 uint8_t rt = ao_lisp_poly_type(right);
430 if (ao_lisp_integer_typep(lt) && ao_lisp_integer_typep(rt)) {
431 int32_t l = ao_lisp_poly_integer(left);
432 int32_t r = ao_lisp_poly_integer(right);
437 return _ao_lisp_bool_false;
439 case builtin_greater:
441 return _ao_lisp_bool_false;
443 case builtin_less_equal:
445 return _ao_lisp_bool_false;
447 case builtin_greater_equal:
449 return _ao_lisp_bool_false;
454 } else if (lt == AO_LISP_STRING && rt == AO_LISP_STRING) {
455 int c = strcmp(ao_lisp_poly_string(left),
456 ao_lisp_poly_string(right));
460 return _ao_lisp_bool_false;
462 case builtin_greater:
464 return _ao_lisp_bool_false;
466 case builtin_less_equal:
468 return _ao_lisp_bool_false;
470 case builtin_greater_equal:
472 return _ao_lisp_bool_false;
480 cons = ao_lisp_poly_cons(cons->cdr);
482 return _ao_lisp_bool_true;
486 ao_lisp_do_equal(struct ao_lisp_cons *cons)
488 return ao_lisp_compare(cons, builtin_equal);
492 ao_lisp_do_less(struct ao_lisp_cons *cons)
494 return ao_lisp_compare(cons, builtin_less);
498 ao_lisp_do_greater(struct ao_lisp_cons *cons)
500 return ao_lisp_compare(cons, builtin_greater);
504 ao_lisp_do_less_equal(struct ao_lisp_cons *cons)
506 return ao_lisp_compare(cons, builtin_less_equal);
510 ao_lisp_do_greater_equal(struct ao_lisp_cons *cons)
512 return ao_lisp_compare(cons, builtin_greater_equal);
516 ao_lisp_do_list_to_string(struct ao_lisp_cons *cons)
518 if (!ao_lisp_check_argc(_ao_lisp_atom_list2d3estring, cons, 1, 1))
520 if (!ao_lisp_check_argt(_ao_lisp_atom_list2d3estring, cons, 0, AO_LISP_CONS, 1))
522 return ao_lisp_string_pack(ao_lisp_poly_cons(ao_lisp_arg(cons, 0)));
526 ao_lisp_do_string_to_list(struct ao_lisp_cons *cons)
528 if (!ao_lisp_check_argc(_ao_lisp_atom_string2d3elist, cons, 1, 1))
530 if (!ao_lisp_check_argt(_ao_lisp_atom_string2d3elist, cons, 0, AO_LISP_STRING, 0))
532 return ao_lisp_string_unpack(ao_lisp_poly_string(ao_lisp_arg(cons, 0)));
536 ao_lisp_do_flush(struct ao_lisp_cons *cons)
538 if (!ao_lisp_check_argc(_ao_lisp_atom_flush, cons, 0, 0))
541 return _ao_lisp_bool_true;
545 ao_lisp_do_led(struct ao_lisp_cons *cons)
548 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
550 if (!ao_lisp_check_argt(_ao_lisp_atom_led, cons, 0, AO_LISP_INT, 0))
552 led = ao_lisp_arg(cons, 0);
553 ao_lisp_os_led(ao_lisp_poly_int(led));
558 ao_lisp_do_delay(struct ao_lisp_cons *cons)
561 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
563 if (!ao_lisp_check_argt(_ao_lisp_atom_led, cons, 0, AO_LISP_INT, 0))
565 delay = ao_lisp_arg(cons, 0);
566 ao_lisp_os_delay(ao_lisp_poly_int(delay));
571 ao_lisp_do_eval(struct ao_lisp_cons *cons)
573 if (!ao_lisp_check_argc(_ao_lisp_atom_eval, cons, 1, 1))
575 ao_lisp_stack->state = eval_sexpr;
580 ao_lisp_do_apply(struct ao_lisp_cons *cons)
582 if (!ao_lisp_check_argc(_ao_lisp_atom_apply, cons, 2, INT_MAX))
584 ao_lisp_stack->state = eval_apply;
585 return ao_lisp_cons_poly(cons);
589 ao_lisp_do_read(struct ao_lisp_cons *cons)
591 if (!ao_lisp_check_argc(_ao_lisp_atom_read, cons, 0, 0))
593 return ao_lisp_read();
597 ao_lisp_do_collect(struct ao_lisp_cons *cons)
601 free = ao_lisp_collect(AO_LISP_COLLECT_FULL);
602 return ao_lisp_int_poly(free);
606 ao_lisp_do_nullp(struct ao_lisp_cons *cons)
608 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
610 if (ao_lisp_arg(cons, 0) == AO_LISP_NIL)
611 return _ao_lisp_bool_true;
613 return _ao_lisp_bool_false;
617 ao_lisp_do_not(struct ao_lisp_cons *cons)
619 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
621 if (ao_lisp_arg(cons, 0) == _ao_lisp_bool_false)
622 return _ao_lisp_bool_true;
624 return _ao_lisp_bool_false;
628 ao_lisp_do_typep(int type, struct ao_lisp_cons *cons)
630 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
632 if (ao_lisp_poly_type(ao_lisp_arg(cons, 0)) == type)
633 return _ao_lisp_bool_true;
634 return _ao_lisp_bool_false;
638 ao_lisp_do_pairp(struct ao_lisp_cons *cons)
640 return ao_lisp_do_typep(AO_LISP_CONS, cons);
644 ao_lisp_do_numberp(struct ao_lisp_cons *cons)
646 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
648 switch (ao_lisp_poly_type(ao_lisp_arg(cons, 0))) {
651 return _ao_lisp_bool_true;
653 return _ao_lisp_bool_false;
658 ao_lisp_do_stringp(struct ao_lisp_cons *cons)
660 return ao_lisp_do_typep(AO_LISP_STRING, cons);
664 ao_lisp_do_symbolp(struct ao_lisp_cons *cons)
666 return ao_lisp_do_typep(AO_LISP_ATOM, cons);
670 ao_lisp_do_booleanp(struct ao_lisp_cons *cons)
672 return ao_lisp_do_typep(AO_LISP_BOOL, cons);
676 ao_lisp_do_procedurep(struct ao_lisp_cons *cons)
678 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
680 switch (ao_lisp_poly_type(ao_lisp_arg(cons, 0))) {
681 case AO_LISP_BUILTIN:
683 return _ao_lisp_bool_true;
685 return _ao_lisp_bool_false;
689 /* This one is special -- a list is either nil or
690 * a 'proper' list with only cons cells
693 ao_lisp_do_listp(struct ao_lisp_cons *cons)
696 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
698 v = ao_lisp_arg(cons, 0);
700 if (v == AO_LISP_NIL)
701 return _ao_lisp_bool_true;
702 if (ao_lisp_poly_type(v) != AO_LISP_CONS)
703 return _ao_lisp_bool_false;
704 v = ao_lisp_poly_cons(v)->cdr;
709 ao_lisp_do_set_car(struct ao_lisp_cons *cons)
711 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 2, 2))
713 if (!ao_lisp_check_argt(_ao_lisp_atom_led, cons, 0, AO_LISP_CONS, 0))
715 return ao_lisp_poly_cons(ao_lisp_arg(cons, 0))->car = ao_lisp_arg(cons, 1);
719 ao_lisp_do_set_cdr(struct ao_lisp_cons *cons)
721 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 2, 2))
723 if (!ao_lisp_check_argt(_ao_lisp_atom_led, cons, 0, AO_LISP_CONS, 0))
725 return ao_lisp_poly_cons(ao_lisp_arg(cons, 0))->cdr = ao_lisp_arg(cons, 1);
729 ao_lisp_do_symbol_to_string(struct ao_lisp_cons *cons)
731 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
733 if (!ao_lisp_check_argt(_ao_lisp_atom_led, cons, 0, AO_LISP_ATOM, 0))
735 return ao_lisp_string_poly(ao_lisp_string_copy(ao_lisp_poly_atom(ao_lisp_arg(cons, 0))->name));
739 ao_lisp_do_string_to_symbol(struct ao_lisp_cons *cons)
741 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
743 if (!ao_lisp_check_argt(_ao_lisp_atom_led, cons, 0, AO_LISP_STRING, 0))
746 return ao_lisp_atom_poly(ao_lisp_atom_intern(ao_lisp_poly_string(ao_lisp_arg(cons, 0))));
750 ao_lisp_do_read_char(struct ao_lisp_cons *cons)
753 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 0, 0))
756 return ao_lisp_int_poly(c);
760 ao_lisp_do_write_char(struct ao_lisp_cons *cons)
762 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
764 if (!ao_lisp_check_argt(_ao_lisp_atom_led, cons, 0, AO_LISP_INT, 0))
766 putchar(ao_lisp_poly_integer(ao_lisp_arg(cons, 0)));
767 return _ao_lisp_bool_true;
771 ao_lisp_do_exit(struct ao_lisp_cons *cons)
773 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 0, 0))
775 ao_lisp_exception |= AO_LISP_EXIT;
776 return _ao_lisp_bool_true;
780 ao_lisp_do_current_jiffy(struct ao_lisp_cons *cons)
784 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 0, 0))
786 jiffy = ao_lisp_os_jiffy();
787 return (ao_lisp_int_poly(jiffy));
791 ao_lisp_do_current_second(struct ao_lisp_cons *cons)
795 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 0, 0))
797 second = ao_lisp_os_jiffy() / AO_LISP_JIFFIES_PER_SECOND;
798 return (ao_lisp_int_poly(second));
802 ao_lisp_do_jiffies_per_second(struct ao_lisp_cons *cons)
804 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 0, 0))
806 return (ao_lisp_int_poly(AO_LISP_JIFFIES_PER_SECOND));
809 #define AO_LISP_BUILTIN_FUNCS
810 #include "ao_lisp_builtin.h"