fix a bug

This commit is contained in:
blueloveTH 2023-06-23 22:26:27 +08:00
parent ad2dd26cb2
commit 65d20a384a
3 changed files with 20 additions and 1 deletions

View File

@ -301,6 +301,9 @@ class Compiler {
EXPR_TUPLE(); // () is just for change precedence
match_newlines_repl();
consume(TK(")"));
if(ctx()->s_expr.top()->is_tuple()) return;
Expr_ g = make_expr<GroupedExpr>(ctx()->s_expr.popx());
ctx()->s_expr.push(std::move(g));
}
template<typename T>

View File

@ -24,6 +24,7 @@ struct Expr{
virtual bool is_attrib() const { return false; }
virtual bool is_compare() const { return false; }
virtual int star_level() const { return 0; }
virtual bool is_tuple() const { return false; }
bool is_starred() const { return star_level() > 0; }
// for OP_DELETE_XXX
@ -445,6 +446,7 @@ struct SetExpr: SequenceExpr{
struct TupleExpr: SequenceExpr{
using SequenceExpr::SequenceExpr;
std::string str() const override { return "Tuple()"; }
bool is_tuple() const override { return true; }
Opcode opcode() const override {
for(auto& e: items) if(e->is_starred()) return OP_BUILD_TUPLE_UNPACK;
return OP_BUILD_TUPLE;
@ -734,6 +736,17 @@ struct CallExpr: Expr{
}
};
struct GroupedExpr: Expr{
Expr_ a;
std::string str() const override { return "Grouped()"; }
GroupedExpr(Expr_&& a): a(std::move(a)) {}
void emit(CodeEmitContext* ctx) override{
a->emit(ctx);
}
};
struct BinaryExpr: Expr{
TokenIndex op;
Expr_ lhs;

View File

@ -13,4 +13,7 @@ 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
assert a[0]+1 == a[1] < a[2]+1 < 5
assert (4>3<2) == False
# assert ((4>3)<2) == True