mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 11:30:18 +00:00
some fix
This commit is contained in:
parent
4542f23143
commit
2540205b77
@ -1,22 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include "pocketpy/pocketpy.h"
|
||||
#include "pocketpy/common/str.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef uint16_t StrName;
|
||||
uint16_t py_name2(c11_sv name);
|
||||
c11_sv py_name2sv(uint16_t index);
|
||||
|
||||
uint16_t pk_StrName__map(const char*);
|
||||
uint16_t pk_StrName__map2(c11_sv);
|
||||
const char* pk_StrName__rmap(uint16_t index);
|
||||
c11_sv pk_StrName__rmap2(uint16_t index);
|
||||
|
||||
void pk_StrName__initialize();
|
||||
void pk_StrName__finalize();
|
||||
void py_Name__initialize();
|
||||
void py_Name__finalize();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ typedef struct pk_TypeInfo {
|
||||
void (*dtor)(void*);
|
||||
void (*gc_mark)(void*);
|
||||
|
||||
c11_vector /*T=StrName*/ annotated_fields;
|
||||
c11_vector /*T=py_Name*/ annotated_fields;
|
||||
|
||||
py_CFunction on_end_subclass; // backdoor for enum module
|
||||
|
||||
|
@ -74,7 +74,7 @@ typedef struct CodeObject {
|
||||
c11_vector/*T=CodeObjectByteCodeEx*/ codes_ex;
|
||||
|
||||
c11_vector/*T=py_TValue*/ consts; // constants
|
||||
c11_vector/*T=StrName*/ varnames; // local variables
|
||||
c11_vector/*T=py_Name*/ varnames; // local variables
|
||||
int nlocals; // cached varnames.size()
|
||||
|
||||
c11_smallmap_n2i varnames_inv;
|
||||
|
@ -17,7 +17,7 @@ extern "C" {
|
||||
// } pkpy_ExceptionFrame;
|
||||
|
||||
// typedef struct pkpy_Exception {
|
||||
// StrName type;
|
||||
// py_Name type;
|
||||
// py_Str msg;
|
||||
// bool is_re;
|
||||
|
||||
@ -29,7 +29,7 @@ extern "C" {
|
||||
// c11_vector/*T=pkpy_ExceptionFrame*/ stacktrace;
|
||||
// } pkpy_Exception;
|
||||
|
||||
// void pkpy_Exception__ctor(pkpy_Exception* self, StrName type);
|
||||
// void pkpy_Exception__ctor(pkpy_Exception* self, py_Name type);
|
||||
// void pkpy_Exception__dtor(pkpy_Exception* self);
|
||||
// void pkpy_Exception__stpush(pkpy_Exception* self, pk_SourceData_ src, int lineno, const char* cursor, const char* name);
|
||||
// py_Str pkpy_Exception__summary(pkpy_Exception* self);
|
||||
|
@ -11,7 +11,7 @@ static c11_smallmap_s2n _interned;
|
||||
static c11_vector /*T=char* */ _r_interned;
|
||||
static bool _initialized = false;
|
||||
|
||||
void pk_StrName__initialize() {
|
||||
void py_Name__initialize() {
|
||||
if(_initialized) return;
|
||||
c11_smallmap_s2n__ctor(&_interned);
|
||||
for(int i = 0; i < _r_interned.count; i++) {
|
||||
@ -30,7 +30,7 @@ void pk_StrName__initialize() {
|
||||
}
|
||||
}
|
||||
|
||||
void pk_StrName__finalize() {
|
||||
void py_Name__finalize() {
|
||||
if(!_initialized) return;
|
||||
// free all char*
|
||||
for(int i = 0; i < _r_interned.count; i++) {
|
||||
@ -40,19 +40,19 @@ void pk_StrName__finalize() {
|
||||
c11_vector__dtor(&_r_interned);
|
||||
}
|
||||
|
||||
uint16_t pk_StrName__map(const char* name) {
|
||||
return pk_StrName__map2((c11_sv){name, strlen(name)});
|
||||
py_Name py_name(const char* name) {
|
||||
return py_name2((c11_sv){name, strlen(name)});
|
||||
}
|
||||
|
||||
uint16_t pk_StrName__map2(c11_sv name) {
|
||||
py_Name py_name2(c11_sv name) {
|
||||
// TODO: PK_GLOBAL_SCOPE_LOCK()
|
||||
if(!_initialized) {
|
||||
pk_StrName__initialize(); // lazy init
|
||||
py_Name__initialize(); // lazy init
|
||||
}
|
||||
uint16_t index = c11_smallmap_s2n__get(&_interned, name, 0);
|
||||
if(index != 0) return index;
|
||||
// generate new index
|
||||
if(_interned.count > 65530) { PK_FATAL_ERROR("StrName index overflow\n"); }
|
||||
if(_interned.count > 65530) { PK_FATAL_ERROR("py_Name index overflow\n"); }
|
||||
// NOTE: we must allocate the string in the heap so iterators are not invalidated
|
||||
char* p = malloc(name.size + 1);
|
||||
memcpy(p, name.data, name.size);
|
||||
@ -65,24 +65,17 @@ uint16_t pk_StrName__map2(c11_sv name) {
|
||||
return index;
|
||||
}
|
||||
|
||||
const char* pk_StrName__rmap(uint16_t index) {
|
||||
const char* py_name2str(py_Name index) {
|
||||
assert(_initialized);
|
||||
assert(index > 0 && index <= _interned.count);
|
||||
return c11__getitem(char*, &_r_interned, index - 1);
|
||||
}
|
||||
|
||||
c11_sv pk_StrName__rmap2(uint16_t index) {
|
||||
const char* p = pk_StrName__rmap(index);
|
||||
c11_sv py_name2sv(py_Name index) {
|
||||
const char* p = py_name2str(index);
|
||||
return (c11_sv){p, strlen(p)};
|
||||
}
|
||||
|
||||
py_Name py_name(const char* name) {
|
||||
return pk_StrName__map(name);
|
||||
}
|
||||
|
||||
const char* py_name2str(py_Name name) {
|
||||
return pk_StrName__rmap(name);
|
||||
}
|
||||
|
||||
bool py_ismagicname(py_Name name){
|
||||
return name <= __missing__;
|
||||
|
@ -64,7 +64,7 @@ typedef struct Ctx {
|
||||
int curr_iblock;
|
||||
bool is_compiling_class;
|
||||
c11_vector /*T=Expr* */ s_expr;
|
||||
c11_vector /*T=StrName*/ global_names;
|
||||
c11_vector /*T=py_Name*/ global_names;
|
||||
c11_smallmap_s2n co_consts_string_dedup_map;
|
||||
} Ctx;
|
||||
|
||||
@ -80,11 +80,11 @@ int Ctx__emit_virtual(Ctx* self, Opcode opcode, uint16_t arg, int line, bool vir
|
||||
void Ctx__revert_last_emit_(Ctx* self);
|
||||
int Ctx__emit_int(Ctx* self, int64_t value, int line);
|
||||
void Ctx__patch_jump(Ctx* self, int index);
|
||||
bool Ctx__add_label(Ctx* self, StrName name);
|
||||
int Ctx__add_varname(Ctx* self, StrName name);
|
||||
bool Ctx__add_label(Ctx* self, py_Name name);
|
||||
int Ctx__add_varname(Ctx* self, py_Name name);
|
||||
int Ctx__add_const(Ctx* self, py_Ref);
|
||||
int Ctx__add_const_string(Ctx* self, c11_sv);
|
||||
void Ctx__emit_store_name(Ctx* self, NameScope scope, StrName name, int line);
|
||||
void Ctx__emit_store_name(Ctx* self, NameScope scope, py_Name name, int line);
|
||||
void Ctx__try_merge_for_iter_store(Ctx* self, int);
|
||||
void Ctx__s_emit_top(Ctx*); // emit top -> pop -> delete
|
||||
void Ctx__s_push(Ctx*, Expr*); // push
|
||||
@ -97,7 +97,7 @@ void Ctx__s_emit_decorators(Ctx*, int count);
|
||||
/* expr.c */
|
||||
typedef struct NameExpr {
|
||||
EXPR_COMMON_HEADER
|
||||
StrName name;
|
||||
py_Name name;
|
||||
NameScope scope;
|
||||
} NameExpr;
|
||||
|
||||
@ -144,7 +144,7 @@ bool NameExpr__emit_store(Expr* self_, Ctx* ctx) {
|
||||
return true;
|
||||
}
|
||||
|
||||
NameExpr* NameExpr__new(int line, StrName name, NameScope scope) {
|
||||
NameExpr* NameExpr__new(int line, py_Name name, NameScope scope) {
|
||||
const static ExprVt Vt = {.emit_ = NameExpr__emit_,
|
||||
.emit_del = NameExpr__emit_del,
|
||||
.emit_store = NameExpr__emit_store,
|
||||
@ -630,8 +630,8 @@ static void _load_simple_expr(Ctx* ctx, c11_sv expr, int line) {
|
||||
// name or name.name
|
||||
bool is_fastpath = false;
|
||||
if(is_identifier(expr)) {
|
||||
// ctx->emit_(OP_LOAD_NAME, StrName(expr.sv()).index, line);
|
||||
Ctx__emit_(ctx, OP_LOAD_NAME, pk_StrName__map2(expr), line);
|
||||
// ctx->emit_(OP_LOAD_NAME, py_Name(expr.sv()).index, line);
|
||||
Ctx__emit_(ctx, OP_LOAD_NAME, py_name2(expr), line);
|
||||
is_fastpath = true;
|
||||
} else {
|
||||
int dot = c11_sv__index(expr, '.');
|
||||
@ -639,8 +639,8 @@ static void _load_simple_expr(Ctx* ctx, c11_sv expr, int line) {
|
||||
c11_sv a = {expr.data, dot}; // expr[:dot]
|
||||
c11_sv b = {expr.data + (dot + 1), expr.size - (dot + 1)}; // expr[dot+1:]
|
||||
if(is_identifier(a) && is_identifier(b)) {
|
||||
Ctx__emit_(ctx, OP_LOAD_NAME, pk_StrName__map2(a), line);
|
||||
Ctx__emit_(ctx, OP_LOAD_ATTR, pk_StrName__map2(b), line);
|
||||
Ctx__emit_(ctx, OP_LOAD_NAME, py_name2(a), line);
|
||||
Ctx__emit_(ctx, OP_LOAD_ATTR, py_name2(b), line);
|
||||
is_fastpath = true;
|
||||
}
|
||||
}
|
||||
@ -1073,7 +1073,7 @@ SubscrExpr* SubscrExpr__new(int line) {
|
||||
typedef struct AttribExpr {
|
||||
EXPR_COMMON_HEADER
|
||||
Expr* child;
|
||||
StrName name;
|
||||
py_Name name;
|
||||
} AttribExpr;
|
||||
|
||||
void AttribExpr__emit_(Expr* self_, Ctx* ctx) {
|
||||
@ -1111,7 +1111,7 @@ bool AttribExpr__emit_istore(Expr* self_, Ctx* ctx) {
|
||||
return true;
|
||||
}
|
||||
|
||||
AttribExpr* AttribExpr__new(int line, Expr* child, StrName name) {
|
||||
AttribExpr* AttribExpr__new(int line, Expr* child, py_Name name) {
|
||||
const static ExprVt Vt = {.emit_ = AttribExpr__emit_,
|
||||
.emit_del = AttribExpr__emit_del,
|
||||
.emit_store = AttribExpr__emit_store,
|
||||
@ -1128,7 +1128,7 @@ AttribExpr* AttribExpr__new(int line, Expr* child, StrName name) {
|
||||
}
|
||||
|
||||
typedef struct CallExprKwArg {
|
||||
StrName key;
|
||||
py_Name key;
|
||||
Expr* val;
|
||||
} CallExprKwArg;
|
||||
|
||||
@ -1209,7 +1209,7 @@ void Ctx__ctor(Ctx* self, CodeObject* co, FuncDecl* func, int level) {
|
||||
self->curr_iblock = 0;
|
||||
self->is_compiling_class = false;
|
||||
c11_vector__ctor(&self->s_expr, sizeof(Expr*));
|
||||
c11_vector__ctor(&self->global_names, sizeof(StrName));
|
||||
c11_vector__ctor(&self->global_names, sizeof(py_Name));
|
||||
c11_smallmap_s2n__ctor(&self->co_consts_string_dedup_map);
|
||||
}
|
||||
|
||||
@ -1326,14 +1326,14 @@ void Ctx__patch_jump(Ctx* self, int index) {
|
||||
Bytecode__set_signed_arg(&co_codes[index], target - index);
|
||||
}
|
||||
|
||||
bool Ctx__add_label(Ctx* self, StrName name) {
|
||||
bool Ctx__add_label(Ctx* self, py_Name name) {
|
||||
bool ok = c11_smallmap_n2i__contains(&self->co->labels, name);
|
||||
if(ok) return false;
|
||||
c11_smallmap_n2i__set(&self->co->labels, name, self->co->codes.count);
|
||||
return true;
|
||||
}
|
||||
|
||||
int Ctx__add_varname(Ctx* self, StrName name) {
|
||||
int Ctx__add_varname(Ctx* self, py_Name name) {
|
||||
// PK_MAX_CO_VARNAMES will be checked when pop_context(), not here
|
||||
int index = c11_smallmap_n2i__get(&self->co->varnames_inv, name, -1);
|
||||
if(index >= 0) return index;
|
||||
@ -1366,7 +1366,7 @@ int Ctx__add_const(Ctx* self, py_Ref v) {
|
||||
return self->co->consts.count - 1;
|
||||
}
|
||||
|
||||
void Ctx__emit_store_name(Ctx* self, NameScope scope, StrName name, int line) {
|
||||
void Ctx__emit_store_name(Ctx* self, NameScope scope, py_Name name, int line) {
|
||||
switch(scope) {
|
||||
case NAME_LOCAL: Ctx__emit_(self, OP_STORE_FAST, Ctx__add_varname(self, name), line); break;
|
||||
case NAME_GLOBAL: Ctx__emit_(self, OP_STORE_GLOBAL, name, line); break;
|
||||
@ -1795,11 +1795,11 @@ static Error* exprGroup(Compiler* self) {
|
||||
}
|
||||
|
||||
static Error* exprName(Compiler* self) {
|
||||
StrName name = pk_StrName__map2(Token__sv(prev()));
|
||||
py_Name name = py_name2(Token__sv(prev()));
|
||||
NameScope scope = name_scope(self);
|
||||
// promote this name to global scope if needed
|
||||
c11_vector* global_names = &ctx()->global_names;
|
||||
c11__foreach(StrName, global_names, it) {
|
||||
c11__foreach(py_Name, global_names, it) {
|
||||
if(*it == name) scope = NAME_GLOBAL;
|
||||
}
|
||||
NameExpr* e = NameExpr__new(prev()->line, name, scope);
|
||||
@ -1809,7 +1809,7 @@ static Error* exprName(Compiler* self) {
|
||||
|
||||
static Error* exprAttrib(Compiler* self) {
|
||||
consume(TK_ID);
|
||||
StrName name = pk_StrName__map2(Token__sv(prev()));
|
||||
py_Name name = py_name2(Token__sv(prev()));
|
||||
AttribExpr* e = AttribExpr__new(prev()->line, Ctx__s_popx(ctx()), name);
|
||||
Ctx__s_push(ctx(), (Expr*)e);
|
||||
return NULL;
|
||||
@ -1922,7 +1922,7 @@ static Error* exprCall(Compiler* self) {
|
||||
if(curr()->type == TK_RPAREN) break;
|
||||
if(curr()->type == TK_ID && next()->type == TK_ASSIGN) {
|
||||
consume(TK_ID);
|
||||
StrName key = pk_StrName__map2(Token__sv(prev()));
|
||||
py_Name key = py_name2(Token__sv(prev()));
|
||||
consume(TK_ASSIGN);
|
||||
check(EXPR(self));
|
||||
CallExprKwArg kw = {key, Ctx__s_popx(ctx())};
|
||||
|
@ -2,7 +2,7 @@
|
||||
// #include "pocketpy/common/strname.h"
|
||||
// #include "pocketpy/common/sstream.h"
|
||||
|
||||
// void pkpy_Exception__ctor(pkpy_Exception* self, StrName type){
|
||||
// void pkpy_Exception__ctor(pkpy_Exception* self, py_Name type){
|
||||
// self->type = type;
|
||||
// self->is_re = true;
|
||||
// self->_ip_on_error = -1;
|
||||
@ -48,7 +48,7 @@
|
||||
// c11_sbuf__write_cstr(&ss, "\n");
|
||||
// }
|
||||
|
||||
// const char* name = pk_StrName__rmap(self->type);
|
||||
// const char* name = py_Name__rmap(self->type);
|
||||
// c11_sbuf__write_cstr(&ss, name);
|
||||
|
||||
// if(self->msg.size > 0){
|
||||
|
@ -138,7 +138,7 @@ pk_FrameResult pk_VM__run_top_frame(pk_VM* self) {
|
||||
// obj =
|
||||
// new_object<Function>(tp_function, decl, frame->_module, nullptr,
|
||||
// captured);
|
||||
// uint16_t name = pk_StrName__map2(py_Str__sv(&decl->code->name));
|
||||
// uint16_t name = py_Name__map2(py_Str__sv(&decl->code->name));
|
||||
// captured->set(name, obj);
|
||||
// } else {
|
||||
// obj = new_object<Function>(tp_function, decl, frame->_module, nullptr,
|
||||
@ -340,7 +340,7 @@ pk_FrameResult pk_VM__run_top_frame(pk_VM* self) {
|
||||
DISPATCH();
|
||||
}
|
||||
case OP_DELETE_NAME: {
|
||||
StrName name = byte.arg;
|
||||
py_Name name = byte.arg;
|
||||
if(frame->function) {
|
||||
py_TValue* slot = Frame__f_locals_try_get(frame, name);
|
||||
if(slot) {
|
||||
@ -366,7 +366,7 @@ pk_FrameResult pk_VM__run_top_frame(pk_VM* self) {
|
||||
DISPATCH();
|
||||
}
|
||||
case OP_DELETE_GLOBAL: {
|
||||
StrName name = byte.arg;
|
||||
py_Name name = byte.arg;
|
||||
bool ok = pk_NameDict__del(Frame__f_globals(frame), name);
|
||||
if(!ok) {
|
||||
NameError(name);
|
||||
@ -598,7 +598,7 @@ pk_FrameResult pk_VM__run_top_frame(pk_VM* self) {
|
||||
DISPATCH_JUMP_ABSOLUTE(target);
|
||||
}
|
||||
// case OP_GOTO: {
|
||||
// StrName _name(byte.arg);
|
||||
// py_Name _name(byte.arg);
|
||||
// int target = c11_smallmap_n2i__get(&frame->co->labels, byte.arg, -1);
|
||||
// if(target < 0) RuntimeError(_S("label ", _name.escape(), " not found"));
|
||||
// frame->prepare_jump_break(&s_data, target);
|
||||
|
@ -132,7 +132,7 @@ void Frame__set_unwind_target(Frame* self, py_TValue* sp) {
|
||||
}
|
||||
}
|
||||
|
||||
py_TValue* Frame__f_closure_try_get(Frame* self, StrName name){
|
||||
py_TValue* Frame__f_closure_try_get(Frame* self, py_Name name){
|
||||
// if(self->function == NULL) return NULL;
|
||||
// pkpy::Function* fn = PyObject__as(pkpy::Function, self->function);
|
||||
// if(fn->_closure == nullptr) return nullptr;
|
||||
|
@ -11,7 +11,7 @@ static pk_VM pk_default_vm;
|
||||
|
||||
void py_initialize() {
|
||||
pk_MemoryPools__initialize();
|
||||
pk_StrName__initialize();
|
||||
py_Name__initialize();
|
||||
pk_current_vm = &pk_default_vm;
|
||||
pk_VM__ctor(&pk_default_vm);
|
||||
}
|
||||
@ -19,7 +19,7 @@ void py_initialize() {
|
||||
void py_finalize() {
|
||||
pk_VM__dtor(&pk_default_vm);
|
||||
pk_current_vm = NULL;
|
||||
pk_StrName__finalize();
|
||||
py_Name__finalize();
|
||||
pk_MemoryPools__finalize();
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user