swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 1 | #include "std.h" |
swissChili | a890aed | 2022-07-30 17:13:07 -0700 | [diff] [blame] | 2 | #include "../gc.h" |
swissChili | 04d9416 | 2022-07-30 21:46:49 -0700 | [diff] [blame] | 3 | #include "../compiler.h" |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 4 | #include "../plat/plat.h" |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 5 | #include <stdlib.h> |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 6 | #include <string.h> |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 7 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 8 | value_t l_plus(value_t a, value_t b) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 9 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 10 | if (!integerp(a) || !integerp(b)) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 11 | return nil; |
| 12 | |
| 13 | return (((a >> 2) + (b >> 2)) << 2) | INT_TAG; |
| 14 | } |
| 15 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 16 | value_t l_minus(value_t a, value_t b) |
swissChili | 6aff2bb | 2021-04-20 15:02:53 -0700 | [diff] [blame] | 17 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 18 | if (!integerp(a) || !integerp(b)) |
swissChili | 6aff2bb | 2021-04-20 15:02:53 -0700 | [diff] [blame] | 19 | return nil; |
| 20 | |
| 21 | return (((a >> 2) - (b >> 2)) << 2) | INT_TAG; |
| 22 | } |
| 23 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 24 | value_t l_times(value_t a, value_t b) |
swissChili | 6aff2bb | 2021-04-20 15:02:53 -0700 | [diff] [blame] | 25 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 26 | if (!integerp(a) || !integerp(b)) |
swissChili | 6aff2bb | 2021-04-20 15:02:53 -0700 | [diff] [blame] | 27 | return nil; |
| 28 | |
| 29 | return (((a >> 2) * (b >> 2)) << 2) | INT_TAG; |
| 30 | } |
| 31 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 32 | value_t l_divide(value_t a, value_t b) |
swissChili | 6aff2bb | 2021-04-20 15:02:53 -0700 | [diff] [blame] | 33 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 34 | if (!integerp(a) || !integerp(b)) |
swissChili | 6aff2bb | 2021-04-20 15:02:53 -0700 | [diff] [blame] | 35 | return nil; |
| 36 | |
| 37 | return (((a >> 2) / (b >> 2)) << 2) | INT_TAG; |
| 38 | } |
| 39 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 40 | value_t l_printval(value_t val) |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 41 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 42 | printval(val, 0); |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 43 | return nil; |
| 44 | } |
| 45 | |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 46 | value_t l_apply(value_t func, value_t args) |
| 47 | { |
| 48 | if (!closurep(func)) |
| 49 | return nil; |
| 50 | |
| 51 | if (!listp(args)) |
| 52 | return nil; |
| 53 | |
| 54 | return call_list_closure((struct closure *)(func ^ CLOSURE_TAG), args); |
| 55 | } |
| 56 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 57 | value_t l_nilp(value_t val) |
| 58 | { |
| 59 | return nilp(val) ? t : nil; |
| 60 | } |
| 61 | |
| 62 | void add_function(struct environment *env, char *name, void *func, struct args *args, enum namespace ns) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 63 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 64 | struct function *last, *new = malloc(sizeof(struct function)); |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 65 | |
| 66 | last = env->first; |
| 67 | new->prev = last; |
| 68 | new->name = name; |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 69 | new->args = args; |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 70 | new->code_ptr = func; |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 71 | new->namespace = ns; |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 72 | |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 73 | env->first = new; |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 74 | } |
| 75 | |
swissChili | 1e8b756 | 2021-12-22 21:22:57 -0800 | [diff] [blame] | 76 | void add_c_varargs(struct environment *env, char *name, void *func, int nargs) |
| 77 | { |
| 78 | struct args *args = new_args(); |
| 79 | args->num_required = nargs; |
| 80 | args->variadic = true; |
| 81 | |
| 82 | add_function(env, name, func, args, NS_FUNCTION); |
| 83 | } |
| 84 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 85 | void add_c_function(struct environment *env, char *name, void *func, int nargs) |
| 86 | { |
| 87 | struct args *args = new_args(); |
| 88 | args->num_required = nargs; |
| 89 | |
| 90 | add_function(env, name, func, args, NS_FUNCTION); |
| 91 | } |
| 92 | |
| 93 | value_t l_elt(value_t seq, value_t i) |
| 94 | { |
| 95 | if (!listp(seq) || !integerp(i)) |
| 96 | return nil; |
| 97 | |
| 98 | return elt(seq, i >> 2); |
| 99 | } |
| 100 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 101 | value_t l_read_stdin() |
| 102 | { |
| 103 | char *string = read_input_line("lisp> "); |
| 104 | if (!string) |
| 105 | return nil; |
| 106 | |
| 107 | struct istream *is = new_stristream_nt(string); |
| 108 | |
| 109 | value_t val = nil; |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 110 | struct error err = { 0 }; |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 111 | |
| 112 | if (!IS_OKAY((err = read1(is, &val)))) |
| 113 | { |
| 114 | ereport(err); |
| 115 | |
| 116 | del_stristream(is); |
| 117 | free(string); |
| 118 | // tail recursion, yay! |
| 119 | return l_read_stdin(); |
| 120 | } |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 121 | |
| 122 | del_stristream(is); |
| 123 | free(string); |
| 124 | |
| 125 | return val; |
| 126 | } |
| 127 | |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 128 | value_t l_num_eq(value_t a, value_t b) |
| 129 | { |
| 130 | if (!integerp(a) || !integerp(b)) |
| 131 | { |
| 132 | return nil; |
| 133 | } |
| 134 | |
swissChili | 1e8b756 | 2021-12-22 21:22:57 -0800 | [diff] [blame] | 135 | return (a >> 2) == (b >> 2) ? t : nil; |
| 136 | } |
| 137 | |
| 138 | value_t l_num_gt(value_t a, value_t b) |
| 139 | { |
| 140 | if (!integerp(a) || !integerp(b)) |
| 141 | return nil; |
| 142 | |
| 143 | return (a >> 2) > (b >> 2) ? t : nil; |
| 144 | } |
| 145 | |
| 146 | value_t l_num_lt(value_t a, value_t b) |
| 147 | { |
| 148 | if (!integerp(a) || !integerp(b)) |
| 149 | return nil; |
| 150 | |
| 151 | return (a >> 2) < (b >> 2) ? t : nil; |
| 152 | } |
| 153 | |
| 154 | value_t l_append(value_t l) |
| 155 | { |
| 156 | if (nilp(l)) |
| 157 | return l; |
| 158 | |
| 159 | value_t first = nil; |
| 160 | value_t *last = NULL; |
| 161 | |
| 162 | for (value_t item = l; !nilp(item); item = cdr(item)) |
| 163 | { |
| 164 | value_t a = car(item); |
| 165 | |
| 166 | if (!listp(a)) |
| 167 | { |
| 168 | value_t new = cons(a, nil); |
| 169 | *last = new; |
| 170 | last = cdrref(new); |
| 171 | continue; |
| 172 | } |
| 173 | |
| 174 | for (value_t i = a; !nilp(i); i = cdr(i)) |
| 175 | { |
| 176 | value_t b = car(i); |
| 177 | |
| 178 | if (!last) |
| 179 | { |
| 180 | first = cons(b, nil); |
| 181 | last = cdrref(first); |
| 182 | } |
| 183 | else |
| 184 | { |
| 185 | value_t new = cons(b, nil); |
| 186 | *last = new; |
| 187 | last = cdrref(new); |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | return first; |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 193 | } |
| 194 | |
swissChili | a890aed | 2022-07-30 17:13:07 -0700 | [diff] [blame] | 195 | value_t l_gc_stats() |
| 196 | { |
| 197 | struct gc_stats stats = gc_get_stats(); |
| 198 | |
| 199 | return cons(intval(stats.total_allocs), |
| 200 | cons(intval(stats.gc_runs), |
| 201 | nil)); |
| 202 | } |
| 203 | |
swissChili | 04d9416 | 2022-07-30 21:46:49 -0700 | [diff] [blame] | 204 | value_t l_list_functions(value_t env) |
| 205 | { |
| 206 | if (!integerp(env)) |
| 207 | return nil; |
| 208 | |
| 209 | struct environment *e = (void *)env; |
| 210 | value_t list = nil; |
| 211 | |
| 212 | for (struct function *fun = e->first; fun; fun = fun->prev) |
| 213 | { |
| 214 | list = cons(symval(fun->name), list); |
| 215 | } |
| 216 | |
| 217 | return list; |
| 218 | } |
| 219 | |
swissChili | fc5c941 | 2021-08-08 19:08:26 -0700 | [diff] [blame] | 220 | #define LISP_PREDICATE(name) value_t l_##name(value_t v) { return name(v) ? t : nil; } |
| 221 | |
| 222 | LISP_PREDICATE(listp) |
| 223 | LISP_PREDICATE(integerp) |
| 224 | LISP_PREDICATE(symbolp) |
| 225 | LISP_PREDICATE(closurep) |
| 226 | LISP_PREDICATE(consp) |
| 227 | |
| 228 | #undef LISP_PREDICATE |
| 229 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 230 | struct error load_std(struct environment *env) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 231 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 232 | E_INIT(); |
| 233 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 234 | add_c_function(env, "+", l_plus, 2); |
| 235 | add_c_function(env, "-", l_minus, 2); |
| 236 | add_c_function(env, "*", l_times, 2); |
| 237 | add_c_function(env, "/", l_divide, 2); |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 238 | add_c_function(env, "=", l_num_eq, 2); |
swissChili | 1e8b756 | 2021-12-22 21:22:57 -0800 | [diff] [blame] | 239 | add_c_function(env, "<", l_num_lt, 2); |
| 240 | add_c_function(env, ">", l_num_gt, 2); |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 241 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 242 | add_c_function(env, "car", car, 1); |
| 243 | add_c_function(env, "cdr", cdr, 1); |
| 244 | add_c_function(env, "cons", cons, 2); |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 245 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 246 | add_c_function(env, "print", l_printval, 1); |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 247 | add_c_function(env, "read-stdin", l_read_stdin, 0); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 248 | add_c_function(env, "apply", l_apply, 2); |
swissChili | 1e8b756 | 2021-12-22 21:22:57 -0800 | [diff] [blame] | 249 | add_c_varargs(env, "append", l_append, 0); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 250 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 251 | add_c_function(env, "nilp", l_nilp, 1); |
swissChili | fc5c941 | 2021-08-08 19:08:26 -0700 | [diff] [blame] | 252 | add_c_function(env, "listp", l_listp, 1); |
| 253 | add_c_function(env, "integerp", l_integerp, 1); |
| 254 | add_c_function(env, "symbolp", l_symbolp, 1); |
| 255 | add_c_function(env, "closurep", l_closurep, 1); |
| 256 | add_c_function(env, "functionp", l_closurep, 1); |
| 257 | add_c_function(env, "consp", l_consp, 1); |
| 258 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 259 | add_c_function(env, "elt", l_elt, 2); |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 260 | |
swissChili | a890aed | 2022-07-30 17:13:07 -0700 | [diff] [blame] | 261 | add_c_function(env, "gc-stats", l_gc_stats, 0); |
| 262 | |
swissChili | 04d9416 | 2022-07-30 21:46:49 -0700 | [diff] [blame] | 263 | add_c_function(env, "env-functions", l_list_functions, 1); |
| 264 | |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 265 | if (!load_library(env, "std")) |
| 266 | { |
swissChili | 1e8b756 | 2021-12-22 21:22:57 -0800 | [diff] [blame] | 267 | fprintf(stderr, "Not found std\n"); |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 268 | THROW(ENOTFOUND, "Could not load library `std`, make sure your $LISP_LIBRARY_PATH is correct."); |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 269 | } |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 270 | |
| 271 | OKAY(); |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | bool load_library(struct environment *env, char *name) |
| 275 | { |
| 276 | char *lib_paths = getenv("LISP_LIBRARY_PATH"); |
| 277 | |
| 278 | if (!lib_paths) |
| 279 | lib_paths = "/lib/lisp"; |
| 280 | |
| 281 | for (char *p = strtok(lib_paths, ":"); p; p = strtok(NULL, ":")) |
| 282 | { |
| 283 | static char path[512]; |
| 284 | snprintf(path, 512, "%s/%s.lisp", p, name); |
| 285 | |
| 286 | if (file_exists(path)) |
| 287 | { |
swissChili | 1e8b756 | 2021-12-22 21:22:57 -0800 | [diff] [blame] | 288 | // fprintf(stderr, "loading path: %s\n", path); |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 289 | return load(env, path); |
| 290 | } |
| 291 | |
| 292 | snprintf(path, 512, "%s/%s/%s.lisp", p, name, name); |
| 293 | |
| 294 | if (file_exists(path)) |
| 295 | { |
swissChili | 1e8b756 | 2021-12-22 21:22:57 -0800 | [diff] [blame] | 296 | // fprintf(stderr, "loading path: %s\n", path); |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 297 | return load(env, path); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | return false; |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 302 | } |