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.
18 builtin_size(void *addr)
21 return sizeof (struct ao_lisp_builtin);
25 builtin_mark(void *addr)
31 builtin_move(void *addr)
36 const struct ao_lisp_type ao_lisp_builtin_type = {
42 #ifdef AO_LISP_MAKE_CONST
43 char *ao_lisp_builtin_name(enum ao_lisp_builtin_id b) {
46 char *ao_lisp_args_name(uint8_t args) {
50 static const ao_poly builtin_names[] = {
51 [builtin_lambda] = _ao_lisp_atom_lambda,
52 [builtin_lexpr] = _ao_lisp_atom_lexpr,
53 [builtin_nlambda] = _ao_lisp_atom_nlambda,
54 [builtin_macro] = _ao_lisp_atom_macro,
55 [builtin_car] = _ao_lisp_atom_car,
56 [builtin_cdr] = _ao_lisp_atom_cdr,
57 [builtin_cons] = _ao_lisp_atom_cons,
58 [builtin_last] = _ao_lisp_atom_last,
59 [builtin_quote] = _ao_lisp_atom_quote,
60 [builtin_set] = _ao_lisp_atom_set,
61 [builtin_setq] = _ao_lisp_atom_setq,
62 [builtin_cond] = _ao_lisp_atom_cond,
63 [builtin_print] = _ao_lisp_atom_print,
64 [builtin_patom] = _ao_lisp_atom_patom,
65 [builtin_plus] = _ao_lisp_atom_2b,
66 [builtin_minus] = _ao_lisp_atom_2d,
67 [builtin_times] = _ao_lisp_atom_2a,
68 [builtin_divide] = _ao_lisp_atom_2f,
69 [builtin_mod] = _ao_lisp_atom_25,
70 [builtin_equal] = _ao_lisp_atom_3d,
71 [builtin_less] = _ao_lisp_atom_3c,
72 [builtin_greater] = _ao_lisp_atom_3e,
73 [builtin_less_equal] = _ao_lisp_atom_3c3d,
74 [builtin_greater_equal] = _ao_lisp_atom_3e3d,
75 [builtin_delay] = _ao_lisp_atom_delay,
76 [builtin_led] = _ao_lisp_atom_led,
80 ao_lisp_builtin_name(enum ao_lisp_builtin_id b) {
81 if (b < _builtin_last)
82 return ao_lisp_poly_atom(builtin_names[b])->name;
86 static const ao_poly ao_lisp_args_atoms[] = {
87 [AO_LISP_FUNC_LAMBDA] = _ao_lisp_atom_lambda,
88 [AO_LISP_FUNC_LEXPR] = _ao_lisp_atom_lexpr,
89 [AO_LISP_FUNC_NLAMBDA] = _ao_lisp_atom_nlambda,
90 [AO_LISP_FUNC_MACRO] = _ao_lisp_atom_macro,
94 ao_lisp_args_name(uint8_t args)
96 if (args < sizeof ao_lisp_args_atoms / sizeof ao_lisp_args_atoms[0])
97 return ao_lisp_poly_atom(ao_lisp_args_atoms[args])->name;
103 ao_lisp_builtin_print(ao_poly b)
105 struct ao_lisp_builtin *builtin = ao_lisp_poly_builtin(b);
106 printf("[builtin %s %s]",
107 ao_lisp_args_name(builtin->args),
108 ao_lisp_builtin_name(builtin->func));
112 ao_lisp_check_argc(ao_poly name, struct ao_lisp_cons *cons, int min, int max)
116 while (cons && argc <= max) {
118 cons = ao_lisp_poly_cons(cons->cdr);
120 if (argc < min || argc > max)
121 return ao_lisp_error(AO_LISP_INVALID, "%s: invalid arg count", ao_lisp_poly_atom(name)->name);
122 return _ao_lisp_atom_t;
126 ao_lisp_arg(struct ao_lisp_cons *cons, int argc)
133 cons = ao_lisp_poly_cons(cons->cdr);
139 ao_lisp_check_argt(ao_poly name, struct ao_lisp_cons *cons, int argc, int type, int nil_ok)
141 ao_poly car = ao_lisp_arg(cons, argc);
143 if ((!car && !nil_ok) || ao_lisp_poly_type(car) != type)
144 return ao_lisp_error(AO_LISP_INVALID, "%s: invalid type for arg %d", ao_lisp_poly_atom(name)->name, argc);
145 return _ao_lisp_atom_t;
149 ao_lisp_car(struct ao_lisp_cons *cons)
151 if (!ao_lisp_check_argc(_ao_lisp_atom_car, cons, 1, 1))
153 if (!ao_lisp_check_argt(_ao_lisp_atom_car, cons, 0, AO_LISP_CONS, 0))
155 return ao_lisp_poly_cons(cons->car)->car;
159 ao_lisp_cdr(struct ao_lisp_cons *cons)
161 if (!ao_lisp_check_argc(_ao_lisp_atom_cdr, cons, 1, 1))
163 if (!ao_lisp_check_argt(_ao_lisp_atom_cdr, cons, 0, AO_LISP_CONS, 0))
165 return ao_lisp_poly_cons(cons->car)->cdr;
169 ao_lisp_cons(struct ao_lisp_cons *cons)
172 if(!ao_lisp_check_argc(_ao_lisp_atom_cons, cons, 2, 2))
174 if (!ao_lisp_check_argt(_ao_lisp_atom_cons, cons, 1, AO_LISP_CONS, 1))
176 car = ao_lisp_arg(cons, 0);
177 cdr = ao_lisp_arg(cons, 1);
178 return ao_lisp_cons_poly(ao_lisp_cons_cons(car, ao_lisp_poly_cons(cdr)));
182 ao_lisp_last(struct ao_lisp_cons *cons)
185 if (!ao_lisp_check_argc(_ao_lisp_atom_last, cons, 1, 1))
187 if (!ao_lisp_check_argt(_ao_lisp_atom_last, cons, 0, AO_LISP_CONS, 1))
189 l = ao_lisp_arg(cons, 0);
191 struct ao_lisp_cons *list = ao_lisp_poly_cons(l);
200 ao_lisp_quote(struct ao_lisp_cons *cons)
202 if (!ao_lisp_check_argc(_ao_lisp_atom_quote, cons, 1, 1))
204 return ao_lisp_arg(cons, 0);
208 ao_lisp_set(struct ao_lisp_cons *cons)
210 if (!ao_lisp_check_argc(_ao_lisp_atom_set, cons, 2, 2))
212 if (!ao_lisp_check_argt(_ao_lisp_atom_set, cons, 0, AO_LISP_ATOM, 0))
215 return ao_lisp_atom_set(ao_lisp_arg(cons, 0), ao_lisp_arg(cons, 1));
219 ao_lisp_setq(struct ao_lisp_cons *cons)
221 struct ao_lisp_cons *expand = 0;
222 if (!ao_lisp_check_argc(_ao_lisp_atom_setq, cons, 2, 2))
224 expand = ao_lisp_cons_cons(_ao_lisp_atom_set,
225 ao_lisp_cons_cons(ao_lisp_cons_poly(ao_lisp_cons_cons(_ao_lisp_atom_quote,
226 ao_lisp_cons_cons(cons->car, NULL))),
227 ao_lisp_poly_cons(cons->cdr)));
228 return ao_lisp_cons_poly(expand);
232 ao_lisp_cond(struct ao_lisp_cons *cons)
234 ao_lisp_set_cond(cons);
239 ao_lisp_print(struct ao_lisp_cons *cons)
241 ao_poly val = AO_LISP_NIL;
244 ao_lisp_poly_print(val);
245 cons = ao_lisp_poly_cons(cons->cdr);
254 ao_lisp_patom(struct ao_lisp_cons *cons)
256 ao_poly val = AO_LISP_NIL;
259 ao_lisp_poly_patom(val);
260 cons = ao_lisp_poly_cons(cons->cdr);
266 ao_lisp_math(struct ao_lisp_cons *cons, enum ao_lisp_builtin_id op)
268 ao_poly ret = AO_LISP_NIL;
271 ao_poly car = cons->car;
272 uint8_t rt = ao_lisp_poly_type(ret);
273 uint8_t ct = ao_lisp_poly_type(car);
275 cons = ao_lisp_poly_cons(cons->cdr);
277 if (rt == AO_LISP_NIL)
280 else if (rt == AO_LISP_INT && ct == AO_LISP_INT) {
281 int r = ao_lisp_poly_int(ret);
282 int c = ao_lisp_poly_int(car);
296 return ao_lisp_error(AO_LISP_DIVIDE_BY_ZERO, "divide by zero");
301 return ao_lisp_error(AO_LISP_DIVIDE_BY_ZERO, "mod by zero");
307 ret = ao_lisp_int_poly(r);
310 else if (rt == AO_LISP_STRING && ct == AO_LISP_STRING && op == builtin_plus)
311 ret = ao_lisp_string_poly(ao_lisp_string_cat(ao_lisp_poly_string(ret),
312 ao_lisp_poly_string(car)));
314 return ao_lisp_error(AO_LISP_INVALID, "invalid args");
320 ao_lisp_plus(struct ao_lisp_cons *cons)
322 return ao_lisp_math(cons, builtin_plus);
326 ao_lisp_minus(struct ao_lisp_cons *cons)
328 return ao_lisp_math(cons, builtin_minus);
332 ao_lisp_times(struct ao_lisp_cons *cons)
334 return ao_lisp_math(cons, builtin_times);
338 ao_lisp_divide(struct ao_lisp_cons *cons)
340 return ao_lisp_math(cons, builtin_divide);
344 ao_lisp_mod(struct ao_lisp_cons *cons)
346 return ao_lisp_math(cons, builtin_mod);
350 ao_lisp_compare(struct ao_lisp_cons *cons, enum ao_lisp_builtin_id op)
355 return _ao_lisp_atom_t;
358 cons = ao_lisp_poly_cons(cons->cdr);
360 ao_poly right = cons->car;
362 if (op == builtin_equal) {
366 uint8_t lt = ao_lisp_poly_type(left);
367 uint8_t rt = ao_lisp_poly_type(right);
368 if (lt == AO_LISP_INT && rt == AO_LISP_INT) {
369 int l = ao_lisp_poly_int(left);
370 int r = ao_lisp_poly_int(right);
377 case builtin_greater:
381 case builtin_less_equal:
385 case builtin_greater_equal:
392 } else if (lt == AO_LISP_STRING && rt == AO_LISP_STRING) {
393 int c = strcmp(ao_lisp_poly_string(left),
394 ao_lisp_poly_string(right));
400 case builtin_greater:
404 case builtin_less_equal:
408 case builtin_greater_equal:
418 cons = ao_lisp_poly_cons(cons->cdr);
420 return _ao_lisp_atom_t;
424 ao_lisp_equal(struct ao_lisp_cons *cons)
426 return ao_lisp_compare(cons, builtin_equal);
430 ao_lisp_less(struct ao_lisp_cons *cons)
432 return ao_lisp_compare(cons, builtin_less);
436 ao_lisp_greater(struct ao_lisp_cons *cons)
438 return ao_lisp_compare(cons, builtin_greater);
442 ao_lisp_less_equal(struct ao_lisp_cons *cons)
444 return ao_lisp_compare(cons, builtin_less_equal);
448 ao_lisp_greater_equal(struct ao_lisp_cons *cons)
450 return ao_lisp_compare(cons, builtin_greater_equal);
454 ao_lisp_led(struct ao_lisp_cons *cons)
457 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
459 if (!ao_lisp_check_argt(_ao_lisp_atom_led, cons, 0, AO_LISP_INT, 0))
461 led = ao_lisp_arg(cons, 0);
462 ao_lisp_os_led(ao_lisp_poly_int(led));
467 ao_lisp_delay(struct ao_lisp_cons *cons)
470 if (!ao_lisp_check_argc(_ao_lisp_atom_led, cons, 1, 1))
472 if (!ao_lisp_check_argt(_ao_lisp_atom_led, cons, 0, AO_LISP_INT, 0))
474 delay = ao_lisp_arg(cons, 0);
475 ao_lisp_os_delay(ao_lisp_poly_int(delay));
479 const ao_lisp_func_t ao_lisp_builtins[] = {
480 [builtin_lambda] = ao_lisp_lambda,
481 [builtin_lexpr] = ao_lisp_lexpr,
482 [builtin_nlambda] = ao_lisp_nlambda,
483 [builtin_macro] = ao_lisp_macro,
484 [builtin_car] = ao_lisp_car,
485 [builtin_cdr] = ao_lisp_cdr,
486 [builtin_cons] = ao_lisp_cons,
487 [builtin_last] = ao_lisp_last,
488 [builtin_quote] = ao_lisp_quote,
489 [builtin_set] = ao_lisp_set,
490 [builtin_setq] = ao_lisp_setq,
491 [builtin_cond] = ao_lisp_cond,
492 [builtin_print] = ao_lisp_print,
493 [builtin_patom] = ao_lisp_patom,
494 [builtin_plus] = ao_lisp_plus,
495 [builtin_minus] = ao_lisp_minus,
496 [builtin_times] = ao_lisp_times,
497 [builtin_divide] = ao_lisp_divide,
498 [builtin_mod] = ao_lisp_mod,
499 [builtin_equal] = ao_lisp_equal,
500 [builtin_less] = ao_lisp_less,
501 [builtin_greater] = ao_lisp_greater,
502 [builtin_less_equal] = ao_lisp_less_equal,
503 [builtin_greater_equal] = ao_lisp_greater_equal,
504 [builtin_led] = ao_lisp_led,
505 [builtin_delay] = ao_lisp_delay,