Compare commits

..

4 Commits

Author SHA1 Message Date
blueloveTH
a43ac63a82 Update pyrightconfig.json 2024-10-01 16:47:24 +08:00
blueloveTH
f65eec9add add conio module 2024-10-01 14:00:31 +08:00
blueloveTH
b6caac01de add array2d.render 2024-10-01 12:53:33 +08:00
blueloveTH
9c7fdceda3 add os.system 2024-10-01 12:38:07 +08:00
8 changed files with 70 additions and 2 deletions

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
void pk__add_module_pkpy(); void pk__add_module_pkpy();
void pk__add_module_conio();
void pk__add_module_os(); void pk__add_module_os();
void pk__add_module_sys(); void pk__add_module_sys();
void pk__add_module_math(); void pk__add_module_math();

View File

@ -52,6 +52,7 @@ class array2d(Generic[T]):
def copy_(self, other: 'array2d[T] | list[T]') -> None: ... def copy_(self, other: 'array2d[T] | list[T]') -> None: ...
def tolist(self) -> list[list[T]]: ... def tolist(self) -> list[list[T]]: ...
def render(self) -> str: ...
# algorithms # algorithms
def count(self, value: T) -> int: def count(self, value: T) -> int:

View File

@ -0,0 +1,2 @@
def _kbhit() -> int: ...
def _getch() -> int: ...

View File

@ -1,5 +1,6 @@
{ {
"stubPath": "include/typings", "stubPath": "include/typings",
"reportMissingModuleSource": "none", "reportMissingModuleSource": "none",
"reportArgumentType": "none",
"pythonVersion": "3.10" "pythonVersion": "3.10"
} }

View File

@ -205,6 +205,8 @@ void VM__ctor(VM* self) {
// add modules // add modules
pk__add_module_pkpy(); pk__add_module_pkpy();
pk__add_module_conio();
pk__add_module_os(); pk__add_module_os();
pk__add_module_sys(); pk__add_module_sys();
pk__add_module_math(); pk__add_module_math();

View File

@ -316,6 +316,25 @@ static bool array2d_tolist(int argc, py_Ref argv) {
return true; return true;
} }
static bool array2d_render(int argc, py_Ref argv){
PY_CHECK_ARGC(1);
c11_sbuf buf;
c11_sbuf__ctor(&buf);
c11_array2d* self = py_touserdata(argv);
for(int j = 0; j < self->n_rows; j++) {
for(int i = 0; i < self->n_cols; i++) {
py_Ref item = py_array2d__get(self, i, j);
if(!py_str(item)) return false;
c11_sbuf__write_sv(&buf, py_tosv(py_retval()));
}
if(j < self->n_rows - 1){
c11_sbuf__write_char(&buf, '\n');
}
}
c11_sbuf__py_submit(&buf, py_retval());
return true;
}
static bool array2d_count(int argc, py_Ref argv) { static bool array2d_count(int argc, py_Ref argv) {
// def count(self, value: T) -> int: ... // def count(self, value: T) -> int: ...
PY_CHECK_ARGC(2); PY_CHECK_ARGC(2);
@ -586,6 +605,7 @@ void pk__add_module_array2d() {
py_bindmethod(array2d, "copy_", array2d_copy_); py_bindmethod(array2d, "copy_", array2d_copy_);
py_bindmethod(array2d, "tolist", array2d_tolist); py_bindmethod(array2d, "tolist", array2d_tolist);
py_bindmethod(array2d, "render", array2d_render);
py_bindmethod(array2d, "count", array2d_count); py_bindmethod(array2d, "count", array2d_count);
py_bindmethod(array2d, "find_bounding_rect", array2d_find_bounding_rect); py_bindmethod(array2d, "find_bounding_rect", array2d_find_bounding_rect);
py_bindmethod(array2d, "count_neighbors", array2d_count_neighbors); py_bindmethod(array2d, "count_neighbors", array2d_count_neighbors);

View File

@ -43,10 +43,24 @@ static bool os_getcwd(int argc, py_Ref argv) {
return true; return true;
} }
static bool os_system(int argc, py_Ref argv) {
PY_CHECK_ARGC(1);
PY_CHECK_ARG_TYPE(0, tp_str);
#if PK_IS_DESKTOP_PLATFORM
const char* cmd = py_tostr(py_arg(0));
int code = system(cmd);
py_newint(py_retval(), code);
return true;
#else
return py_exception(tp_OSError, "system() is not supported on this platform");
#endif
}
void pk__add_module_os() { void pk__add_module_os() {
py_Ref mod = py_newmodule("os"); py_Ref mod = py_newmodule("os");
py_bindfunc(mod, "chdir", os_chdir); py_bindfunc(mod, "chdir", os_chdir);
py_bindfunc(mod, "getcwd", os_getcwd); py_bindfunc(mod, "getcwd", os_getcwd);
py_bindfunc(mod, "system", os_system);
} }
void pk__add_module_sys() { void pk__add_module_sys() {

View File

@ -1,5 +1,32 @@
#include "pocketpy/pocketpy.h" #include "pocketpy/pocketpy.h"
void pk__add_module_pkpy() { void pk__add_module_pkpy() { py_newmodule("pkpy"); }
py_newmodule("pkpy");
#ifdef _WIN32
#include <conio.h>
static bool conio__kbhit(int argc, py_Ref argv) {
PY_CHECK_ARGC(0);
int ret = _kbhit();
py_newint(py_retval(), ret);
return true;
}
static bool conio__getch(int argc, py_Ref argv) {
PY_CHECK_ARGC(0);
int ret = _getch();
py_newint(py_retval(), ret);
return true;
}
#endif
void pk__add_module_conio() {
py_Ref mod = py_newmodule("conio");
#ifdef _WIN32
py_bindfunc(mod, "_kbhit", conio__kbhit);
py_bindfunc(mod, "_getch", conio__getch);
#endif
} }