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 | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 36 | void add_function (struct environment *env, char *name, void *func, int nargs) |
| 37 | { |
| 38 | struct function *last, |
| 39 | *new = malloc (sizeof (struct function)); |
| 40 | |
| 41 | last = env->first; |
| 42 | new->prev = last; |
| 43 | new->name = name; |
| 44 | new->nargs = nargs; |
| 45 | new->code_ptr = func; |
| 46 | |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 47 | env->first = new; |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void load_std (struct environment *env) |
| 51 | { |
| 52 | add_function (env, "+", l_plus, 2); |
swissChili | 6aff2bb | 2021-04-20 15:02:53 -0700 | [diff] [blame^] | 53 | add_function (env, "-", l_minus, 2); |
| 54 | add_function (env, "*", l_times, 2); |
| 55 | add_function (env, "/", l_divide, 2); |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 56 | } |