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 | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 74 | void add_c_function(struct environment *env, char *name, void *func, int nargs) |
| 75 | { |
| 76 | struct args *args = new_args(); |
| 77 | args->num_required = nargs; |
| 78 | |
| 79 | add_function(env, name, func, args, NS_FUNCTION); |
| 80 | } |
| 81 | |
| 82 | value_t l_elt(value_t seq, value_t i) |
| 83 | { |
| 84 | if (!listp(seq) || !integerp(i)) |
| 85 | return nil; |
| 86 | |
| 87 | return elt(seq, i >> 2); |
| 88 | } |
| 89 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 90 | value_t l_read_stdin() |
| 91 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 92 | #ifndef NO_READLINE |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 93 | char *string = read_input_line("lisp> "); |
| 94 | if (!string) |
| 95 | return nil; |
| 96 | |
| 97 | struct istream *is = new_stristream_nt(string); |
| 98 | |
| 99 | value_t val = nil; |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 100 | struct error err; |
| 101 | |
| 102 | if (!IS_OKAY((err = read1(is, &val)))) |
| 103 | { |
| 104 | ereport(err); |
| 105 | |
| 106 | del_stristream(is); |
| 107 | free(string); |
| 108 | // tail recursion, yay! |
| 109 | return l_read_stdin(); |
| 110 | } |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 111 | |
| 112 | del_stristream(is); |
| 113 | free(string); |
| 114 | |
| 115 | return val; |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 116 | #else |
| 117 | return nil; |
| 118 | #endif |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 119 | } |
| 120 | |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 121 | value_t l_num_eq(value_t a, value_t b) |
| 122 | { |
| 123 | if (!integerp(a) || !integerp(b)) |
| 124 | { |
| 125 | return nil; |
| 126 | } |
| 127 | |
| 128 | return (a >> 3) == (b >> 3) ? t : nil; |
| 129 | } |
| 130 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 131 | struct error load_std(struct environment *env) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 132 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 133 | E_INIT(); |
| 134 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 135 | add_c_function(env, "+", l_plus, 2); |
| 136 | add_c_function(env, "-", l_minus, 2); |
| 137 | add_c_function(env, "*", l_times, 2); |
| 138 | add_c_function(env, "/", l_divide, 2); |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 139 | add_c_function(env, "=", l_num_eq, 2); |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 140 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 141 | add_c_function(env, "car", car, 1); |
| 142 | add_c_function(env, "cdr", cdr, 1); |
| 143 | add_c_function(env, "cons", cons, 2); |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 144 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 145 | add_c_function(env, "print", l_printval, 1); |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 146 | add_c_function(env, "read-stdin", l_read_stdin, 0); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 147 | add_c_function(env, "apply", l_apply, 2); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 148 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 149 | add_c_function(env, "nilp", l_nilp, 1); |
| 150 | add_c_function(env, "elt", l_elt, 2); |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 151 | |
| 152 | if (!load_library(env, "std")) |
| 153 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 154 | 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] | 155 | } |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 156 | |
| 157 | OKAY(); |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | bool load_library(struct environment *env, char *name) |
| 161 | { |
| 162 | char *lib_paths = getenv("LISP_LIBRARY_PATH"); |
| 163 | |
| 164 | if (!lib_paths) |
| 165 | lib_paths = "/lib/lisp"; |
| 166 | |
| 167 | for (char *p = strtok(lib_paths, ":"); p; p = strtok(NULL, ":")) |
| 168 | { |
| 169 | static char path[512]; |
| 170 | snprintf(path, 512, "%s/%s.lisp", p, name); |
| 171 | |
| 172 | if (file_exists(path)) |
| 173 | { |
| 174 | return load(env, path); |
| 175 | } |
| 176 | |
| 177 | snprintf(path, 512, "%s/%s/%s.lisp", p, name, name); |
| 178 | |
| 179 | if (file_exists(path)) |
| 180 | { |
| 181 | return load(env, path); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | return false; |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 186 | } |