script optimization support for using strings to index table[pattern] values

This commit is contained in:
Vern Paxson 2023-11-01 11:44:58 +01:00 committed by Arne Welzel
parent fd1094a184
commit 61fcca8482
5 changed files with 81 additions and 5 deletions

View file

@ -388,7 +388,26 @@ string CPPCompile::GenIndexExpr(const Expr* e, GenType gt) {
string func;
if ( aggr_t->Tag() == TYPE_TABLE ) {
func = inside_when ? "when_index_table__CPP" : "index_table__CPP";
auto ind_expr = e->GetOp2()->AsListExpr()->Exprs()[0];
auto is_pat_str_ind = false;
auto& indices = aggr_t->AsTableType()->GetIndices()->GetTypes();
if ( indices.size() == 1 && indices[0]->Tag() == TYPE_PATTERN && ind_expr->GetType()->Tag() == TYPE_STRING )
is_pat_str_ind = true;
if ( inside_when ) {
if ( is_pat_str_ind )
func = "when_index_patstr__CPP";
else
func = "when_index_table__CPP";
}
else {
if ( is_pat_str_ind )
func = "index_patstr_table__CPP";
else
func = "index_table__CPP";
}
gen = func + "(" + GenExpr(aggr, GEN_NATIVE) + ", {" + GenExpr(e->GetOp2(), GEN_VAL_PTR) + "})";
}

View file

@ -44,6 +44,10 @@ ValPtr index_table__CPP(const TableValPtr& t, vector<ValPtr> indices) {
return v;
}
ValPtr index_patstr_table__CPP(const TableValPtr& t, vector<ValPtr> indices) {
return t->LookupPattern(indices[0]->AsStringVal());
}
ValPtr index_vec__CPP(const VectorValPtr& vec, int index) {
if ( index < 0 )
index += vec->Size();
@ -66,6 +70,13 @@ ValPtr when_index_table__CPP(const TableValPtr& t, vector<ValPtr> indices) {
return v;
}
ValPtr when_index_patstr__CPP(const TableValPtr& t, vector<ValPtr> indices) {
auto v = index_patstr_table__CPP(t, std::move(indices));
if ( v && IndexExprWhen::evaluating > 0 )
IndexExprWhen::results.emplace_back(v);
return v;
}
ValPtr when_index_vec__CPP(const VectorValPtr& vec, int index) {
auto v = index_vec__CPP(vec, index);
if ( v && IndexExprWhen::evaluating > 0 )

View file

@ -32,13 +32,16 @@ extern ListValPtr index_val__CPP(std::vector<ValPtr> indices);
// Returns the value corresponding to indexing the given table/vector/string
// with the given set of indices. These are functions rather than something
// generated directly so that they can package up the error handling for
// the case where there's no such index.
// the case where there's no such index. "patstr" refers to indexing a
// table[pattern] of X with a string value.
extern ValPtr index_table__CPP(const TableValPtr& t, std::vector<ValPtr> indices);
extern ValPtr index_patstr_table__CPP(const TableValPtr& t, std::vector<ValPtr> indices);
extern ValPtr index_vec__CPP(const VectorValPtr& vec, int index);
extern ValPtr index_string__CPP(const StringValPtr& svp, std::vector<ValPtr> indices);
// The same, but for indexing happening inside a "when" clause.
extern ValPtr when_index_table__CPP(const TableValPtr& t, std::vector<ValPtr> indices);
extern ValPtr when_index_patstr__CPP(const TableValPtr& t, std::vector<ValPtr> indices);
extern ValPtr when_index_vec__CPP(const VectorValPtr& vec, int index);
// For vector slices, we use the existing index_slice(), but we need a