-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathenv.c
57 lines (52 loc) · 1.1 KB
/
env.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
#include <stdlib.h>
#include <assert.h>
#include "util.h"
#include "obj.h"
#include "alloc.h"
#include "lisp.h"
#include "symbol.h"
#include "env.h"
obj_t *lookup_env(env, sym)
obj_t *env, *sym;
{
obj_t *entry;
assert(TSYMBOL == sym->type);
for (; nil != env; env = CDR(env)) {
entry = CAR(env);
assert(TCONS == entry->type);
if (sym == CAR(entry))
return entry;
}
return NULL;
}
obj_t *push_env(env, sym, val)
obj_t *env, *sym, *val;
{
obj_t *entry = alloc_cons(sym, val);
return alloc_cons(entry, env);
}
/* syms: list of symbols
vals: list of evaluated values */
obj_t *augment_env(env, syms, vals)
obj_t *env, *syms, *vals;
{
obj_t *entry;
obj_t *aug_env = env;
if (list_length(syms) != list_length(vals)) fuck("fun/arg mismatch");
while (nil != syms) {
entry = alloc_cons(CAR(syms), CAR(vals));
aug_env = alloc_cons(entry, aug_env);
syms = CDR(syms);
vals = CDR(vals);
}
assert(nil == syms);
assert(nil == vals);
return aug_env;
}
obj_t *pop_env(env)
obj_t *env;
{
/* memory leak */
if (nil == env) return nil;
return CDR(env);
}