-
Notifications
You must be signed in to change notification settings - Fork 11
/
Env.HC
163 lines (145 loc) · 3.41 KB
/
Env.HC
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
#ifndef ENV_HC
#define ENV_HC
// An environment mapping strings to Malvals
#include "GCCommon.HC"
#include "Hashmap.HC"
#include "String.HC"
extern class Malval;
extern String *PrintMalval(Malval*, BOOL, BOOL);
extern Malval *MalStringMk(String *s);
class Env
{
BOOL _reachable; // for GC
CHAR _gc_tag;
Env *_outer; // parent environment
Hashmap *_data; // symbol table
};
VOID EnvGCMark(Env *e)
{
if (!e->_reachable) {
e->_reachable = TRUE;
if (e->_outer) EnvGCMark(e->_outer);
HashGCMark(e->_data);
}
}
Env *EnvEmpty(Env *outer)
{
Env *m = mal_alloc(sizeof(Env));
m->_gc_tag = GC_ENV;
m->_outer = outer;
m->_data = HashmapEmpty;
return m;
}
VOID EnvPrint(Env *m)
{
NullCheck(m, "environment");
if (m->_outer)
EnvPrint(m->_outer);
INT i;
PArray *keys = HashmapKeys(m->_data);
Malval *key, *val;
String *valString;
CHAR *c;
for (i = 0; i < PArraySize(keys); ++i) {
key = PArrayGet(keys, i)(Malval*);
val = HashmapGet(m->_data, key);
StringPrint(PrintMalval(key, TRUE, TRUE));
valString = PrintMalval(val, TRUE, TRUE);
c = StringCString(valString);
":%s\n", c;
Free(c);
}
}
VOID EnvDelete(Env *m)
{
NullCheck(m, "environment");
HashmapDelete(m->_data);
Free(m);
}
// doesn't make a copy of key or val
VOID EnvSet(Env *m, String *key, Malval *val)
{
NullCheck(m, "environment");
NullCheck(key, "key");
HashmapSet(m->_data, MalStringMk(key), val);
}
Malval *EnvGet(Env *m, String *key)
{
NullCheck(m, "environment");
NullCheck(key, "key");
Malval *val = HashmapGet(m->_data, MalStringMk(key))(Malval*);
if (val) return val;
else if (m->_outer) return EnvGet(m->_outer, key);
else return NULL;
}
Malval *EnvGetC(Env *m, CHAR *key)
{
NullCheck(m, "environment");
NullCheck(key, "key");
Malval *val = HashmapGet(m->_data, MalStringMk(StringMk(key)))(Malval*);
if (val) return val;
else if (m->_outer) return EnvGetC(m->_outer, key);
else return NULL;
}
Env *EnvCopy(Env *m)
{
NullCheck(m, "environment");
Env *e = mal_alloc(sizeof(Env));
e->_outer = m->_outer;
e->_data = HashmapCopy(m->_data);
return e;
}
PArray *EnvKeys(Env *m)
{
NullCheck(m, "environment");
PArray *keys = PArrayEmpty;
PArray *hashmap_keys = HashmapKeys(m->_data);
PArrayConcat(keys, hashmap_keys);
return keys;
}
PArray *EnvValues(Env *m)
{
NullCheck(m, "environment");
PArray *vals = PArrayEmpty;
PArray *hashmap_vals = HashmapValues(m->_data);
PArrayConcat(vals, hashmap_vals);
return vals;
}
// Returns a PArray containing the key/value pairs as alternating elements.
// ONLY FOR USE BY THE GC
PArray *_EnvEntries_GC(Env *m)
{
NullCheck(m, "environment");
PArray *outer_entries;
PArray *entries = _HashmapKeysValues_GC(m->_data);
if (m->_outer) {
outer_entries = _EnvEntries_GC(m->_outer);
PArrayConcat(entries, outer_entries);
PArrayDelete(outer_entries);
}
return entries;
}
// Merge two environments m1 and m2 with m2 taking priority.
Env *EnvMerge(Env *m1, Env *m2)
{
NullCheck(m1, "environment 1");
NullCheck(m2, "environment 2");
INT i;
Env *copy = EnvCopy(m1);
PArray *keys = EnvKeys(m2);
String *key;
Malval *val;
for (i = 0; i < PArraySize(keys); ++i) {
key = StringOfMalval(PArrayGet(keys, i));
val = EnvGet(m2, key);
EnvSet(copy, key, val);
}
return copy;
}
Env *EnvRoot(Env *m)
{
NullCheck(m, "environment");
if (m->_outer) return EnvRoot(m->_outer);
else return m;
}
#endif