py: Move global/nonlocal decl code to compiler for proper SyntaxError.
This patch gives proper SyntaxError exceptions for bad global/nonlocal declarations. It also reduces code size: 304 bytes on unix x64, 132 bytes on stmhal.
This commit is contained in:
parent
b063b9b36d
commit
584ba6762f
58
py/compile.c
58
py/compile.c
|
@ -1614,30 +1614,58 @@ STATIC void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
STATIC void compile_declare_global(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
|
||||||
|
bool added;
|
||||||
|
id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
|
||||||
|
if (!added) {
|
||||||
|
compile_syntax_error(comp, pn, "identifier already used");
|
||||||
|
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(comp->scope_cur, qst);
|
||||||
|
if (id_info != NULL) {
|
||||||
|
id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
STATIC void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
|
STATIC void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
|
||||||
if (comp->pass == MP_PASS_SCOPE) {
|
if (comp->pass == MP_PASS_SCOPE) {
|
||||||
if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
|
mp_parse_node_t *nodes;
|
||||||
scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
|
int n = list_get(&pns->nodes[0], PN_name_list, &nodes);
|
||||||
} else {
|
for (int i = 0; i < n; i++) {
|
||||||
pns = (mp_parse_node_struct_t*)pns->nodes[0];
|
compile_declare_global(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
|
||||||
int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
|
|
||||||
for (int i = 0; i < num_nodes; i++) {
|
|
||||||
scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
STATIC void compile_declare_nonlocal(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
|
||||||
|
bool added;
|
||||||
|
id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
|
||||||
|
if (!added) {
|
||||||
|
compile_syntax_error(comp, pn, "identifier already used");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
id_info_t *id_info2 = scope_find_local_in_parent(comp->scope_cur, qst);
|
||||||
|
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)) {
|
||||||
|
compile_syntax_error(comp, pn, "no binding for nonlocal found");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
id_info->kind = ID_INFO_KIND_FREE;
|
||||||
|
scope_close_over_in_parents(comp->scope_cur, qst);
|
||||||
|
}
|
||||||
|
|
||||||
STATIC void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
|
STATIC void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
|
||||||
if (comp->pass == MP_PASS_SCOPE) {
|
if (comp->pass == MP_PASS_SCOPE) {
|
||||||
if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
|
if (comp->scope_cur->kind == SCOPE_MODULE) {
|
||||||
scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
|
compile_syntax_error(comp, (mp_parse_node_t)pns, "can't declare nonlocal in outer code");
|
||||||
} else {
|
return;
|
||||||
pns = (mp_parse_node_struct_t*)pns->nodes[0];
|
|
||||||
int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
|
|
||||||
for (int i = 0; i < num_nodes; i++) {
|
|
||||||
scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
|
|
||||||
}
|
}
|
||||||
|
mp_parse_node_t *nodes;
|
||||||
|
int n = list_get(&pns->nodes[0], PN_name_list, &nodes);
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
compile_declare_nonlocal(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
43
py/scope.c
43
py/scope.c
|
@ -26,7 +26,6 @@
|
||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
#include "mpconfig.h"
|
#include "mpconfig.h"
|
||||||
|
@ -159,47 +158,9 @@ void scope_close_over_in_parents(scope_t *scope, qstr qst) {
|
||||||
assert(0); // we should have found the variable in one of the parents
|
assert(0); // we should have found the variable in one of the parents
|
||||||
}
|
}
|
||||||
|
|
||||||
void scope_declare_global(scope_t *scope, qstr qst) {
|
|
||||||
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, qst, &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, qst);
|
|
||||||
if (id_info != NULL) {
|
|
||||||
id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void scope_declare_nonlocal(scope_t *scope, qstr qst) {
|
|
||||||
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, qst, &added);
|
|
||||||
if (!added) {
|
|
||||||
printf("SyntaxError?: identifier already declared something\n");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
id_info_t *id_info2 = scope_find_local_in_parent(scope, qst);
|
|
||||||
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(qst));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
id_info->kind = ID_INFO_KIND_FREE;
|
|
||||||
scope_close_over_in_parents(scope, qst);
|
|
||||||
}
|
|
||||||
|
|
||||||
#if MICROPY_EMIT_CPYTHON
|
#if MICROPY_EMIT_CPYTHON
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
void scope_print_info(scope_t *s) {
|
void scope_print_info(scope_t *s) {
|
||||||
if (s->kind == SCOPE_MODULE) {
|
if (s->kind == SCOPE_MODULE) {
|
||||||
printf("code <module>\n");
|
printf("code <module>\n");
|
||||||
|
|
|
@ -76,6 +76,4 @@ id_info_t *scope_find(scope_t *scope, qstr qstr);
|
||||||
id_info_t *scope_find_global(scope_t *scope, qstr qstr);
|
id_info_t *scope_find_global(scope_t *scope, qstr qstr);
|
||||||
id_info_t *scope_find_local_in_parent(scope_t *scope, qstr qstr);
|
id_info_t *scope_find_local_in_parent(scope_t *scope, qstr qstr);
|
||||||
void scope_close_over_in_parents(scope_t *scope, qstr qstr);
|
void scope_close_over_in_parents(scope_t *scope, qstr qstr);
|
||||||
void scope_declare_global(scope_t *scope, qstr qstr);
|
|
||||||
void scope_declare_nonlocal(scope_t *scope, qstr qstr);
|
|
||||||
void scope_print_info(scope_t *s);
|
void scope_print_info(scope_t *s);
|
||||||
|
|
Loading…
Reference in New Issue