blob: 81759404dd4c06c8506d523b8f5995fa16c3e1bf [file] [log] [blame]
swissChilib3ca4fb2021-04-20 10:33:00 -07001#include "std.h"
swissChilif3e7f182021-04-20 13:57:22 -07002#include <stdlib.h>
swissChilib3ca4fb2021-04-20 10:33:00 -07003
4value_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
swissChili6aff2bb2021-04-20 15:02:53 -070012value_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
20value_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
28value_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
swissChilib3ca4fb2021-04-20 10:33:00 -070036void 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
swissChilif3e7f182021-04-20 13:57:22 -070047 env->first = new;
swissChilib3ca4fb2021-04-20 10:33:00 -070048}
49
50void load_std (struct environment *env)
51{
52 add_function (env, "+", l_plus, 2);
swissChili6aff2bb2021-04-20 15:02:53 -070053 add_function (env, "-", l_minus, 2);
54 add_function (env, "*", l_times, 2);
55 add_function (env, "/", l_divide, 2);
swissChilib3ca4fb2021-04-20 10:33:00 -070056}