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