From db0acc854ccbacae1ece1d3247427da0d35bd5bf Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Tue, 30 Jul 2024 12:24:47 +0800 Subject: [PATCH] ... --- include/pocketpy/interpreter/vm.h | 6 +++--- src/interpreter/ceval.c | 3 ++- src/interpreter/vm.c | 26 ++++++++------------------ src/public/modules.c | 2 +- src/public/py_exception.c | 3 ++- src/public/vm.c | 3 ++- 6 files changed, 18 insertions(+), 25 deletions(-) diff --git a/include/pocketpy/interpreter/vm.h b/include/pocketpy/interpreter/vm.h index e73d7f73..0a855eaf 100644 --- a/include/pocketpy/interpreter/vm.h +++ b/include/pocketpy/interpreter/vm.h @@ -38,9 +38,9 @@ typedef struct pk_VM { py_TValue builtins; // builtins module py_TValue main; // __main__ module - void (*_ceval_on_step)(Frame*, Bytecode); - unsigned char* (*_import_file)(const char*); - void (*_stdout)(const char*, ...); + void (*ceval_on_step)(Frame*, Bytecode); + unsigned char* (*import_file)(const char*); + void (*print)(const char*); py_TValue last_retval; py_TValue curr_exception; diff --git a/src/interpreter/ceval.c b/src/interpreter/ceval.c index f6ba5941..76e94a77 100644 --- a/src/interpreter/ceval.c +++ b/src/interpreter/ceval.c @@ -117,7 +117,8 @@ pk_FrameResult pk_VM__run_top_frame(pk_VM* self) { if(TOP()->type != tp_NoneType) { bool ok = py_repr(TOP()); if(!ok) goto __ERROR; - self->_stdout("%s\n", py_tostr(&self->last_retval)); + self->print(py_tostr(&self->last_retval)); + self->print("\n"); } POP(); DISPATCH(); diff --git a/src/interpreter/vm.c b/src/interpreter/vm.c index e7496cdf..32933dc1 100644 --- a/src/interpreter/vm.c +++ b/src/interpreter/vm.c @@ -11,13 +11,7 @@ static unsigned char* pk_default_import_file(const char* path) { return NULL; } -static void pk_default_stdout(const char* fmt, ...) { - va_list args; - va_start(args, fmt); - vprintf(fmt, args); - va_end(args); - fflush(stdout); -} +static void pk_default_print(const char* data) { printf("%s", data); } static void pk_TypeInfo__ctor(pk_TypeInfo* self, py_Name name, @@ -54,9 +48,9 @@ void pk_VM__ctor(pk_VM* self) { self->builtins = *py_NIL; self->main = *py_NIL; - self->_ceval_on_step = NULL; - self->_import_file = pk_default_import_file; - self->_stdout = pk_default_stdout; + self->ceval_on_step = NULL; + self->import_file = pk_default_import_file; + self->print = pk_default_print; self->last_retval = *py_NIL; self->curr_exception = *py_NIL; @@ -168,10 +162,10 @@ void pk_VM__ctor(pk_VM* self) { "ImportError", "AssertionError", "KeyError", - NULL, // sentinel + NULL, // sentinel }; const char** it = builtin_exceptions; - while(*it){ + while(*it) { py_Type type = pk_newtype(*it, tp_Exception, &self->builtins, NULL, false, true); py_setdict(&self->builtins, py_name(*it), py_tpobject(type)); it++; @@ -269,9 +263,7 @@ bool pk__parse_int_slice(py_Ref slice, int length, int* start, int* stop, int* s bool pk__normalize_index(int* index, int length) { if(*index < 0) *index += length; - if(*index < 0 || *index >= length) { - return IndexError("%d not in [0, %d)", *index, length); - } + if(*index < 0 || *index >= length) { return IndexError("%d not in [0, %d)", *index, length); } return true; } @@ -285,9 +277,7 @@ py_Type pk_newtype(const char* name, py_Type index = types->count; pk_TypeInfo* ti = c11_vector__emplace(types); pk_TypeInfo__ctor(ti, py_name(name), index, base, module ? *module : *py_NIL); - if(!dtor && base){ - dtor = c11__at(pk_TypeInfo, types, base)->dtor; - } + if(!dtor && base) { dtor = c11__at(pk_TypeInfo, types, base)->dtor; } ti->dtor = dtor; ti->is_python = is_python; ti->is_sealed = is_sealed; diff --git a/src/public/modules.c b/src/public/modules.c index aac9d084..d6210e71 100644 --- a/src/public/modules.c +++ b/src/public/modules.c @@ -193,7 +193,7 @@ static bool _py_builtins__print(int argc, py_Ref argv) { } c11_sbuf__write_sv(&buf, end); c11_string* res = c11_sbuf__submit(&buf); - pk_current_vm->_stdout("%s", res->data); + pk_current_vm->print(res->data); c11_string__delete(res); py_newnone(py_retval()); return true; diff --git a/src/public/py_exception.c b/src/public/py_exception.c index 6ca9c41b..31b1446d 100644 --- a/src/public/py_exception.c +++ b/src/public/py_exception.c @@ -122,7 +122,8 @@ bool py_checkexc() { void py_printexc() { char* msg = py_formatexc(); if(!msg) return; - pk_current_vm->_stdout("%s\n", msg); + pk_current_vm->print(msg); + pk_current_vm->print("\n"); free(msg); } diff --git a/src/public/vm.c b/src/public/vm.c index be8aa005..74dd61de 100644 --- a/src/public/vm.c +++ b/src/public/vm.c @@ -168,7 +168,8 @@ static void disassemble(CodeObject* co) { } c11_string* output = c11_sbuf__submit(&ss); - pk_current_vm->_stdout("%s\n", output->data); + pk_current_vm->print(output->data); + pk_current_vm->print("\n"); c11_string__delete(output); c11_vector__dtor(&jumpTargets); }