-
Notifications
You must be signed in to change notification settings - Fork 0
/
lenv.c
86 lines (65 loc) · 2.14 KB
/
lenv.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
#include <stdlib.h>
#include "lenv.h"
/* lenv CONSTRUCOR */
lenv * lenv_new(void) {
lenv * v = malloc(sizeof(lenv));
v->parent = NULL;
v->count = 0;
v->syms = NULL;
v->vals = NULL;
return v;
}
/* lenv DESTRUCTOR */
void lenv_del(lenv * e) {
for (int i = 0; i < e->count; i++) {
free(e->syms[i]);
lval_del(e->vals[i]);
}
free(e->syms);
free(e->vals);
free(e);
}
/* lenv interface */
lval * lenv_get(lenv * e, lval * name) {
for (int i = 0; i < e->count; i++)
if (strcmp(e->syms[i], name->sym) == 0)
return lval_copy(e->vals[i]);
if (e->parent)
return lenv_get(e->parent, name);
else
return lval_err("Undound Symbol '%s'", name->sym);
}
/* define variable locally */
void lenv_put(lenv * e, lval * name, lval * value) {
for (int i = 0; i < e->count; i++) {
if (strcmp(e->syms[i], name->sym) == 0) {
lval_del(e->vals[i]);
e->vals[i] = lval_copy(value);
return;
}
}
e->count++;
e->syms = realloc(e->syms, sizeof(char*) * e->count);
e->vals = realloc(e->vals, sizeof(lval*) * e->count);
e->syms[e->count - 1] = malloc(strlen(name->sym) + 1);
strcpy(e->syms[e->count - 1], name->sym);
e->vals[e->count - 1] = lval_copy(value);
}
/* define variable globally */
void lenv_def(lenv * e, lval * name, lval * value) {
while (e->parent) e = e->parent;
lenv_put(e, name, value);
}
lenv * lenv_copy(lenv * e) {
lenv * copy = malloc(sizeof(lenv));
copy->parent = e->parent;
copy->count = e->count;
copy->syms = malloc(sizeof(char*) * copy->count);
copy->vals = malloc(sizeof(lval*) * copy->count);
for (int i = 0; i < e->count; i++) {
copy->syms[i] = malloc(strlen(e->syms[i]) + 1);
strcpy(copy->syms[i], e->syms[i]);
copy->vals[i] = lval_copy(e->vals[i]);
}
return copy;
}