blob: c5816871c2eb65bac9cb55a6ae928afad97c53d7 [file] [log] [blame]
Damien429d7192013-10-04 19:53:11 +01001#include <stdint.h>
2#include <stdio.h>
3#include <string.h>
4#include <assert.h>
5
6#include "misc.h"
Damienc025ebb2013-10-12 14:30:21 +01007#include "mpyconfig.h"
Damien429d7192013-10-04 19:53:11 +01008#include "parse.h"
9#include "scope.h"
10
Damien6cdd3af2013-10-05 18:08:26 +010011scope_t *scope_new(scope_kind_t kind, py_parse_node_t pn, uint unique_code_id, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +010012 scope_t *scope = m_new(scope_t, 1);
13 scope->kind = kind;
14 scope->parent = NULL;
15 scope->next = NULL;
16 scope->pn = pn;
17 switch (kind) {
18 case SCOPE_MODULE:
19 scope->simple_name = 0;
20 break;
21 case SCOPE_FUNCTION:
22 case SCOPE_CLASS:
23 assert(PY_PARSE_NODE_IS_STRUCT(pn));
24 scope->simple_name = PY_PARSE_NODE_LEAF_ARG(((py_parse_node_struct_t*)pn)->nodes[0]);
25 break;
26 case SCOPE_LAMBDA:
27 scope->simple_name = qstr_from_str_static("<lambda>");
28 break;
29 case SCOPE_LIST_COMP:
30 scope->simple_name = qstr_from_str_static("<listcomp>");
31 break;
32 case SCOPE_DICT_COMP:
33 scope->simple_name = qstr_from_str_static("<dictcomp>");
34 break;
35 case SCOPE_SET_COMP:
36 scope->simple_name = qstr_from_str_static("<setcomp>");
37 break;
38 case SCOPE_GEN_EXPR:
39 scope->simple_name = qstr_from_str_static("<genexpr>");
40 break;
41 default:
42 assert(0);
43 }
44 scope->id_info_alloc = 8;
45 scope->id_info_len = 0;
46 scope->id_info = m_new(id_info_t, scope->id_info_alloc);
47
48 scope->flags = 0;
49 scope->num_params = 0;
50 /* not needed
51 scope->num_default_params = 0;
52 scope->num_dict_params = 0;
53 */
54 scope->num_locals = 0;
Damienb05d7072013-10-05 13:37:10 +010055 scope->unique_code_id = unique_code_id;
Damien6cdd3af2013-10-05 18:08:26 +010056 scope->emit_options = emit_options;
Damien429d7192013-10-04 19:53:11 +010057
58 return scope;
59}
60
61id_info_t *scope_find_or_add_id(scope_t *scope, qstr qstr, bool *added) {
62 for (int i = 0; i < scope->id_info_len; i++) {
63 if (scope->id_info[i].qstr == qstr) {
64 *added = false;
65 return &scope->id_info[i];
66 }
67 }
68
69 // make sure we have enough memory
70 if (scope->id_info_len >= scope->id_info_alloc) {
71 scope->id_info_alloc *= 2;
72 scope->id_info = m_renew(id_info_t, scope->id_info, scope->id_info_alloc);
73 }
74
75 id_info_t *id_info;
76
77 {
78 /*
79 // just pick next slot in array
80 id_info = &scope->id_info[scope->id_info_len++];
81 */
82 }
83
Damien6cdd3af2013-10-05 18:08:26 +010084 if (0) {
85 // sort insert into id_info array, so we are equivalent to CPython (no other reason to do it)
86 // actually, seems that this is not what CPython does...
87 scope->id_info_len += 1;
88 for (int i = scope->id_info_len - 1;; i--) {
89 if (i == 0 || strcmp(qstr_str(scope->id_info[i - 1].qstr), qstr_str(qstr)) < 0) {
90 id_info = &scope->id_info[i];
91 break;
92 } else {
93 scope->id_info[i] = scope->id_info[i - 1];
94 }
Damien429d7192013-10-04 19:53:11 +010095 }
Damien6cdd3af2013-10-05 18:08:26 +010096 } else {
97 // just add new id to end of array of all ids; this seems to match CPython
98 // important thing is that function arguments are first, but that is
99 // handled by the compiler because it adds arguments before compiling the body
100 id_info = &scope->id_info[scope->id_info_len++];
Damien429d7192013-10-04 19:53:11 +0100101 }
102
103 id_info->param = false;
104 id_info->kind = 0;
105 id_info->qstr = qstr;
106 *added = true;
107 return id_info;
108}
109
110id_info_t *scope_find(scope_t *scope, qstr qstr) {
111 for (int i = 0; i < scope->id_info_len; i++) {
112 if (scope->id_info[i].qstr == qstr) {
113 return &scope->id_info[i];
114 }
115 }
116 return NULL;
117}
118
119id_info_t *scope_find_global(scope_t *scope, qstr qstr) {
120 while (scope->parent != NULL) {
121 scope = scope->parent;
122 }
123 for (int i = 0; i < scope->id_info_len; i++) {
124 if (scope->id_info[i].qstr == qstr) {
125 return &scope->id_info[i];
126 }
127 }
128 return NULL;
129}
130
131id_info_t *scope_find_local_in_parent(scope_t *scope, qstr qstr) {
132 if (scope->parent == NULL) {
133 return NULL;
134 }
135 for (scope_t *s = scope->parent; s->parent != NULL; s = s->parent) {
136 for (int i = 0; i < s->id_info_len; i++) {
137 if (s->id_info[i].qstr == qstr) {
138 return &s->id_info[i];
139 }
140 }
141 }
142 return NULL;
143}
144
145void scope_close_over_in_parents(scope_t *scope, qstr qstr) {
146 assert(scope->parent != NULL); // we should have at least 1 parent
147 for (scope_t *s = scope->parent; s->parent != NULL; s = s->parent) {
148 id_info_t *id = NULL;
149 for (int i = 0; i < s->id_info_len; i++) {
150 if (s->id_info[i].qstr == qstr) {
151 id = &s->id_info[i];
152 break;
153 }
154 }
155 if (id == NULL) {
156 // variable not declared in this scope, so declare it as free and keep searching parents
157 bool added;
158 id = scope_find_or_add_id(s, qstr, &added);
159 assert(added);
160 id->kind = ID_INFO_KIND_FREE;
161 } else {
162 // variable is declared in this scope, so finish
163 switch (id->kind) {
164 case ID_INFO_KIND_LOCAL: id->kind = ID_INFO_KIND_CELL; break; // variable local to this scope, close it over
165 case ID_INFO_KIND_FREE: break; // variable already closed over in a parent scope
166 case ID_INFO_KIND_CELL: break; // variable already closed over in this scope
167 default: assert(0); // TODO
168 }
169 return;
170 }
171 }
172 assert(0); // we should have found the variable in one of the parents
173}
174
Damien415eb6f2013-10-05 12:19:06 +0100175void scope_declare_global(scope_t *scope, qstr qstr) {
176 if (scope->kind == SCOPE_MODULE) {
177 printf("SyntaxError?: can't declare global in outer code\n");
178 return;
179 }
180 bool added;
181 id_info_t *id_info = scope_find_or_add_id(scope, qstr, &added);
182 if (!added) {
183 printf("SyntaxError?: identifier already declared something\n");
184 return;
185 }
186 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
187
188 // if the id exists in the global scope, set its kind to EXPLICIT_GLOBAL
189 id_info = scope_find_global(scope, qstr);
190 if (id_info != NULL) {
191 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
192 }
193}
194
195void scope_declare_nonlocal(scope_t *scope, qstr qstr) {
196 if (scope->kind == SCOPE_MODULE) {
197 printf("SyntaxError?: can't declare nonlocal in outer code\n");
198 return;
199 }
200 bool added;
201 id_info_t *id_info = scope_find_or_add_id(scope, qstr, &added);
202 if (!added) {
203 printf("SyntaxError?: identifier already declared something\n");
204 return;
205 }
206 id_info_t *id_info2 = scope_find_local_in_parent(scope, qstr);
207 if (id_info2 == NULL || !(id_info2->kind == ID_INFO_KIND_LOCAL || id_info2->kind == ID_INFO_KIND_CELL || id_info2->kind == ID_INFO_KIND_FREE)) {
208 printf("SyntaxError: no binding for nonlocal '%s' found\n", qstr_str(qstr));
209 return;
210 }
211 id_info->kind = ID_INFO_KIND_FREE;
212 scope_close_over_in_parents(scope, qstr);
213}
214
Damien429d7192013-10-04 19:53:11 +0100215void scope_print_info(scope_t *s) {
216 if (s->kind == SCOPE_MODULE) {
217 printf("code <module>\n");
218 } else if (s->kind == SCOPE_LAMBDA) {
219 printf("code <lambda>\n");
220 } else if (s->kind == SCOPE_LIST_COMP) {
221 printf("code <listcomp>\n");
222 } else if (s->kind == SCOPE_DICT_COMP) {
223 printf("code <dictcomp>\n");
224 } else if (s->kind == SCOPE_SET_COMP) {
225 printf("code <setcomp>\n");
226 } else if (s->kind == SCOPE_GEN_EXPR) {
227 printf("code <genexpr>\n");
228 } else {
229 printf("code %s\n", qstr_str(s->simple_name));
230 }
231 /*
232 printf("var global:");
233 for (int i = 0; i < s->id_info_len; i++) {
234 if (s->id_info[i].kind == ID_INFO_KIND_GLOBAL_EXPLICIT) {
235 printf(" %s", qstr_str(s->id_info[i].qstr));
236 }
237 }
238 printf("\n");
239 printf("var name:");
240 for (int i = 0; i < s->id_info_len; i++) {
241 if (s->id_info[i].kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
242 printf(" %s", qstr_str(s->id_info[i].qstr));
243 }
244 }
245 printf("\n");
246 printf("var local:");
247 for (int i = 0; i < s->id_info_len; i++) {
248 if (s->id_info[i].kind == ID_INFO_KIND_LOCAL) {
249 printf(" %s", qstr_str(s->id_info[i].qstr));
250 }
251 }
252 printf("\n");
253 printf("var free:");
254 for (int i = 0; i < s->id_info_len; i++) {
255 if (s->id_info[i].kind == ID_INFO_KIND_FREE) {
256 printf(" %s", qstr_str(s->id_info[i].qstr));
257 }
258 }
259 printf("\n");
260 */
261 printf(" flags %04x\n", s->flags);
262 printf(" argcount %d\n", s->num_params);
263 printf(" nlocals %d\n", s->num_locals);
264 printf(" stacksize %d\n", s->stack_size);
265}