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