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 | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 42 | 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] | 43 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 44 | struct function *last, *new = malloc(sizeof(struct function)); |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 45 | |
| 46 | last = env->first; |
| 47 | new->prev = last; |
| 48 | new->name = name; |
| 49 | new->nargs = nargs; |
| 50 | new->code_ptr = func; |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 51 | new->namespace = ns; |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 52 | |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 53 | env->first = new; |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 54 | } |
| 55 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 56 | void load_std(struct environment *env) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 57 | { |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 58 | add_function(env, "+", l_plus, 2, NS_FUNCTION); |
| 59 | add_function(env, "-", l_minus, 2, NS_FUNCTION); |
| 60 | add_function(env, "*", l_times, 2, NS_FUNCTION); |
| 61 | add_function(env, "/", l_divide, 2, NS_FUNCTION); |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 62 | |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 63 | add_function(env, "car", car, 1, NS_FUNCTION); |
| 64 | add_function(env, "cdr", cdr, 1, NS_FUNCTION); |
| 65 | add_function(env, "cons", cons, 2, NS_FUNCTION); |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 66 | |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 67 | add_function(env, "print", l_printval, 1, NS_FUNCTION); |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 68 | } |