-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv.c
147 lines (121 loc) · 2.98 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
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
#include "private.h"
#include <gc/gc.h>
#include <string.h>
/*
* Utterly simple hash table implementation
*/
static unsigned ht_hashf(lreg_t key)
{
return ((uintptr_t)lreg_raw_ptr(key) >> 5) % HT_SIZE;
}
static int _ht_findptr(struct ht_entry *hte, lreg_t key, struct ht_entry **e)
{
int i = 0;
struct ht_entry *ptr;
for (ptr = hte; ptr != NULL; ptr = ptr->next) {
i++;
if ( ptr->key == key )
{
*e = ptr;
return 0;
}
}
return 1;
}
/* Ret values: < 0 => error, 0 => found, 1 not found */
static int ht_findptr(ht_t *ht, lreg_t key, struct ht_entry **e)
{
unsigned n = ht_hashf(key);
return _ht_findptr(ht->table[n], key, e);
}
/* Ret values: < 0 => error, 0 => found, 1 not found */
static int ht_find(ht_t *ht, lreg_t key, lreg_t *res)
{
int n;
struct ht_entry *hte;
n = ht_findptr(ht, key, &hte);
if ( n == 0 )
*res = hte->value;
return n;
}
static int _ht_insert(struct ht_entry **htep, lreg_t key, lreg_t value)
{
struct ht_entry *hte = *htep;
struct ht_entry *e = GC_malloc(sizeof(struct ht_entry));
e->key = key;
e->value = value;
e->next = hte;
*htep = e;
return 0;
}
static int ht_insert(ht_t *ht, lreg_t key, lreg_t value)
{
unsigned n = ht_hashf(key);
assert(n < HT_SIZE);
return _ht_insert(ht->table + n, key, value);
}
/*
* Environment stacks. Mostly Activation Records.
*/
/* Ret values: < 0 => error, 0 => found, 1 not found */
lreg_t env_lookup(lenv_t *env, lreg_t key)
{
int r;
lreg_t res;
r = ht_find(&env->htable, key, &res);
if (r == 1) {
raise_exception("Symbol not found", key);
}
if (r) {
raise_exception("Internal error", key);
}
return res;
}
int env_define(lenv_t *env, lreg_t key, lreg_t value)
{
return ht_insert(&env->htable, key, value);
}
/* Ret values: < 0 => error, 0 => found, 1 not found */
int env_set(lenv_t *env, lreg_t key, lreg_t value)
{
int r;
struct ht_entry *hte;
r = ht_findptr(&env->htable, key, &hte);
if ( r == 0 )
hte->value = value;
return r;
}
void env_pushnew(lenv_t *env, lenv_t *new)
{
new->htable = env->htable;
}
#if 0
static lenv_t le;
int main ()
{
int n1, n2;
lreg_t res = 0;
lenv_t *le2;
GC_INIT();
le.htable = GC_malloc(sizeof(ht_t));
n1 = env_set(&le, LREG(0x505000, 5), LREG(0xa0a000, 0xa));
printf("n1 = %d\n", n1);
n1 = env_lookup(&le, LREG(0x505000, 5), &res);
printf("%llx: %d\n", res, n1);
n1 = env_define(&le, LREG(0x505000, 5), LREG(0xa0a000, 0xa));
printf("n1 = %d\n", n1);
n1 = env_lookup(&le, LREG(0x505000, 5), &res);
printf("%llx: %d\n", res, n1);
printf("Creating new env\n");
le2 = env_pushnew(&le);
n1 = env_lookup(le2, LREG(0x505000, 5), &res);
printf("%llx: %d\n", res, n1);
n1 = env_define(le2, LREG(0x505000, 5), LREG(0xb0b000, 0xa));
printf("n1 = %d\n", n1);
n1 = env_lookup(le2, LREG(0x505000, 5), &res);
printf("%llx: %d\n", res, n1);
printf("Back to old env\n");
n1 = env_lookup(&le, LREG(0x505000, 5), &res);
printf("%llx: %d\n", res, n1);
}
#endif