blob: 224c086a58917c3bef7c059367cb343b7f0b6757 [file] [log] [blame]
swissChilib3ca4fb2021-04-20 10:33:00 -07001#include "std.h"
swissChilif68671f2021-07-05 14:14:44 -07002#include "../plat/plat.h"
swissChilif3e7f182021-04-20 13:57:22 -07003#include <stdlib.h>
swissChilif68671f2021-07-05 14:14:44 -07004#include <string.h>
swissChilib3ca4fb2021-04-20 10:33:00 -07005
swissChili53472e82021-05-08 16:06:32 -07006value_t l_plus(value_t a, value_t b)
swissChilib3ca4fb2021-04-20 10:33:00 -07007{
swissChili53472e82021-05-08 16:06:32 -07008 if (!integerp(a) || !integerp(b))
swissChilib3ca4fb2021-04-20 10:33:00 -07009 return nil;
10
11 return (((a >> 2) + (b >> 2)) << 2) | INT_TAG;
12}
13
swissChili53472e82021-05-08 16:06:32 -070014value_t l_minus(value_t a, value_t b)
swissChili6aff2bb2021-04-20 15:02:53 -070015{
swissChili53472e82021-05-08 16:06:32 -070016 if (!integerp(a) || !integerp(b))
swissChili6aff2bb2021-04-20 15:02:53 -070017 return nil;
18
19 return (((a >> 2) - (b >> 2)) << 2) | INT_TAG;
20}
21
swissChili53472e82021-05-08 16:06:32 -070022value_t l_times(value_t a, value_t b)
swissChili6aff2bb2021-04-20 15:02:53 -070023{
swissChili53472e82021-05-08 16:06:32 -070024 if (!integerp(a) || !integerp(b))
swissChili6aff2bb2021-04-20 15:02:53 -070025 return nil;
26
27 return (((a >> 2) * (b >> 2)) << 2) | INT_TAG;
28}
29
swissChili53472e82021-05-08 16:06:32 -070030value_t l_divide(value_t a, value_t b)
swissChili6aff2bb2021-04-20 15:02:53 -070031{
swissChili53472e82021-05-08 16:06:32 -070032 if (!integerp(a) || !integerp(b))
swissChili6aff2bb2021-04-20 15:02:53 -070033 return nil;
34
35 return (((a >> 2) / (b >> 2)) << 2) | INT_TAG;
36}
37
swissChili53472e82021-05-08 16:06:32 -070038value_t l_printval(value_t val)
swissChili8fc5e2f2021-04-22 13:45:10 -070039{
swissChili53472e82021-05-08 16:06:32 -070040 printval(val, 0);
swissChili8fc5e2f2021-04-22 13:45:10 -070041 return nil;
42}
43
swissChiliddc97542021-07-04 11:47:42 -070044value_t l_apply(value_t func, value_t args)
45{
46 if (!closurep(func))
47 return nil;
48
49 if (!listp(args))
50 return nil;
51
52 return call_list_closure((struct closure *)(func ^ CLOSURE_TAG), args);
53}
54
swissChili15f1cae2021-07-05 19:08:47 -070055value_t l_nilp(value_t val)
56{
57 return nilp(val) ? t : nil;
58}
59
60void add_function(struct environment *env, char *name, void *func, struct args *args, enum namespace ns)
swissChilib3ca4fb2021-04-20 10:33:00 -070061{
swissChili53472e82021-05-08 16:06:32 -070062 struct function *last, *new = malloc(sizeof(struct function));
swissChilib3ca4fb2021-04-20 10:33:00 -070063
64 last = env->first;
65 new->prev = last;
66 new->name = name;
swissChili15f1cae2021-07-05 19:08:47 -070067 new->args = args;
swissChilib3ca4fb2021-04-20 10:33:00 -070068 new->code_ptr = func;
swissChili2999dd12021-07-02 14:19:53 -070069 new->namespace = ns;
swissChilib3ca4fb2021-04-20 10:33:00 -070070
swissChilif3e7f182021-04-20 13:57:22 -070071 env->first = new;
swissChilib3ca4fb2021-04-20 10:33:00 -070072}
73
swissChili1e8b7562021-12-22 21:22:57 -080074void add_c_varargs(struct environment *env, char *name, void *func, int nargs)
75{
76 struct args *args = new_args();
77 args->num_required = nargs;
78 args->variadic = true;
79
80 add_function(env, name, func, args, NS_FUNCTION);
81}
82
swissChili15f1cae2021-07-05 19:08:47 -070083void add_c_function(struct environment *env, char *name, void *func, int nargs)
84{
85 struct args *args = new_args();
86 args->num_required = nargs;
87
88 add_function(env, name, func, args, NS_FUNCTION);
89}
90
91value_t l_elt(value_t seq, value_t i)
92{
93 if (!listp(seq) || !integerp(i))
94 return nil;
95
96 return elt(seq, i >> 2);
97}
98
swissChili7e1393c2021-07-07 12:59:12 -070099value_t l_read_stdin()
100{
swissChili6d02af42021-08-05 19:49:01 -0700101#ifndef NO_READLINE
swissChili7e1393c2021-07-07 12:59:12 -0700102 char *string = read_input_line("lisp> ");
103 if (!string)
104 return nil;
105
106 struct istream *is = new_stristream_nt(string);
107
108 value_t val = nil;
swissChili36f2c692021-08-08 14:31:44 -0700109 struct error err = { 0 };
swissChili6d02af42021-08-05 19:49:01 -0700110
111 if (!IS_OKAY((err = read1(is, &val))))
112 {
113 ereport(err);
114
115 del_stristream(is);
116 free(string);
117 // tail recursion, yay!
118 return l_read_stdin();
119 }
swissChili7e1393c2021-07-07 12:59:12 -0700120
121 del_stristream(is);
122 free(string);
123
124 return val;
swissChili6d02af42021-08-05 19:49:01 -0700125#else
126 return nil;
127#endif
swissChili7e1393c2021-07-07 12:59:12 -0700128}
129
swissChili53e7cd12021-08-02 21:55:53 -0700130value_t l_num_eq(value_t a, value_t b)
131{
132 if (!integerp(a) || !integerp(b))
133 {
134 return nil;
135 }
136
swissChili1e8b7562021-12-22 21:22:57 -0800137 return (a >> 2) == (b >> 2) ? t : nil;
138}
139
140value_t l_num_gt(value_t a, value_t b)
141{
142 if (!integerp(a) || !integerp(b))
143 return nil;
144
145 return (a >> 2) > (b >> 2) ? t : nil;
146}
147
148value_t l_num_lt(value_t a, value_t b)
149{
150 if (!integerp(a) || !integerp(b))
151 return nil;
152
153 return (a >> 2) < (b >> 2) ? t : nil;
154}
155
156value_t l_append(value_t l)
157{
158 if (nilp(l))
159 return l;
160
161 value_t first = nil;
162 value_t *last = NULL;
163
164 for (value_t item = l; !nilp(item); item = cdr(item))
165 {
166 value_t a = car(item);
167
168 if (!listp(a))
169 {
170 value_t new = cons(a, nil);
171 *last = new;
172 last = cdrref(new);
173 continue;
174 }
175
176 for (value_t i = a; !nilp(i); i = cdr(i))
177 {
178 value_t b = car(i);
179
180 if (!last)
181 {
182 first = cons(b, nil);
183 last = cdrref(first);
184 }
185 else
186 {
187 value_t new = cons(b, nil);
188 *last = new;
189 last = cdrref(new);
190 }
191 }
192 }
193
194 return first;
swissChili53e7cd12021-08-02 21:55:53 -0700195}
196
swissChilifc5c9412021-08-08 19:08:26 -0700197#define LISP_PREDICATE(name) value_t l_##name(value_t v) { return name(v) ? t : nil; }
198
199LISP_PREDICATE(listp)
200LISP_PREDICATE(integerp)
201LISP_PREDICATE(symbolp)
202LISP_PREDICATE(closurep)
203LISP_PREDICATE(consp)
204
205#undef LISP_PREDICATE
206
swissChili6d02af42021-08-05 19:49:01 -0700207struct error load_std(struct environment *env)
swissChilib3ca4fb2021-04-20 10:33:00 -0700208{
swissChili6d02af42021-08-05 19:49:01 -0700209 E_INIT();
210
swissChili15f1cae2021-07-05 19:08:47 -0700211 add_c_function(env, "+", l_plus, 2);
212 add_c_function(env, "-", l_minus, 2);
213 add_c_function(env, "*", l_times, 2);
214 add_c_function(env, "/", l_divide, 2);
swissChili53e7cd12021-08-02 21:55:53 -0700215 add_c_function(env, "=", l_num_eq, 2);
swissChili1e8b7562021-12-22 21:22:57 -0800216 add_c_function(env, "<", l_num_lt, 2);
217 add_c_function(env, ">", l_num_gt, 2);
swissChili8fc5e2f2021-04-22 13:45:10 -0700218
swissChili15f1cae2021-07-05 19:08:47 -0700219 add_c_function(env, "car", car, 1);
220 add_c_function(env, "cdr", cdr, 1);
221 add_c_function(env, "cons", cons, 2);
swissChili8fc5e2f2021-04-22 13:45:10 -0700222
swissChili15f1cae2021-07-05 19:08:47 -0700223 add_c_function(env, "print", l_printval, 1);
swissChili7e1393c2021-07-07 12:59:12 -0700224 add_c_function(env, "read-stdin", l_read_stdin, 0);
swissChili15f1cae2021-07-05 19:08:47 -0700225 add_c_function(env, "apply", l_apply, 2);
swissChili1e8b7562021-12-22 21:22:57 -0800226 add_c_varargs(env, "append", l_append, 0);
swissChiliddc97542021-07-04 11:47:42 -0700227
swissChili15f1cae2021-07-05 19:08:47 -0700228 add_c_function(env, "nilp", l_nilp, 1);
swissChilifc5c9412021-08-08 19:08:26 -0700229 add_c_function(env, "listp", l_listp, 1);
230 add_c_function(env, "integerp", l_integerp, 1);
231 add_c_function(env, "symbolp", l_symbolp, 1);
232 add_c_function(env, "closurep", l_closurep, 1);
233 add_c_function(env, "functionp", l_closurep, 1);
234 add_c_function(env, "consp", l_consp, 1);
235
swissChili15f1cae2021-07-05 19:08:47 -0700236 add_c_function(env, "elt", l_elt, 2);
swissChilif68671f2021-07-05 14:14:44 -0700237
238 if (!load_library(env, "std"))
239 {
swissChili1e8b7562021-12-22 21:22:57 -0800240 fprintf(stderr, "Not found std\n");
swissChili6d02af42021-08-05 19:49:01 -0700241 THROW(ENOTFOUND, "Could not load library `std`, make sure your $LISP_LIBRARY_PATH is correct.");
swissChilif68671f2021-07-05 14:14:44 -0700242 }
swissChili6d02af42021-08-05 19:49:01 -0700243
244 OKAY();
swissChilif68671f2021-07-05 14:14:44 -0700245}
246
247bool load_library(struct environment *env, char *name)
248{
249 char *lib_paths = getenv("LISP_LIBRARY_PATH");
250
251 if (!lib_paths)
252 lib_paths = "/lib/lisp";
253
254 for (char *p = strtok(lib_paths, ":"); p; p = strtok(NULL, ":"))
255 {
256 static char path[512];
257 snprintf(path, 512, "%s/%s.lisp", p, name);
258
259 if (file_exists(path))
260 {
swissChili1e8b7562021-12-22 21:22:57 -0800261 // fprintf(stderr, "loading path: %s\n", path);
swissChilif68671f2021-07-05 14:14:44 -0700262 return load(env, path);
263 }
264
265 snprintf(path, 512, "%s/%s/%s.lisp", p, name, name);
266
267 if (file_exists(path))
268 {
swissChili1e8b7562021-12-22 21:22:57 -0800269 // fprintf(stderr, "loading path: %s\n", path);
swissChilif68671f2021-07-05 14:14:44 -0700270 return load(env, path);
271 }
272 }
273
274 return false;
swissChilib3ca4fb2021-04-20 10:33:00 -0700275}