blob: 03e17fca0127c1bdf4c64c28ef94cb10ef688748 [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
swissChili53472e82021-05-08 16:06:32 -07004value_t l_plus(value_t a, value_t b)
swissChilib3ca4fb2021-04-20 10:33:00 -07005{
swissChili53472e82021-05-08 16:06:32 -07006 if (!integerp(a) || !integerp(b))
swissChilib3ca4fb2021-04-20 10:33:00 -07007 return nil;
8
9 return (((a >> 2) + (b >> 2)) << 2) | INT_TAG;
10}
11
swissChili53472e82021-05-08 16:06:32 -070012value_t l_minus(value_t a, value_t b)
swissChili6aff2bb2021-04-20 15:02:53 -070013{
swissChili53472e82021-05-08 16:06:32 -070014 if (!integerp(a) || !integerp(b))
swissChili6aff2bb2021-04-20 15:02:53 -070015 return nil;
16
17 return (((a >> 2) - (b >> 2)) << 2) | INT_TAG;
18}
19
swissChili53472e82021-05-08 16:06:32 -070020value_t l_times(value_t a, value_t b)
swissChili6aff2bb2021-04-20 15:02:53 -070021{
swissChili53472e82021-05-08 16:06:32 -070022 if (!integerp(a) || !integerp(b))
swissChili6aff2bb2021-04-20 15:02:53 -070023 return nil;
24
25 return (((a >> 2) * (b >> 2)) << 2) | INT_TAG;
26}
27
swissChili53472e82021-05-08 16:06:32 -070028value_t l_divide(value_t a, value_t b)
swissChili6aff2bb2021-04-20 15:02:53 -070029{
swissChili53472e82021-05-08 16:06:32 -070030 if (!integerp(a) || !integerp(b))
swissChili6aff2bb2021-04-20 15:02:53 -070031 return nil;
32
33 return (((a >> 2) / (b >> 2)) << 2) | INT_TAG;
34}
35
swissChili53472e82021-05-08 16:06:32 -070036value_t l_printval(value_t val)
swissChili8fc5e2f2021-04-22 13:45:10 -070037{
swissChili53472e82021-05-08 16:06:32 -070038 printval(val, 0);
swissChili8fc5e2f2021-04-22 13:45:10 -070039 return nil;
40}
41
swissChili53472e82021-05-08 16:06:32 -070042void add_function(struct environment *env, char *name, void *func, int nargs)
swissChilib3ca4fb2021-04-20 10:33:00 -070043{
swissChili53472e82021-05-08 16:06:32 -070044 struct function *last, *new = malloc(sizeof(struct function));
swissChilib3ca4fb2021-04-20 10:33:00 -070045
46 last = env->first;
47 new->prev = last;
48 new->name = name;
49 new->nargs = nargs;
50 new->code_ptr = func;
51
swissChilif3e7f182021-04-20 13:57:22 -070052 env->first = new;
swissChilib3ca4fb2021-04-20 10:33:00 -070053}
54
swissChili53472e82021-05-08 16:06:32 -070055void load_std(struct environment *env)
swissChilib3ca4fb2021-04-20 10:33:00 -070056{
swissChili53472e82021-05-08 16:06:32 -070057 add_function(env, "+", l_plus, 2);
58 add_function(env, "-", l_minus, 2);
59 add_function(env, "*", l_times, 2);
60 add_function(env, "/", l_divide, 2);
swissChili8fc5e2f2021-04-22 13:45:10 -070061
swissChili53472e82021-05-08 16:06:32 -070062 add_function(env, "car", car, 1);
63 add_function(env, "cdr", cdr, 1);
64 add_function(env, "cons", cons, 2);
swissChili8fc5e2f2021-04-22 13:45:10 -070065
swissChili53472e82021-05-08 16:06:32 -070066 add_function(env, "print", l_printval, 1);
swissChilib3ca4fb2021-04-20 10:33:00 -070067}