swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 1 | #include "std.h" |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 2 | #include <stdlib.h> |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 3 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 4 | value_t l_plus(value_t a, value_t b) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 5 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 6 | if (!integerp(a) || !integerp(b)) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 7 | return nil; |
| 8 | |
| 9 | return (((a >> 2) + (b >> 2)) << 2) | INT_TAG; |
| 10 | } |
| 11 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 12 | value_t l_minus(value_t a, value_t b) |
swissChili | 6aff2bb | 2021-04-20 15:02:53 -0700 | [diff] [blame] | 13 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 14 | if (!integerp(a) || !integerp(b)) |
swissChili | 6aff2bb | 2021-04-20 15:02:53 -0700 | [diff] [blame] | 15 | return nil; |
| 16 | |
| 17 | return (((a >> 2) - (b >> 2)) << 2) | INT_TAG; |
| 18 | } |
| 19 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 20 | value_t l_times(value_t a, value_t b) |
swissChili | 6aff2bb | 2021-04-20 15:02:53 -0700 | [diff] [blame] | 21 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 22 | if (!integerp(a) || !integerp(b)) |
swissChili | 6aff2bb | 2021-04-20 15:02:53 -0700 | [diff] [blame] | 23 | return nil; |
| 24 | |
| 25 | return (((a >> 2) * (b >> 2)) << 2) | INT_TAG; |
| 26 | } |
| 27 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 28 | value_t l_divide(value_t a, value_t b) |
swissChili | 6aff2bb | 2021-04-20 15:02:53 -0700 | [diff] [blame] | 29 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 30 | if (!integerp(a) || !integerp(b)) |
swissChili | 6aff2bb | 2021-04-20 15:02:53 -0700 | [diff] [blame] | 31 | return nil; |
| 32 | |
| 33 | return (((a >> 2) / (b >> 2)) << 2) | INT_TAG; |
| 34 | } |
| 35 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 36 | value_t l_printval(value_t val) |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 37 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 38 | printval(val, 0); |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 39 | return nil; |
| 40 | } |
| 41 | |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 42 | value_t l_apply(value_t func, value_t args) |
| 43 | { |
| 44 | if (!closurep(func)) |
| 45 | return nil; |
| 46 | |
| 47 | if (!listp(args)) |
| 48 | return nil; |
| 49 | |
| 50 | return call_list_closure((struct closure *)(func ^ CLOSURE_TAG), args); |
| 51 | } |
| 52 | |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 53 | 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] | 54 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 55 | struct function *last, *new = malloc(sizeof(struct function)); |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 56 | |
| 57 | last = env->first; |
| 58 | new->prev = last; |
| 59 | new->name = name; |
| 60 | new->nargs = nargs; |
| 61 | new->code_ptr = func; |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 62 | new->namespace = ns; |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 63 | |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 64 | env->first = new; |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 65 | } |
| 66 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 67 | void load_std(struct environment *env) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 68 | { |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 69 | add_function(env, "+", l_plus, 2, NS_FUNCTION); |
| 70 | add_function(env, "-", l_minus, 2, NS_FUNCTION); |
| 71 | add_function(env, "*", l_times, 2, NS_FUNCTION); |
| 72 | add_function(env, "/", l_divide, 2, NS_FUNCTION); |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 73 | |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 74 | add_function(env, "car", car, 1, NS_FUNCTION); |
| 75 | add_function(env, "cdr", cdr, 1, NS_FUNCTION); |
| 76 | add_function(env, "cons", cons, 2, NS_FUNCTION); |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 77 | |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 78 | add_function(env, "print", l_printval, 1, NS_FUNCTION); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 79 | |
| 80 | add_function(env, "apply", l_apply, 2, NS_FUNCTION); |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 81 | } |