~tim/lydia

ref: ac491010406397ebf135aa7cc0de4489660823a3 lydia/src/closure.c -rw-r--r-- 4.5 KiB View raw
ac491010Tim Morgan Backfill vars from function closure on tail calls. 8 years ago
                                                                                
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
164
165
#include "lidija.h"

// creates and initializes an empty closure
LClosure *l_closure_new(LNode *node) {
  l_debug(L_DEBUG_STACK) printf("+++ creating new closure\n");
  LClosure *closure = GC_MALLOC(sizeof(LClosure));
  closure->vars = create_hashmap();
  closure->locals = create_hashmap();
  closure->parent = NULL;
  closure->cloneable = true;
  closure->node = node;
  return closure;
}

// creates a new closure from the given parent closure
LClosure *l_closure_clone(LClosure *parent, LNode *node) {
  l_debug(L_DEBUG_STACK) printf("+++ cloning closure\n");
  if(!parent->cloneable) return parent;
  LClosure *closure = GC_MALLOC(sizeof(LClosure));
  closure->vars = create_hashmap();
  closure->locals = create_hashmap();
  closure->parent = parent;
  closure->cloneable = true;
  closure->node = node;
  // copy vars from function closure
  l_clone_vars(parent->vars, closure->vars);
  l_clone_vars(parent->locals, closure->locals);
  return closure;
}

// copies keys from the source that don't exist on the target
LClosure *l_closure_backfill(LClosure *source, LClosure *target, LNode *node) {
  l_debug(L_DEBUG_STACK) printf("+++ backfilling closure\n");
  target->node = node;
  l_clone_vars_if_missing(source->vars, target->vars);
  l_clone_vars_if_missing(source->locals, target->locals);
  return target;
}

static void l_clone_var_iter(char *key, void *ref, void *to) {
  l_ref_put((hashmap_p)to, key, (LValue**)ref);
}


void l_clone_vars(hashmap_p from, hashmap_p to) {
  hashmap_each(from, l_clone_var_iter, to);
}

static void l_clone_var_if_missing_iter(char *key, void *ref, void *to) {
  if(!hashmap_get((hashmap_p)to, key)) {
    l_ref_put((hashmap_p)to, key, (LValue**)ref);
  }
}

void l_clone_vars_if_missing(hashmap_p from, hashmap_p to) {
  hashmap_each(from, l_clone_var_if_missing_iter, to);
}

void l_closure_free(LClosure *closure) {
  l_debug(L_DEBUG_STACK) printf("--- freeing closure\n");
  if(closure->parent == NULL) return;
}

// given a closure, returns the root closure
LClosure *l_closure_root(LClosure *closure) {
  LClosure *parent = closure;
  while(parent->parent != NULL) {
    parent = parent->parent;
  }
  return parent;
}

LValue **l_ref_get(hashmap_p hash, char *name) {
  LValue **ref;
  if((ref = hashmap_get(hash, name))) {
    return ref;
  }
  return NULL;
}

void l_ref_put(hashmap_p hash, char *name, LValue **ref) {
  hashmap_put(hash, name, ref, sizeof(ref));
}

// sets a key in the closure
void l_closure_set(LClosure *closure, char *name, LValue *value, bool local) {
  LValue **ref = NULL;
  if(local || (ref = l_ref_get(closure->locals, name))) {
    if(ref == NULL) {
      ref = GC_MALLOC(sizeof(LValue*));
      *ref = value;
      l_ref_put(closure->locals, name, ref);
    } else {
      *ref = value;
    }
  } else if((ref = l_ref_get(closure->vars, name))) {
    *ref = value;
  } else {
    ref = GC_MALLOC(sizeof(LValue*));
    *ref = value;
    l_ref_put(closure->vars, name, ref);
  }
}

// sets all built-in functions
// should be used after l_closure_new
void l_closure_set_funcs(LClosure *closure) {
  closure->cloneable = false;
  l_create_funcs(closure);
  l_create_globals(closure);
  l_load_lib(closure);
  closure->cloneable = true;
}

// gets a key in the closure
LValue *l_closure_get(LClosure *closure, char *name) {
  LValue **ref;
  if((ref = l_closure_get_ref(closure, name))) {
    return *ref;
  } else {
    return NULL;
  }
}

LValue **l_closure_get_ref(LClosure *closure, char *name) {
  LValue **ref;
  if((ref = l_ref_get(closure->locals, name)) ||
     (ref = l_ref_get(closure->vars, name))) {
    return ref;
  } else {
    return NULL;
  }
}

LValue **l_closure_pass_by_ref(LNode *node, char *name, LClosure *outer_closure, LClosure *inner_closure) {
  LValue **ref = l_closure_get_ref(outer_closure, node->val);
  if(ref != NULL) {
    if(name != NULL) l_ref_put(inner_closure->locals, name, ref);
    return ref;
  } else {
    // handle error
    l_eval_var_node(node, outer_closure);
    return NULL;
  }
}

static void l_inspect_closure_iter(char *key, void *ref, void *opt) {
  char buf[255] = "";
  printf("  %s = %s\n", key, l_inspect_to_str(*(LValue**)ref, buf, 255));
}

// prints keys and vals in a closure
void l_inspect_closure(LClosure* closure) {
  printf("--------------------\n");
  printf("Closure var contents:\n");
  hashmap_each(closure->vars, l_inspect_closure_iter, NULL);
  printf("Closure local contents:\n");
  hashmap_each(closure->locals, l_inspect_closure_iter, NULL);
  printf("====================\n");
}

int l_closure_size(LClosure *closure) {
  return closure->vars->size;
}