-
Notifications
You must be signed in to change notification settings - Fork 0
/
enviroment.c
172 lines (156 loc) · 3.56 KB
/
enviroment.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "enviroment.h"
struct Value *make_enviroment(void)
{
struct Enviroment *e;
if (!(e = allocate(sizeof(struct Enviroment))))
return 0;
e->root = 0;
return make_value(ENVIROMENT, e);
}
struct Value *extend(struct Value *name, struct Value *value, struct Value *env)
{
struct Frame *f;
if (!(f = allocate(sizeof(struct Frame))))
return 0;
f->name = name;
f->value = value;
f->parent = ((struct Enviroment*)env->value)->root;
((struct Enviroment*)env->value)->root = f;
ref_inc(value);
ref_inc(name);
return env;
}
struct Value *extended(struct Value *name, struct Value *value,
struct Value *env)
{
struct Frame *f;
struct Value *result;
if (!(f = allocate(sizeof(struct Frame))))
return 0;
f->name = name;
f->value = value;
f->parent = ((struct Enviroment*)env->value)->root;
if (!(result = make_enviroment()))
return 0;
((struct Enviroment*)result->value)->root = f;
ref_inc(value);
ref_inc(name);
return result;
}
struct Value *bind(struct Value *name, struct Value *value, struct Value *env)
{
struct Frame *f;
f = ((struct Enviroment*)env->value)->root;
while (f)
{
if (*(int*)equal(f->name, name)->value)
{
ref_dec(f->value);
f->value = value;
ref_inc(f->value);
ref_inc(f->name);
return env;
}
f = f->parent;
}
fprintf(stderr, "no such binding\n");
return env;
}
void print_enviroment(struct Enviroment *e)
{
struct Frame *i;
printf("#<enviroment\n");
i = e->root;
while (i)
{
printf(" ");
print_value(i->name);
printf(" => ");
print_value(i->value);
if (i->parent)
{
printf(",\n");
i = i->parent;
}
else
{
printf(" >");
return;
}
}
}
struct Value *resolve(struct Value *name, struct Value *env)
{
struct Frame *current;
current = ((struct Enviroment*)env->value)->root;
while (current)
{
if (*((int*)(equal(current->name, name)->value)))
return current->value;
current = current->parent;
}
return 0;
}
struct Value *equal_enviroment(struct Enviroment *a, struct Enviroment *b)
{
struct Frame *aa, *bb;
aa = a->root;
bb = b->root;
while (1)
{
if (aa == bb)
return boolean(1);
if (!aa || !bb)
return boolean(0);
if (!*(int*)equal(aa->name, bb->name)->value ||
!*(int*)equal(aa->value, bb->name)->value)
return boolean(0);
aa = aa->parent;
bb = bb->parent;
}
}
void free_enviroment(struct Enviroment *e)
{
struct Frame *i, *t;
if (!e) return;
for (i = e->root; i; i = t)
{
ref_dec(i->name);
i->name = 0;
ref_dec(i->value);
i->value = 0;
t = i->parent;
deallocate(i, sizeof(struct Frame));
}
deallocate(e, sizeof(struct Enviroment));
}
void builtin_extend(struct Value *argument, struct Value *env,
struct Value **out)
{
struct Value *name, *value;
name = ((struct Pair*)argument->value)->car;
value = ((struct Pair*)argument->value)->cdr;
if (value->type != PAIR)
return;
value = ((struct Pair*)value->value)->car;
evaluate(value, env, &value);
extend(name, value, env);
*out = value;
}
void builtin_set(struct Value *argument, struct Value *env, struct Value **out)
{
struct Value *name, *value;
name = ((struct Pair*)argument->value)->car;
value = ((struct Pair*)argument->value)->cdr;
if (value->type != PAIR)
return;
value = ((struct Pair*)value->value)->car;
evaluate(value, env, &value);
bind(name, value, env);
*out = value;
}
void builtin_env(struct Value *argument, struct Value *env, struct Value **out)
{
(void) argument;
*out = env;
}