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 variable shadow scoping #1748

Merged
merged 1 commit into from
Nov 25, 2015
Merged
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
20 changes: 15 additions & 5 deletions src/environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,20 @@
namespace Sass {

template <typename T>
Environment<T>::Environment() : local_frame_(std::map<std::string, T>()), parent_(0) { }
Environment<T>::Environment(bool is_shadow)
: local_frame_(std::map<std::string, T>()),
parent_(0), is_shadow_(false)
{ }
template <typename T>
Environment<T>::Environment(Environment<T>* env) : local_frame_(std::map<std::string, T>()), parent_(env) { }
Environment<T>::Environment(Environment<T>* env, bool is_shadow)
: local_frame_(std::map<std::string, T>()),
parent_(env), is_shadow_(is_shadow)
{ }
template <typename T>
Environment<T>::Environment(Environment<T>& env) : local_frame_(std::map<std::string, T>()), parent_(&env) { }
Environment<T>::Environment(Environment<T>& env, bool is_shadow)
: local_frame_(std::map<std::string, T>()),
parent_(&env), is_shadow_(is_shadow)
{ }

// link parent to create a stack
template <typename T>
Expand Down Expand Up @@ -118,12 +127,13 @@ namespace Sass {
template <typename T>
void Environment<T>::set_lexical(const std::string& key, T val)
{
auto cur = this;
while (cur->is_lexical()) {
auto cur = this; bool shadow = false;
while (cur->is_lexical() || shadow) {
if (cur->has_local(key)) {
cur->set_local(key, val);
return;
}
shadow = cur->is_shadow();
cur = cur->parent_;
}
set_local(key, val);
Expand Down
8 changes: 5 additions & 3 deletions src/environment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ namespace Sass {
// TODO: test with map
std::map<std::string, T> local_frame_;
ADD_PROPERTY(Environment*, parent)
ADD_PROPERTY(bool, is_shadow)

public:
Memory_Manager mem;
Environment();
Environment(Environment* env);
Environment(Environment& env);
Environment(bool is_shadow = false);
Environment(Environment* env, bool is_shadow = false);
Environment(Environment& env, bool is_shadow = false);

// link parent to create a stack
void link(Environment& env);
Expand Down Expand Up @@ -87,6 +88,7 @@ namespace Sass {
#endif

};

}

#endif
49 changes: 21 additions & 28 deletions src/eval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,10 @@ namespace Sass {
double start = sass_start->value();
double end = sass_end->value();
// only create iterator once in this environment
Env* env = exp.environment();
Number* it = SASS_MEMORY_NEW(env->mem, Number, low->pstate(), start, sass_end->unit());
AST_Node* old_var = env->has_local(variable) ? env->get_local(variable) : 0;
env->set_local(variable, it);
Env env(environment(), true);
exp.env_stack.push_back(&env);
Number* it = SASS_MEMORY_NEW(env.mem, Number, low->pstate(), start, sass_end->unit());
env.set_local(variable, it);
Block* body = f->block();
Expression* val = 0;
if (start < end) {
Expand All @@ -190,7 +190,7 @@ namespace Sass {
i < end;
++i) {
it->value(i);
env->set_local(variable, it);
env.set_local(variable, it);
val = body->perform(this);
if (val) break;
}
Expand All @@ -200,14 +200,12 @@ namespace Sass {
i > end;
--i) {
it->value(i);
env->set_local(variable, it);
env.set_local(variable, it);
val = body->perform(this);
if (val) break;
}
}
// restore original environment
if (!old_var) env->del_local(variable);
else env->set_local(variable, old_var);
exp.env_stack.pop_back();
return val;
}

Expand All @@ -217,7 +215,8 @@ namespace Sass {
{
std::vector<std::string> variables(e->variables());
Expression* expr = e->list()->perform(this);
Env* env = exp.environment();
Env env(environment(), true);
exp.env_stack.push_back(&env);
List* list = 0;
Map* map = 0;
if (expr->concrete_type() == Expression::MAP) {
Expand All @@ -230,12 +229,7 @@ namespace Sass {
else {
list = static_cast<List*>(expr);
}
// remember variables and then reset them
std::vector<AST_Node*> old_vars(variables.size());
for (size_t i = 0, L = variables.size(); i < L; ++i) {
old_vars[i] = env->has_local(variables[i]) ? env->get_local(variables[i]) : 0;
env->set_local(variables[i], 0);
}

Block* body = e->block();
Expression* val = 0;

Expand All @@ -247,10 +241,10 @@ namespace Sass {
List* variable = SASS_MEMORY_NEW(ctx.mem, List, map->pstate(), 2, SASS_SPACE);
*variable << key;
*variable << value;
env->set_local(variables[0], variable);
env.set_local(variables[0], variable);
} else {
env->set_local(variables[0], key);
env->set_local(variables[1], value);
env.set_local(variables[0], key);
env.set_local(variables[1], value);
}

val = body->perform(this);
Expand All @@ -266,44 +260,43 @@ namespace Sass {
if (List* scalars = dynamic_cast<List*>(e)) {
if (variables.size() == 1) {
Expression* var = scalars;
env->set_local(variables[0], var);
env.set_local(variables[0], var);
} else {
for (size_t j = 0, K = variables.size(); j < K; ++j) {
Expression* res = j >= scalars->length()
? SASS_MEMORY_NEW(ctx.mem, Null, expr->pstate())
: (*scalars)[j];
env->set_local(variables[j], res);
env.set_local(variables[j], res);
}
}
} else {
if (variables.size() > 0) {
env->set_local(variables[0], e);
env.set_local(variables[0], e);
for (size_t j = 1, K = variables.size(); j < K; ++j) {
Expression* res = SASS_MEMORY_NEW(ctx.mem, Null, expr->pstate());
env->set_local(variables[j], res);
env.set_local(variables[j], res);
}
}
}
val = body->perform(this);
if (val) break;
}
}
// restore original environment
for (size_t j = 0, K = variables.size(); j < K; ++j) {
if(!old_vars[j]) env->del_local(variables[j]);
else env->set_local(variables[j], old_vars[j]);
}
exp.env_stack.pop_back();
return val;
}

Expression* Eval::operator()(While* w)
{
Expression* pred = w->predicate();
Block* body = w->block();
Env env(environment(), true);
exp.env_stack.push_back(&env);
while (*pred->perform(this)) {
Expression* val = body->perform(this);
if (val) return val;
}
exp.env_stack.pop_back();
return 0;
}

Expand Down
59 changes: 32 additions & 27 deletions src/expand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,21 @@ namespace Sass {
if (sel == 0) throw std::runtime_error("Expanded null selector");

selector_stack.push_back(sel);
Env* env = 0;
if (block_stack.back()->is_root()) {
env = new Env(environment());
env_stack.push_back(env);
}
Block* blk = r->block()->perform(this)->block();
Ruleset* rr = SASS_MEMORY_NEW(ctx.mem, Ruleset,
r->pstate(),
sel,
blk);
selector_stack.pop_back();
if (block_stack.back()->is_root()) {
env_stack.pop_back();
delete env;
}
rr->tabs(r->tabs());

return rr;
Expand Down Expand Up @@ -361,13 +370,16 @@ namespace Sass {

Statement* Expand::operator()(If* i)
{
Env env(environment(), true);
env_stack.push_back(&env);
if (*i->predicate()->perform(&eval)) {
append_block(i->block());
}
else {
Block* alt = i->alternative();
if (alt) append_block(alt);
}
env_stack.pop_back();
return 0;
}

Expand Down Expand Up @@ -396,18 +408,18 @@ namespace Sass {
double start = sass_start->value();
double end = sass_end->value();
// only create iterator once in this environment
Env* env = environment();
Number* it = SASS_MEMORY_NEW(env->mem, Number, low->pstate(), start, sass_end->unit());
AST_Node* old_var = env->has_local(variable) ? env->get_local(variable) : 0;
env->set_local(variable, it);
Env env(environment(), true);
env_stack.push_back(&env);
Number* it = SASS_MEMORY_NEW(env.mem, Number, low->pstate(), start, sass_end->unit());
env.set_local(variable, it);
Block* body = f->block();
if (start < end) {
if (f->is_inclusive()) ++end;
for (double i = start;
i < end;
++i) {
it->value(i);
env->set_local(variable, it);
env.set_local(variable, it);
append_block(body);
}
} else {
Expand All @@ -416,13 +428,11 @@ namespace Sass {
i > end;
--i) {
it->value(i);
env->set_local(variable, it);
env.set_local(variable, it);
append_block(body);
}
}
// restore original environment
if (!old_var) env->del_local(variable);
else env->set_local(variable, old_var);
env_stack.pop_back();
return 0;
}

Expand All @@ -445,12 +455,8 @@ namespace Sass {
list = static_cast<List*>(expr);
}
// remember variables and then reset them
Env* env = environment();
std::vector<AST_Node*> old_vars(variables.size());
for (size_t i = 0, L = variables.size(); i < L; ++i) {
old_vars[i] = env->has_local(variables[i]) ? env->get_local(variables[i]) : 0;
env->set_local(variables[i], 0);
}
Env env(environment(), true);
env_stack.push_back(&env);
Block* body = e->block();

if (map) {
Expand All @@ -462,10 +468,10 @@ namespace Sass {
List* variable = SASS_MEMORY_NEW(ctx.mem, List, map->pstate(), 2, SASS_SPACE);
*variable << k;
*variable << v;
env->set_local(variables[0], variable);
env.set_local(variables[0], variable);
} else {
env->set_local(variables[0], k);
env->set_local(variables[1], v);
env.set_local(variables[0], k);
env.set_local(variables[1], v);
}
append_block(body);
}
Expand All @@ -481,42 +487,41 @@ namespace Sass {
if (variables.size() == 1) {
Expression* var = scalars;
if (arglist) var = (*scalars)[0];
env->set_local(variables[0], var);
env.set_local(variables[0], var);
} else {
for (size_t j = 0, K = variables.size(); j < K; ++j) {
Expression* res = j >= scalars->length()
? SASS_MEMORY_NEW(ctx.mem, Null, expr->pstate())
: (*scalars)[j]->perform(&eval);
env->set_local(variables[j], res);
env.set_local(variables[j], res);
}
}
} else {
if (variables.size() > 0) {
env->set_local(variables[0], e);
env.set_local(variables[0], e);
for (size_t j = 1, K = variables.size(); j < K; ++j) {
Expression* res = SASS_MEMORY_NEW(ctx.mem, Null, expr->pstate());
env->set_local(variables[j], res);
env.set_local(variables[j], res);
}
}
}
append_block(body);
}
}
// restore original environment
for (size_t j = 0, K = variables.size(); j < K; ++j) {
if(!old_vars[j]) env->del_local(variables[j]);
else env->set_local(variables[j], old_vars[j]);
}
env_stack.pop_back();
return 0;
}

Statement* Expand::operator()(While* w)
{
Expression* pred = w->predicate();
Block* body = w->block();
Env env(environment(), true);
env_stack.push_back(&env);
while (*pred->perform(&eval)) {
append_block(body);
}
env_stack.pop_back();
return 0;
}

Expand Down