mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 03:20:18 +00:00
...
This commit is contained in:
parent
a87641c04d
commit
0b649f3bef
@ -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
|
||||
|
@ -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 ...:
|
||||
try:
|
||||
value = next(it)
|
||||
if value is StopIteration:
|
||||
raise TypeError("reduce() of empty iterable with no initial value")
|
||||
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
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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();
|
||||
|
@ -25,3 +25,13 @@ def f3(x, y):
|
||||
|
||||
a = f3(1, 2)
|
||||
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
|
@ -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
|
Loading…
x
Reference in New Issue
Block a user