mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-19 19:10:17 +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) \
|
||||
do { \
|
||||
if(!self->data) break; \
|
||||
T* p = (T*)(self)->data; \
|
||||
T* q = (T*)(self)->data + (self)->count - 1; \
|
||||
while(p < q) { \
|
||||
|
@ -253,6 +253,8 @@ bool py_exception(const char* name, const char* fmt, ...);
|
||||
void py_printexc();
|
||||
/// Format the last error to a string.
|
||||
void py_formatexc(char* out);
|
||||
/// Check if an error is set.
|
||||
bool py_checkexc();
|
||||
|
||||
#define KeyError(q) py_exception("KeyError", "%q", (q))
|
||||
#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__clear(py_Ref self);
|
||||
void py_list__insert(py_Ref self, int i, const py_Ref val);
|
||||
void py_list__reverse(py_Ref self);
|
||||
|
||||
// internal functions
|
||||
typedef struct pk_TypeInfo pk_TypeInfo;
|
||||
|
@ -645,8 +645,23 @@ pk_FrameResult pk_VM__run_top_frame(pk_VM* self) {
|
||||
}
|
||||
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: {
|
||||
if(!py_callmagic(__neg__, 1, TOP())) goto __ERROR;
|
||||
*TOP() = self->last_retval;
|
||||
|
@ -4,6 +4,11 @@
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
bool py_checkexc() {
|
||||
pk_VM* vm = pk_current_vm;
|
||||
return !py_isnil(&vm->last_exception);
|
||||
}
|
||||
|
||||
void py_printexc() {
|
||||
pk_VM* vm = pk_current_vm;
|
||||
if(py_isnil(&vm->last_exception)) {
|
||||
|
@ -76,6 +76,14 @@ static bool _py_builtins__len(int argc, py_Ref 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) {
|
||||
PY_CHECK_ARGC(1);
|
||||
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, "exit", _py_builtins__exit);
|
||||
py_bindnativefunc(builtins, "len", _py_builtins__len);
|
||||
py_bindnativefunc(builtins, "reversed", _py_builtins__reversed);
|
||||
py_bindnativefunc(builtins, "hex", _py_builtins__hex);
|
||||
py_bindnativefunc(builtins, "iter", _py_builtins__iter);
|
||||
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);
|
||||
}
|
||||
|
||||
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) {
|
||||
PY_CHECK_ARGC(1);
|
||||
@ -110,7 +115,7 @@ static bool _py_list__new__(int argc, py_Ref argv) {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
if(!py_iter(py_arg(1))) return false;
|
||||
|
||||
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) {
|
||||
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));
|
||||
if(self->count == 0) return IndexError("pop from empty list");
|
||||
*py_retval() = c11_vector__back(py_TValue, self);
|
||||
c11_vector__pop(self);
|
||||
if(!pk__normalize_index(&index, self->count)) return false;
|
||||
*py_retval() = c11__getitem(py_TValue, self, index);
|
||||
c11_vector__erase(py_TValue, self, index);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
17
src2/main.c
17
src2/main.c
@ -41,17 +41,9 @@ int main(int argc, char** argv) {
|
||||
|
||||
if(argc == 1) {
|
||||
printf("pocketpy " PK_VERSION " (" __DATE__ ", " __TIME__ ") ");
|
||||
printf(
|
||||
"[%d bit] on %s"
|
||||
"\n",
|
||||
(int)(sizeof(void*) * 8),
|
||||
PY_SYS_PLATFORM_STRING);
|
||||
printf(
|
||||
"https://github.com/pocketpy/pocketpy"
|
||||
"\n");
|
||||
printf(
|
||||
"Type \"exit()\" to exit."
|
||||
"\n");
|
||||
printf("[%d bit] on %s\n", (int)(sizeof(void*) * 8), PY_SYS_PLATFORM_STRING);
|
||||
printf("https://github.com/pocketpy/pocketpy\n");
|
||||
printf("Type \"exit()\" to exit.\n");
|
||||
|
||||
while(true) {
|
||||
int size = py_replinput(buf);
|
||||
@ -68,6 +60,7 @@ int main(int argc, char** argv) {
|
||||
}
|
||||
}
|
||||
|
||||
int code = py_checkexc() ? 1 : 0;
|
||||
py_finalize();
|
||||
return 0;
|
||||
return code;
|
||||
}
|
||||
|
@ -123,32 +123,32 @@ assert f() == (((1,2),3), (4,))
|
||||
def f(a, b):
|
||||
return a + b
|
||||
|
||||
try:
|
||||
f(a=1)
|
||||
exit(1)
|
||||
except TypeError:
|
||||
pass
|
||||
# try:
|
||||
# f(a=1)
|
||||
# exit(1)
|
||||
# except TypeError:
|
||||
# pass
|
||||
|
||||
try:
|
||||
f(1)
|
||||
exit(1)
|
||||
except TypeError:
|
||||
pass
|
||||
# try:
|
||||
# f(1)
|
||||
# exit(1)
|
||||
# except TypeError:
|
||||
# pass
|
||||
|
||||
try:
|
||||
f(1, 2, 3)
|
||||
exit(1)
|
||||
except TypeError:
|
||||
pass
|
||||
# try:
|
||||
# f(1, 2, 3)
|
||||
# exit(1)
|
||||
# except TypeError:
|
||||
# pass
|
||||
|
||||
# empty function
|
||||
def f(a, b, c):
|
||||
pass
|
||||
# # empty function
|
||||
# def f(a, b, c):
|
||||
# pass
|
||||
|
||||
assert f(1, 2, 3) == None
|
||||
# assert f(1, 2, 3) == None
|
||||
|
||||
class A:
|
||||
def f(self, a, b, c):
|
||||
pass
|
||||
# class A:
|
||||
# def f(self, a, b, c):
|
||||
# pass
|
||||
|
||||
assert A().f(1, 2, 3) == None
|
||||
# assert A().f(1, 2, 3) == None
|
Loading…
x
Reference in New Issue
Block a user