From 0caa14172dfa805bf828867b5d4269da5eb3198e Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sun, 8 Oct 2023 20:55:31 +0800 Subject: [PATCH] fix `++` for `global` names --- src/compiler.cpp | 5 ++++- tests/99_bugs.py | 9 +++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/compiler.cpp b/src/compiler.cpp index 38daac0d..879ca10c 100644 --- a/src/compiler.cpp +++ b/src/compiler.cpp @@ -775,7 +775,10 @@ __EAT_DOTS_END: case TK("++"):{ consume(TK("@id")); StrName name(prev().sv()); - switch(name_scope()){ + NameScope scope = name_scope(); + bool is_global = ctx()->global_names.count(name.sv()); + if(is_global) scope = NAME_GLOBAL; + switch(scope){ case NAME_LOCAL: ctx()->emit(OP_INC_FAST, ctx()->add_varname(name), prev().line); break; diff --git a/tests/99_bugs.py b/tests/99_bugs.py index c86a8813..07f7949a 100644 --- a/tests/99_bugs.py +++ b/tests/99_bugs.py @@ -70,3 +70,12 @@ try: exit(1) except UnboundLocalError: pass + + +g = 1 +def f(): + global g + ++g + +f(); f() +assert g == 3 \ No newline at end of file