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.
17 enum math_op { math_plus, math_minus, math_times, math_divide, math_mod };
20 ao_lisp_math(struct ao_lisp_cons *cons, enum math_op op)
22 ao_lisp_poly ret = AO_LISP_NIL;
25 ao_lisp_poly car = cons->car;
26 uint8_t rt = ao_lisp_poly_type(ret);
27 uint8_t ct = ao_lisp_poly_type(car);
31 if (rt == AO_LISP_NIL)
34 else if (rt == AO_LISP_INT && ct == AO_LISP_INT) {
35 int r = ao_lisp_poly_int(ret);
36 int c = ao_lisp_poly_int(car);
59 ret = ao_lisp_int_poly(r);
62 else if (rt == AO_LISP_STRING && ct == AO_LISP_STRING && op == math_plus)
63 ret = ao_lisp_string_poly(ao_lisp_string_cat(ao_lisp_poly_string(ret),
64 ao_lisp_poly_string(car)));
74 ao_lisp_plus(struct ao_lisp_cons *cons)
76 return ao_lisp_math(cons, math_plus);
80 ao_lisp_minus(struct ao_lisp_cons *cons)
82 return ao_lisp_math(cons, math_minus);
86 ao_lisp_times(struct ao_lisp_cons *cons)
88 return ao_lisp_math(cons, math_times);
92 ao_lisp_divide(struct ao_lisp_cons *cons)
94 return ao_lisp_math(cons, math_divide);
98 ao_lisp_mod(struct ao_lisp_cons *cons)
100 return ao_lisp_math(cons, math_mod);
103 static const struct ao_lisp_builtin builtin_plus = {
104 .type = AO_LISP_BUILTIN,
105 .func = ao_lisp_plus,
109 static const struct ao_lisp_atom atom_plus = {
110 .type = AO_LISP_ATOM,
111 .val = AO_LISP_OTHER_POLY(&builtin_plus),
112 .next = AO_LISP_ATOM_CONST,
117 static const struct ao_lisp_builtin builtin_minus = {
118 .type = AO_LISP_BUILTIN,
119 .func = ao_lisp_minus
122 static const struct ao_lisp_builtin builtin_times = {
123 .type = AO_LISP_BUILTIN,
124 .func = ao_lisp_times
129 const struct ao_lisp_atom const *ao_lisp_builtins[] = {