mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 11:30:18 +00:00
up
This commit is contained in:
parent
e737c33b85
commit
212ab7214f
@ -55,7 +55,6 @@ public:
|
|||||||
#define NO_INFIX nullptr, PREC_NONE
|
#define NO_INFIX nullptr, PREC_NONE
|
||||||
for(_TokenType i=0; i<__TOKENS_LEN; i++) rules[i] = { nullptr, NO_INFIX };
|
for(_TokenType i=0; i<__TOKENS_LEN; i++) rules[i] = { nullptr, NO_INFIX };
|
||||||
rules[TK(".")] = { nullptr, METHOD(exprAttrib), PREC_ATTRIB };
|
rules[TK(".")] = { nullptr, METHOD(exprAttrib), PREC_ATTRIB };
|
||||||
rules[TK("->")] = { nullptr, METHOD(exprAttribPtr), PREC_ATTRIB };
|
|
||||||
rules[TK("(")] = { METHOD(exprGrouping), METHOD(exprCall), PREC_CALL };
|
rules[TK("(")] = { METHOD(exprGrouping), METHOD(exprCall), PREC_CALL };
|
||||||
rules[TK("[")] = { METHOD(exprList), METHOD(exprSubscript), PREC_SUBSCRIPT };
|
rules[TK("[")] = { METHOD(exprList), METHOD(exprSubscript), PREC_SUBSCRIPT };
|
||||||
rules[TK("{")] = { METHOD(exprMap), NO_INFIX };
|
rules[TK("{")] = { METHOD(exprMap), NO_INFIX };
|
||||||
@ -102,7 +101,7 @@ public:
|
|||||||
rules[TK(",")] = { nullptr, METHOD(exprComma), PREC_COMMA };
|
rules[TK(",")] = { nullptr, METHOD(exprComma), PREC_COMMA };
|
||||||
rules[TK("<<")] = { nullptr, METHOD(exprBinaryOp), PREC_BITWISE_SHIFT };
|
rules[TK("<<")] = { nullptr, METHOD(exprBinaryOp), PREC_BITWISE_SHIFT };
|
||||||
rules[TK(">>")] = { nullptr, METHOD(exprBinaryOp), PREC_BITWISE_SHIFT };
|
rules[TK(">>")] = { nullptr, METHOD(exprBinaryOp), PREC_BITWISE_SHIFT };
|
||||||
rules[TK("&")] = { METHOD(exprUnaryOp), METHOD(exprBinaryOp), PREC_BITWISE_AND };
|
rules[TK("&")] = { nullptr, METHOD(exprBinaryOp), PREC_BITWISE_AND };
|
||||||
rules[TK("|")] = { nullptr, METHOD(exprBinaryOp), PREC_BITWISE_OR };
|
rules[TK("|")] = { nullptr, METHOD(exprBinaryOp), PREC_BITWISE_OR };
|
||||||
rules[TK("^")] = { nullptr, METHOD(exprBinaryOp), PREC_BITWISE_XOR };
|
rules[TK("^")] = { nullptr, METHOD(exprBinaryOp), PREC_BITWISE_XOR };
|
||||||
#undef METHOD
|
#undef METHOD
|
||||||
@ -492,8 +491,7 @@ public:
|
|||||||
switch (op) {
|
switch (op) {
|
||||||
case TK("-"): emitCode(OP_UNARY_NEGATIVE); break;
|
case TK("-"): emitCode(OP_UNARY_NEGATIVE); break;
|
||||||
case TK("not"): emitCode(OP_UNARY_NOT); break;
|
case TK("not"): emitCode(OP_UNARY_NOT); break;
|
||||||
case TK("&"): emitCode(OP_UNARY_REF); break;
|
case TK("*"): syntaxError("cannot use '*' as unary operator"); break;
|
||||||
case TK("*"): emitCode(OP_UNARY_DEREF); break;
|
|
||||||
default: UNREACHABLE();
|
default: UNREACHABLE();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -619,13 +617,6 @@ __LISTCOMP:
|
|||||||
emitCode(OP_BUILD_ATTR_REF, index);
|
emitCode(OP_BUILD_ATTR_REF, index);
|
||||||
}
|
}
|
||||||
|
|
||||||
void exprAttribPtr(){
|
|
||||||
consume(TK("@id"));
|
|
||||||
const _Str& name = parser->previous.str();
|
|
||||||
int index = getCode()->addName(name, NAME_ATTR);
|
|
||||||
emitCode(OP_BUILD_ATTR_REF_PTR, index);
|
|
||||||
}
|
|
||||||
|
|
||||||
// [:], [:b]
|
// [:], [:b]
|
||||||
// [a], [a:], [a:b]
|
// [a], [a:], [a:b]
|
||||||
void exprSubscript() {
|
void exprSubscript() {
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
#include "pocketpy.h"
|
#include "pocketpy.h"
|
||||||
|
|
||||||
#define PK_DEBUG_TIME
|
//#define PK_DEBUG_TIME
|
||||||
//#define PK_DEBUG_THREADED
|
//#define PK_DEBUG_THREADED
|
||||||
|
|
||||||
struct Timer{
|
struct Timer{
|
||||||
|
@ -56,14 +56,11 @@ OPCODE(BUILD_INDEX_REF) // no arg, [ptr, expr] -> (*ptr)[expr]
|
|||||||
OPCODE(STORE_NAME_REF) // arg for the name_ptr, [expr], directly store to the name_ptr without pushing it to the stack
|
OPCODE(STORE_NAME_REF) // arg for the name_ptr, [expr], directly store to the name_ptr without pushing it to the stack
|
||||||
OPCODE(STORE_REF) // no arg, [ptr, expr] -> *ptr = expr
|
OPCODE(STORE_REF) // no arg, [ptr, expr] -> *ptr = expr
|
||||||
OPCODE(DELETE_REF) // no arg, [ptr] -> [] -> delete ptr
|
OPCODE(DELETE_REF) // no arg, [ptr] -> [] -> delete ptr
|
||||||
OPCODE(BUILD_ATTR_REF_PTR) // arg for the name_ptr, [ptr, name_ptr] -> (*ptr)->name_ptr
|
|
||||||
|
|
||||||
OPCODE(BUILD_SMART_TUPLE) // if all elements are pointers, build a compound pointer, otherwise build a tuple
|
OPCODE(BUILD_SMART_TUPLE) // if all elements are pointers, build a compound pointer, otherwise build a tuple
|
||||||
OPCODE(BUILD_STRING) // arg is the expr count, build a string from the top of the stack
|
OPCODE(BUILD_STRING) // arg is the expr count, build a string from the top of the stack
|
||||||
|
|
||||||
OPCODE(GOTO)
|
OPCODE(GOTO)
|
||||||
OPCODE(UNARY_REF) // for &
|
|
||||||
OPCODE(UNARY_DEREF) // for *
|
|
||||||
|
|
||||||
OPCODE(WITH_ENTER)
|
OPCODE(WITH_ENTER)
|
||||||
OPCODE(WITH_EXIT)
|
OPCODE(WITH_EXIT)
|
||||||
|
@ -55,13 +55,3 @@ struct TupleRef : BaseRef {
|
|||||||
void set(VM* vm, Frame* frame, PyVar val) const;
|
void set(VM* vm, Frame* frame, PyVar val) const;
|
||||||
void del(VM* vm, Frame* frame) const;
|
void del(VM* vm, Frame* frame) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct UserPointer : BaseRef {
|
|
||||||
PyVarRef p;
|
|
||||||
uint64_t f_id;
|
|
||||||
UserPointer(PyVarRef p, uint64_t f_id) : p(p), f_id(f_id) {}
|
|
||||||
|
|
||||||
PyVar get(VM* vm, Frame* frame) const;
|
|
||||||
void set(VM* vm, Frame* frame, PyVar val) const;
|
|
||||||
void del(VM* vm, Frame* frame) const;
|
|
||||||
};
|
|
20
src/str.h
20
src/str.h
@ -25,8 +25,24 @@ public:
|
|||||||
_Str(const char* s) : std::string(s) {}
|
_Str(const char* s) : std::string(s) {}
|
||||||
_Str(const char* s, size_t n) : std::string(s, n) {}
|
_Str(const char* s, size_t n) : std::string(s, n) {}
|
||||||
_Str(const std::string& s) : std::string(s) {}
|
_Str(const std::string& s) : std::string(s) {}
|
||||||
_Str(const _Str& s) : std::string(s) {}
|
_Str(const _Str& s) : std::string(s) {
|
||||||
_Str(_Str&& s) : std::string(std::move(s)) {}
|
if(s._u8_index != nullptr){
|
||||||
|
_u8_index = new std::vector<uint16_t>(*s._u8_index);
|
||||||
|
}
|
||||||
|
if(s.hash_initialized){
|
||||||
|
_hash = s._hash;
|
||||||
|
hash_initialized = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_Str(_Str&& s) : std::string(std::move(s)) {
|
||||||
|
if(_u8_index != nullptr) delete _u8_index;
|
||||||
|
_u8_index = s._u8_index;
|
||||||
|
s._u8_index = nullptr;
|
||||||
|
if(s.hash_initialized){
|
||||||
|
_hash = s._hash;
|
||||||
|
hash_initialized = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
size_t hash() const{
|
size_t hash() const{
|
||||||
if(!hash_initialized){
|
if(!hash_initialized){
|
||||||
|
50
src/vm.h
50
src/vm.h
@ -67,14 +67,6 @@ protected:
|
|||||||
PyVar obj = frame->popValue(this);
|
PyVar obj = frame->popValue(this);
|
||||||
frame->push(PyRef(AttrRef(obj, NameRef(&attr))));
|
frame->push(PyRef(AttrRef(obj, NameRef(&attr))));
|
||||||
} break;
|
} break;
|
||||||
case OP_BUILD_ATTR_REF_PTR: {
|
|
||||||
const auto& attr = frame->code->co_names[byte.arg];
|
|
||||||
PyVar obj = frame->popValue(this);
|
|
||||||
__checkType(obj, _tp_user_pointer);
|
|
||||||
const PyVarRef& var = UNION_GET(PyVarRef, obj);
|
|
||||||
auto p = PyRef_AS_C(var);
|
|
||||||
frame->push(PyRef(AttrRef(p->get(this, frame), &attr)));
|
|
||||||
} break;
|
|
||||||
case OP_BUILD_INDEX_REF: {
|
case OP_BUILD_INDEX_REF: {
|
||||||
PyVar index = frame->popValue(this);
|
PyVar index = frame->popValue(this);
|
||||||
PyVarRef obj = frame->popValue(this);
|
PyVarRef obj = frame->popValue(this);
|
||||||
@ -200,23 +192,6 @@ protected:
|
|||||||
const PyVar& obj_bool = asBool(obj);
|
const PyVar& obj_bool = asBool(obj);
|
||||||
frame->push(PyBool(!PyBool_AS_C(obj_bool)));
|
frame->push(PyBool(!PyBool_AS_C(obj_bool)));
|
||||||
} break;
|
} break;
|
||||||
case OP_UNARY_REF:
|
|
||||||
{
|
|
||||||
// _pointer to pointer
|
|
||||||
PyVarRef obj = frame->__pop();
|
|
||||||
__checkType(obj, _tp_ref);
|
|
||||||
frame->push(newObject(
|
|
||||||
_tp_user_pointer,
|
|
||||||
PyRef(UserPointer(obj, frame->id))
|
|
||||||
));
|
|
||||||
} break;
|
|
||||||
case OP_UNARY_DEREF:
|
|
||||||
{
|
|
||||||
// pointer to _pointer
|
|
||||||
PyVar obj = frame->popValue(this);
|
|
||||||
__checkType(obj, _tp_user_pointer);
|
|
||||||
frame->push(UNION_GET(PyVarRef, obj));
|
|
||||||
} break;
|
|
||||||
case OP_POP_JUMP_IF_FALSE:
|
case OP_POP_JUMP_IF_FALSE:
|
||||||
if(!PyBool_AS_C(asBool(frame->popValue(this)))) frame->jump(byte.arg);
|
if(!PyBool_AS_C(asBool(frame->popValue(this)))) frame->jump(byte.arg);
|
||||||
break;
|
break;
|
||||||
@ -791,7 +766,7 @@ public:
|
|||||||
PyVar _tp_list, _tp_tuple;
|
PyVar _tp_list, _tp_tuple;
|
||||||
PyVar _tp_function, _tp_native_function, _tp_native_iterator, _tp_bounded_method;
|
PyVar _tp_function, _tp_native_function, _tp_native_iterator, _tp_bounded_method;
|
||||||
PyVar _tp_slice, _tp_range, _tp_module, _tp_ref;
|
PyVar _tp_slice, _tp_range, _tp_module, _tp_ref;
|
||||||
PyVar _tp_user_pointer, _tp_super;
|
PyVar _tp_super;
|
||||||
|
|
||||||
template<typename P>
|
template<typename P>
|
||||||
inline PyVarRef PyRef(P&& value) {
|
inline PyVarRef PyRef(P&& value) {
|
||||||
@ -842,8 +817,7 @@ public:
|
|||||||
_tp_slice = newClassType("slice");
|
_tp_slice = newClassType("slice");
|
||||||
_tp_range = newClassType("range");
|
_tp_range = newClassType("range");
|
||||||
_tp_module = newClassType("module");
|
_tp_module = newClassType("module");
|
||||||
_tp_ref = newClassType("_pointer");
|
_tp_ref = newClassType("_ref");
|
||||||
_tp_user_pointer = newClassType("pointer");
|
|
||||||
|
|
||||||
newClassType("NoneType");
|
newClassType("NoneType");
|
||||||
newClassType("ellipsis");
|
newClassType("ellipsis");
|
||||||
@ -927,10 +901,6 @@ public:
|
|||||||
_error("SystemError", msg);
|
_error("SystemError", msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
void nullPointerError(){
|
|
||||||
_error("NullPointerError", "pointer is invalid");
|
|
||||||
}
|
|
||||||
|
|
||||||
void zeroDivisionError(){
|
void zeroDivisionError(){
|
||||||
_error("ZeroDivisionError", "division by zero");
|
_error("ZeroDivisionError", "division by zero");
|
||||||
}
|
}
|
||||||
@ -1076,22 +1046,6 @@ void TupleRef::del(VM* vm, Frame* frame) const{
|
|||||||
for (auto& r : varRefs) vm->PyRef_AS_C(r)->del(vm, frame);
|
for (auto& r : varRefs) vm->PyRef_AS_C(r)->del(vm, frame);
|
||||||
}
|
}
|
||||||
|
|
||||||
PyVar UserPointer::get(VM* vm, Frame* frame) const{
|
|
||||||
frame = vm->__findFrame(f_id);
|
|
||||||
if(frame == nullptr) vm->nullPointerError();
|
|
||||||
return vm->PyRef_AS_C(p)->get(vm, frame);
|
|
||||||
}
|
|
||||||
|
|
||||||
void UserPointer::set(VM* vm, Frame* frame, PyVar val) const{
|
|
||||||
frame = vm->__findFrame(f_id);
|
|
||||||
if(frame == nullptr) vm->nullPointerError();
|
|
||||||
vm->PyRef_AS_C(p)->set(vm, frame, val);
|
|
||||||
}
|
|
||||||
|
|
||||||
void UserPointer::del(VM* vm, Frame* frame) const{
|
|
||||||
vm->typeError("delete is unsupported");
|
|
||||||
}
|
|
||||||
|
|
||||||
/***** Frame's Impl *****/
|
/***** Frame's Impl *****/
|
||||||
inline PyVar Frame::__deref_pointer(VM* vm, PyVar v){
|
inline PyVar Frame::__deref_pointer(VM* vm, PyVar v){
|
||||||
if(v->isType(vm->_tp_ref)) return vm->PyRef_AS_C(v)->get(vm, this);
|
if(v->isType(vm->_tp_ref)) return vm->PyRef_AS_C(v)->get(vm, this);
|
||||||
|
@ -1,36 +0,0 @@
|
|||||||
a = 1
|
|
||||||
assert a == 1
|
|
||||||
assert *&a == 1
|
|
||||||
b = &a
|
|
||||||
*b = 2
|
|
||||||
assert a == 2
|
|
||||||
|
|
||||||
def swap(a,b):
|
|
||||||
t = *a
|
|
||||||
*a = *b
|
|
||||||
*b = t
|
|
||||||
|
|
||||||
def f():
|
|
||||||
a,b = 5,6
|
|
||||||
swap(&a,&b)
|
|
||||||
assert a == 6
|
|
||||||
assert b == 5
|
|
||||||
|
|
||||||
f()
|
|
||||||
|
|
||||||
a = [1, 2, 3]
|
|
||||||
b = &a
|
|
||||||
b->append(4)
|
|
||||||
assert a == [1, 2, 3, 4]
|
|
||||||
|
|
||||||
def add(a, b):
|
|
||||||
return a+b
|
|
||||||
|
|
||||||
p = &add
|
|
||||||
assert p->__call__(1, 2) == 3
|
|
||||||
assert p->__call__.__call__.__call__.__call__.__call__(3, 4) == 7
|
|
||||||
|
|
||||||
fun = lambda :6
|
|
||||||
p = &fun
|
|
||||||
assert p->__call__() == 6
|
|
||||||
assert p->__call__.__call__.__call__.__call__.__call__() == 6
|
|
Loading…
x
Reference in New Issue
Block a user