This commit is contained in:
blueloveTH 2024-08-04 22:45:00 +08:00
parent a87641c04d
commit 0b649f3bef
8 changed files with 28 additions and 20 deletions

View File

@ -373,7 +373,7 @@ int py_dict__len(py_Ref self);
bool py_dict__apply(py_Ref self, bool (*f)(py_Ref key, py_Ref val, void* ctx), void* ctx); bool py_dict__apply(py_Ref self, bool (*f)(py_Ref key, py_Ref val, void* ctx), void* ctx);
/************* Others *************/ /************* Others *************/
int py_replinput(char* buf); int py_replinput(char* buf, int max_size);
/// Python favored string formatting. (just put here, not for users) /// Python favored string formatting. (just put here, not for users)
/// %d: int /// %d: int

View File

@ -1,5 +1,3 @@
from pkpy import next
class cache: class cache:
def __init__(self, f): def __init__(self, f):
self.f = f self.f = f
@ -13,9 +11,10 @@ class cache:
def reduce(function, sequence, initial=...): def reduce(function, sequence, initial=...):
it = iter(sequence) it = iter(sequence)
if initial is ...: if initial is ...:
try:
value = next(it) value = next(it)
if value is StopIteration: except StopIteration:
raise TypeError("reduce() of empty iterable with no initial value") raise TypeError("reduce() of empty sequence with no initial value")
else: else:
value = initial value = initial
for element in it: for element in it:

File diff suppressed because one or more lines are too long

View File

@ -147,7 +147,7 @@ c11_string* c11_sbuf__submit(c11_sbuf* self) {
return retval; return retval;
} }
void c11_sbuf__py_submit(c11_sbuf *self, py_Ref out){ void c11_sbuf__py_submit(c11_sbuf* self, py_Ref out) {
c11_string* res = c11_sbuf__submit(self); c11_string* res = c11_sbuf__submit(self);
py_newstrn(out, res->data, res->size); py_newstrn(out, res->data, res->size);
c11_string__delete(res); c11_string__delete(res);
@ -233,7 +233,9 @@ void pk_sprintf(c11_sbuf* ss, const char* fmt, ...) {
va_end(args); va_end(args);
} }
int py_replinput(char* buf) { int py_replinput(char* buf, int max_size) {
buf[0] = '\0';
int size = 0; int size = 0;
bool multiline = false; bool multiline = false;
printf(">>> "); printf(">>> ");
@ -252,7 +254,7 @@ int py_replinput(char* buf) {
printf("... "); printf("... ");
} }
} else { } else {
if(last == ':' || last == '(' || last == '[' || last == '{') { if(last == ':' || last == '(' || last == '[' || last == '{' || buf[0] == '@') {
printf("... "); printf("... ");
multiline = true; multiline = true;
} else { } else {
@ -261,6 +263,11 @@ int py_replinput(char* buf) {
} }
} }
if(size == max_size - 1) {
buf[size] = '\0';
return size;
}
buf[size++] = c; buf[size++] = c;
} }

View File

@ -46,7 +46,7 @@ int main(int argc, char** argv) {
printf("Type \"exit()\" to exit.\n"); printf("Type \"exit()\" to exit.\n");
while(true) { while(true) {
int size = py_replinput(buf); int size = py_replinput(buf, sizeof(buf));
assert(size < sizeof(buf)); assert(size < sizeof(buf));
if(size >= 0) { if(size >= 0) {
if(!py_exec(buf, "<stdin>", REPL_MODE, NULL)) py_printexc(); if(!py_exec(buf, "<stdin>", REPL_MODE, NULL)) py_printexc();

View File

@ -25,3 +25,13 @@ def f3(x, y):
a = f3(1, 2) a = f3(1, 2)
assert a(3) == 6 assert a(3) == 6
# closure ex
def f(n):
def g(x):
if x==n:
return n
return g(x+1)
return g(0)
assert f(10) == 10

View File

@ -1,8 +0,0 @@
def f(n):
def g(x):
if x==n:
return n
return g(x+1)
return g(0)
assert f(10) == 10