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 ao_lisp_builtin_print(ao_poly b)
24 enum math_op { math_plus, math_minus, math_times, math_divide, math_mod };
27 ao_lisp_car(struct ao_lisp_cons *cons)
30 ao_lisp_exception |= AO_LISP_INVALID;
34 ao_lisp_exception |= AO_LISP_INVALID;
37 if (ao_lisp_poly_type(cons->car) != AO_LISP_CONS) {
38 ao_lisp_exception |= AO_LISP_INVALID;
41 return ao_lisp_poly_cons(cons->car)->car;
45 ao_lisp_cdr(struct ao_lisp_cons *cons)
48 ao_lisp_exception |= AO_LISP_INVALID;
52 ao_lisp_exception |= AO_LISP_INVALID;
55 if (ao_lisp_poly_type(cons->car) != AO_LISP_CONS) {
56 ao_lisp_exception |= AO_LISP_INVALID;
59 return ao_lisp_poly_cons(cons->car)->cdr;
63 ao_lisp_cons(struct ao_lisp_cons *cons)
67 ao_lisp_exception |= AO_LISP_INVALID;
73 ao_lisp_exception |= AO_LISP_INVALID;
76 cdr = ao_lisp_poly_cons(cdr)->car;
77 if (ao_lisp_poly_type(cdr) != AO_LISP_CONS) {
78 ao_lisp_exception |= AO_LISP_INVALID;
81 return ao_lisp_cons_poly(ao_lisp_cons_cons(car, ao_lisp_poly_cons(cdr)));
85 ao_lisp_quote(struct ao_lisp_cons *cons)
88 ao_lisp_exception |= AO_LISP_INVALID;
95 ao_lisp_print(struct ao_lisp_cons *cons)
97 ao_poly val = AO_LISP_NIL;
100 ao_lisp_poly_print(val);
101 cons = ao_lisp_poly_cons(cons->cdr);
107 ao_lisp_math(struct ao_lisp_cons *cons, enum math_op op)
109 ao_poly ret = AO_LISP_NIL;
112 ao_poly car = cons->car;
113 uint8_t rt = ao_lisp_poly_type(ret);
114 uint8_t ct = ao_lisp_poly_type(car);
116 cons = ao_lisp_poly_cons(cons->cdr);
118 if (rt == AO_LISP_NIL)
121 else if (rt == AO_LISP_INT && ct == AO_LISP_INT) {
122 int r = ao_lisp_poly_int(ret);
123 int c = ao_lisp_poly_int(car);
137 ao_lisp_exception |= AO_LISP_DIVIDE_BY_ZERO;
144 ao_lisp_exception |= AO_LISP_DIVIDE_BY_ZERO;
150 ret = ao_lisp_int_poly(r);
153 else if (rt == AO_LISP_STRING && ct == AO_LISP_STRING && op == math_plus)
154 ret = ao_lisp_string_poly(ao_lisp_string_cat(ao_lisp_poly_string(ret),
155 ao_lisp_poly_string(car)));
157 ao_lisp_exception |= AO_LISP_INVALID;
165 ao_lisp_plus(struct ao_lisp_cons *cons)
167 return ao_lisp_math(cons, math_plus);
171 ao_lisp_minus(struct ao_lisp_cons *cons)
173 return ao_lisp_math(cons, math_minus);
177 ao_lisp_times(struct ao_lisp_cons *cons)
179 return ao_lisp_math(cons, math_times);
183 ao_lisp_divide(struct ao_lisp_cons *cons)
185 return ao_lisp_math(cons, math_divide);
189 ao_lisp_mod(struct ao_lisp_cons *cons)
191 return ao_lisp_math(cons, math_mod);
194 ao_lisp_func_t ao_lisp_builtins[] = {
195 [builtin_car] = ao_lisp_car,
196 [builtin_cdr] = ao_lisp_cdr,
197 [builtin_cons] = ao_lisp_cons,
198 [builtin_quote] = ao_lisp_quote,
199 [builtin_print] = ao_lisp_print,
200 [builtin_plus] = ao_lisp_plus,
201 [builtin_minus] = ao_lisp_minus,
202 [builtin_times] = ao_lisp_times,
203 [builtin_divide] = ao_lisp_divide,
204 [builtin_mod] = ao_lisp_mod