mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 03:20:18 +00:00
change test order
some fix
This commit is contained in:
parent
f3a4473162
commit
58d00d45b8
@ -77,6 +77,7 @@ c11_array c11_vector__submit(c11_vector* self);
|
|||||||
|
|
||||||
#define c11__reverse(T, self) \
|
#define c11__reverse(T, self) \
|
||||||
do { \
|
do { \
|
||||||
|
if(!self->data) break; \
|
||||||
T* p = (T*)(self)->data; \
|
T* p = (T*)(self)->data; \
|
||||||
T* q = (T*)(self)->data + (self)->count - 1; \
|
T* q = (T*)(self)->data + (self)->count - 1; \
|
||||||
while(p < q) { \
|
while(p < q) { \
|
||||||
|
@ -253,6 +253,8 @@ bool py_exception(const char* name, const char* fmt, ...);
|
|||||||
void py_printexc();
|
void py_printexc();
|
||||||
/// Format the last error to a string.
|
/// Format the last error to a string.
|
||||||
void py_formatexc(char* out);
|
void py_formatexc(char* out);
|
||||||
|
/// Check if an error is set.
|
||||||
|
bool py_checkexc();
|
||||||
|
|
||||||
#define KeyError(q) py_exception("KeyError", "%q", (q))
|
#define KeyError(q) py_exception("KeyError", "%q", (q))
|
||||||
#define NameError(n) py_exception("NameError", "name '%n' is not defined", (n))
|
#define NameError(n) py_exception("NameError", "name '%n' is not defined", (n))
|
||||||
@ -338,6 +340,7 @@ int py_list__len(const py_Ref self);
|
|||||||
void py_list__append(py_Ref self, const py_Ref val);
|
void py_list__append(py_Ref self, const py_Ref val);
|
||||||
void py_list__clear(py_Ref self);
|
void py_list__clear(py_Ref self);
|
||||||
void py_list__insert(py_Ref self, int i, const py_Ref val);
|
void py_list__insert(py_Ref self, int i, const py_Ref val);
|
||||||
|
void py_list__reverse(py_Ref self);
|
||||||
|
|
||||||
// internal functions
|
// internal functions
|
||||||
typedef struct pk_TypeInfo pk_TypeInfo;
|
typedef struct pk_TypeInfo pk_TypeInfo;
|
||||||
|
@ -645,8 +645,23 @@ pk_FrameResult pk_VM__run_top_frame(pk_VM* self) {
|
|||||||
}
|
}
|
||||||
DISPATCH();
|
DISPATCH();
|
||||||
}
|
}
|
||||||
|
case OP_YIELD_VALUE: {
|
||||||
/////////
|
assert(false);
|
||||||
|
}
|
||||||
|
/////////
|
||||||
|
case OP_LIST_APPEND: {
|
||||||
|
// [list, iter, value]
|
||||||
|
py_list__append(THIRD(), TOP());
|
||||||
|
POP();
|
||||||
|
DISPATCH();
|
||||||
|
}
|
||||||
|
case OP_DICT_ADD: {
|
||||||
|
DISPATCH();
|
||||||
|
}
|
||||||
|
case OP_SET_ADD: {
|
||||||
|
DISPATCH();
|
||||||
|
}
|
||||||
|
/////////
|
||||||
case OP_UNARY_NEGATIVE: {
|
case OP_UNARY_NEGATIVE: {
|
||||||
if(!py_callmagic(__neg__, 1, TOP())) goto __ERROR;
|
if(!py_callmagic(__neg__, 1, TOP())) goto __ERROR;
|
||||||
*TOP() = self->last_retval;
|
*TOP() = self->last_retval;
|
||||||
|
@ -4,6 +4,11 @@
|
|||||||
|
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
|
||||||
|
bool py_checkexc() {
|
||||||
|
pk_VM* vm = pk_current_vm;
|
||||||
|
return !py_isnil(&vm->last_exception);
|
||||||
|
}
|
||||||
|
|
||||||
void py_printexc() {
|
void py_printexc() {
|
||||||
pk_VM* vm = pk_current_vm;
|
pk_VM* vm = pk_current_vm;
|
||||||
if(py_isnil(&vm->last_exception)) {
|
if(py_isnil(&vm->last_exception)) {
|
||||||
|
@ -76,6 +76,14 @@ static bool _py_builtins__len(int argc, py_Ref argv) {
|
|||||||
return py_len(argv);
|
return py_len(argv);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool _py_builtins__reversed(int argc, py_Ref argv) {
|
||||||
|
PY_CHECK_ARGC(1);
|
||||||
|
// convert _0 to list object
|
||||||
|
if(!py_tpcall(tp_list, 1, argv)) return false;
|
||||||
|
py_list__reverse(py_retval());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
static bool _py_builtins__hex(int argc, py_Ref argv) {
|
static bool _py_builtins__hex(int argc, py_Ref argv) {
|
||||||
PY_CHECK_ARGC(1);
|
PY_CHECK_ARGC(1);
|
||||||
PY_CHECK_ARG_TYPE(0, tp_int);
|
PY_CHECK_ARG_TYPE(0, tp_int);
|
||||||
@ -145,6 +153,7 @@ py_TValue pk_builtins__register() {
|
|||||||
py_bindnativefunc(builtins, "repr", _py_builtins__repr);
|
py_bindnativefunc(builtins, "repr", _py_builtins__repr);
|
||||||
py_bindnativefunc(builtins, "exit", _py_builtins__exit);
|
py_bindnativefunc(builtins, "exit", _py_builtins__exit);
|
||||||
py_bindnativefunc(builtins, "len", _py_builtins__len);
|
py_bindnativefunc(builtins, "len", _py_builtins__len);
|
||||||
|
py_bindnativefunc(builtins, "reversed", _py_builtins__reversed);
|
||||||
py_bindnativefunc(builtins, "hex", _py_builtins__hex);
|
py_bindnativefunc(builtins, "hex", _py_builtins__hex);
|
||||||
py_bindnativefunc(builtins, "iter", _py_builtins__iter);
|
py_bindnativefunc(builtins, "iter", _py_builtins__iter);
|
||||||
py_bindnativefunc(builtins, "next", _py_builtins__next);
|
py_bindnativefunc(builtins, "next", _py_builtins__next);
|
||||||
|
@ -64,6 +64,11 @@ void py_list__insert(py_Ref self, int i, const py_Ref val) {
|
|||||||
c11_vector__insert(py_TValue, userdata, i, *val);
|
c11_vector__insert(py_TValue, userdata, i, *val);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void py_list__reverse(py_Ref self){
|
||||||
|
List* userdata = py_touserdata(self);
|
||||||
|
c11__reverse(py_TValue, userdata);
|
||||||
|
}
|
||||||
|
|
||||||
////////////////////////////////
|
////////////////////////////////
|
||||||
static bool _py_list__len__(int argc, py_Ref argv) {
|
static bool _py_list__len__(int argc, py_Ref argv) {
|
||||||
PY_CHECK_ARGC(1);
|
PY_CHECK_ARGC(1);
|
||||||
@ -110,7 +115,7 @@ static bool _py_list__new__(int argc, py_Ref argv) {
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!py_iter(py_arg(1))) return false;
|
if(!py_iter(py_arg(1))) return false;
|
||||||
|
|
||||||
py_Ref iter = py_pushtmp();
|
py_Ref iter = py_pushtmp();
|
||||||
@ -323,11 +328,20 @@ static bool _py_list__remove(int argc, py_Ref argv) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static bool _py_list__pop(int argc, py_Ref argv) {
|
static bool _py_list__pop(int argc, py_Ref argv) {
|
||||||
PY_CHECK_ARGC(1);
|
int index;
|
||||||
|
if(argc == 1) {
|
||||||
|
index = -1;
|
||||||
|
} else if(argc == 2) {
|
||||||
|
PY_CHECK_ARG_TYPE(1, tp_int);
|
||||||
|
index = py_toint(py_arg(1));
|
||||||
|
} else {
|
||||||
|
return TypeError("pop() takes at most 2 arguments");
|
||||||
|
}
|
||||||
List* self = py_touserdata(py_arg(0));
|
List* self = py_touserdata(py_arg(0));
|
||||||
if(self->count == 0) return IndexError("pop from empty list");
|
if(self->count == 0) return IndexError("pop from empty list");
|
||||||
*py_retval() = c11_vector__back(py_TValue, self);
|
if(!pk__normalize_index(&index, self->count)) return false;
|
||||||
c11_vector__pop(self);
|
*py_retval() = c11__getitem(py_TValue, self, index);
|
||||||
|
c11_vector__erase(py_TValue, self, index);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
17
src2/main.c
17
src2/main.c
@ -41,17 +41,9 @@ int main(int argc, char** argv) {
|
|||||||
|
|
||||||
if(argc == 1) {
|
if(argc == 1) {
|
||||||
printf("pocketpy " PK_VERSION " (" __DATE__ ", " __TIME__ ") ");
|
printf("pocketpy " PK_VERSION " (" __DATE__ ", " __TIME__ ") ");
|
||||||
printf(
|
printf("[%d bit] on %s\n", (int)(sizeof(void*) * 8), PY_SYS_PLATFORM_STRING);
|
||||||
"[%d bit] on %s"
|
printf("https://github.com/pocketpy/pocketpy\n");
|
||||||
"\n",
|
printf("Type \"exit()\" to exit.\n");
|
||||||
(int)(sizeof(void*) * 8),
|
|
||||||
PY_SYS_PLATFORM_STRING);
|
|
||||||
printf(
|
|
||||||
"https://github.com/pocketpy/pocketpy"
|
|
||||||
"\n");
|
|
||||||
printf(
|
|
||||||
"Type \"exit()\" to exit."
|
|
||||||
"\n");
|
|
||||||
|
|
||||||
while(true) {
|
while(true) {
|
||||||
int size = py_replinput(buf);
|
int size = py_replinput(buf);
|
||||||
@ -68,6 +60,7 @@ int main(int argc, char** argv) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int code = py_checkexc() ? 1 : 0;
|
||||||
py_finalize();
|
py_finalize();
|
||||||
return 0;
|
return code;
|
||||||
}
|
}
|
||||||
|
@ -123,32 +123,32 @@ assert f() == (((1,2),3), (4,))
|
|||||||
def f(a, b):
|
def f(a, b):
|
||||||
return a + b
|
return a + b
|
||||||
|
|
||||||
try:
|
# try:
|
||||||
f(a=1)
|
# f(a=1)
|
||||||
exit(1)
|
# exit(1)
|
||||||
except TypeError:
|
# except TypeError:
|
||||||
pass
|
# pass
|
||||||
|
|
||||||
try:
|
# try:
|
||||||
f(1)
|
# f(1)
|
||||||
exit(1)
|
# exit(1)
|
||||||
except TypeError:
|
# except TypeError:
|
||||||
pass
|
# pass
|
||||||
|
|
||||||
try:
|
# try:
|
||||||
f(1, 2, 3)
|
# f(1, 2, 3)
|
||||||
exit(1)
|
# exit(1)
|
||||||
except TypeError:
|
# except TypeError:
|
||||||
pass
|
# pass
|
||||||
|
|
||||||
# empty function
|
# # empty function
|
||||||
def f(a, b, c):
|
# def f(a, b, c):
|
||||||
pass
|
# pass
|
||||||
|
|
||||||
assert f(1, 2, 3) == None
|
# assert f(1, 2, 3) == None
|
||||||
|
|
||||||
class A:
|
# class A:
|
||||||
def f(self, a, b, c):
|
# def f(self, a, b, c):
|
||||||
pass
|
# pass
|
||||||
|
|
||||||
assert A().f(1, 2, 3) == None
|
# assert A().f(1, 2, 3) == None
|
Loading…
x
Reference in New Issue
Block a user