blob: 7f844108e38abbccdb210e83edecb2af536e4d50 [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;
Damien27fb45e2013-10-20 15:07:49 +0100106 id_info->local_num = 0;
Damien429d7192013-10-04 19:53:11 +0100107 *added = true;
108 return id_info;
109}
110
111id_info_t *scope_find(scope_t *scope, qstr qstr) {
112 for (int i = 0; i < scope->id_info_len; i++) {
113 if (scope->id_info[i].qstr == qstr) {
114 return &scope->id_info[i];
115 }
116 }
117 return NULL;
118}
119
120id_info_t *scope_find_global(scope_t *scope, qstr qstr) {
121 while (scope->parent != NULL) {
122 scope = scope->parent;
123 }
124 for (int i = 0; i < scope->id_info_len; i++) {
125 if (scope->id_info[i].qstr == qstr) {
126 return &scope->id_info[i];
127 }
128 }
129 return NULL;
130}
131
132id_info_t *scope_find_local_in_parent(scope_t *scope, qstr qstr) {
133 if (scope->parent == NULL) {
134 return NULL;
135 }
136 for (scope_t *s = scope->parent; s->parent != NULL; s = s->parent) {
137 for (int i = 0; i < s->id_info_len; i++) {
138 if (s->id_info[i].qstr == qstr) {
139 return &s->id_info[i];
140 }
141 }
142 }
143 return NULL;
144}
145
146void scope_close_over_in_parents(scope_t *scope, qstr qstr) {
147 assert(scope->parent != NULL); // we should have at least 1 parent
148 for (scope_t *s = scope->parent; s->parent != NULL; s = s->parent) {
149 id_info_t *id = NULL;
150 for (int i = 0; i < s->id_info_len; i++) {
151 if (s->id_info[i].qstr == qstr) {
152 id = &s->id_info[i];
153 break;
154 }
155 }
156 if (id == NULL) {
157 // variable not declared in this scope, so declare it as free and keep searching parents
158 bool added;
159 id = scope_find_or_add_id(s, qstr, &added);
160 assert(added);
161 id->kind = ID_INFO_KIND_FREE;
162 } else {
163 // variable is declared in this scope, so finish
164 switch (id->kind) {
165 case ID_INFO_KIND_LOCAL: id->kind = ID_INFO_KIND_CELL; break; // variable local to this scope, close it over
166 case ID_INFO_KIND_FREE: break; // variable already closed over in a parent scope
167 case ID_INFO_KIND_CELL: break; // variable already closed over in this scope
168 default: assert(0); // TODO
169 }
170 return;
171 }
172 }
173 assert(0); // we should have found the variable in one of the parents
174}
175
Damien415eb6f2013-10-05 12:19:06 +0100176void scope_declare_global(scope_t *scope, qstr qstr) {
177 if (scope->kind == SCOPE_MODULE) {
178 printf("SyntaxError?: can't declare global in outer code\n");
179 return;
180 }
181 bool added;
182 id_info_t *id_info = scope_find_or_add_id(scope, qstr, &added);
183 if (!added) {
184 printf("SyntaxError?: identifier already declared something\n");
185 return;
186 }
187 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
188
189 // if the id exists in the global scope, set its kind to EXPLICIT_GLOBAL
190 id_info = scope_find_global(scope, qstr);
191 if (id_info != NULL) {
192 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
193 }
194}
195
196void scope_declare_nonlocal(scope_t *scope, qstr qstr) {
197 if (scope->kind == SCOPE_MODULE) {
198 printf("SyntaxError?: can't declare nonlocal in outer code\n");
199 return;
200 }
201 bool added;
202 id_info_t *id_info = scope_find_or_add_id(scope, qstr, &added);
203 if (!added) {
204 printf("SyntaxError?: identifier already declared something\n");
205 return;
206 }
207 id_info_t *id_info2 = scope_find_local_in_parent(scope, qstr);
208 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)) {
209 printf("SyntaxError: no binding for nonlocal '%s' found\n", qstr_str(qstr));
210 return;
211 }
212 id_info->kind = ID_INFO_KIND_FREE;
213 scope_close_over_in_parents(scope, qstr);
214}
215
Damien429d7192013-10-04 19:53:11 +0100216void scope_print_info(scope_t *s) {
217 if (s->kind == SCOPE_MODULE) {
218 printf("code <module>\n");
219 } else if (s->kind == SCOPE_LAMBDA) {
220 printf("code <lambda>\n");
221 } else if (s->kind == SCOPE_LIST_COMP) {
222 printf("code <listcomp>\n");
223 } else if (s->kind == SCOPE_DICT_COMP) {
224 printf("code <dictcomp>\n");
225 } else if (s->kind == SCOPE_SET_COMP) {
226 printf("code <setcomp>\n");
227 } else if (s->kind == SCOPE_GEN_EXPR) {
228 printf("code <genexpr>\n");
229 } else {
230 printf("code %s\n", qstr_str(s->simple_name));
231 }
232 /*
233 printf("var global:");
234 for (int i = 0; i < s->id_info_len; i++) {
235 if (s->id_info[i].kind == ID_INFO_KIND_GLOBAL_EXPLICIT) {
236 printf(" %s", qstr_str(s->id_info[i].qstr));
237 }
238 }
239 printf("\n");
240 printf("var name:");
241 for (int i = 0; i < s->id_info_len; i++) {
242 if (s->id_info[i].kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
243 printf(" %s", qstr_str(s->id_info[i].qstr));
244 }
245 }
246 printf("\n");
247 printf("var local:");
248 for (int i = 0; i < s->id_info_len; i++) {
249 if (s->id_info[i].kind == ID_INFO_KIND_LOCAL) {
250 printf(" %s", qstr_str(s->id_info[i].qstr));
251 }
252 }
253 printf("\n");
254 printf("var free:");
255 for (int i = 0; i < s->id_info_len; i++) {
256 if (s->id_info[i].kind == ID_INFO_KIND_FREE) {
257 printf(" %s", qstr_str(s->id_info[i].qstr));
258 }
259 }
260 printf("\n");
261 */
262 printf(" flags %04x\n", s->flags);
263 printf(" argcount %d\n", s->num_params);
264 printf(" nlocals %d\n", s->num_locals);
265 printf(" stacksize %d\n", s->stack_size);
266}