This commit is contained in:
blueloveTH 2024-07-30 12:24:47 +08:00
parent 1bbba50003
commit db0acc854c
6 changed files with 18 additions and 25 deletions

View File

@ -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;

View File

@ -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();

View File

@ -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;
@ -171,7 +165,7 @@ void pk_VM__ctor(pk_VM* self) {
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;

View File

@ -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;

View File

@ -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);
}

View File

@ -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);
}