Compare commits

..

5 Commits

Author SHA1 Message Date
blueloveTH
61e36941a9 ... 2025-03-12 19:56:26 +08:00
blueloveTH
e25cc48463 improve chr 2025-03-12 19:43:56 +08:00
blueloveTH
b320e8d9a3 Update algorithm.h 2025-03-12 19:24:20 +08:00
blueloveTH
a4f186057d ... 2025-03-12 19:00:13 +08:00
blueloveTH
4ca291b179 Update ideas.md 2025-03-12 17:19:48 +08:00
10 changed files with 87 additions and 22 deletions

View File

@ -8,7 +8,7 @@ label: "Project Ideas"
+ Difficulty Level: 3/5 (Medium) + Difficulty Level: 3/5 (Medium)
+ Skill: TypeScript; C + Skill: TypeScript; C
+ Project Length: Small + Project Length: Medium
Community users have reported that there is no convenient way to debug python applications interpreted by pocketpy. Fortunately, VSCode provides a mechanism of [Debugger Extension](https://code.visualstudio.com/api/extension-guides/debugger-extension) that allows us to integrate pocketpy debugger into VSCode UI through Debug Adapter Protocol (DAP). Community users have reported that there is no convenient way to debug python applications interpreted by pocketpy. Fortunately, VSCode provides a mechanism of [Debugger Extension](https://code.visualstudio.com/api/extension-guides/debugger-extension) that allows us to integrate pocketpy debugger into VSCode UI through Debug Adapter Protocol (DAP).
@ -18,7 +18,7 @@ This project aims to develop a VSCode plugin like [Python Debugger](https://mark
+ Difficulty Level: 4/5 (Hard) + Difficulty Level: 4/5 (Hard)
+ Skill: C; Further Mathematics + Skill: C; Further Mathematics
+ Project Length: Medium + Project Length: Small or Medium
pocketpy is providing a tensor library `cTensor` for users who want to integrate neural networks into their applications. `cTensor` implements automatic differentiation and dynamic compute graph. It allows users to train and deploy neural networks on client-side devices like mobile phones and microcontrollers (e.g. ESP32-C3). We have a runable prototype located at [pocketpy/cTensor](https://github.com/pocketpy/cTensor). But math operators have not been implemented yet. pocketpy is providing a tensor library `cTensor` for users who want to integrate neural networks into their applications. `cTensor` implements automatic differentiation and dynamic compute graph. It allows users to train and deploy neural networks on client-side devices like mobile phones and microcontrollers (e.g. ESP32-C3). We have a runable prototype located at [pocketpy/cTensor](https://github.com/pocketpy/cTensor). But math operators have not been implemented yet.

View File

@ -8,7 +8,7 @@
do { \ do { \
T* __first = ptr; \ T* __first = ptr; \
int __len = count; \ int __len = count; \
while(__len > 8) { \ while(__len >= 8) { \
int __l2 = __len >> 1; \ int __l2 = __len >> 1; \
T* __m = __first + __l2; \ T* __m = __first + __l2; \
if(less((*__m), (key))) { \ if(less((*__m), (key))) { \
@ -18,9 +18,23 @@
__len = __l2; \ __len = __l2; \
} \ } \
} \ } \
while(__len && less(*__first, (key))) { \ switch(__len) { \
++__first; \ case 7: \
--__len; \ if(less(*__first, (key))) __first++; \
case 6: \
if(less(*__first, (key))) __first++; \
case 5: \
if(less(*__first, (key))) __first++; \
case 4: \
if(less(*__first, (key))) __first++; \
case 3: \
if(less(*__first, (key))) __first++; \
case 2: \
if(less(*__first, (key))) __first++; \
case 1: \
if(less(*__first, (key))) __first++; \
case 0: break; \
default: c11__unreachable(); \
} \ } \
*(out_index) = __first - (T*)(ptr); \ *(out_index) = __first - (T*)(ptr); \
} while(0) } while(0)

View File

@ -66,6 +66,7 @@ int c11__byte_index_to_unicode(const char* data, int n);
bool c11__is_unicode_Lo_char(int c); bool c11__is_unicode_Lo_char(int c);
int c11__u8_header(unsigned char c, bool suppress); int c11__u8_header(unsigned char c, bool suppress);
int c11__u8_value(int u8bytes, const char* data); int c11__u8_value(int u8bytes, const char* data);
int c11__u32_to_u8(uint32_t utf32_char, char utf8_output[4]);
typedef enum IntParsingResult { typedef enum IntParsingResult {
IntParsing_SUCCESS, IntParsing_SUCCESS,

View File

@ -594,7 +594,7 @@ PK_API bool py_isidentical(py_Ref, py_Ref);
/// Call a function. /// Call a function.
/// It prepares the stack and then performs a `vectorcall(argc, 0, false)`. /// It prepares the stack and then performs a `vectorcall(argc, 0, false)`.
/// The result will be set to `py_retval()`. /// The result will be set to `py_retval()`.
/// The stack remains unchanged after the operation. /// The stack remains unchanged if successful.
PK_API bool py_call(py_Ref f, int argc, py_Ref argv) PY_RAISE PY_RETURN; PK_API bool py_call(py_Ref f, int argc, py_Ref argv) PY_RAISE PY_RETURN;
#ifndef NDEBUG #ifndef NDEBUG

View File

@ -316,6 +316,38 @@ int c11__u8_value(int u8bytes, const char* data) {
return (int)value; return (int)value;
} }
int c11__u32_to_u8(uint32_t utf32_char, char utf8_output[4]) {
int length = 0;
if(utf32_char <= 0x7F) {
// 1-byte UTF-8
utf8_output[0] = (char)utf32_char;
length = 1;
} else if(utf32_char <= 0x7FF) {
// 2-byte UTF-8
utf8_output[0] = (char)(0xC0 | ((utf32_char >> 6) & 0x1F));
utf8_output[1] = (char)(0x80 | (utf32_char & 0x3F));
length = 2;
} else if(utf32_char <= 0xFFFF) {
// 3-byte UTF-8
utf8_output[0] = (char)(0xE0 | ((utf32_char >> 12) & 0x0F));
utf8_output[1] = (char)(0x80 | ((utf32_char >> 6) & 0x3F));
utf8_output[2] = (char)(0x80 | (utf32_char & 0x3F));
length = 3;
} else if(utf32_char <= 0x10FFFF) {
// 4-byte UTF-8
utf8_output[0] = (char)(0xF0 | ((utf32_char >> 18) & 0x07));
utf8_output[1] = (char)(0x80 | ((utf32_char >> 12) & 0x3F));
utf8_output[2] = (char)(0x80 | ((utf32_char >> 6) & 0x3F));
utf8_output[3] = (char)(0x80 | (utf32_char & 0x3F));
length = 4;
} else {
// Invalid UTF-32 character
return -1;
}
return length;
}
IntParsingResult c11__parse_uint(c11_sv text, int64_t* out, int base) { IntParsingResult c11__parse_uint(c11_sv text, int64_t* out, int base) {
*out = 0; *out = 0;

View File

@ -447,7 +447,10 @@ static Error* lex_one_token(Lexer* self, bool* eof, bool is_fstring) {
} }
case ',': add_token(self, TK_COMMA); return NULL; case ',': add_token(self, TK_COMMA); return NULL;
case ':': { case ':': {
if(is_fstring) { return eat_fstring_spec(self, eof); } if(is_fstring) {
// BUG: f"{stack[2:]}"
return eat_fstring_spec(self, eof);
}
add_token(self, TK_COLON); add_token(self, TK_COLON);
return NULL; return NULL;
} }

View File

@ -74,7 +74,7 @@ void VM__ctor(VM* self) {
self->recursion_depth = 0; self->recursion_depth = 0;
self->max_recursion_depth = 1000; self->max_recursion_depth = 1000;
self->is_curr_exc_handled = false; self->is_curr_exc_handled = false;
self->ctx = NULL; self->ctx = NULL;
@ -92,7 +92,7 @@ void VM__ctor(VM* self) {
char* p = py_newstrn(&self->ascii_literals[i], 1); char* p = py_newstrn(&self->ascii_literals[i], 1);
*p = i; *p = i;
} }
py_newstrn(&self->ascii_literals[128], 0); py_newstrn(&self->ascii_literals[128], 0); // empty string
// 0: unused // 0: unused
void* placeholder = TypeList__emplace(&self->types); void* placeholder = TypeList__emplace(&self->types);

View File

@ -114,13 +114,11 @@ bool py_call(py_Ref f, int argc, py_Ref argv) {
if(f->type == tp_nativefunc) { if(f->type == tp_nativefunc) {
return py_callcfunc(f->_cfunc, argc, argv); return py_callcfunc(f->_cfunc, argc, argv);
} else { } else {
py_StackRef p0 = py_peek(0);
py_push(f); py_push(f);
py_pushnil(); py_pushnil();
for(int i = 0; i < argc; i++) for(int i = 0; i < argc; i++)
py_push(py_offset(argv, i)); py_push(py_offset(argv, i));
bool ok = py_vectorcall(argc, 0); bool ok = py_vectorcall(argc, 0);
pk_current_vm->stack.sp = p0;
return ok; return ok;
} }
} }

View File

@ -449,9 +449,16 @@ static bool builtins_delattr(int argc, py_Ref argv) {
static bool builtins_chr(int argc, py_Ref argv) { static bool builtins_chr(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);
py_i64 val = py_toint(py_arg(0)); uint32_t val = py_toint(py_arg(0));
if(val < 0 || val > 128) { return ValueError("chr() arg not in range(128)"); } if(val >= 0 && val < 128) {
py_assign(py_retval(), &pk_current_vm->ascii_literals[val]); py_assign(py_retval(), &pk_current_vm->ascii_literals[val]);
} else {
// convert to utf-8
char utf8[4];
int len = c11__u32_to_u8(val, utf8);
if(len == -1) return ValueError("invalid unicode code point: %d", val);
py_newstrv(py_retval(), (c11_sv){utf8, len});
}
return true; return true;
} }

View File

@ -191,8 +191,13 @@ assert (1 == '1') is False
assert 1 == 1.0 assert 1 == 1.0
assert chr(97) is 'a' assert chr(97) is 'a'
assert ord('a') == 97
exit() assert ord('🥕') == 0x1f955
assert chr(0x1f955) == '🥕'
assert ord('') == 27979
assert chr(27979) == ''
# test format() # test format()
assert "Hello, {}!".format("World") == "Hello, World!" assert "Hello, {}!".format("World") == "Hello, World!"
@ -207,14 +212,19 @@ assert "{0}={1}".format('{0}', '{1}') == "{0}={1}"
assert "{{{0}}}".format(1) == "{1}" assert "{{{0}}}".format(1) == "{1}"
assert "{0}{1}{1}".format(1, 2, 3) == "122" assert "{0}{1}{1}".format(1, 2, 3) == "122"
# try: try:
# "{0}={1}}".format(1, 2) "{0}={1}}".format(1, 2)
# exit(1) exit(1)
# except ValueError: except ValueError:
# pass pass
assert "{{{}xxx{}x}}".format(1, 2) == "{1xxx2x}" assert "{{{}xxx{}x}}".format(1, 2) == "{1xxx2x}"
assert "{{abc}}".format() == "{abc}" assert "{{abc}}".format() == "{abc}"
# test f-string # test f-string
# stack=[1,2,3,4]; assert f"{stack[2:]}" == '[3, 4]' assert f"{1+2}" == "3"
# assert f"{1, 2, 3}" == "(1, 2, 3)"
assert f"{(1, 2, 3)}" == "(1, 2, 3)"
# stack=[1,2,3,4]
# assert f"{stack[2:]}" == '[3, 4]'