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