From eb5b84f21ad4b49c0e08d6d1abbf6029e227dc9f Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sun, 24 Nov 2024 14:47:05 +0800 Subject: [PATCH] clean up `#defines` --- plugins/flutter/pocketpy/pubspec.yaml | 2 +- scripts/check_undef.py | 49 +++++++++++++++++++++++++++ src/common/memorypool.c | 1 + src/compiler/compiler.c | 25 +++++++++++++- src/compiler/lexer.c | 1 + src/interpreter/ceval.c | 19 ++++++++++- src/interpreter/typeinfo.c | 5 ++- src/modules/array2d.c | 5 ++- src/modules/linalg.c | 8 ++++- src/modules/math.c | 5 ++- src/modules/pkpy.c | 4 ++- src/modules/random.c | 7 ++++ src/modules/time.c | 3 ++ src/public/py_dict.c | 6 ++-- src/public/py_number.c | 12 +++---- src/public/py_str.c | 4 ++- 16 files changed, 138 insertions(+), 18 deletions(-) create mode 100644 scripts/check_undef.py diff --git a/plugins/flutter/pocketpy/pubspec.yaml b/plugins/flutter/pocketpy/pubspec.yaml index 53ee98cb..d5505825 100644 --- a/plugins/flutter/pocketpy/pubspec.yaml +++ b/plugins/flutter/pocketpy/pubspec.yaml @@ -1,6 +1,6 @@ name: pocketpy description: A lightweight Python interpreter for game engines. It supports Android/iOS/Windows/Linux/MacOS. -version: 2.0.1+8 +version: 2.0.2 homepage: https://pocketpy.dev repository: https://github.com/pocketpy/pocketpy diff --git a/scripts/check_undef.py b/scripts/check_undef.py new file mode 100644 index 00000000..9e275718 --- /dev/null +++ b/scripts/check_undef.py @@ -0,0 +1,49 @@ +import re + +def check_define_undef_pairs(code): + # 使用正则表达式匹配#define和#undef指令 + define_pattern = re.compile(r'#define\s+(\w+)') + undef_pattern = re.compile(r'#undef\s+(\w+)') + + # 查找所有的#define和#undef + defines = define_pattern.findall(code) + undefs = undef_pattern.findall(code) + + # 使用集合计算差集,找出不匹配的部分 + define_set = set(defines) + undef_set = set(undefs) + + unmatched_defines = define_set - undef_set + unmatched_undefs = undef_set - define_set + + if unmatched_defines or unmatched_undefs: + if unmatched_defines: + print("mismatched #define") + for define in unmatched_defines: + print(f"- {define}") + + if unmatched_undefs: + print("mismatched #undef") + for undef in unmatched_undefs: + print(f"- {undef}") + + +# iterate over all the files in `path` directory + +import os +import sys + +def check_undef_in_dir(path): + for root, dirs, files in os.walk(path): + for file in files: + if file.endswith(".c") or file.endswith(".h"): + with open(os.path.join(root, file), "r", encoding='utf-8') as f: + print(f"==> {os.path.join(root, file)}") + check_define_undef_pairs(f.read()) + +if __name__ == "__main__": + if len(sys.argv) < 2: + print("Usage: python scripts/check_undef.py ") + sys.exit(1) + + check_undef_in_dir(sys.argv[1]) \ No newline at end of file diff --git a/src/common/memorypool.c b/src/common/memorypool.c index bd3adc8c..41116d59 100644 --- a/src/common/memorypool.c +++ b/src/common/memorypool.c @@ -322,3 +322,4 @@ void Pools_debug_info(char* buffer, int size) { ); } +#undef LinkedList__apply \ No newline at end of file diff --git a/src/compiler/compiler.c b/src/compiler/compiler.c index 88ce30a9..f72ccacb 100644 --- a/src/compiler/compiler.c +++ b/src/compiler/compiler.c @@ -2865,4 +2865,27 @@ const static PrattRule rules[TK__COUNT__] = { [TK_LBRACE] = { exprMap }, [TK_COLON] = { exprSlice0, exprSlice1, PREC_PRIMARY } }; -// clang-format on \ No newline at end of file +// clang-format on + +#undef static_assert_expr_size +#undef vtcall +#undef vtemit_ +#undef vtemit_del +#undef vtemit_store +#undef vtemit_inplace +#undef vtemit_istore +#undef vtdelete +#undef EXPR_COMMON_HEADER +#undef is_compare_expr +#undef tk +#undef prev +#undef curr +#undef next +#undef advance +#undef mode +#undef ctx +#undef match_newlines +#undef consume +#undef consume_end_stmt +#undef check +#undef match \ No newline at end of file diff --git a/src/compiler/lexer.c b/src/compiler/lexer.c index 1d5dfde6..f2bb4ea6 100644 --- a/src/compiler/lexer.c +++ b/src/compiler/lexer.c @@ -708,3 +708,4 @@ const char* TokenSymbols[] = { "yield", }; +#undef is_raw_string_used \ No newline at end of file diff --git a/src/interpreter/ceval.c b/src/interpreter/ceval.c index 6d72040b..3e282dda 100644 --- a/src/interpreter/ceval.c +++ b/src/interpreter/ceval.c @@ -1350,4 +1350,21 @@ static bool stack_format_object(VM* self, c11_sv spec) { // inplace update c11_sbuf__py_submit(&buf, val); return true; -} \ No newline at end of file +} + +#undef CHECK_RETURN_FROM_EXCEPT_OR_FINALLY +#undef DISPATCH +#undef DISPATCH_JUMP +#undef DISPATCH_JUMP_ABSOLUTE +#undef TOP +#undef SECOND +#undef THIRD +#undef FOURTH +#undef STACK_SHRINK +#undef STACK_GROW +#undef PUSH +#undef POP +#undef POPX +#undef SP +#undef INSERT_THIRD +#undef vectorcall_opcall \ No newline at end of file diff --git a/src/interpreter/typeinfo.c b/src/interpreter/typeinfo.c index 231a932b..8e9c97dd 100644 --- a/src/interpreter/typeinfo.c +++ b/src/interpreter/typeinfo.c @@ -38,4 +38,7 @@ void TypeList__apply(TypeList* self, void (*f)(py_TypeInfo*, void*), void* ctx) py_TypeInfo* info = TypeList__get(self, i); f(info, ctx); } -} \ No newline at end of file +} + +#undef CHUNK_SIZE +#undef LOG2_CHUNK_SIZE \ No newline at end of file diff --git a/src/modules/array2d.c b/src/modules/array2d.c index 611cbb7f..189f42d6 100644 --- a/src/modules/array2d.c +++ b/src/modules/array2d.c @@ -615,4 +615,7 @@ void pk__add_module_array2d() { py_bindmethod(array2d, "count", array2d_count); py_bindmethod(array2d, "find_bounding_rect", array2d_find_bounding_rect); py_bindmethod(array2d, "count_neighbors", array2d_count_neighbors); -} \ No newline at end of file +} + +#undef INC_COUNT +#undef HANDLE_SLICE diff --git a/src/modules/linalg.c b/src/modules/linalg.c index e2809aae..f8907221 100644 --- a/src/modules/linalg.c +++ b/src/modules/linalg.c @@ -986,4 +986,10 @@ void pk__add_module_linalg() { (c11_vec3){ {1, 1, 1} }); -} \ No newline at end of file +} + +#undef DEFINE_VEC_FIELD +#undef DEFINE_BOOL_NE +#undef DEF_VECTOR_ELEMENT_WISE +#undef DEF_VECTOR_OPS +#undef DEF_VECTOR_INT_OPS \ No newline at end of file diff --git a/src/modules/math.c b/src/modules/math.c index cfc4b155..2ad90b8b 100644 --- a/src/modules/math.c +++ b/src/modules/math.c @@ -198,4 +198,7 @@ void pk__add_module_math() { py_bindfunc(mod, "modf", math_modf); py_bindfunc(mod, "factorial", math_factorial); -} \ No newline at end of file +} + +#undef ONE_ARG_FUNC +#undef TWO_ARG_FUNC \ No newline at end of file diff --git a/src/modules/pkpy.c b/src/modules/pkpy.c index 104c1219..4706c564 100644 --- a/src/modules/pkpy.c +++ b/src/modules/pkpy.c @@ -66,4 +66,6 @@ void pk__add_module_pkpy() { py_setdict(mod, py_name("TValue"), TValue_dict); py_pop(); -} \ No newline at end of file +} + +#undef DEF_TVALUE_METHODS \ No newline at end of file diff --git a/src/modules/random.c b/src/modules/random.c index 85f1fc4b..2137e245 100644 --- a/src/modules/random.c +++ b/src/modules/random.c @@ -303,3 +303,10 @@ __ERROR: py_printexc(); c11__abort("failed to add module random"); } + +#undef N +#undef M +#undef MATRIX_A +#undef UPPER_MASK +#undef LOWER_MASK +#undef ADD_INST_BOUNDMETHOD \ No newline at end of file diff --git a/src/modules/time.c b/src/modules/time.c index 0bb58781..f29c57f0 100644 --- a/src/modules/time.c +++ b/src/modules/time.c @@ -104,3 +104,6 @@ void pk__add_module_time() { py_bindfunc(mod, "sleep", time_sleep); py_bindfunc(mod, "localtime", time_localtime); } + +#undef NANOS_PER_SEC +#undef DEF_STRUCT_TIME__PROPERTY \ No newline at end of file diff --git a/src/public/py_dict.c b/src/public/py_dict.c index 30b25618..a3def2a1 100644 --- a/src/public/py_dict.c +++ b/src/public/py_dict.c @@ -167,7 +167,7 @@ static void Dict__compact_entries(Dict* self) { } self->entries.length = n; // update indices - for(int i = 0; i < self->capacity; i++) { + for(uint32_t i = 0; i < self->capacity; i++) { for(int j = 0; j < PK_DICT_MAX_COLLISION; j++) { int idx = self->indices[i]._[j]; if(idx == -1) continue; @@ -203,7 +203,7 @@ static bool Dict__set(Dict* self, py_TValue* key, py_TValue* val) { if(res == -1) return false; // error } // no empty slot found - if(self->capacity >= self->entries.length * 10) { + if(self->capacity >= (uint32_t)self->entries.length * 10) { // raise error if we reach the minimum load factor (10%) return RuntimeError("dict has too much collision: %d/%d/%d", self->entries.length, @@ -623,3 +623,5 @@ bool py_dict_apply(py_Ref self, bool (*f)(py_Ref, py_Ref, void*), void* ctx) { } return true; } + +#undef PK_DICT_MAX_COLLISION \ No newline at end of file diff --git a/src/public/py_number.c b/src/public/py_number.c index ddebd052..ff3f1b9f 100644 --- a/src/public/py_number.c +++ b/src/public/py_number.c @@ -51,8 +51,6 @@ DEF_NUM_BINARY_OP(__le__, <=, py_newbool, py_newbool) DEF_NUM_BINARY_OP(__gt__, >, py_newbool, py_newbool) DEF_NUM_BINARY_OP(__ge__, >=, py_newbool, py_newbool) -#undef DEF_NUM_BINARY_OP - static bool int__neg__(int argc, py_Ref argv) { PY_CHECK_ARGC(1); py_i64 val = py_toint(&argv[0]); @@ -203,8 +201,6 @@ DEF_INT_BITWISE_OP(__xor__, ^) DEF_INT_BITWISE_OP(__lshift__, <<) DEF_INT_BITWISE_OP(__rshift__, >>) -#undef DEF_INT_BITWISE_OP - static bool int__repr__(int argc, py_Ref argv) { PY_CHECK_ARGC(1); py_i64 val = py_toint(&argv[0]); @@ -442,8 +438,6 @@ DEF_BOOL_BITWISE(__and__, &&) DEF_BOOL_BITWISE(__or__, ||) DEF_BOOL_BITWISE(__xor__, !=) -#undef DEF_BOOL_BITWISE - void pk_number__register() { /****** tp_int & tp_float ******/ py_bindmagic(tp_int, __add__, int__add__); @@ -520,4 +514,8 @@ void pk_number__register() { py_bindmagic(tp_bool, __and__, bool__and__); py_bindmagic(tp_bool, __or__, bool__or__); py_bindmagic(tp_bool, __xor__, bool__xor__); -} \ No newline at end of file +} + +#undef DEF_NUM_BINARY_OP +#undef DEF_INT_BITWISE_OP +#undef DEF_BOOL_BITWISE \ No newline at end of file diff --git a/src/public/py_str.c b/src/public/py_str.c index 1255be3e..6e032e03 100644 --- a/src/public/py_str.c +++ b/src/public/py_str.c @@ -679,4 +679,6 @@ bool py_str(py_Ref val) { bool py_repr(py_Ref val) { return pk_callmagic(__repr__, 1, val); } -bool py_len(py_Ref val) { return pk_callmagic(__len__, 1, val); } \ No newline at end of file +bool py_len(py_Ref val) { return pk_callmagic(__len__, 1, val); } + +#undef DEF_STR_CMP_OP \ No newline at end of file