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 | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 55 | void add_function(struct environment *env, char *name, void *func, int nargs, enum namespace ns) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 56 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 57 | struct function *last, *new = malloc(sizeof(struct function)); |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 58 | |
| 59 | last = env->first; |
| 60 | new->prev = last; |
| 61 | new->name = name; |
| 62 | new->nargs = nargs; |
| 63 | new->code_ptr = func; |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 64 | new->namespace = ns; |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 65 | |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 66 | env->first = new; |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 67 | } |
| 68 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 69 | void load_std(struct environment *env) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 70 | { |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 71 | add_function(env, "+", l_plus, 2, NS_FUNCTION); |
| 72 | add_function(env, "-", l_minus, 2, NS_FUNCTION); |
| 73 | add_function(env, "*", l_times, 2, NS_FUNCTION); |
| 74 | add_function(env, "/", l_divide, 2, NS_FUNCTION); |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 75 | |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 76 | add_function(env, "car", car, 1, NS_FUNCTION); |
| 77 | add_function(env, "cdr", cdr, 1, NS_FUNCTION); |
| 78 | add_function(env, "cons", cons, 2, NS_FUNCTION); |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 79 | |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 80 | add_function(env, "print", l_printval, 1, NS_FUNCTION); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 81 | |
| 82 | add_function(env, "apply", l_apply, 2, NS_FUNCTION); |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame^] | 83 | |
| 84 | if (!load_library(env, "std")) |
| 85 | { |
| 86 | err("Could not load library `std`, make sure your $LISP_LIBRARY_PATH is correct."); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | bool load_library(struct environment *env, char *name) |
| 91 | { |
| 92 | char *lib_paths = getenv("LISP_LIBRARY_PATH"); |
| 93 | |
| 94 | if (!lib_paths) |
| 95 | lib_paths = "/lib/lisp"; |
| 96 | |
| 97 | for (char *p = strtok(lib_paths, ":"); p; p = strtok(NULL, ":")) |
| 98 | { |
| 99 | static char path[512]; |
| 100 | snprintf(path, 512, "%s/%s.lisp", p, name); |
| 101 | |
| 102 | if (file_exists(path)) |
| 103 | { |
| 104 | return load(env, path); |
| 105 | } |
| 106 | |
| 107 | snprintf(path, 512, "%s/%s/%s.lisp", p, name, name); |
| 108 | |
| 109 | if (file_exists(path)) |
| 110 | { |
| 111 | return load(env, path); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | return false; |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 116 | } |