Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement indexed map access (incubating feature) #2068

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ namespace Sass {
std::vector<Expression*> list_;
protected:
size_t hash_;
std::deque<Expression*> ordered;
Expression* duplicate_key_;
void reset_hash() { hash_ = 0; }
void reset_duplicate_key() { duplicate_key_ = 0; }
Expand All @@ -321,15 +322,24 @@ namespace Sass {
Hashed& operator<<(std::pair<Expression*, Expression*> p)
{
reset_hash();

ordered.push_back(p.first);
ordered.push_back(p.second);
if (!has(p.first)) list_.push_back(p.first);
else if (!duplicate_key_) duplicate_key_ = p.first;

elements_[p.first] = p.second;

adjust_after_pushing(p);
return *this;
}
Expression* key(size_t i) {
return ordered.at(i * 2);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keys().at(i)

}
Expression* value(size_t i) {
return ordered.at(i * 2 + 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

elements().at(keys(i))

}
Expression* value(size_t i, Expression* v) {
ordered[i * 2 + 1] = v;
return v;
}
Hashed& operator+=(Hashed* h)
{
if (length() == 0) {
Expand Down
55 changes: 45 additions & 10 deletions src/functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1248,6 +1248,10 @@ namespace Sass {
if (index < 0 || index > len - 1) error("index out of bounds for `" + std::string(sig) + "`", pstate);
// return (*sl)[static_cast<int>(index)];
Listize listize(ctx.mem);
if (m) {
List* l = SASS_MEMORY_NEW(ctx.mem, List, pstate, 2);
*l << m->key(index) << m->value(index);
}
return (*sl)[static_cast<int>(index)]->perform(&listize);
}
List* l = dynamic_cast<List*>(env["$list"]);
Expand All @@ -1265,8 +1269,8 @@ namespace Sass {

if (m) {
l = SASS_MEMORY_NEW(ctx.mem, List, pstate, 1);
*l << m->keys()[static_cast<unsigned int>(index)];
*l << m->at(m->keys()[static_cast<unsigned int>(index)]);
*l << m->key(index);
*l << m->value(index);
return l;
}
else {
Expand All @@ -1280,27 +1284,58 @@ namespace Sass {
BUILT_IN(set_nth)
{
List* l = dynamic_cast<List*>(env["$list"]);
Map* m = dynamic_cast<Map*>(env["$list"]);
Number* n = ARG("$n", Number);
Expression* v = ARG("$value", Expression);
if (!l) {
if (!m && !l) {
l = SASS_MEMORY_NEW(ctx.mem, List, pstate, 1);
*l << ARG("$list", Expression);
}
if (l->empty()) error("argument `$list` of `" + std::string(sig) + "` must not be empty", pstate);
double index = std::floor(n->value() < 0 ? l->length() + n->value() : n->value() - 1);
if (index < 0 || index > l->length() - 1) error("index out of bounds for `" + std::string(sig) + "`", pstate);
List* result = SASS_MEMORY_NEW(ctx.mem, List, pstate, l->length(), l->separator());
for (size_t i = 0, L = l->length(); i < L; ++i) {
*result << ((i == index) ? v : (*l)[i]);
size_t len = m ? m->length() : l->length();
bool empty = m ? m->empty() : l->empty();
if (empty) error("argument `$list` of `" + std::string(sig) + "` must not be empty", pstate);
double index = std::floor(n->value() < 0 ? len + n->value() : n->value() - 1);
if (index < 0 || index > len - 1) error("index out of bounds for `" + std::string(sig) + "`", pstate);
if (m) {
m->value(index, v); // update the value
Listize listize(ctx.mem);
return m->perform(&listize);
}
else {
List* result = SASS_MEMORY_NEW(ctx.mem, List, pstate, l->length(), l->separator());
for (size_t i = 0, L = l->length(); i < L; ++i) {
*result << ((i == index) ? v : (*l)[i]);
}
return result;
}
return result;
}

Signature index_sig = "index($list, $value)";
BUILT_IN(index)
{
Map* m = dynamic_cast<Map*>(env["$list"]);
List* l = dynamic_cast<List*>(env["$list"]);
Expression* v = ARG("$value", Expression);

if (m) {
if (List* vl = dynamic_cast<List*>(v)) {
if (vl->size() == 2 && vl->separator() == SASS_SPACE) {
for (size_t i = 0, L = m->length(); i < L; i ++) {
if (Eval::eq(m->key(i), vl->at(0))) {
if (Eval::eq(m->value(i), vl->at(1))) {
// only return the index if the value matches too
return SASS_MEMORY_NEW(ctx.mem, Number, pstate, (double)(i+1));
} else {
// found key, which is unique
// so nothing more to match
break;
}
}
}
}
}
}

if (!l) {
l = SASS_MEMORY_NEW(ctx.mem, List, pstate, 1);
*l << ARG("$list", Expression);
Expand Down
11 changes: 11 additions & 0 deletions src/listize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ namespace Sass {
: mem(mem)
{ }

Expression* Listize::operator()(Map* m)
{
List* l = SASS_MEMORY_NEW(mem, List, m->pstate(), m->length(), SASS_COMMA);
for (size_t i = 0, L = m->length(); i < L; i++) {
List* il = SASS_MEMORY_NEW(mem, List, m->pstate(), 2, SASS_SPACE);
*il << m->key(i) << m->value(i);
*l << il;
}
return l;
}

Expression* Listize::operator()(Selector_List* sel)
{
List* l = SASS_MEMORY_NEW(mem, List, sel->pstate(), sel->length(), SASS_COMMA);
Expand Down
1 change: 1 addition & 0 deletions src/listize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace Sass {
Listize(Memory_Manager&);
~Listize() { }

Expression* operator()(Map*);
Expression* operator()(Selector_List*);
Expression* operator()(Complex_Selector*);
Expression* operator()(Compound_Selector*);
Expand Down