blob: 028dd3d0e24e58a1c7ed8bc03b266e0f319a816b [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
swissChiliddc97542021-07-04 11:47:42 -070042value_t l_apply(value_t func, value_t args)
43{
44 if (!closurep(func))
45 return nil;
46
47 if (!listp(args))
48 return nil;
49
50 return call_list_closure((struct closure *)(func ^ CLOSURE_TAG), args);
51}
52
swissChili2999dd12021-07-02 14:19:53 -070053void add_function(struct environment *env, char *name, void *func, int nargs, enum namespace ns)
swissChilib3ca4fb2021-04-20 10:33:00 -070054{
swissChili53472e82021-05-08 16:06:32 -070055 struct function *last, *new = malloc(sizeof(struct function));
swissChilib3ca4fb2021-04-20 10:33:00 -070056
57 last = env->first;
58 new->prev = last;
59 new->name = name;
60 new->nargs = nargs;
61 new->code_ptr = func;
swissChili2999dd12021-07-02 14:19:53 -070062 new->namespace = ns;
swissChilib3ca4fb2021-04-20 10:33:00 -070063
swissChilif3e7f182021-04-20 13:57:22 -070064 env->first = new;
swissChilib3ca4fb2021-04-20 10:33:00 -070065}
66
swissChili53472e82021-05-08 16:06:32 -070067void load_std(struct environment *env)
swissChilib3ca4fb2021-04-20 10:33:00 -070068{
swissChili2999dd12021-07-02 14:19:53 -070069 add_function(env, "+", l_plus, 2, NS_FUNCTION);
70 add_function(env, "-", l_minus, 2, NS_FUNCTION);
71 add_function(env, "*", l_times, 2, NS_FUNCTION);
72 add_function(env, "/", l_divide, 2, NS_FUNCTION);
swissChili8fc5e2f2021-04-22 13:45:10 -070073
swissChili2999dd12021-07-02 14:19:53 -070074 add_function(env, "car", car, 1, NS_FUNCTION);
75 add_function(env, "cdr", cdr, 1, NS_FUNCTION);
76 add_function(env, "cons", cons, 2, NS_FUNCTION);
swissChili8fc5e2f2021-04-22 13:45:10 -070077
swissChili2999dd12021-07-02 14:19:53 -070078 add_function(env, "print", l_printval, 1, NS_FUNCTION);
swissChiliddc97542021-07-04 11:47:42 -070079
80 add_function(env, "apply", l_apply, 2, NS_FUNCTION);
swissChilib3ca4fb2021-04-20 10:33:00 -070081}