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);
/************* Others *************/
int py_replinput(char* buf);
int py_replinput(char* buf, int max_size);
/// Python favored string formatting. (just put here, not for users)
/// %d: int

View File

@ -1,5 +1,3 @@
from pkpy import next
class cache:
def __init__(self, f):
self.f = f
@ -13,9 +11,10 @@ class cache:
def reduce(function, sequence, initial=...):
it = iter(sequence)
if initial is ...:
value = next(it)
if value is StopIteration:
raise TypeError("reduce() of empty iterable with no initial value")
try:
value = next(it)
except StopIteration:
raise TypeError("reduce() of empty sequence with no initial value")
else:
value = initial
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;
}
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);
py_newstrn(out, res->data, res->size);
c11_string__delete(res);
@ -233,7 +233,9 @@ void pk_sprintf(c11_sbuf* ss, const char* fmt, ...) {
va_end(args);
}
int py_replinput(char* buf) {
int py_replinput(char* buf, int max_size) {
buf[0] = '\0';
int size = 0;
bool multiline = false;
printf(">>> ");
@ -252,7 +254,7 @@ int py_replinput(char* buf) {
printf("... ");
}
} else {
if(last == ':' || last == '(' || last == '[' || last == '{') {
if(last == ':' || last == '(' || last == '[' || last == '{' || buf[0] == '@') {
printf("... ");
multiline = true;
} else {
@ -261,6 +263,11 @@ int py_replinput(char* buf) {
}
}
if(size == max_size - 1) {
buf[size] = '\0';
return size;
}
buf[size++] = c;
}

View File

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

View File

@ -24,4 +24,14 @@ def f3(x, y):
return lambda z: x + y + z
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