replace std::vector<Expr_> with small_vector

This commit is contained in:
blueloveTH 2024-02-18 21:31:27 +08:00
parent ac284eee66
commit 19a2b8950b
3 changed files with 16 additions and 15 deletions

View File

@ -124,10 +124,10 @@ class Compiler {
bool try_compile_assignment(); bool try_compile_assignment();
void compile_stmt(); void compile_stmt();
void consume_type_hints(); void consume_type_hints();
void _add_decorators(const std::vector<Expr_>& decorators); void _add_decorators(const Expr_vector& decorators);
void compile_class(const std::vector<Expr_>& decorators={}); void compile_class(const Expr_vector& decorators={});
void _compile_f_args(FuncDecl_ decl, bool enable_type_hints); void _compile_f_args(FuncDecl_ decl, bool enable_type_hints);
void compile_function(const std::vector<Expr_>& decorators={}); void compile_function(const Expr_vector& decorators={});
PyObject* to_object(const TokenValue& value); PyObject* to_object(const TokenValue& value);
PyObject* read_literal(); PyObject* read_literal();

View File

@ -51,6 +51,7 @@ public:
}; };
typedef unique_ptr_64<Expr> Expr_; typedef unique_ptr_64<Expr> Expr_;
typedef small_vector<Expr_, 6> Expr_vector;
struct Expr{ struct Expr{
int line = 0; int line = 0;
@ -80,7 +81,7 @@ struct CodeEmitContext{
VM* vm; VM* vm;
FuncDecl_ func; // optional FuncDecl_ func; // optional
CodeObject_ co; // 1 CodeEmitContext <=> 1 CodeObject_ CodeObject_ co; // 1 CodeEmitContext <=> 1 CodeObject_
// some bugs on MSVC (error C2280) when using std::vector<Expr_> // some bugs on MSVC (error C2280) when using Expr_vector
// so we use stack_no_copy instead // so we use stack_no_copy instead
stack_no_copy<Expr_> s_expr; stack_no_copy<Expr_> s_expr;
int level; int level;
@ -209,8 +210,8 @@ struct DictItemExpr: Expr{
}; };
struct SequenceExpr: Expr{ struct SequenceExpr: Expr{
std::vector<Expr_> items; Expr_vector items;
SequenceExpr(std::vector<Expr_>&& items): items(std::move(items)) {} SequenceExpr(Expr_vector&& items): items(std::move(items)) {}
virtual Opcode opcode() const = 0; virtual Opcode opcode() const = 0;
void emit_(CodeEmitContext* ctx) override { void emit_(CodeEmitContext* ctx) override {
@ -326,7 +327,7 @@ struct AttribExpr: Expr{
struct CallExpr: Expr{ struct CallExpr: Expr{
Expr_ callable; Expr_ callable;
std::vector<Expr_> args; Expr_vector args;
// **a will be interpreted as a special keyword argument: {"**": a} // **a will be interpreted as a special keyword argument: {"**": a}
std::vector<std::pair<Str, Expr_>> kwargs; std::vector<std::pair<Str, Expr_>> kwargs;
void emit_(CodeEmitContext* ctx) override; void emit_(CodeEmitContext* ctx) override;

View File

@ -180,7 +180,7 @@ namespace pkpy{
parse_expression(PREC_LOWEST+1, allow_slice); parse_expression(PREC_LOWEST+1, allow_slice);
if(!match(TK(","))) return; if(!match(TK(","))) return;
// tuple expression // tuple expression
std::vector<Expr_> items; Expr_vector items;
items.push_back(ctx()->s_expr.popx()); items.push_back(ctx()->s_expr.popx());
do { do {
if(curr().brackets_level) match_newlines_repl(); if(curr().brackets_level) match_newlines_repl();
@ -194,7 +194,7 @@ namespace pkpy{
// special case for `for loop` and `comp` // special case for `for loop` and `comp`
Expr_ Compiler::EXPR_VARS(){ Expr_ Compiler::EXPR_VARS(){
std::vector<Expr_> items; Expr_vector items;
do { do {
consume(TK("@id")); consume(TK("@id"));
items.push_back(make_expr<NameExpr>(prev().str(), name_scope())); items.push_back(make_expr<NameExpr>(prev().str(), name_scope()));
@ -313,7 +313,7 @@ namespace pkpy{
void Compiler::exprList() { void Compiler::exprList() {
int line = prev().line; int line = prev().line;
std::vector<Expr_> items; Expr_vector items;
do { do {
match_newlines_repl(); match_newlines_repl();
if (curr().type == TK("]")) break; if (curr().type == TK("]")) break;
@ -335,7 +335,7 @@ namespace pkpy{
void Compiler::exprMap() { void Compiler::exprMap() {
bool parsing_dict = false; // {...} may be dict or set bool parsing_dict = false; // {...} may be dict or set
std::vector<Expr_> items; Expr_vector items;
do { do {
match_newlines_repl(); match_newlines_repl();
if (curr().type == TK("}")) break; if (curr().type == TK("}")) break;
@ -717,7 +717,7 @@ __EAT_DOTS_END:
} }
void Compiler::compile_decorated(){ void Compiler::compile_decorated(){
std::vector<Expr_> decorators; Expr_vector decorators;
do{ do{
EXPR(); EXPR();
decorators.push_back(ctx()->s_expr.popx()); decorators.push_back(ctx()->s_expr.popx());
@ -982,7 +982,7 @@ __EAT_DOTS_END:
ctx()->s_expr.pop(); ctx()->s_expr.pop();
} }
void Compiler::_add_decorators(const std::vector<Expr_>& decorators){ void Compiler::_add_decorators(const Expr_vector& decorators){
// [obj] // [obj]
for(auto it=decorators.rbegin(); it!=decorators.rend(); ++it){ for(auto it=decorators.rbegin(); it!=decorators.rend(); ++it){
(*it)->emit_(ctx()); // [obj, f] (*it)->emit_(ctx()); // [obj, f]
@ -993,7 +993,7 @@ __EAT_DOTS_END:
} }
} }
void Compiler::compile_class(const std::vector<Expr_>& decorators){ void Compiler::compile_class(const Expr_vector& decorators){
consume(TK("@id")); consume(TK("@id"));
int namei = StrName(prev().sv()).index; int namei = StrName(prev().sv()).index;
Expr_ base = nullptr; Expr_ base = nullptr;
@ -1092,7 +1092,7 @@ __EAT_DOTS_END:
} while (match(TK(","))); } while (match(TK(",")));
} }
void Compiler::compile_function(const std::vector<Expr_>& decorators){ void Compiler::compile_function(const Expr_vector& decorators){
const char* _start = curr().start; const char* _start = curr().start;
consume(TK("@id")); consume(TK("@id"));
Str decl_name = prev().str(); Str decl_name = prev().str();