This commit is contained in:
blueloveTH 2023-06-09 22:16:53 +08:00
parent b87cd6ed52
commit 636dbdd64a
2 changed files with 26 additions and 11 deletions

View File

@ -322,7 +322,6 @@ class Compiler {
if (curr().type == TK("]")) break; if (curr().type == TK("]")) break;
EXPR(); EXPR();
items.push_back(ctx()->s_expr.popx()); items.push_back(ctx()->s_expr.popx());
if(items.back()->is_starred()) SyntaxError();
match_newlines_repl(); match_newlines_repl();
if(items.size()==1 && match(TK("for"))){ if(items.size()==1 && match(TK("for"))){
_consume_comp<ListCompExpr>(std::move(items[0])); _consume_comp<ListCompExpr>(std::move(items[0]));
@ -351,12 +350,9 @@ class Compiler {
auto dict_item = make_expr<DictItemExpr>(); auto dict_item = make_expr<DictItemExpr>();
dict_item->key = ctx()->s_expr.popx(); dict_item->key = ctx()->s_expr.popx();
dict_item->value = ctx()->s_expr.popx(); dict_item->value = ctx()->s_expr.popx();
if(dict_item->key->is_starred()) SyntaxError();
if(dict_item->value->is_starred()) SyntaxError();
items.push_back(std::move(dict_item)); items.push_back(std::move(dict_item));
}else{ }else{
items.push_back(ctx()->s_expr.popx()); items.push_back(ctx()->s_expr.popx());
if(items.back()->is_starred()) SyntaxError();
} }
match_newlines_repl(); match_newlines_repl();
if(items.size()==1 && match(TK("for"))){ if(items.size()==1 && match(TK("for"))){

View File

@ -370,11 +370,17 @@ struct DictItemExpr: Expr{
Expr_ value; Expr_ value;
std::string str() const override { return "DictItem()"; } std::string str() const override { return "DictItem()"; }
int star_level() const override { return value->star_level(); }
void emit(CodeEmitContext* ctx) override { void emit(CodeEmitContext* ctx) override {
if(is_starred()){
value->emit(ctx);
}else{
value->emit(ctx); value->emit(ctx);
key->emit(ctx); // reverse order key->emit(ctx); // reverse order
ctx->emit(OP_BUILD_TUPLE, 2, line); ctx->emit(OP_BUILD_TUPLE, 2, line);
} }
}
}; };
struct SequenceExpr: Expr{ struct SequenceExpr: Expr{
@ -391,7 +397,11 @@ struct SequenceExpr: Expr{
struct ListExpr: SequenceExpr{ struct ListExpr: SequenceExpr{
using SequenceExpr::SequenceExpr; using SequenceExpr::SequenceExpr;
std::string str() const override { return "List()"; } std::string str() const override { return "List()"; }
Opcode opcode() const override { return OP_BUILD_LIST; }
Opcode opcode() const override {
for(auto& e: items) if(e->is_starred()) return OP_BUILD_LIST_UNPACK;
return OP_BUILD_LIST;
}
bool is_json_object() const override { return true; } bool is_json_object() const override { return true; }
}; };
@ -399,7 +409,10 @@ struct ListExpr: SequenceExpr{
struct DictExpr: SequenceExpr{ struct DictExpr: SequenceExpr{
using SequenceExpr::SequenceExpr; using SequenceExpr::SequenceExpr;
std::string str() const override { return "Dict()"; } std::string str() const override { return "Dict()"; }
Opcode opcode() const override { return OP_BUILD_DICT; } Opcode opcode() const override {
for(auto& e: items) if(e->is_starred()) return OP_BUILD_DICT_UNPACK;
return OP_BUILD_DICT;
}
bool is_json_object() const override { return true; } bool is_json_object() const override { return true; }
}; };
@ -407,13 +420,19 @@ struct DictExpr: SequenceExpr{
struct SetExpr: SequenceExpr{ struct SetExpr: SequenceExpr{
using SequenceExpr::SequenceExpr; using SequenceExpr::SequenceExpr;
std::string str() const override { return "Set()"; } std::string str() const override { return "Set()"; }
Opcode opcode() const override { return OP_BUILD_SET; } Opcode opcode() const override {
for(auto& e: items) if(e->is_starred()) return OP_BUILD_SET_UNPACK;
return OP_BUILD_SET;
}
}; };
struct TupleExpr: SequenceExpr{ struct TupleExpr: SequenceExpr{
using SequenceExpr::SequenceExpr; using SequenceExpr::SequenceExpr;
std::string str() const override { return "Tuple()"; } std::string str() const override { return "Tuple()"; }
Opcode opcode() const override { return OP_BUILD_TUPLE; } Opcode opcode() const override {
for(auto& e: items) if(e->is_starred()) return OP_BUILD_TUPLE_UNPACK;
return OP_BUILD_TUPLE;
}
bool emit_store(CodeEmitContext* ctx) override { bool emit_store(CodeEmitContext* ctx) override {
// TOS is an iterable // TOS is an iterable