From f756bd813d93959183aeb3c6c6ef4d0d820dd628 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Tue, 6 Aug 2024 13:31:41 +0800 Subject: [PATCH] ... --- include/pocketpy/common/_generated.h | 8 -------- include/pocketpy/common/algorithm.h | 9 --------- include/pocketpy/common/memorypool.h | 9 --------- include/pocketpy/common/smallmap.h | 8 -------- include/pocketpy/common/sstream.h | 8 -------- include/pocketpy/common/str.h | 8 -------- include/pocketpy/common/strname.h | 8 -------- include/pocketpy/common/utils.h | 8 -------- include/pocketpy/common/vector.h | 8 -------- include/pocketpy/compiler/compiler.h | 8 -------- include/pocketpy/compiler/lexer.h | 8 -------- include/pocketpy/interpreter/frame.h | 8 -------- include/pocketpy/interpreter/gc.h | 8 -------- include/pocketpy/interpreter/generator.h | 2 ++ include/pocketpy/interpreter/modules.h | 8 -------- include/pocketpy/interpreter/vm.h | 8 -------- include/pocketpy/objects/base.h | 8 -------- include/pocketpy/objects/codeobject.h | 10 +--------- include/pocketpy/objects/error.h | 8 -------- include/pocketpy/objects/namedict.h | 7 ------- include/pocketpy/objects/object.h | 8 -------- include/pocketpy/objects/sourcedata.h | 8 -------- prebuild.py | 9 --------- src/interpreter/ceval.c | 4 +++- src/interpreter/generator.c | 1 + src/interpreter/vm.c | 8 +++++--- 26 files changed, 12 insertions(+), 183 deletions(-) create mode 100644 include/pocketpy/interpreter/generator.h create mode 100644 src/interpreter/generator.c diff --git a/include/pocketpy/common/_generated.h b/include/pocketpy/common/_generated.h index 2429f556..f355730e 100644 --- a/include/pocketpy/common/_generated.h +++ b/include/pocketpy/common/_generated.h @@ -1,10 +1,6 @@ #pragma once // generated by prebuild.py -#ifdef __cplusplus -extern "C" { -#endif - const char* load_kPythonLib(const char* name); extern const char kPythonLibs__enum[]; @@ -22,7 +18,3 @@ extern const char kPythonLibs_operator[]; extern const char kPythonLibs_pickle[]; extern const char kPythonLibs_this[]; extern const char kPythonLibs_typing[]; - -#ifdef __cplusplus -} // extern "C" -#endif diff --git a/include/pocketpy/common/algorithm.h b/include/pocketpy/common/algorithm.h index f3d1b731..3e36d32b 100644 --- a/include/pocketpy/common/algorithm.h +++ b/include/pocketpy/common/algorithm.h @@ -2,10 +2,6 @@ #include -#ifdef __cplusplus -extern "C" { -#endif - #define c11__less(a, b) ((a) < (b)) #define c11__lower_bound(T, ptr, count, key, less, out_index) \ @@ -42,8 +38,3 @@ bool c11__stable_sort(void* ptr, int elem_size, int (*f_lt)(const void* a, const void* b, void* extra), void* extra); - -#ifdef __cplusplus -} - -#endif \ No newline at end of file diff --git a/include/pocketpy/common/memorypool.h b/include/pocketpy/common/memorypool.h index 481bc814..4afc6fb1 100644 --- a/include/pocketpy/common/memorypool.h +++ b/include/pocketpy/common/memorypool.h @@ -1,9 +1,5 @@ #pragma once -#ifdef __cplusplus -extern "C" { -#endif - #define kPoolExprBlockSize 128 #define kPoolFrameBlockSize 80 #define kPoolObjectBlockSize 80 @@ -24,8 +20,3 @@ void PoolObject_dealloc(void* p); void PoolObject_shrink_to_fit(); void Pools_debug_info(char* buffer, int size); - -#ifdef __cplusplus -} -#endif - diff --git a/include/pocketpy/common/smallmap.h b/include/pocketpy/common/smallmap.h index 1b0f33e8..62a0dbf5 100644 --- a/include/pocketpy/common/smallmap.h +++ b/include/pocketpy/common/smallmap.h @@ -4,10 +4,6 @@ #include "pocketpy/common/str.h" #include -#ifdef __cplusplus -extern "C" { -#endif - #define SMALLMAP_T__HEADER #define K uint16_t #define V int @@ -24,7 +20,3 @@ extern "C" { #define equal(a, b) (c11_sv__cmp((a), (b)) == 0) #include "pocketpy/xmacros/smallmap.h" #undef SMALLMAP_T__HEADER - -#ifdef __cplusplus -} -#endif \ No newline at end of file diff --git a/include/pocketpy/common/sstream.h b/include/pocketpy/common/sstream.h index ec0d09d2..5e5d1a22 100644 --- a/include/pocketpy/common/sstream.h +++ b/include/pocketpy/common/sstream.h @@ -6,10 +6,6 @@ #include -#ifdef __cplusplus -extern "C" { -#endif - typedef struct c11_sbuf { c11_vector data; } c11_sbuf; @@ -34,7 +30,3 @@ void c11_sbuf__py_submit(c11_sbuf* self, py_Ref out); void pk_vsprintf(c11_sbuf* ss, const char* fmt, va_list args); void pk_sprintf(c11_sbuf* ss, const char* fmt, ...); - -#ifdef __cplusplus -} -#endif diff --git a/include/pocketpy/common/str.h b/include/pocketpy/common/str.h index 59d01c78..d11a2539 100644 --- a/include/pocketpy/common/str.h +++ b/include/pocketpy/common/str.h @@ -4,10 +4,6 @@ #include "pocketpy/common/utils.h" #include "pocketpy/pocketpy.h" -#ifdef __cplusplus -extern "C" { -#endif - /* string */ typedef struct c11_string{ // int size | char[] | '\0' @@ -74,7 +70,3 @@ typedef enum IntParsingResult{ } IntParsingResult; IntParsingResult c11__parse_uint(c11_sv text, int64_t* out, int base); - -#ifdef __cplusplus -} -#endif \ No newline at end of file diff --git a/include/pocketpy/common/strname.h b/include/pocketpy/common/strname.h index 445a167a..a7407b8a 100644 --- a/include/pocketpy/common/strname.h +++ b/include/pocketpy/common/strname.h @@ -3,13 +3,5 @@ #include #include "pocketpy/common/str.h" -#ifdef __cplusplus -extern "C" { -#endif - void py_Name__initialize(); void py_Name__finalize(); - -#ifdef __cplusplus -} -#endif diff --git a/include/pocketpy/common/utils.h b/include/pocketpy/common/utils.h index f959699f..39f58436 100644 --- a/include/pocketpy/common/utils.h +++ b/include/pocketpy/common/utils.h @@ -3,10 +3,6 @@ #include "stdio.h" #include "stdlib.h" -#ifdef __cplusplus -extern "C" { -#endif - #define PK_REGION(name) 1 #define PK_SLICE_LOOP(i, start, stop, step) \ @@ -52,7 +48,3 @@ typedef struct RefCounted { free(obj); \ } \ } while(0) - -#ifdef __cplusplus -} -#endif \ No newline at end of file diff --git a/include/pocketpy/common/vector.h b/include/pocketpy/common/vector.h index 804964c9..97ba719b 100644 --- a/include/pocketpy/common/vector.h +++ b/include/pocketpy/common/vector.h @@ -7,10 +7,6 @@ #include #include -#ifdef __cplusplus -extern "C" { -#endif - typedef struct c11_array { void* data; int count; @@ -92,7 +88,3 @@ c11_array c11_vector__submit(c11_vector* self); // NOTE: here we do an extra NULL check for it to avoid UB #define c11__foreach(T, self, it) \ for(T* it = (self)->data; it && it != (T*)(self)->data + (self)->count; it++) - -#ifdef __cplusplus -} -#endif \ No newline at end of file diff --git a/include/pocketpy/compiler/compiler.h b/include/pocketpy/compiler/compiler.h index d8a2139e..4c625a89 100644 --- a/include/pocketpy/compiler/compiler.h +++ b/include/pocketpy/compiler/compiler.h @@ -5,12 +5,4 @@ #include "pocketpy/objects/sourcedata.h" #include "pocketpy/objects/codeobject.h" -#ifdef __cplusplus -extern "C" { -#endif - Error* pk_compile(SourceData_ src, CodeObject* out); - -#ifdef __cplusplus -} -#endif \ No newline at end of file diff --git a/include/pocketpy/compiler/lexer.h b/include/pocketpy/compiler/lexer.h index 28c0d3d7..03201c2c 100644 --- a/include/pocketpy/compiler/lexer.h +++ b/include/pocketpy/compiler/lexer.h @@ -6,10 +6,6 @@ #include "pocketpy/objects/error.h" #include -#ifdef __cplusplus -extern "C" { -#endif - extern const char* TokenSymbols[]; typedef enum TokenIndex{ @@ -95,7 +91,3 @@ Error* Lexer__process_and_dump(SourceData_ src, c11_string** out_string); void TokenArray__dtor(TokenArray* self); #define Token__sv(self) (c11_sv){(self)->start, (self)->length} - -#ifdef __cplusplus -} -#endif \ No newline at end of file diff --git a/include/pocketpy/interpreter/frame.h b/include/pocketpy/interpreter/frame.h index 5e92a881..6720d2d8 100644 --- a/include/pocketpy/interpreter/frame.h +++ b/include/pocketpy/interpreter/frame.h @@ -7,10 +7,6 @@ #include "pocketpy/common/config.h" #include "pocketpy/common/strname.h" -#ifdef __cplusplus -extern "C" { -#endif - py_TValue* FastLocals__try_get_by_name(py_TValue* locals, const CodeObject* co, py_Name name); NameDict* FastLocals__to_namedict(py_TValue* locals, const CodeObject* co); @@ -66,7 +62,3 @@ int Frame__exit_block(Frame* self, ValueStack*, int); void Frame__gc_mark(Frame* self); UnwindTarget* Frame__find_unwind_target(Frame* self, int iblock); void Frame__set_unwind_target(Frame* self, py_TValue* sp); - -#ifdef __cplusplus -} -#endif diff --git a/include/pocketpy/interpreter/gc.h b/include/pocketpy/interpreter/gc.h index 88a6f04c..1f665ae6 100644 --- a/include/pocketpy/interpreter/gc.h +++ b/include/pocketpy/interpreter/gc.h @@ -1,10 +1,6 @@ #include "pocketpy/objects/object.h" #include "pocketpy/common/config.h" -#ifdef __cplusplus -extern "C" { -#endif - typedef struct ManagedHeap{ c11_vector no_gc; c11_vector gen; @@ -28,7 +24,3 @@ PyObject* ManagedHeap__gcnew(ManagedHeap* self, py_Type type, int slots, int uds // external implementation void ManagedHeap__mark(ManagedHeap* self); - -#ifdef __cplusplus -} -#endif \ No newline at end of file diff --git a/include/pocketpy/interpreter/generator.h b/include/pocketpy/interpreter/generator.h new file mode 100644 index 00000000..3f59c932 --- /dev/null +++ b/include/pocketpy/interpreter/generator.h @@ -0,0 +1,2 @@ +#pragma once + diff --git a/include/pocketpy/interpreter/modules.h b/include/pocketpy/interpreter/modules.h index ac33c008..8e7a3de0 100644 --- a/include/pocketpy/interpreter/modules.h +++ b/include/pocketpy/interpreter/modules.h @@ -1,14 +1,6 @@ #pragma once -#ifdef __cplusplus -extern "C" { -#endif - void pk__add_module_pkpy(); void pk__add_module_os(); void pk__add_module_math(); void pk__add_module_dis(); - -#ifdef __cplusplus -} -#endif \ No newline at end of file diff --git a/include/pocketpy/interpreter/vm.h b/include/pocketpy/interpreter/vm.h index 62108e6d..797564df 100644 --- a/include/pocketpy/interpreter/vm.h +++ b/include/pocketpy/interpreter/vm.h @@ -6,10 +6,6 @@ #include "pocketpy/interpreter/frame.h" #include "pocketpy/interpreter/modules.h" -#ifdef __cplusplus -extern "C" { -#endif - typedef struct py_TypeInfo { py_Name name; py_Type base; @@ -128,7 +124,3 @@ py_Type pk_staticmethod__register(); py_Type pk_classmethod__register(); py_TValue pk_builtins__register(); - -#ifdef __cplusplus -} -#endif \ No newline at end of file diff --git a/include/pocketpy/objects/base.h b/include/pocketpy/objects/base.h index 5260d935..d8e74276 100644 --- a/include/pocketpy/objects/base.h +++ b/include/pocketpy/objects/base.h @@ -9,10 +9,6 @@ #include "pocketpy/common/utils.h" #include "pocketpy/pocketpy.h" -#ifdef __cplusplus -extern "C" { -#endif - typedef struct PyObject PyObject; typedef struct VM VM; extern VM* pk_current_vm; @@ -35,7 +31,3 @@ typedef struct py_TValue { // 16 bytes to make py_arg() macro work static_assert(sizeof(py_CFunction) <= 8, "sizeof(py_CFunction) > 8"); static_assert(sizeof(py_TValue) == 16, "sizeof(py_TValue) != 16"); - -#ifdef __cplusplus -} -#endif diff --git a/include/pocketpy/objects/codeobject.h b/include/pocketpy/objects/codeobject.h index 410f5423..949d0e86 100644 --- a/include/pocketpy/objects/codeobject.h +++ b/include/pocketpy/objects/codeobject.h @@ -10,10 +10,6 @@ #include "pocketpy/objects/namedict.h" #include "pocketpy/pocketpy.h" -#ifdef __cplusplus -extern "C" { -#endif - #define BC_NOARG 0 #define BC_KEEPLINE -1 @@ -144,8 +140,4 @@ typedef struct Function { } Function; void Function__ctor(Function* self, FuncDecl_ decl, py_TValue* module); -void Function__dtor(Function* self); - -#ifdef __cplusplus -} -#endif \ No newline at end of file +void Function__dtor(Function* self); \ No newline at end of file diff --git a/include/pocketpy/objects/error.h b/include/pocketpy/objects/error.h index 6e7a3dfc..ef3e0638 100644 --- a/include/pocketpy/objects/error.h +++ b/include/pocketpy/objects/error.h @@ -7,10 +7,6 @@ #include "pocketpy/objects/object.h" #include "pocketpy/pocketpy.h" -#ifdef __cplusplus -extern "C" { -#endif - typedef struct{ SourceData_ src; int lineno; @@ -20,7 +16,3 @@ typedef struct{ void py_BaseException__set_lineno(py_Ref, int lineno, const CodeObject* code); int py_BaseException__get_lineno(py_Ref, const CodeObject* code); void py_BaseException__stpush(py_Ref, SourceData_ src, int lineno, const char* func_name); - -#ifdef __cplusplus -} -#endif \ No newline at end of file diff --git a/include/pocketpy/objects/namedict.h b/include/pocketpy/objects/namedict.h index a8090625..0bd1c3ca 100644 --- a/include/pocketpy/objects/namedict.h +++ b/include/pocketpy/objects/namedict.h @@ -5,10 +5,6 @@ #include "pocketpy/objects/base.h" #include -#ifdef __cplusplus -extern "C" { -#endif - #define SMALLMAP_T__HEADER #define K uint16_t #define V py_TValue @@ -16,6 +12,3 @@ extern "C" { #include "pocketpy/xmacros/smallmap.h" #undef SMALLMAP_T__HEADER -#ifdef __cplusplus -} -#endif diff --git a/include/pocketpy/objects/object.h b/include/pocketpy/objects/object.h index c86c2e0d..622dc187 100644 --- a/include/pocketpy/objects/object.h +++ b/include/pocketpy/objects/object.h @@ -3,10 +3,6 @@ #include "pocketpy/objects/namedict.h" #include "pocketpy/objects/base.h" -#ifdef __cplusplus -extern "C" { -#endif - typedef struct PyObject { py_Type type; // we have a duplicated type here for convenience bool gc_is_large; @@ -29,7 +25,3 @@ void* PyObject__userdata(PyObject* self); PyObject* PyObject__new(py_Type type, int slots, int size); void PyObject__delete(PyObject* self); - -#ifdef __cplusplus -} -#endif \ No newline at end of file diff --git a/include/pocketpy/objects/sourcedata.h b/include/pocketpy/objects/sourcedata.h index 6d0ca73e..a31a3bdc 100644 --- a/include/pocketpy/objects/sourcedata.h +++ b/include/pocketpy/objects/sourcedata.h @@ -6,10 +6,6 @@ #include "pocketpy/common/sstream.h" #include "pocketpy/common/vector.h" -#ifdef __cplusplus -extern "C" { -#endif - struct SourceData { RefCounted rc; enum py_CompileMode mode; @@ -38,7 +34,3 @@ void SourceData__snapshot(const struct SourceData* self, int lineno, const char* cursor, const char* name); - -#ifdef __cplusplus -} -#endif diff --git a/prebuild.py b/prebuild.py index f9257996..1a00d5d9 100644 --- a/prebuild.py +++ b/prebuild.py @@ -27,21 +27,12 @@ with open("include/pocketpy/common/_generated.h", "wt", encoding='utf-8', newlin data = '''#pragma once // generated by prebuild.py -#ifdef __cplusplus -extern "C" { -#endif - const char* load_kPythonLib(const char* name); ''' for key in sorted(sources.keys()): value = sources[key] data += f'extern const char kPythonLibs_{key}[];\n' - data += ''' -#ifdef __cplusplus -} // extern "C" -#endif -''' f.write(data) with open("src/common/_generated.c", "wt", encoding='utf-8', newline='\n') as f: diff --git a/src/interpreter/ceval.c b/src/interpreter/ceval.c index 39fa140e..ad571ad1 100644 --- a/src/interpreter/ceval.c +++ b/src/interpreter/ceval.c @@ -701,7 +701,9 @@ FrameResult VM__run_top_frame(VM* self) { DISPATCH(); } case OP_YIELD_VALUE: { - assert(false); + py_assign(py_retval(), TOP()); + POP(); + return RES_YIELD; } ///////// case OP_LIST_APPEND: { diff --git a/src/interpreter/generator.c b/src/interpreter/generator.c new file mode 100644 index 00000000..9ae75b07 --- /dev/null +++ b/src/interpreter/generator.c @@ -0,0 +1 @@ +#include "pocketpy/interpreter/generator.h" \ No newline at end of file diff --git a/src/interpreter/vm.c b/src/interpreter/vm.c index f3646ed8..fc893cbc 100644 --- a/src/interpreter/vm.c +++ b/src/interpreter/vm.c @@ -435,9 +435,11 @@ FrameResult VM__vectorcall(VM* self, uint16_t argc, uint16_t kwargc, bool opcall // submit the call VM__push_frame(self, Frame__new(co, &fn->module, p0, p0, argv)); return opcall ? RES_CALL : VM__run_top_frame(self); - case FuncType_GENERATOR: - assert(false); - break; + case FuncType_GENERATOR: { + bool ok = prepare_py_call(self->__vectorcall_buffer, argv, p1, kwargc, fn->decl); + if(!ok) return RES_ERROR; + return RES_RETURN; + } // prepare_py_call(__vectorcall_buffer, args, kwargs, fn.decl); // s_data.reset(p0); // callstack.emplace(nullptr, co, fn._module, callable.get(), nullptr);