altos: Add lambda support to lisp
[fw/altos] / src / lisp / ao_lisp_builtin.c
1 /*
2  * Copyright © 2016 Keith Packard <keithp@keithp.com>
3  *
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.
8  *
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.
13  */
14
15 #include "ao_lisp.h"
16
17 static int
18 builtin_size(void *addr)
19 {
20         (void) addr;
21         return sizeof (struct ao_lisp_builtin);
22 }
23
24 static void
25 builtin_mark(void *addr)
26 {
27         (void) addr;
28 }
29
30 static void
31 builtin_move(void *addr)
32 {
33         (void) addr;
34 }
35
36 const struct ao_lisp_type ao_lisp_builtin_type = {
37         .size = builtin_size,
38         .mark = builtin_mark,
39         .move = builtin_move
40 };
41
42 void
43 ao_lisp_builtin_print(ao_poly b)
44 {
45         (void) b;
46         printf("[builtin]");
47 }
48
49 ao_poly
50 ao_lisp_check_argc(ao_poly name, struct ao_lisp_cons *cons, int min, int max)
51 {
52         int     argc = 0;
53
54         while (cons && argc <= max) {
55                 argc++;
56                 cons = ao_lisp_poly_cons(cons->cdr);
57         }
58         if (argc < min || argc > max)
59                 return ao_lisp_error(AO_LISP_INVALID, "%s: invalid arg count", ao_lisp_poly_atom(name)->name);
60         return _ao_lisp_atom_t;
61 }
62
63 ao_poly
64 ao_lisp_arg(struct ao_lisp_cons *cons, int argc)
65 {
66         while (argc--) {
67                 if (!cons)
68                         return AO_LISP_NIL;
69                 cons = ao_lisp_poly_cons(cons->cdr);
70         }
71         return cons->car;
72 }
73
74 ao_poly
75 ao_lisp_check_argt(ao_poly name, struct ao_lisp_cons *cons, int argc, int type, int nil_ok)
76 {
77         ao_poly car = ao_lisp_arg(cons, argc);
78
79         if ((!car && !nil_ok) || ao_lisp_poly_type(car) != type)
80                 return ao_lisp_error(AO_LISP_INVALID, "%s: invalid type for arg %d", ao_lisp_poly_atom(name)->name, argc);
81         return _ao_lisp_atom_t;
82 }
83
84 enum math_op { math_plus, math_minus, math_times, math_divide, math_mod };
85
86 ao_poly
87 ao_lisp_car(struct ao_lisp_cons *cons)
88 {
89         if (!ao_lisp_check_argc(_ao_lisp_atom_car, cons, 1, 1))
90                 return AO_LISP_NIL;
91         if (!ao_lisp_check_argt(_ao_lisp_atom_car, cons, 0, AO_LISP_CONS, 0))
92                 return AO_LISP_NIL;
93         return ao_lisp_poly_cons(cons->car)->car;
94 }
95
96 ao_poly
97 ao_lisp_cdr(struct ao_lisp_cons *cons)
98 {
99         if (!ao_lisp_check_argc(_ao_lisp_atom_cdr, cons, 1, 1))
100                 return AO_LISP_NIL;
101         if (!ao_lisp_check_argt(_ao_lisp_atom_cdr, cons, 0, AO_LISP_CONS, 0))
102                 return AO_LISP_NIL;
103         return ao_lisp_poly_cons(cons->car)->cdr;
104 }
105
106 ao_poly
107 ao_lisp_cons(struct ao_lisp_cons *cons)
108 {
109         ao_poly car, cdr;
110         if(!ao_lisp_check_argc(_ao_lisp_atom_cons, cons, 2, 2))
111                 return AO_LISP_NIL;
112         if (!ao_lisp_check_argt(_ao_lisp_atom_cons, cons, 1, AO_LISP_CONS, 1))
113                 return AO_LISP_NIL;
114         car = ao_lisp_arg(cons, 0);
115         cdr = ao_lisp_arg(cons, 1);
116         return ao_lisp_cons_poly(ao_lisp_cons_cons(car, ao_lisp_poly_cons(cdr)));
117 }
118
119 ao_poly
120 ao_lisp_quote(struct ao_lisp_cons *cons)
121 {
122         if (!ao_lisp_check_argc(_ao_lisp_atom_quote, cons, 1, 1))
123                 return AO_LISP_NIL;
124         return ao_lisp_arg(cons, 0);
125 }
126
127 ao_poly
128 ao_lisp_set(struct ao_lisp_cons *cons)
129 {
130         if (!ao_lisp_check_argc(_ao_lisp_atom_set, cons, 2, 2))
131                 return AO_LISP_NIL;
132         if (!ao_lisp_check_argt(_ao_lisp_atom_set, cons, 0, AO_LISP_ATOM, 0))
133                 return AO_LISP_NIL;
134
135         return ao_lisp_atom_set(ao_lisp_arg(cons, 0), ao_lisp_poly_cons(ao_lisp_arg(cons, 1))->car);
136 }
137
138 ao_poly
139 ao_lisp_setq(struct ao_lisp_cons *cons)
140 {
141         struct ao_lisp_cons     *expand = 0;
142         if (!ao_lisp_check_argc(_ao_lisp_atom_setq, cons, 2, 2))
143                 return AO_LISP_NIL;
144         expand = ao_lisp_cons_cons(_ao_lisp_atom_set,
145                                    ao_lisp_cons_cons(ao_lisp_cons_poly(ao_lisp_cons_cons(_ao_lisp_atom_quote,
146                                                                        ao_lisp_cons_cons(cons->car, NULL))),
147                                                      ao_lisp_poly_cons(cons->cdr)));
148         return ao_lisp_cons_poly(expand);
149 }
150
151 ao_poly
152 ao_lisp_cond(struct ao_lisp_cons *cons)
153 {
154         int                     argc;
155         struct ao_lisp_cons     *arg;
156
157         argc = 0;
158         for (arg = cons, argc = 0; arg; arg = ao_lisp_poly_cons(arg->cdr), argc++) {
159                 if (ao_lisp_poly_type(arg->car) != AO_LISP_CONS)
160                         return ao_lisp_error(AO_LISP_INVALID, "%s: invalid type for arg %d",
161                                              ao_lisp_poly_atom(_ao_lisp_atom_cond)->name, argc);
162         }
163         ao_lisp_set_cond(cons);
164         return AO_LISP_NIL;
165 }
166
167 ao_poly
168 ao_lisp_print(struct ao_lisp_cons *cons)
169 {
170         ao_poly val = AO_LISP_NIL;
171         while (cons) {
172                 val = cons->car;
173                 ao_lisp_poly_print(val);
174                 cons = ao_lisp_poly_cons(cons->cdr);
175                 if (cons)
176                         printf(" ");
177         }
178         return val;
179 }
180
181 ao_poly
182 ao_lisp_math(struct ao_lisp_cons *cons, enum math_op op)
183 {
184         ao_poly ret = AO_LISP_NIL;
185
186         while (cons) {
187                 ao_poly         car = cons->car;
188                 uint8_t         rt = ao_lisp_poly_type(ret);
189                 uint8_t         ct = ao_lisp_poly_type(car);
190
191                 cons = ao_lisp_poly_cons(cons->cdr);
192
193                 if (rt == AO_LISP_NIL)
194                         ret = car;
195
196                 else if (rt == AO_LISP_INT && ct == AO_LISP_INT) {
197                         int     r = ao_lisp_poly_int(ret);
198                         int     c = ao_lisp_poly_int(car);
199
200                         switch(op) {
201                         case math_plus:
202                                 r += c;
203                                 break;
204                         case math_minus:
205                                 r -= c;
206                                 break;
207                         case math_times:
208                                 r *= c;
209                                 break;
210                         case math_divide:
211                                 if (c == 0)
212                                         return ao_lisp_error(AO_LISP_DIVIDE_BY_ZERO, "divide by zero");
213                                 r /= c;
214                                 break;
215                         case math_mod:
216                                 if (c == 0)
217                                         return ao_lisp_error(AO_LISP_DIVIDE_BY_ZERO, "mod by zero");
218                                 r %= c;
219                                 break;
220                         }
221                         ret = ao_lisp_int_poly(r);
222                 }
223
224                 else if (rt == AO_LISP_STRING && ct == AO_LISP_STRING && op == math_plus)
225                         ret = ao_lisp_string_poly(ao_lisp_string_cat(ao_lisp_poly_string(ret),
226                                                                      ao_lisp_poly_string(car)));
227                 else
228                         return ao_lisp_error(AO_LISP_INVALID, "invalid args");
229         }
230         return ret;
231 }
232
233 ao_poly
234 ao_lisp_plus(struct ao_lisp_cons *cons)
235 {
236         return ao_lisp_math(cons, math_plus);
237 }
238
239 ao_poly
240 ao_lisp_minus(struct ao_lisp_cons *cons)
241 {
242         return ao_lisp_math(cons, math_minus);
243 }
244
245 ao_poly
246 ao_lisp_times(struct ao_lisp_cons *cons)
247 {
248         return ao_lisp_math(cons, math_times);
249 }
250
251 ao_poly
252 ao_lisp_divide(struct ao_lisp_cons *cons)
253 {
254         return ao_lisp_math(cons, math_divide);
255 }
256
257 ao_poly
258 ao_lisp_mod(struct ao_lisp_cons *cons)
259 {
260         return ao_lisp_math(cons, math_mod);
261 }
262
263 ao_lisp_func_t ao_lisp_builtins[] = {
264         [builtin_car] = ao_lisp_car,
265         [builtin_cdr] = ao_lisp_cdr,
266         [builtin_cons] = ao_lisp_cons,
267         [builtin_quote] = ao_lisp_quote,
268         [builtin_set] = ao_lisp_set,
269         [builtin_setq] = ao_lisp_setq,
270         [builtin_cond] = ao_lisp_cond,
271         [builtin_print] = ao_lisp_print,
272         [builtin_plus] = ao_lisp_plus,
273         [builtin_minus] = ao_lisp_minus,
274         [builtin_times] = ao_lisp_times,
275         [builtin_divide] = ao_lisp_divide,
276         [builtin_mod] = ao_lisp_mod
277 };
278