mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-19 19:10:17 +00:00
support yield
implicit None
This commit is contained in:
parent
0a77c96c0a
commit
18fc4c02d3
@ -2498,10 +2498,14 @@ static Error* compile_stmt(Compiler* self) {
|
|||||||
break;
|
break;
|
||||||
case TK_YIELD:
|
case TK_YIELD:
|
||||||
if(self->contexts.length <= 1) return SyntaxError(self, "'yield' outside function");
|
if(self->contexts.length <= 1) return SyntaxError(self, "'yield' outside function");
|
||||||
check(EXPR_TUPLE(self));
|
if(match_end_stmt(self)) {
|
||||||
Ctx__s_emit_top(ctx());
|
Ctx__emit_(ctx(), OP_YIELD_VALUE, 1, kw_line);
|
||||||
Ctx__emit_(ctx(), OP_YIELD_VALUE, BC_NOARG, kw_line);
|
} else {
|
||||||
consume_end_stmt();
|
check(EXPR_TUPLE(self));
|
||||||
|
Ctx__s_emit_top(ctx());
|
||||||
|
Ctx__emit_(ctx(), OP_YIELD_VALUE, BC_NOARG, kw_line);
|
||||||
|
consume_end_stmt();
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case TK_YIELD_FROM:
|
case TK_YIELD_FROM:
|
||||||
check(compile_yield_from(self, kw_line));
|
check(compile_yield_from(self, kw_line));
|
||||||
|
@ -715,8 +715,12 @@ FrameResult VM__run_top_frame(VM* self) {
|
|||||||
DISPATCH();
|
DISPATCH();
|
||||||
}
|
}
|
||||||
case OP_YIELD_VALUE: {
|
case OP_YIELD_VALUE: {
|
||||||
py_assign(py_retval(), TOP());
|
if(byte.arg == 1) {
|
||||||
POP();
|
py_newnone(py_retval());
|
||||||
|
} else {
|
||||||
|
py_assign(py_retval(), TOP());
|
||||||
|
POP();
|
||||||
|
}
|
||||||
return RES_YIELD;
|
return RES_YIELD;
|
||||||
}
|
}
|
||||||
/////////
|
/////////
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
def g():
|
def g():
|
||||||
yield 1
|
yield 1
|
||||||
yield 2
|
yield 2
|
||||||
|
yield
|
||||||
|
|
||||||
a = g()
|
a = g()
|
||||||
assert next(a) == 1
|
assert next(a) == 1
|
||||||
assert next(a, None) == 2
|
assert next(a, None) == 2
|
||||||
|
assert next(a) == None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
next(a)
|
next(a)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user