add a<b<c support

This commit is contained in:
blueloveTH 2023-06-04 21:28:23 +08:00
parent 919d7a465c
commit caadfefc01
6 changed files with 28 additions and 10 deletions

View File

@ -42,4 +42,4 @@ The easiest way to test a feature is to [try it on your browser](https://pocketp
6. `__ne__` is not required. Define `__eq__` is enough. 6. `__ne__` is not required. Define `__eq__` is enough.
7. Raw string cannot have boundary quotes in it, even escaped. See [#55](https://github.com/blueloveTH/pocketpy/issues/55). 7. Raw string cannot have boundary quotes in it, even escaped. See [#55](https://github.com/blueloveTH/pocketpy/issues/55).
8. In a starred unpacked assignment, e.g. `a, b, *c = x`, the starred variable can only be presented in the last position. `a, *b, c = x` is not supported. 8. In a starred unpacked assignment, e.g. `a, b, *c = x`, the starred variable can only be presented in the last position. `a, *b, c = x` is not supported.
9. `a < b < c` does not work as you expected. Use `a < b and b < c` instead. 9. `a < b < c` does not work as you expected. Use `a < b and b < c` instead. **(available in main branch now)**

View File

@ -420,6 +420,7 @@ __NEXT_STEP:;
PUSH(vm->False); // [False] PUSH(vm->False); // [False]
frame->jump_abs(byte.arg); frame->jump_abs(byte.arg);
} else POP(); // [b] } else POP(); // [b]
DISPATCH();
TARGET(LOOP_CONTINUE) TARGET(LOOP_CONTINUE)
frame->jump_abs(co_blocks[byte.block].start); frame->jump_abs(co_blocks[byte.block].start);
DISPATCH(); DISPATCH();

View File

@ -178,9 +178,9 @@ inline PyObject* const PY_OP_CALL = (PyObject*)0b100011;
inline PyObject* const PY_OP_YIELD = (PyObject*)0b110011; inline PyObject* const PY_OP_YIELD = (PyObject*)0b110011;
#ifdef _WIN32 #ifdef _WIN32
char kPlatformSep = '\\'; inline const char kPlatformSep = '\\';
#else #else
char kPlatformSep = '/'; inline const char kPlatformSep = '/';
#endif #endif
} // namespace pkpy } // namespace pkpy

View File

@ -716,13 +716,11 @@ struct BinaryExpr: Expr{
} }
void emit(CodeEmitContext* ctx) override { void emit(CodeEmitContext* ctx) override {
std::vector<int> jmps;
if(is_compare() && lhs->is_compare()){ if(is_compare() && lhs->is_compare()){
// (a < b) < c // (a < b) < c
std::vector<int> jmps;
static_cast<BinaryExpr*>(lhs.get())->_emit_compare(ctx, jmps); static_cast<BinaryExpr*>(lhs.get())->_emit_compare(ctx, jmps);
// [b, RES] // [b, RES]
for(int i: jmps) ctx->patch_jump(i);
}else{ }else{
// (1 + 2) < c // (1 + 2) < c
lhs->emit(ctx); lhs->emit(ctx);
@ -759,6 +757,8 @@ struct BinaryExpr: Expr{
case TK("@"): ctx->emit(OP_BINARY_MATMUL, BC_NOARG, line); break; case TK("@"): ctx->emit(OP_BINARY_MATMUL, BC_NOARG, line); break;
default: FATAL_ERROR(); default: FATAL_ERROR();
} }
for(int i: jmps) ctx->patch_jump(i);
} }
}; };

View File

@ -1005,7 +1005,7 @@ inline Str VM::disassemble(CodeObject_ co){
std::vector<int> jumpTargets; std::vector<int> jumpTargets;
for(auto byte : co->codes){ for(auto byte : co->codes){
if(byte.op == OP_JUMP_ABSOLUTE || byte.op == OP_POP_JUMP_IF_FALSE){ if(byte.op == OP_JUMP_ABSOLUTE || byte.op == OP_POP_JUMP_IF_FALSE || byte.op == OP_POP_JUMP_IF_FALSE || byte.op == OP_SHORTCUT_IF_FALSE_OR_POP){
jumpTargets.push_back(byte.arg); jumpTargets.push_back(byte.arg);
} }
} }
@ -1027,11 +1027,12 @@ inline Str VM::disassemble(CodeObject_ co){
pointer = " "; pointer = " ";
} }
ss << pad(line, 8) << pointer << pad(std::to_string(i), 3); ss << pad(line, 8) << pointer << pad(std::to_string(i), 3);
ss << " " << pad(OP_NAMES[byte.op], 20) << " "; ss << " " << pad(OP_NAMES[byte.op], 25) << " ";
// ss << pad(byte.arg == -1 ? "" : std::to_string(byte.arg), 5); // ss << pad(byte.arg == -1 ? "" : std::to_string(byte.arg), 5);
std::string argStr = _opcode_argstr(this, byte, co.get()); std::string argStr = _opcode_argstr(this, byte, co.get());
ss << pad(argStr, 40); // may overflow ss << argStr;
ss << co->blocks[byte.block].type; // ss << pad(argStr, 40); // may overflow
// ss << co->blocks[byte.block].type;
if(i != co->codes.size() - 1) ss << '\n'; if(i != co->codes.size() - 1) ss << '\n';
} }

16
tests/31_cmp.py Normal file
View File

@ -0,0 +1,16 @@
assert 1<2
assert 1+1==2
assert 2+1>=2
assert 1<2<3
assert 1<2<3<4
assert 1<2<3<4<5
assert 1<1+1<3
assert 1<1+1<3<4
assert 1<1+1<3<2+2<5
a = [1,2,3]
assert a[0] < a[1] < a[2]
assert a[0]+1 == a[1] < a[2]
assert a[0]+1 == a[1] < a[2]+1 < 5