swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 1 | #include "std.h" |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 2 | #include "../plat/plat.h" |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 3 | #include <stdlib.h> |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 4 | #include <string.h> |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 5 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 6 | value_t l_plus(value_t a, value_t b) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 7 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 8 | if (!integerp(a) || !integerp(b)) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 9 | return nil; |
| 10 | |
| 11 | return (((a >> 2) + (b >> 2)) << 2) | INT_TAG; |
| 12 | } |
| 13 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 14 | value_t l_minus(value_t a, value_t b) |
swissChili | 6aff2bb | 2021-04-20 15:02:53 -0700 | [diff] [blame] | 15 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 16 | if (!integerp(a) || !integerp(b)) |
swissChili | 6aff2bb | 2021-04-20 15:02:53 -0700 | [diff] [blame] | 17 | return nil; |
| 18 | |
| 19 | return (((a >> 2) - (b >> 2)) << 2) | INT_TAG; |
| 20 | } |
| 21 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 22 | value_t l_times(value_t a, value_t b) |
swissChili | 6aff2bb | 2021-04-20 15:02:53 -0700 | [diff] [blame] | 23 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 24 | if (!integerp(a) || !integerp(b)) |
swissChili | 6aff2bb | 2021-04-20 15:02:53 -0700 | [diff] [blame] | 25 | return nil; |
| 26 | |
| 27 | return (((a >> 2) * (b >> 2)) << 2) | INT_TAG; |
| 28 | } |
| 29 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 30 | value_t l_divide(value_t a, value_t b) |
swissChili | 6aff2bb | 2021-04-20 15:02:53 -0700 | [diff] [blame] | 31 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 32 | if (!integerp(a) || !integerp(b)) |
swissChili | 6aff2bb | 2021-04-20 15:02:53 -0700 | [diff] [blame] | 33 | return nil; |
| 34 | |
| 35 | return (((a >> 2) / (b >> 2)) << 2) | INT_TAG; |
| 36 | } |
| 37 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 38 | value_t l_printval(value_t val) |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 39 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 40 | printval(val, 0); |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 41 | return nil; |
| 42 | } |
| 43 | |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 44 | value_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 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 55 | value_t l_nilp(value_t val) |
| 56 | { |
| 57 | return nilp(val) ? t : nil; |
| 58 | } |
| 59 | |
| 60 | 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] | 61 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 62 | struct function *last, *new = malloc(sizeof(struct function)); |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 63 | |
| 64 | last = env->first; |
| 65 | new->prev = last; |
| 66 | new->name = name; |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 67 | new->args = args; |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 68 | new->code_ptr = func; |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 69 | new->namespace = ns; |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 70 | |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 71 | env->first = new; |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 72 | } |
| 73 | |
swissChili | 1e8b756 | 2021-12-22 21:22:57 -0800 | [diff] [blame] | 74 | void 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 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 83 | void 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 | |
| 91 | value_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 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 99 | value_t l_read_stdin() |
| 100 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 101 | #ifndef NO_READLINE |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 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; |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 125 | #else |
| 126 | return nil; |
| 127 | #endif |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 128 | } |
| 129 | |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 130 | value_t l_num_eq(value_t a, value_t b) |
| 131 | { |
| 132 | if (!integerp(a) || !integerp(b)) |
| 133 | { |
| 134 | return nil; |
| 135 | } |
| 136 | |
swissChili | 1e8b756 | 2021-12-22 21:22:57 -0800 | [diff] [blame] | 137 | return (a >> 2) == (b >> 2) ? t : nil; |
| 138 | } |
| 139 | |
| 140 | value_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 | |
| 148 | value_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 | |
| 156 | value_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; |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 195 | } |
| 196 | |
swissChili | fc5c941 | 2021-08-08 19:08:26 -0700 | [diff] [blame] | 197 | #define LISP_PREDICATE(name) value_t l_##name(value_t v) { return name(v) ? t : nil; } |
| 198 | |
| 199 | LISP_PREDICATE(listp) |
| 200 | LISP_PREDICATE(integerp) |
| 201 | LISP_PREDICATE(symbolp) |
| 202 | LISP_PREDICATE(closurep) |
| 203 | LISP_PREDICATE(consp) |
| 204 | |
| 205 | #undef LISP_PREDICATE |
| 206 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 207 | struct error load_std(struct environment *env) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 208 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 209 | E_INIT(); |
| 210 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 211 | 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); |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 215 | add_c_function(env, "=", l_num_eq, 2); |
swissChili | 1e8b756 | 2021-12-22 21:22:57 -0800 | [diff] [blame] | 216 | add_c_function(env, "<", l_num_lt, 2); |
| 217 | add_c_function(env, ">", l_num_gt, 2); |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 218 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 219 | add_c_function(env, "car", car, 1); |
| 220 | add_c_function(env, "cdr", cdr, 1); |
| 221 | add_c_function(env, "cons", cons, 2); |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 222 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 223 | add_c_function(env, "print", l_printval, 1); |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 224 | add_c_function(env, "read-stdin", l_read_stdin, 0); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 225 | add_c_function(env, "apply", l_apply, 2); |
swissChili | 1e8b756 | 2021-12-22 21:22:57 -0800 | [diff] [blame] | 226 | add_c_varargs(env, "append", l_append, 0); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 227 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 228 | add_c_function(env, "nilp", l_nilp, 1); |
swissChili | fc5c941 | 2021-08-08 19:08:26 -0700 | [diff] [blame] | 229 | 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 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 236 | add_c_function(env, "elt", l_elt, 2); |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 237 | |
| 238 | if (!load_library(env, "std")) |
| 239 | { |
swissChili | 1e8b756 | 2021-12-22 21:22:57 -0800 | [diff] [blame] | 240 | fprintf(stderr, "Not found std\n"); |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 241 | 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] | 242 | } |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 243 | |
| 244 | OKAY(); |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | bool 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 | { |
swissChili | 1e8b756 | 2021-12-22 21:22:57 -0800 | [diff] [blame] | 261 | // fprintf(stderr, "loading path: %s\n", path); |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 262 | return load(env, path); |
| 263 | } |
| 264 | |
| 265 | snprintf(path, 512, "%s/%s/%s.lisp", p, name, 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 | |
| 274 | return false; |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 275 | } |