-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperator.c
51 lines (45 loc) · 1.12 KB
/
operator.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "operator.h"
struct Value *operator(struct Value *args, struct Value *body,
struct Value *env)
{
struct Operator *o;
if (!(o = allocate(sizeof(struct Operator))))
return 0;
o->args = args;
o->body = body;
o->env = env;
ref_inc(args);
ref_inc(body);
ref_inc(env);
return make_value(OPERATOR, o);
}
void print_operator()
{
printf("#<closure>");
}
struct Value *equal_operator(struct Operator *a, struct Operator *b)
{
if (*((int*) equal(a->args, b->args)->value) &&
*((int*) equal(a->body, b->body)->value) &&
*((int*) equal(a->env, b->env)->value))
return boolean(1);
return boolean(0);
}
void free_operator(struct Operator *c)
{
ref_dec(c->args);
ref_dec(c->body);
ref_dec(c->env);
deallocate(c, sizeof(struct Operator));
}
void builtin_operator(struct Value *argument, struct Value *env,
struct Value **out)
{
struct Value *arg, *body;
arg = ((struct Pair*)argument->value)->car;
body = ((struct Pair*)argument->value)->cdr;
if (body->type != PAIR)
return;
body = ((struct Pair*)body->value)->car;
*out = operator(arg, body, env);
}