-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenv.c
68 lines (61 loc) · 1.6 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
58
59
60
61
62
63
64
65
66
67
68
#include <string.h>
#include <stdio.h>
#include "tinylisp.h"
tl_object *tl_env_get_kv(tl_interp *in, tl_object *env, tl_object *nm) {
for(tl_list_iter(env, frame)) {
for(tl_list_iter(frame, kv)) {
tl_object *key = tl_first(kv);
tl_object *val = tl_next(kv);
if(key && tl_is_sym(key) && tl_sym_eq(key, nm)) {
return kv;
}
}
}
return NULL;
}
tl_object *tl_env_set_global(tl_interp *in, tl_object *env, tl_object *nm, tl_object *val) {
tl_object *kv = tl_env_get_kv(in, env, nm);
if(kv && tl_is_pair(kv)) {
kv->next = val;
return env;
}
if(!env) {
env = tl_new_pair(in, TL_EMPTY_LIST, env);
}
for(tl_list_iter(env, frame)) {
if(!tl_next(l_frame)) {
l_frame->first = tl_frm_set(in, frame, nm, val);
}
}
return env;
}
tl_object *tl_env_set_local(tl_interp *in, tl_object *env, tl_object *nm, tl_object *val) {
if(!env) {
env = tl_new_pair(in, TL_EMPTY_LIST, env);
}
env->first = tl_frm_set(in, tl_first(env), nm, val);
return env;
}
tl_object *tl_frm_set(tl_interp *in, tl_object *frm, tl_object *nm, tl_object *val) {
for(tl_list_iter(frm, kv)) {
if(kv && tl_is_pair(kv) && tl_is_sym(tl_first(kv)) && tl_sym_eq(tl_first(kv), nm)) {
kv->next = val;
return frm;
}
}
return tl_new_pair(in, tl_new_pair(in, nm, val), frm);
}
tl_object *tl_env_top_pair(tl_interp *in) {
tl_object *frm = NULL;
for(tl_list_iter(in->env, kv)) {
frm = l_kv;
}
return frm;
}
void tl_env_merge(tl_interp *in, tl_object *pair, tl_object *frame) {
if(!tl_is_pair(pair)) return;
while(frame) {
pair->first = tl_new_pair(in, tl_first(frame), tl_first(pair));
frame = tl_next(frame);
}
}