This commit is contained in:
blueloveTH 2024-07-03 01:24:29 +08:00
parent e365124af3
commit 841cc25a4e
4 changed files with 28 additions and 19 deletions

View File

@ -12,7 +12,7 @@
void c11_sbuf__ctor(c11_sbuf* self) {
c11_vector__ctor(&self->data, sizeof(char));
c11_vector__reserve(&self->data, 100 + sizeof(c11_string));
c11_vector__reserve(&self->data, sizeof(c11_string) + 100);
self->data.count = sizeof(c11_string);
}
@ -23,14 +23,16 @@ void c11_sbuf__write_char(c11_sbuf* self, char c) { c11_vector__push(char, &self
void c11_sbuf__write_int(c11_sbuf* self, int i) {
// len('-2147483648') == 11
c11_vector__reserve(&self->data, self->data.count + 11 + 1);
int n = snprintf(self->data.data, 11 + 1, "%d", i);
char* p = self->data.data + self->data.count;
int n = snprintf(p, 11 + 1, "%d", i);
self->data.count += n;
}
void c11_sbuf__write_i64(c11_sbuf* self, int64_t val) {
// len('-9223372036854775808') == 20
c11_vector__reserve(&self->data, self->data.count + 20 + 1);
int n = snprintf(self->data.data, 20 + 1, "%lld", (long long)val);
char* p = self->data.data + self->data.count;
int n = snprintf(p, 20 + 1, "%lld", (long long)val);
self->data.count += n;
}
@ -134,7 +136,7 @@ void c11_sbuf__write_ptr(c11_sbuf* self, void* p) {
c11_string* c11_sbuf__submit(c11_sbuf* self) {
c11_vector__push(char, &self->data, '\0');
c11_array arr = c11_vector__submit(&self->data);
c11_string* retval = (c11_string*)arr.data;
c11_string* retval = arr.data;
retval->size = arr.count - sizeof(c11_string) - 1;
return retval;
}

View File

@ -653,6 +653,7 @@ pk_FrameResult pk_VM__run_top_frame(pk_VM* self) {
__ERROR:
// 1. Exception can be handled inside the current frame
// 2. Exception need to be propagated to the upper frame
printf("byte.op: %d, line: %d\n", byte.op, Frame__lineno(frame));
assert(false);
return RES_ERROR;
}

View File

@ -49,9 +49,9 @@ static void disassemble(CodeObject* co) {
Bytecode byte = c11__getitem(Bytecode, &co->codes, i);
BytecodeEx ex = c11__getitem(BytecodeEx, &co->codes_ex, i);
char line[8];
char line[8] = "";
if(ex.lineno == prev_line) {
line[0] = '\0';
// do nothing
} else {
snprintf(line, sizeof(line), "%d", ex.lineno);
if(prev_line != -1) c11_sbuf__write_char(&ss, '\n');
@ -66,7 +66,7 @@ static void disassemble(CodeObject* co) {
}
}
char buf[64];
char buf[32];
snprintf(buf, sizeof(buf), "%-8s%-3s%-3d", line, pointer, i);
c11_sbuf__write_cstr(&ss, buf);
@ -132,6 +132,13 @@ static void disassemble(CodeObject* co) {
c11_sbuf__write_char(&ss, ')');
break;
}
case OP_BINARY_OP: {
py_Name name = byte.arg & 0xFF;
c11_sbuf__write_cstr(&ss, " (");
c11_sbuf__write_cstr(&ss, py_name2str(name));
c11_sbuf__write_char(&ss, ')');
break;
}
}
} while(0);

View File

@ -25,6 +25,7 @@ int main(int argc, char** argv) {
SetConsoleOutputCP(CP_UTF8);
#endif
#if 0
py_initialize();
const char* source = "1 < 2";
@ -47,20 +48,18 @@ int main(int argc, char** argv) {
py_finalize();
return 0;
#endif
// if(argc != 2) goto __HELP;
// char* source = read_file(argv[1]);
// py_initialize();
if(argc != 2) goto __HELP;
char* source = read_file(argv[1]);
py_initialize();
// if(py_exec(source)){
// py_Error* err = py_getlasterror();
// py_Error__print(err);
// }
if(!py_exec(source)) py_printexc();
// py_finalize();
// free(source);
py_finalize();
free(source);
// __HELP:
// printf("Usage: pocketpy [filename]\n");
// return 0;
__HELP:
printf("Usage: pocketpy [filename]\n");
return 0;
}