summaryrefslogtreecommitdiff
path: root/py/compile.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2016-09-30 13:53:00 +1000
committerDamien George <damien.p.george@gmail.com>2016-09-30 13:53:00 +1000
commit0d10517a45fc90e7da9fc527ccfe51ab10205548 (patch)
treebef561084c21afdf93a6eede74371df8518bfbb5 /py/compile.c
parentd5495966cea38f452646b2f171859f66bebcf05e (diff)
py/scope: Factor common code to find locals and close over them.
Saves 50-100 bytes of code.
Diffstat (limited to 'py/compile.c')
-rw-r--r--py/compile.c15
1 files changed, 6 insertions, 9 deletions
diff --git a/py/compile.c b/py/compile.c
index 2253ccd3f..1d371cb33 100644
--- a/py/compile.c
+++ b/py/compile.c
@@ -1172,17 +1172,14 @@ STATIC void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
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 && id_info->kind != ID_INFO_KIND_FREE) {
+ if (added) {
+ scope_find_local_and_close_over(comp->scope_cur, id_info, qst);
+ if (id_info->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
+ compile_syntax_error(comp, pn, "no binding for nonlocal found");
+ }
+ } else if (id_info->kind != ID_INFO_KIND_FREE) {
compile_syntax_error(comp, pn, "identifier redefined as nonlocal");
- 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) {