Skip to content

Commit

Permalink
Evaluate @each with maps in functions
Browse files Browse the repository at this point in the history
  • Loading branch information
xzyfer committed Oct 28, 2014
1 parent 82ca15d commit e8c549a
Showing 1 changed file with 36 additions and 6 deletions.
42 changes: 36 additions & 6 deletions eval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,26 +105,56 @@ namespace Sass {

Expression* Eval::operator()(Each* e)
{
string variable(e->variables()[0]);
vector<string> variables(e->variables());
Expression* expr = e->list()->perform(this);
List* list = 0;
Map* map = 0;
if (expr->concrete_type() != Expression::LIST) {
map = static_cast<Map*>(expr);
}
else if (expr->concrete_type() != Expression::LIST) {
list = new (ctx.mem) List(expr->path(), expr->position(), 1, List::COMMA);
*list << expr;
}
else {
list = static_cast<List*>(expr);
}
Env new_env;
new_env[variable] = 0;
for (size_t i = 0, L = variables.size(); i < L; ++i) new_env[variables[i]] = 0;
new_env.link(env);
env = &new_env;
Block* body = e->block();
Expression* val = 0;
for (size_t i = 0, L = list->length(); i < L; ++i) {
(*env)[variable] = (*list)[i];
val = body->perform(this);
if (val) break;

if (map) {
for (auto key : map->keys()) {
(*env)[variables[0]] = key;
(*env)[variables[1]] = map->at(key);
val = body->perform(this);
if (val) break;
}
}
else {
for (size_t i = 0, L = list->length(); i < L; ++i) {
List* variable = 0;
if ((*list)[i]->concrete_type() != Expression::LIST || variables.size() == 1) {
variable = new (ctx.mem) List((*list)[i]->path(), (*list)[i]->position(), 1, List::COMMA);
*variable << (*list)[i];
}
else {
variable = static_cast<List*>((*list)[i]);
}
for (size_t j = 0, K = variables.size(); j < K; ++j) {
if (j < variable->length()) {
(*env)[variables[j]] = (*variable)[j];
}
else {
(*env)[variables[j]] = new (ctx.mem) Null(expr->path(), expr->position());
}
val = body->perform(this);
if (val) break;
}
}
}
env = new_env.parent();
return val;
Expand Down

0 comments on commit e8c549a

Please sign in to comment.