Exemplo n.º 1
0
STATIC void emit_pass1_load_id(emit_t *emit, qstr qst) {
    // name adding/lookup
    bool added;
    id_info_t *id = scope_find_or_add_id(emit->scope, qst, &added);
    if (added) {
#if MICROPY_EMIT_CPYTHON
        if (qst == MP_QSTR_super && emit->scope->kind == SCOPE_FUNCTION) {
            // special case, super is a global, and also counts as use of __class__
            id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
            id_info_t *id2 = scope_find_local_in_parent(emit->scope, MP_QSTR___class__);
            if (id2 != NULL) {
                id2 = scope_find_or_add_id(emit->scope, MP_QSTR___class__, &added);
                if (added) {
                    id2->kind = ID_INFO_KIND_FREE;
                    scope_close_over_in_parents(emit->scope, MP_QSTR___class__);
                }
            }
        } else
#endif
        {
            id_info_t *id2 = scope_find_local_in_parent(emit->scope, qst);
            if (id2 != NULL && (id2->kind == ID_INFO_KIND_LOCAL || id2->kind == ID_INFO_KIND_CELL || id2->kind == ID_INFO_KIND_FREE)) {
                id->kind = ID_INFO_KIND_FREE;
                scope_close_over_in_parents(emit->scope, qst);
            } else {
                id->kind = ID_INFO_KIND_GLOBAL_IMPLICIT;
            }
        }
    }
}
Exemplo n.º 2
0
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);
}