-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv.cpp
More file actions
64 lines (49 loc) · 1.65 KB
/
env.cpp
File metadata and controls
64 lines (49 loc) · 1.65 KB
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
#include "binding.h"
#include "assert.h"
#include "value_helpers.h"
#include "env.h"
#include <stdio.h>
#include <stdlib.h>
value_t *environment_binding_find(vm_t *p_vm, value_t * p_symbol, bool p_func)
{
return environment_binding_find(p_vm, p_vm->m_current_env[p_vm->m_ev - 1], p_symbol, p_func);
}
value_t *environment_binding_find(vm_t *p_vm, value_t * p_env, value_t * p_symbol, bool p_func)
{
if (p_env == p_vm->nil || is_fixnum(p_env) || p_env == NULL || p_env->m_type != VT_ENVIRONMENT) {
return NULL;
}
if (p_symbol == p_vm->nil || is_fixnum(p_symbol) || p_symbol == NULL || p_symbol->m_type != VT_SYMBOL) {
return NULL;
}
while(p_env) {
environment_t *env = (environment_t *)p_env->m_data;
assert((env->m_parent == NULL) || is_environment(p_vm, env->m_parent));
assert((env->m_bindings == NULL) || is_binding(p_vm, env->m_bindings));
assert((env->m_function_bindings == NULL) || is_binding(p_vm, env->m_function_bindings));
value_t *top_binding = p_func == true ?
((environment_t *)p_env->m_data)->m_function_bindings :
((environment_t *)p_env->m_data)->m_bindings;
value_t *b = binding_find(p_vm, top_binding, p_symbol);
if (b != NULL) {
return b;
}
p_env = env->m_parent;
}
return NULL;
}
value_t *environment_get_bindings(value_t *p_env)
{
environment_t *env = (environment_t *)p_env->m_data;
return env->m_bindings;
}
value_t *environment_get_fbindings(value_t *p_env)
{
environment_t *env = (environment_t *)p_env->m_data;
return env->m_function_bindings;
}
value_t *environment_get_parent(value_t *p_env)
{
environment_t *env = (environment_t *)p_env->m_data;
return env->m_parent;
}