Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1 | #include <stdint.h> |
| 2 | #include <stdio.h> |
| 3 | #include <string.h> |
| 4 | #include <assert.h> |
| 5 | |
| 6 | #include "misc.h" |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 7 | #include "mpconfig.h" |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 8 | #include "qstr.h" |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 9 | #include "parse.h" |
| 10 | #include "scope.h" |
| 11 | |
Damien George | cbd2f74 | 2014-01-19 11:48:48 +0000 | [diff] [blame] | 12 | scope_t *scope_new(scope_kind_t kind, mp_parse_node_t pn, qstr source_file, uint unique_code_id, uint emit_options) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 13 | scope_t *scope = m_new(scope_t, 1); |
| 14 | scope->kind = kind; |
| 15 | scope->parent = NULL; |
| 16 | scope->next = NULL; |
| 17 | scope->pn = pn; |
Damien George | cbd2f74 | 2014-01-19 11:48:48 +0000 | [diff] [blame] | 18 | scope->source_file = source_file; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 19 | switch (kind) { |
| 20 | case SCOPE_MODULE: |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 21 | scope->simple_name = QSTR_FROM_STR_STATIC("<module>"); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 22 | break; |
| 23 | case SCOPE_FUNCTION: |
| 24 | case SCOPE_CLASS: |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 25 | assert(MP_PARSE_NODE_IS_STRUCT(pn)); |
| 26 | scope->simple_name = MP_PARSE_NODE_LEAF_ARG(((mp_parse_node_struct_t*)pn)->nodes[0]); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 27 | break; |
| 28 | case SCOPE_LAMBDA: |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 29 | scope->simple_name = QSTR_FROM_STR_STATIC("<lambda>"); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 30 | break; |
| 31 | case SCOPE_LIST_COMP: |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 32 | scope->simple_name = QSTR_FROM_STR_STATIC("<listcomp>"); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 33 | break; |
| 34 | case SCOPE_DICT_COMP: |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 35 | scope->simple_name = QSTR_FROM_STR_STATIC("<dictcomp>"); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 36 | break; |
| 37 | case SCOPE_SET_COMP: |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 38 | scope->simple_name = QSTR_FROM_STR_STATIC("<setcomp>"); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 39 | break; |
| 40 | case SCOPE_GEN_EXPR: |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 41 | scope->simple_name = QSTR_FROM_STR_STATIC("<genexpr>"); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 42 | break; |
| 43 | default: |
| 44 | assert(0); |
| 45 | } |
| 46 | scope->id_info_alloc = 8; |
| 47 | scope->id_info_len = 0; |
| 48 | scope->id_info = m_new(id_info_t, scope->id_info_alloc); |
| 49 | |
| 50 | scope->flags = 0; |
| 51 | scope->num_params = 0; |
| 52 | /* not needed |
| 53 | scope->num_default_params = 0; |
| 54 | scope->num_dict_params = 0; |
| 55 | */ |
| 56 | scope->num_locals = 0; |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 57 | scope->unique_code_id = unique_code_id; |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 58 | scope->emit_options = emit_options; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 59 | |
| 60 | return scope; |
| 61 | } |
| 62 | |
| 63 | id_info_t *scope_find_or_add_id(scope_t *scope, qstr qstr, bool *added) { |
| 64 | for (int i = 0; i < scope->id_info_len; i++) { |
| 65 | if (scope->id_info[i].qstr == qstr) { |
| 66 | *added = false; |
| 67 | return &scope->id_info[i]; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // make sure we have enough memory |
| 72 | if (scope->id_info_len >= scope->id_info_alloc) { |
Damien | 732407f | 2013-12-29 19:33:23 +0000 | [diff] [blame] | 73 | scope->id_info = m_renew(id_info_t, scope->id_info, scope->id_info_alloc, scope->id_info_alloc * 2); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 74 | scope->id_info_alloc *= 2; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 75 | } |
| 76 | |
Damien George | cbd2f74 | 2014-01-19 11:48:48 +0000 | [diff] [blame] | 77 | // add new id to end of array of all ids; this seems to match CPython |
| 78 | // important thing is that function arguments are first, but that is |
| 79 | // handled by the compiler because it adds arguments before compiling the body |
| 80 | id_info_t *id_info = &scope->id_info[scope->id_info_len++]; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 81 | |
| 82 | id_info->param = false; |
| 83 | id_info->kind = 0; |
| 84 | id_info->qstr = qstr; |
Damien | 27fb45e | 2013-10-20 15:07:49 +0100 | [diff] [blame] | 85 | id_info->local_num = 0; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 86 | *added = true; |
| 87 | return id_info; |
| 88 | } |
| 89 | |
| 90 | id_info_t *scope_find(scope_t *scope, qstr qstr) { |
| 91 | for (int i = 0; i < scope->id_info_len; i++) { |
| 92 | if (scope->id_info[i].qstr == qstr) { |
| 93 | return &scope->id_info[i]; |
| 94 | } |
| 95 | } |
| 96 | return NULL; |
| 97 | } |
| 98 | |
| 99 | id_info_t *scope_find_global(scope_t *scope, qstr qstr) { |
| 100 | while (scope->parent != NULL) { |
| 101 | scope = scope->parent; |
| 102 | } |
| 103 | for (int i = 0; i < scope->id_info_len; i++) { |
| 104 | if (scope->id_info[i].qstr == qstr) { |
| 105 | return &scope->id_info[i]; |
| 106 | } |
| 107 | } |
| 108 | return NULL; |
| 109 | } |
| 110 | |
| 111 | id_info_t *scope_find_local_in_parent(scope_t *scope, qstr qstr) { |
| 112 | if (scope->parent == NULL) { |
| 113 | return NULL; |
| 114 | } |
| 115 | for (scope_t *s = scope->parent; s->parent != NULL; s = s->parent) { |
| 116 | for (int i = 0; i < s->id_info_len; i++) { |
| 117 | if (s->id_info[i].qstr == qstr) { |
| 118 | return &s->id_info[i]; |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | return NULL; |
| 123 | } |
| 124 | |
| 125 | void scope_close_over_in_parents(scope_t *scope, qstr qstr) { |
| 126 | assert(scope->parent != NULL); // we should have at least 1 parent |
| 127 | for (scope_t *s = scope->parent; s->parent != NULL; s = s->parent) { |
| 128 | id_info_t *id = NULL; |
| 129 | for (int i = 0; i < s->id_info_len; i++) { |
| 130 | if (s->id_info[i].qstr == qstr) { |
| 131 | id = &s->id_info[i]; |
| 132 | break; |
| 133 | } |
| 134 | } |
| 135 | if (id == NULL) { |
| 136 | // variable not declared in this scope, so declare it as free and keep searching parents |
| 137 | bool added; |
| 138 | id = scope_find_or_add_id(s, qstr, &added); |
| 139 | assert(added); |
| 140 | id->kind = ID_INFO_KIND_FREE; |
| 141 | } else { |
| 142 | // variable is declared in this scope, so finish |
| 143 | switch (id->kind) { |
| 144 | case ID_INFO_KIND_LOCAL: id->kind = ID_INFO_KIND_CELL; break; // variable local to this scope, close it over |
| 145 | case ID_INFO_KIND_FREE: break; // variable already closed over in a parent scope |
| 146 | case ID_INFO_KIND_CELL: break; // variable already closed over in this scope |
| 147 | default: assert(0); // TODO |
| 148 | } |
| 149 | return; |
| 150 | } |
| 151 | } |
| 152 | assert(0); // we should have found the variable in one of the parents |
| 153 | } |
| 154 | |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 155 | void scope_declare_global(scope_t *scope, qstr qstr) { |
| 156 | if (scope->kind == SCOPE_MODULE) { |
| 157 | printf("SyntaxError?: can't declare global in outer code\n"); |
| 158 | return; |
| 159 | } |
| 160 | bool added; |
| 161 | id_info_t *id_info = scope_find_or_add_id(scope, qstr, &added); |
| 162 | if (!added) { |
| 163 | printf("SyntaxError?: identifier already declared something\n"); |
| 164 | return; |
| 165 | } |
| 166 | id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT; |
| 167 | |
| 168 | // if the id exists in the global scope, set its kind to EXPLICIT_GLOBAL |
| 169 | id_info = scope_find_global(scope, qstr); |
| 170 | if (id_info != NULL) { |
| 171 | id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | void scope_declare_nonlocal(scope_t *scope, qstr qstr) { |
| 176 | if (scope->kind == SCOPE_MODULE) { |
| 177 | printf("SyntaxError?: can't declare nonlocal 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_t *id_info2 = scope_find_local_in_parent(scope, qstr); |
| 187 | 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)) { |
| 188 | printf("SyntaxError: no binding for nonlocal '%s' found\n", qstr_str(qstr)); |
| 189 | return; |
| 190 | } |
| 191 | id_info->kind = ID_INFO_KIND_FREE; |
| 192 | scope_close_over_in_parents(scope, qstr); |
| 193 | } |
| 194 | |
Damien George | cbd2f74 | 2014-01-19 11:48:48 +0000 | [diff] [blame] | 195 | #if MICROPY_EMIT_CPYTHON |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 196 | void scope_print_info(scope_t *s) { |
| 197 | if (s->kind == SCOPE_MODULE) { |
| 198 | printf("code <module>\n"); |
| 199 | } else if (s->kind == SCOPE_LAMBDA) { |
| 200 | printf("code <lambda>\n"); |
| 201 | } else if (s->kind == SCOPE_LIST_COMP) { |
| 202 | printf("code <listcomp>\n"); |
| 203 | } else if (s->kind == SCOPE_DICT_COMP) { |
| 204 | printf("code <dictcomp>\n"); |
| 205 | } else if (s->kind == SCOPE_SET_COMP) { |
| 206 | printf("code <setcomp>\n"); |
| 207 | } else if (s->kind == SCOPE_GEN_EXPR) { |
| 208 | printf("code <genexpr>\n"); |
| 209 | } else { |
| 210 | printf("code %s\n", qstr_str(s->simple_name)); |
| 211 | } |
| 212 | /* |
| 213 | printf("var global:"); |
| 214 | for (int i = 0; i < s->id_info_len; i++) { |
| 215 | if (s->id_info[i].kind == ID_INFO_KIND_GLOBAL_EXPLICIT) { |
| 216 | printf(" %s", qstr_str(s->id_info[i].qstr)); |
| 217 | } |
| 218 | } |
| 219 | printf("\n"); |
| 220 | printf("var name:"); |
| 221 | for (int i = 0; i < s->id_info_len; i++) { |
| 222 | if (s->id_info[i].kind == ID_INFO_KIND_GLOBAL_IMPLICIT) { |
| 223 | printf(" %s", qstr_str(s->id_info[i].qstr)); |
| 224 | } |
| 225 | } |
| 226 | printf("\n"); |
| 227 | printf("var local:"); |
| 228 | for (int i = 0; i < s->id_info_len; i++) { |
| 229 | if (s->id_info[i].kind == ID_INFO_KIND_LOCAL) { |
| 230 | printf(" %s", qstr_str(s->id_info[i].qstr)); |
| 231 | } |
| 232 | } |
| 233 | printf("\n"); |
| 234 | printf("var free:"); |
| 235 | for (int i = 0; i < s->id_info_len; i++) { |
| 236 | if (s->id_info[i].kind == ID_INFO_KIND_FREE) { |
| 237 | printf(" %s", qstr_str(s->id_info[i].qstr)); |
| 238 | } |
| 239 | } |
| 240 | printf("\n"); |
| 241 | */ |
| 242 | printf(" flags %04x\n", s->flags); |
| 243 | printf(" argcount %d\n", s->num_params); |
| 244 | printf(" nlocals %d\n", s->num_locals); |
| 245 | printf(" stacksize %d\n", s->stack_size); |
| 246 | } |
Damien George | cbd2f74 | 2014-01-19 11:48:48 +0000 | [diff] [blame] | 247 | #endif |