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 | |
| 4 | value_t l_plus (value_t a, value_t b) |
| 5 | { |
| 6 | if ( !integerp (a) || !integerp (b) ) |
| 7 | return nil; |
| 8 | |
| 9 | return (((a >> 2) + (b >> 2)) << 2) | INT_TAG; |
| 10 | } |
| 11 | |
swissChili | 6aff2bb | 2021-04-20 15:02:53 -0700 | [diff] [blame] | 12 | value_t l_minus (value_t a, value_t b) |
| 13 | { |
| 14 | if ( !integerp (a) || !integerp (b) ) |
| 15 | return nil; |
| 16 | |
| 17 | return (((a >> 2) - (b >> 2)) << 2) | INT_TAG; |
| 18 | } |
| 19 | |
| 20 | value_t l_times (value_t a, value_t b) |
| 21 | { |
| 22 | if ( !integerp (a) || !integerp (b) ) |
| 23 | return nil; |
| 24 | |
| 25 | return (((a >> 2) * (b >> 2)) << 2) | INT_TAG; |
| 26 | } |
| 27 | |
| 28 | value_t l_divide (value_t a, value_t b) |
| 29 | { |
| 30 | if ( !integerp (a) || !integerp (b) ) |
| 31 | return nil; |
| 32 | |
| 33 | return (((a >> 2) / (b >> 2)) << 2) | INT_TAG; |
| 34 | } |
| 35 | |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame^] | 36 | value_t l_printval (value_t val) |
| 37 | { |
| 38 | printval (val, 0); |
| 39 | return nil; |
| 40 | } |
| 41 | |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 42 | void add_function (struct environment *env, char *name, void *func, int nargs) |
| 43 | { |
| 44 | struct function *last, |
| 45 | *new = malloc (sizeof (struct function)); |
| 46 | |
| 47 | last = env->first; |
| 48 | new->prev = last; |
| 49 | new->name = name; |
| 50 | new->nargs = nargs; |
| 51 | new->code_ptr = func; |
| 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 | |
| 56 | void load_std (struct environment *env) |
| 57 | { |
| 58 | add_function (env, "+", l_plus, 2); |
swissChili | 6aff2bb | 2021-04-20 15:02:53 -0700 | [diff] [blame] | 59 | add_function (env, "-", l_minus, 2); |
| 60 | add_function (env, "*", l_times, 2); |
| 61 | add_function (env, "/", l_divide, 2); |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame^] | 62 | |
| 63 | add_function (env, "car", car, 1); |
| 64 | add_function (env, "cdr", cdr, 1); |
| 65 | add_function (env, "cons", cons, 2); |
| 66 | |
| 67 | add_function (env, "print", l_printval, 1); |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 68 | } |