mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 03:20:18 +00:00
some fix
This commit is contained in:
parent
8f6b539b37
commit
78aa295876
@ -15,22 +15,29 @@ typedef struct c11_sv{
|
||||
int size;
|
||||
} c11_sv;
|
||||
|
||||
/* string */
|
||||
typedef struct c11_bytes{
|
||||
int size;
|
||||
const char data[];
|
||||
} c11_bytes;
|
||||
|
||||
// int size | char[] | '\0'
|
||||
typedef c11_bytes c11_string;
|
||||
|
||||
int c11_sv__cmp(c11_sv self, c11_sv other);
|
||||
int c11_sv__cmp2(c11_sv self, const char* other, int size);
|
||||
int c11_sv__cmp3(c11_sv self, const char* other);
|
||||
|
||||
// int size | char[] | '\0'
|
||||
typedef const char c11_string;
|
||||
|
||||
c11_string* c11_string__new(const char* data);
|
||||
c11_string* c11_string__new2(const char* data, int size);
|
||||
void c11_string__ctor(c11_string* self, const char* data);
|
||||
void c11_string__ctor2(c11_string* self, const char* data, int size);
|
||||
c11_string* c11_string__copy(c11_string* self);
|
||||
void c11_string__delete(c11_string* self);
|
||||
int c11_string__len(c11_string* self);
|
||||
c11_sv c11_string__sv(c11_string* self);
|
||||
c11_string* c11_string__replace(c11_string* self, char old, char new_);
|
||||
|
||||
int c11_string__u8_length(const c11_string* self);
|
||||
int c11_string__u8_length(c11_string* self);
|
||||
c11_sv c11_string__u8_getitem(c11_string* self, int i);
|
||||
c11_string* c11_string__u8_slice(c11_string* self, int start, int stop, int step);
|
||||
|
||||
|
@ -321,7 +321,6 @@ const char* py_tpname(py_Type type);
|
||||
/// %p: void*
|
||||
/// %t: py_Type
|
||||
/// %n: py_Name
|
||||
const char* py_fmt(const char* fmt, ...);
|
||||
|
||||
#define MAGIC_METHOD(x) extern uint16_t x;
|
||||
#include "pocketpy/xmacros/magics.h"
|
||||
|
@ -27,7 +27,7 @@ static void pk_SourceData__ctor(struct pk_SourceData* self,
|
||||
}
|
||||
self->source = pk_SStream__submit(&ss);
|
||||
self->is_precompiled = (strncmp(source, "pkpy:", 5) == 0);
|
||||
c11_vector__push(const char*, &self->line_starts, self->source);
|
||||
c11_vector__push(const char*, &self->line_starts, self->source->data);
|
||||
}
|
||||
|
||||
static void pk_SourceData__dtor(struct pk_SourceData* self) {
|
||||
@ -77,7 +77,7 @@ c11_string* pk_SourceData__snapshot(const struct pk_SourceData* self,
|
||||
pk_SStream ss;
|
||||
pk_SStream__ctor(&ss);
|
||||
|
||||
pk_sprintf(&ss, " File \"%S\", line %d", &self->filename, lineno);
|
||||
pk_sprintf(&ss, " File \"%s\", line %d", self->filename->data, lineno);
|
||||
|
||||
if(name && *name) {
|
||||
pk_SStream__write_cstr(&ss, ", in ");
|
||||
|
@ -1,5 +1,6 @@
|
||||
#include "pocketpy/common/sstream.h"
|
||||
#include "pocketpy/common/config.h"
|
||||
#include "pocketpy/common/str.h"
|
||||
#include "pocketpy/common/utils.h"
|
||||
#include "pocketpy/pocketpy.h"
|
||||
|
||||
@ -9,7 +10,7 @@
|
||||
#include <ctype.h>
|
||||
#include <math.h>
|
||||
|
||||
const static int C11_STRING_HEADER_SIZE = sizeof(int);
|
||||
const static int C11_STRING_HEADER_SIZE = sizeof(c11_string);
|
||||
|
||||
void pk_SStream__ctor(pk_SStream* self) {
|
||||
c11_vector__ctor(&self->data, sizeof(char));
|
||||
@ -24,14 +25,14 @@ void pk_SStream__write_char(pk_SStream* self, char c) { c11_vector__push(char, &
|
||||
void pk_SStream__write_int(pk_SStream* self, int i) {
|
||||
// len('-2147483648') == 11
|
||||
c11_vector__reserve(&self->data, self->data.count + 11 + 1);
|
||||
int n = sprintf(self->data.data, "%d", i);
|
||||
int n = snprintf(self->data.data, 11 + 1, "%d", i);
|
||||
self->data.count += n;
|
||||
}
|
||||
|
||||
void pk_SStream__write_i64(pk_SStream* self, int64_t val) {
|
||||
// len('-9223372036854775808') == 20
|
||||
c11_vector__reserve(&self->data, self->data.count + 20 + 1);
|
||||
int n = sprintf(self->data.data, "%lld", (long long)val);
|
||||
int n = snprintf(self->data.data, 20 + 1, "%lld", (long long)val);
|
||||
self->data.count += n;
|
||||
}
|
||||
|
||||
@ -106,9 +107,9 @@ void pk_SStream__write_ptr(pk_SStream* self, void* p) {
|
||||
c11_string* pk_SStream__submit(pk_SStream* self) {
|
||||
c11_vector__push(char, &self->data, '\0');
|
||||
c11_array arr = c11_vector__submit(&self->data);
|
||||
int* p = arr.data;
|
||||
*p = arr.count - C11_STRING_HEADER_SIZE - 1;
|
||||
return (c11_string*)(p + 1);
|
||||
c11_string* retval = (c11_string*)arr.data;
|
||||
retval->size = arr.count - C11_STRING_HEADER_SIZE - 1;
|
||||
return retval;
|
||||
}
|
||||
|
||||
void pk_vsprintf(pk_SStream* ss, const char* fmt, va_list args) {
|
||||
@ -186,18 +187,3 @@ void pk_sprintf(pk_SStream* ss, const char* fmt, ...) {
|
||||
pk_vsprintf(ss, fmt, args);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
const char* py_fmt(const char* fmt, ...) {
|
||||
PK_THREAD_LOCAL pk_SStream ss;
|
||||
if(ss.data.elem_size == 0) {
|
||||
pk_SStream__ctor(&ss);
|
||||
} else {
|
||||
c11_vector__clear(&ss.data);
|
||||
}
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
pk_vsprintf(&ss, fmt, args);
|
||||
va_end(args);
|
||||
pk_SStream__write_char(&ss, '\0');
|
||||
return (const char*)ss.data.data;
|
||||
}
|
||||
|
@ -11,54 +11,53 @@
|
||||
c11_string* c11_string__new(const char* data) { return c11_string__new2(data, strlen(data)); }
|
||||
|
||||
c11_string* c11_string__new2(const char* data, int size) {
|
||||
int* p = malloc(sizeof(int) + size + 1);
|
||||
*p++ = size;
|
||||
c11_string* retval = malloc(sizeof(c11_string) + size + 1);
|
||||
c11_string__ctor2(retval, data, size);
|
||||
return retval;
|
||||
}
|
||||
|
||||
void c11_string__ctor(c11_string* self, const char* data) {
|
||||
c11_string__ctor2(self, data, strlen(data));
|
||||
}
|
||||
|
||||
void c11_string__ctor2(c11_string* self, const char* data, int size) {
|
||||
self->size = size;
|
||||
char* p = (char*)self->data;
|
||||
memcpy(p, data, size);
|
||||
((char*)p)[size] = '\0';
|
||||
return (c11_string*)p;
|
||||
p[size] = '\0';
|
||||
}
|
||||
|
||||
c11_string* c11_string__copy(c11_string* self) {
|
||||
int* p = (int*)self - 1;
|
||||
int total_size = sizeof(int) + *p + 1;
|
||||
int* q = malloc(total_size);
|
||||
memcpy(q, p, total_size);
|
||||
return (c11_string*)(q + 1);
|
||||
int total_size = sizeof(c11_string) + self->size + 1;
|
||||
c11_string* retval = malloc(total_size);
|
||||
memcpy(retval, self, total_size);
|
||||
return retval;
|
||||
}
|
||||
|
||||
void c11_string__delete(c11_string* self) {
|
||||
int* p = (int*)self - 1;
|
||||
free(p);
|
||||
}
|
||||
|
||||
int c11_string__len(c11_string* self) {
|
||||
int* p = (int*)self - 1;
|
||||
return *p;
|
||||
free(self);
|
||||
}
|
||||
|
||||
c11_sv c11_string__sv(c11_string* self) {
|
||||
int* p = (int*)self - 1;
|
||||
return (c11_sv){self, *p};
|
||||
return (c11_sv){self->data, self->size};
|
||||
}
|
||||
|
||||
c11_string* c11_string__replace(c11_string* self, char old, char new_) {
|
||||
c11_string* retval = c11_string__copy(self);
|
||||
char* p = (char*)retval;
|
||||
int size = c11_string__len(retval);
|
||||
for(int i = 0; i < size; i++) {
|
||||
char* p = (char*)retval->data;
|
||||
for(int i = 0; i < retval->size; i++) {
|
||||
if(p[i] == old) p[i] = new_;
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
|
||||
int c11_string__u8_length(c11_string* self) {
|
||||
int size = c11_string__len(self);
|
||||
return c11__byte_index_to_unicode(self, size);
|
||||
return c11__byte_index_to_unicode(self->data, self->size);
|
||||
}
|
||||
|
||||
c11_sv c11_string__u8_getitem(c11_string* self, int i) {
|
||||
i = c11__unicode_index_to_byte(self, i);
|
||||
int size = c11__u8_header(self[i], false);
|
||||
i = c11__unicode_index_to_byte(self->data, i);
|
||||
int size = c11__u8_header(self->data[i], false);
|
||||
return c11_sv__slice2(c11_string__sv(self), i, i + size);
|
||||
}
|
||||
|
||||
|
@ -2054,11 +2054,11 @@ Error* pk_compile(pk_SourceData_ src, CodeObject* out) {
|
||||
if(err) return err;
|
||||
|
||||
Token* data = (Token*)tokens.data;
|
||||
printf("%s\n", src->filename);
|
||||
printf("%s\n", src->filename->data);
|
||||
for(int i = 0; i < tokens.count; i++) {
|
||||
Token* t = data + i;
|
||||
c11_string* tmp = c11_string__new2(t->start, t->length);
|
||||
printf("[%d] %s: %s\n", t->line, pk_TokenSymbols[t->type], tmp);
|
||||
printf("[%d] %s: %s\n", t->line, pk_TokenSymbols[t->type], tmp->data);
|
||||
c11_string__delete(tmp);
|
||||
}
|
||||
|
||||
|
@ -40,7 +40,7 @@ const static TokenValue EmptyTokenValue;
|
||||
static void pk_Lexer__ctor(pk_Lexer* self, pk_SourceData_ src){
|
||||
PK_INCREF(src);
|
||||
self->src = src;
|
||||
self->curr_char = self->token_start = src->source;
|
||||
self->curr_char = self->token_start = src->source->data;
|
||||
self->current_line = 1;
|
||||
self->brackets_level = 0;
|
||||
c11_vector__ctor(&self->nexts, sizeof(Token));
|
||||
@ -549,7 +549,7 @@ static Error* lex_one_token(pk_Lexer* self, bool* eof){
|
||||
|
||||
static Error* from_precompiled(pk_Lexer* self) {
|
||||
TokenDeserializer deserializer;
|
||||
TokenDeserializer__ctor(&deserializer, self->src->source);
|
||||
TokenDeserializer__ctor(&deserializer, self->src->source->data);
|
||||
|
||||
deserializer.curr += 5; // skip "pkpy:"
|
||||
c11_sv version = TokenDeserializer__read_string(&deserializer, '\n');
|
||||
@ -576,8 +576,8 @@ static Error* from_precompiled(pk_Lexer* self) {
|
||||
if(is_raw_string_used(t.type)) {
|
||||
int64_t index = TokenDeserializer__read_uint(&deserializer, ',');
|
||||
c11_string* p = c11__getitem(c11_string*, precompiled_tokens, index);
|
||||
t.start = p;
|
||||
t.length = c11_string__len(p);
|
||||
t.start = p->data;
|
||||
t.length = p->size;
|
||||
} else {
|
||||
t.start = NULL;
|
||||
t.length = 0;
|
||||
@ -937,7 +937,7 @@ double TokenDeserializer__read_float(TokenDeserializer* self, char c){
|
||||
// TODO: optimize this
|
||||
c11_string* nullterm = c11_string__new2(sv.data, sv.size);
|
||||
char* end;
|
||||
double retval = strtod(nullterm, &end);
|
||||
double retval = strtod(nullterm->data, &end);
|
||||
c11_string__delete(nullterm);
|
||||
assert(*end == 0);
|
||||
return retval;
|
||||
|
@ -495,7 +495,7 @@ pk_FrameResult pk_VM__run_top_frame(pk_VM* self) {
|
||||
}
|
||||
SP() = begin;
|
||||
c11_string* res = pk_SStream__submit(&ss);
|
||||
py_newstrn(SP()++, res, c11_string__len(res));
|
||||
py_newstrn(SP()++, res->data, res->size);
|
||||
c11_string__delete(res);
|
||||
DISPATCH();
|
||||
}
|
||||
|
@ -4,65 +4,58 @@
|
||||
#include "pocketpy/objects/object.h"
|
||||
#include "pocketpy/interpreter/vm.h"
|
||||
|
||||
int64_t py_toint(const py_Ref self){
|
||||
int64_t py_toint(const py_Ref self) {
|
||||
assert(self->type == tp_int);
|
||||
return self->_i64;
|
||||
}
|
||||
|
||||
double py_tofloat(const py_Ref self){
|
||||
double py_tofloat(const py_Ref self) {
|
||||
assert(self->type == tp_float);
|
||||
return self->_f64;
|
||||
}
|
||||
|
||||
bool py_castfloat(const py_Ref self, double* out){
|
||||
switch(self->type){
|
||||
case tp_int:
|
||||
*out = (double)self->_i64;
|
||||
return true;
|
||||
case tp_float:
|
||||
*out = self->_f64;
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
bool py_castfloat(const py_Ref self, double* out) {
|
||||
switch(self->type) {
|
||||
case tp_int: *out = (double)self->_i64; return true;
|
||||
case tp_float: *out = self->_f64; return true;
|
||||
default: return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool py_tobool(const py_Ref self){
|
||||
bool py_tobool(const py_Ref self) {
|
||||
assert(self->type == tp_bool);
|
||||
return self->_bool;
|
||||
}
|
||||
|
||||
py_Type py_totype(const py_Ref self){
|
||||
py_Type py_totype(const py_Ref self) {
|
||||
assert(self->type == tp_type);
|
||||
py_Type* ud = py_touserdata(self);
|
||||
return *ud;
|
||||
}
|
||||
|
||||
const char* py_tostr(const py_Ref self){
|
||||
const char* py_tostr(const py_Ref self) {
|
||||
assert(self->type == tp_str);
|
||||
int* p = PyObject__value(self->_obj);
|
||||
return (const char*)(p+1);
|
||||
c11_string* ud = PyObject__value(self->_obj);
|
||||
return ud->data;
|
||||
}
|
||||
|
||||
const char* py_tostrn(const py_Ref self, int* size){
|
||||
const char* py_tostrn(const py_Ref self, int* size) {
|
||||
assert(self->type == tp_str);
|
||||
int* p = PyObject__value(self->_obj);
|
||||
*size = *p;
|
||||
return (const char*)(p+1);
|
||||
c11_string* ud = PyObject__value(self->_obj);
|
||||
*size = ud->size;
|
||||
return ud->data;
|
||||
}
|
||||
|
||||
const unsigned char* py_tobytes(const py_Ref self, int* size){
|
||||
const unsigned char* py_tobytes(const py_Ref self, int* size) {
|
||||
assert(self->type == tp_bytes);
|
||||
int* ud = PyObject__value(self->_obj);
|
||||
*size = *ud;
|
||||
return (unsigned char*)(ud + 1);
|
||||
}
|
||||
|
||||
void* py_touserdata(const py_Ref self){
|
||||
void* py_touserdata(const py_Ref self) {
|
||||
assert(self && self->is_ptr);
|
||||
return PyObject__value(self->_obj);
|
||||
}
|
||||
|
||||
bool py_istype(const py_Ref self, py_Type type){
|
||||
return self->type == type;
|
||||
}
|
||||
bool py_istype(const py_Ref self, py_Type type) { return self->type == type; }
|
||||
|
@ -46,10 +46,10 @@ void py_newstr(py_Ref out, const char* data) {
|
||||
|
||||
void py_newstrn(py_Ref out, const char* data, int size) {
|
||||
pk_ManagedHeap* heap = &pk_current_vm->heap;
|
||||
int total_size = sizeof(int) + size + 1;
|
||||
int total_size = sizeof(c11_string) + size + 1;
|
||||
PyObject* obj = pk_ManagedHeap__gcnew(heap, tp_str, 0, total_size);
|
||||
int* p = PyObject__value(obj);
|
||||
*p = size;
|
||||
c11_string* ud = PyObject__value(obj);
|
||||
c11_string__ctor2(ud, data, size);
|
||||
out->type = tp_str;
|
||||
out->is_ptr = true;
|
||||
out->_obj = obj;
|
||||
|
Loading…
x
Reference in New Issue
Block a user