Compare commits

..

No commits in common. "18fc4c02d308abd9ac7f5d76a8a958892eff73eb" and "7967852eb99ac3ba079ab273f3039e217525661c" have entirely different histories.

14 changed files with 52 additions and 209 deletions

View File

@ -115,7 +115,6 @@ py_Type pk_range__register();
py_Type pk_range_iterator__register();
py_Type pk_BaseException__register();
py_Type pk_Exception__register();
py_Type pk_StopIteration__register();
py_Type pk_super__register();
py_Type pk_property__register();
py_Type pk_staticmethod__register();

View File

@ -83,7 +83,6 @@ OPCODE(UNARY_INVERT)
/**************************/
OPCODE(GET_ITER)
OPCODE(FOR_ITER)
OPCODE(FOR_ITER_YIELD_VALUE)
/**************************/
OPCODE(IMPORT_PATH)
OPCODE(POP_IMPORT_STAR)

View File

@ -1494,12 +1494,19 @@ static Error* pop_context(Compiler* self) {
int codes_length = func->code.codes.length;
for(int i = 0; i < codes_length; i++) {
if(codes[i].op == OP_YIELD_VALUE || codes[i].op == OP_FOR_ITER_YIELD_VALUE) {
if(codes[i].op == OP_YIELD_VALUE) {
func->type = FuncType_GENERATOR;
for(int j = 0; j < codes_length; j++) {
if(codes[j].op == OP_RETURN_VALUE && codes[j].arg == BC_NOARG) {
Error* err =
SyntaxError(self, "'return' with argument inside generator function");
err->lineno = c11__at(BytecodeEx, &func->code.codes_ex, j)->lineno;
return err;
}
}
break;
}
}
if(func->type == FuncType_UNSET) {
bool is_simple = true;
if(func->kwargs.length > 0) is_simple = false;
@ -2028,20 +2035,6 @@ static Error* compile_for_loop(Compiler* self) {
return NULL;
}
static Error* compile_yield_from(Compiler* self, int kw_line) {
Error* err;
if(self->contexts.length <= 1) return SyntaxError(self, "'yield from' outside function");
check(EXPR_TUPLE(self));
Ctx__s_emit_top(ctx());
Ctx__emit_(ctx(), OP_GET_ITER, BC_NOARG, kw_line);
Ctx__enter_block(ctx(), CodeBlockType_FOR_LOOP);
Ctx__emit_(ctx(), OP_FOR_ITER_YIELD_VALUE, ctx()->curr_iblock, kw_line);
Ctx__emit_(ctx(), OP_LOOP_CONTINUE, Ctx__get_loop(ctx()), kw_line);
Ctx__exit_block(ctx());
// StopIteration.value will be pushed onto the stack
return NULL;
}
Error* try_compile_assignment(Compiler* self, bool* is_assign) {
Error* err;
switch(curr()->type) {
@ -2081,24 +2074,15 @@ Error* try_compile_assignment(Compiler* self, bool* is_assign) {
return NULL;
}
case TK_ASSIGN: {
consume(TK_ASSIGN);
int n = 0;
if(match(TK_YIELD_FROM)) {
check(compile_yield_from(self, prev()->line));
n = 1;
} else {
do {
check(EXPR_TUPLE(self));
n += 1;
} while(match(TK_ASSIGN));
// stack size is n+1
Ctx__s_emit_top(ctx());
for(int j = 1; j < n; j++)
Ctx__emit_(ctx(), OP_DUP_TOP, BC_NOARG, BC_KEEPLINE);
while(match(TK_ASSIGN)) {
check(EXPR_TUPLE(self));
n += 1;
}
// stack size is n+1
Ctx__s_emit_top(ctx());
for(int j = 1; j < n; j++)
Ctx__emit_(ctx(), OP_DUP_TOP, BC_NOARG, BC_KEEPLINE);
for(int j = 0; j < n; j++) {
if(Ctx__s_top(ctx())->vt->is_starred)
return SyntaxError(self, "can't use starred expression here");
@ -2498,18 +2482,22 @@ static Error* compile_stmt(Compiler* self) {
break;
case TK_YIELD:
if(self->contexts.length <= 1) return SyntaxError(self, "'yield' outside function");
if(match_end_stmt(self)) {
Ctx__emit_(ctx(), OP_YIELD_VALUE, 1, kw_line);
} else {
check(EXPR_TUPLE(self));
Ctx__s_emit_top(ctx());
Ctx__emit_(ctx(), OP_YIELD_VALUE, BC_NOARG, kw_line);
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;
case TK_YIELD_FROM:
check(compile_yield_from(self, kw_line));
Ctx__emit_(ctx(), OP_POP_TOP, BC_NOARG, kw_line);
if(self->contexts.length <= 1)
return SyntaxError(self, "'yield from' outside function");
check(EXPR_TUPLE(self));
Ctx__s_emit_top(ctx());
Ctx__emit_(ctx(), OP_GET_ITER, BC_NOARG, kw_line);
Ctx__enter_block(ctx(), CodeBlockType_FOR_LOOP);
Ctx__emit_(ctx(), OP_FOR_ITER, ctx()->curr_iblock, kw_line);
Ctx__emit_(ctx(), OP_YIELD_VALUE, BC_NOARG, kw_line);
Ctx__emit_(ctx(), OP_LOOP_CONTINUE, Ctx__get_loop(ctx()), kw_line);
Ctx__exit_block(ctx());
consume_end_stmt();
break;
case TK_RETURN:

View File

@ -715,12 +715,8 @@ FrameResult VM__run_top_frame(VM* self) {
DISPATCH();
}
case OP_YIELD_VALUE: {
if(byte.arg == 1) {
py_newnone(py_retval());
} else {
py_assign(py_retval(), TOP());
POP();
}
py_assign(py_retval(), TOP());
POP();
return RES_YIELD;
}
/////////
@ -785,25 +781,10 @@ FrameResult VM__run_top_frame(VM* self) {
PUSH(py_retval());
DISPATCH();
} else {
assert(self->last_retval.type == tp_StopIteration);
int target = Frame__prepare_loop_break(frame, &self->stack);
DISPATCH_JUMP_ABSOLUTE(target);
}
}
case OP_FOR_ITER_YIELD_VALUE: {
int res = py_next(TOP());
if(res == -1) goto __ERROR;
if(res) {
return RES_YIELD;
} else {
assert(self->last_retval.type == tp_StopIteration);
py_ObjectRef value = py_getslot(&self->last_retval, 0);
int target = Frame__prepare_loop_break(frame, &self->stack);
if(py_isnil(value)) value = py_None();
PUSH(value);
DISPATCH_JUMP_ABSOLUTE(target);
}
}
////////
case OP_IMPORT_PATH: {
py_Ref path_object = c11__at(py_TValue, &frame->co->consts, byte.arg);
@ -1059,33 +1040,6 @@ FrameResult VM__run_top_frame(VM* self) {
return RES_RETURN;
}
const static char* op2str(py_Name op) {
switch(op) {
case __eq__: return "==";
case __ne__: return "!=";
case __lt__: return "<";
case __le__: return "<=";
case __gt__: return ">";
case __ge__: return ">=";
case __add__: return "+";
case __sub__: return "-";
case __mul__: return "*";
case __truediv__: return "/";
case __floordiv__: return "//";
case __mod__: return "%";
case __pow__: return "**";
case __lshift__: return "<<";
case __rshift__: return ">>";
case __and__: return "&";
case __or__: return "|";
case __xor__: return "^";
case __neg__: return "-";
case __invert__: return "~";
case __matmul__: return "@";
default: return py_name2str(op);
}
}
bool pk_stack_binaryop(VM* self, py_Name op, py_Name rop) {
// [a, b]
py_Ref magic = py_tpfindmagic(SECOND()->type, op);
@ -1117,7 +1071,7 @@ bool pk_stack_binaryop(VM* self, py_Name op, py_Name rop) {
py_newbool(py_retval(), !res);
return true;
}
return TypeError("unsupported operand type(s) for '%s'", op2str(op));
return TypeError("unsupported operand type(s) for '%n'", op);
}
bool py_binaryop(py_Ref lhs, py_Ref rhs, py_Name op, py_Name rop) {

View File

@ -67,10 +67,7 @@ static bool generator__next__(int argc, py_Ref argv) {
} else {
assert(res == RES_RETURN);
ud->state = 2;
// raise StopIteration(<retval>)
bool ok = py_tpcall(tp_StopIteration, 1, py_retval());
if(!ok) return false;
return py_raise(py_retval());
return StopIteration();
}
}

View File

@ -147,10 +147,7 @@ void VM__ctor(VM* self) {
INJECT_BUILTIN_EXC(SystemExit, tp_BaseException);
INJECT_BUILTIN_EXC(KeyboardInterrupt, tp_BaseException);
// INJECT_BUILTIN_EXC(StopIteration, tp_Exception);
validate(tp_StopIteration, pk_StopIteration__register());
py_setdict(&self->builtins, py_name("StopIteration"), py_tpobject(tp_StopIteration));
INJECT_BUILTIN_EXC(StopIteration, tp_Exception);
INJECT_BUILTIN_EXC(SyntaxError, tp_Exception);
INJECT_BUILTIN_EXC(StackOverflowError, tp_Exception);
INJECT_BUILTIN_EXC(IOError, tp_Exception);

View File

@ -255,8 +255,4 @@ bool pk_callmagic(py_Name name, int argc, py_Ref argv) {
return py_call(tmp, argc, argv);
}
bool StopIteration() {
bool ok = py_tpcall(tp_StopIteration, 0, NULL);
if(!ok) return false;
return py_raise(py_retval());
}
bool StopIteration() { return py_exception(tp_StopIteration, ""); }

View File

@ -243,17 +243,11 @@ static bool builtins_iter(int argc, py_Ref argv) {
}
static bool builtins_next(int argc, py_Ref argv) {
if(argc == 0 || argc > 2) return TypeError("next() takes 1 or 2 arguments");
PY_CHECK_ARGC(1);
int res = py_next(argv);
if(res == -1) return false;
if(res) return true;
if(argc == 1) {
// StopIteration stored in py_retval()
return py_raise(py_retval());
} else {
py_assign(py_retval(), py_arg(1));
return true;
}
return py_exception(tp_StopIteration, "");
}
static bool builtins_hash(int argc, py_Ref argv) {

View File

@ -96,17 +96,6 @@ static bool BaseException_args(int argc, py_Ref argv){
return true;
}
static bool StopIteration_value(int argc, py_Ref argv) {
PY_CHECK_ARGC(1);
py_Ref arg = py_getslot(argv, 0);
if(py_isnil(arg)) {
py_newnone(py_retval());
}else{
py_assign(py_retval(), arg);
}
return true;
}
py_Type pk_BaseException__register() {
py_Type type = pk_newtype("BaseException", tp_object, NULL, BaseException__dtor, false, false);
@ -123,12 +112,6 @@ py_Type pk_Exception__register() {
return type;
}
py_Type pk_StopIteration__register() {
py_Type type = pk_newtype("StopIteration", tp_Exception, NULL, NULL, false, false);
py_bindproperty(type, "value", StopIteration_value, NULL);
return type;
}
//////////////////////////////////////////////////
bool py_checkexc(bool ignore_handled) {
VM* vm = pk_current_vm;
@ -151,10 +134,13 @@ bool py_matchexc(py_Type type) {
void py_clearexc(py_StackRef p0) {
VM* vm = pk_current_vm;
vm->last_retval = *py_NIL();
vm->curr_exception = *py_NIL();
vm->is_curr_exc_handled = false;
/* Don't clear this, because StopIteration() may corrupt the class defination */
// vm->__curr_class = NULL;
vm->__curr_function = NULL;
if(p0) vm->stack.sp = p0;
}

View File

@ -77,7 +77,6 @@ int py_next(py_Ref val) {
}
if(py_call(tmp, 1, val)) return 1;
if(vm->curr_exception.type == tp_StopIteration) {
vm->last_retval = vm->curr_exception;
py_clearexc(NULL);
return 0;
}

View File

@ -110,37 +110,6 @@ static bool tuple__ne__(int argc, py_Ref argv) {
return true;
}
static bool tuple__lt__(int argc, py_Ref argv) {
PY_CHECK_ARGC(2);
if(!py_istype(py_arg(1), tp_tuple)) {
py_newnotimplemented(py_retval());
return true;
}
py_TValue *p0, *p1;
int lhs_length = py_tuple_len(py_arg(0));
int rhs_length = py_tuple_len(py_arg(1));
p0 = py_tuple_data(py_arg(0));
p1 = py_tuple_data(py_arg(1));
int length = lhs_length < rhs_length ? lhs_length : rhs_length;
for(int i = 0; i < length; i++) {
int res_lt = py_less(p0 + i, p1 + i);
if(res_lt == -1) return false;
if(res_lt) {
py_newbool(py_retval(), true);
return true;
} else {
int res_eq = py_equal(p0 + i, p1 + i);
if(res_eq == -1) return false;
if(!res_eq) {
py_newbool(py_retval(), false);
return true;
}
}
}
py_newbool(py_retval(), lhs_length < rhs_length);
return true;
}
static bool tuple__iter__(int argc, py_Ref argv) {
PY_CHECK_ARGC(1);
return pk_arrayiter(argv);
@ -175,7 +144,6 @@ py_Type pk_tuple__register() {
py_bindmagic(type, __getitem__, tuple__getitem__);
py_bindmagic(type, __eq__, tuple__eq__);
py_bindmagic(type, __ne__, tuple__ne__);
py_bindmagic(type, __lt__, tuple__lt__);
py_bindmagic(type, __iter__, tuple__iter__);
py_bindmagic(type, __contains__, tuple__contains__);
py_bindmagic(type, __hash__, tuple__hash__);

View File

@ -35,20 +35,3 @@ assert repr(tuple()) == '()'
# test in and not in
assert 1 in (1, 2, 3)
assert 4 not in (1, 2, 3)
# test < and == and !=
assert (1,2) == (1,2)
assert (2,1) == (2,1)
assert (1,) == (1,)
assert (1,2) != (1,3)
assert (1,2) != (1,2,3)
assert (1,2) != (1,)
assert (1,2) < (1,3)
assert (1,2) < (2,1)
assert (1,2) < (2,2)
assert (1,2) < (1,2,3)
assert (1,2) < (1,2,1)
assert (1,2) < (1,2,2)

View File

@ -1,12 +1,10 @@
def g():
yield 1
yield 2
yield
a = g()
assert next(a) == 1
assert next(a, None) == 2
assert next(a) == None
assert next(a) == 2
try:
next(a)
@ -14,9 +12,6 @@ try:
except StopIteration:
pass
assert next(a, 3) == 3
assert next(a, 4) == 4
def f(n):
for i in range(n):
yield i
@ -104,27 +99,16 @@ def f():
assert list(f()) == [1, 2]
src = '''
def g():
yield 1
yield 2
return 3
yield 4
assert StopIteration().value == None
assert StopIteration(3).value == 3
'''
try:
iter = g()
assert next(iter) == 1
assert next(iter) == 2
next(iter) # raises StopIteration
print('UNREACHABLE!!')
exec(src)
exit(1)
except StopIteration as e:
assert e.value == 3
def f():
a = yield from g()
yield a
assert list(f()) == [1, 2, 3]
except SyntaxError:
pass

View File

@ -574,12 +574,11 @@ assert max(1, 2, 3) == 3
assert max([1, 2]) == 2
assert max([1, 2, 3], key=lambda x: -x) == 1
assert min([
(3, 1),
(1, 2),
(1, 3),
(1, 4),
]) == (1, 2)
# assert min([
# (1, 2),
# (1, 3),
# (1, 4),
# ]) == (1, 2)
assert min(1, 2) == 1
assert max(1, 2) == 2