diff options
author | Damien <damien.p.george@gmail.com> | 2013-10-05 12:19:06 +0100 |
---|---|---|
committer | Damien <damien.p.george@gmail.com> | 2013-10-05 12:19:06 +0100 |
commit | 415eb6f8507cc8c912143e8173bd9b451f6af917 (patch) | |
tree | 60f05e31b722a94ee304ecb6f46a179c4787793e /py/scope.c | |
parent | 492d0824552da7286fc0e4e1b91ea2fd8ad8262a (diff) |
Restructure emit so it goes through a method table.
Diffstat (limited to 'py/scope.c')
-rw-r--r-- | py/scope.c | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/py/scope.c b/py/scope.c index a715b2b50..5773ae0b4 100644 --- a/py/scope.c +++ b/py/scope.c @@ -165,6 +165,46 @@ void scope_close_over_in_parents(scope_t *scope, qstr qstr) { assert(0); // we should have found the variable in one of the parents } +void scope_declare_global(scope_t *scope, qstr qstr) { + if (scope->kind == SCOPE_MODULE) { + printf("SyntaxError?: can't declare global in outer code\n"); + return; + } + bool added; + id_info_t *id_info = scope_find_or_add_id(scope, qstr, &added); + if (!added) { + printf("SyntaxError?: identifier already declared something\n"); + return; + } + id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT; + + // if the id exists in the global scope, set its kind to EXPLICIT_GLOBAL + id_info = scope_find_global(scope, qstr); + if (id_info != NULL) { + id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT; + } +} + +void scope_declare_nonlocal(scope_t *scope, qstr qstr) { + if (scope->kind == SCOPE_MODULE) { + printf("SyntaxError?: can't declare nonlocal in outer code\n"); + return; + } + bool added; + id_info_t *id_info = scope_find_or_add_id(scope, qstr, &added); + if (!added) { + printf("SyntaxError?: identifier already declared something\n"); + return; + } + id_info_t *id_info2 = scope_find_local_in_parent(scope, qstr); + 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)) { + printf("SyntaxError: no binding for nonlocal '%s' found\n", qstr_str(qstr)); + return; + } + id_info->kind = ID_INFO_KIND_FREE; + scope_close_over_in_parents(scope, qstr); +} + void scope_print_info(scope_t *s) { if (s->kind == SCOPE_MODULE) { printf("code <module>\n"); |