Okay quite a bit was wrong with that, but now it's right

This commit is contained in:
K. Lange 2021-03-06 18:00:14 +09:00
parent 0c08398e4a
commit 1809ebf99e
5 changed files with 1879 additions and 2 deletions

View File

@ -2322,6 +2322,7 @@ static void generatorInner(KrkScanner scannerBefore, Parser parserBefore) {
emitLoop(loopStart);
patchJump(exitJump);
emitByte(OP_POP);
}
/**

View File

@ -90,6 +90,11 @@ KRK_METHOD(generator,__call__,{
size_t stackAfter = krk_currentThread.stackTop - krk_currentThread.stack;
self->running = 0;
if (IS_KWARGS(result) && AS_INTEGER(result) == 0) {
_set_generator_done(self);
return OBJECT_VAL(self);
}
/* Was there an exception? */
if (krk_currentThread.flags & KRK_THREAD_HAS_EXCEPTION) {
_set_generator_done(self);
@ -102,8 +107,9 @@ KRK_METHOD(generator,__call__,{
self->args = realloc(self->args, sizeof(KrkValue) * (self->argCount + newArgs));
self->argCount += newArgs;
} else if (stackAfter < stackBefore) {
_set_generator_done(self);
return OBJECT_VAL(self);
size_t deadArgs = stackBefore - stackAfter;
self->args = realloc(self->args, sizeof(KrkValue) * (self->argCount - deadArgs));
self->argCount -= deadArgs;
}
/* Save stack entries */

View File

@ -1870,6 +1870,9 @@ _resumeHook: (void)0;
}
krk_currentThread.stackTop = &krk_currentThread.stack[frame->outSlots];
if (krk_currentThread.frameCount == (size_t)krk_currentThread.exitOnFrame) {
if (frame->closure->function->isGenerator) {
return KWARGS_VAL(0);
}
return result;
}
krk_push(result);

View File

@ -0,0 +1,16 @@
def agenerator(a=42):
print("First yield")
yield 1
if True:
yield 2
yield 3
print("Into the loops")
for i in range(a):
yield i
print("nest loop", i, a)
for j in range(a):
print("yielding",j)
yield j
print("Done with inner nest from",i)
print(list(agenerator()))

File diff suppressed because one or more lines are too long