blob: 9c1f3cb3ba50c373c4b669b41053fda4a475259d [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
swissChili8fc5e2f2021-04-22 13:45:10 -070036value_t l_printval (value_t val)
37{
38 printval (val, 0);
39 return nil;
40}
41
swissChilib3ca4fb2021-04-20 10:33:00 -070042void 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
swissChilif3e7f182021-04-20 13:57:22 -070053 env->first = new;
swissChilib3ca4fb2021-04-20 10:33:00 -070054}
55
56void load_std (struct environment *env)
57{
58 add_function (env, "+", l_plus, 2);
swissChili6aff2bb2021-04-20 15:02:53 -070059 add_function (env, "-", l_minus, 2);
60 add_function (env, "*", l_times, 2);
61 add_function (env, "/", l_divide, 2);
swissChili8fc5e2f2021-04-22 13:45:10 -070062
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);
swissChilib3ca4fb2021-04-20 10:33:00 -070068}