Skip to content

Commit

Permalink
Merge pull request #986 from mgreter/feature/global-assignments
Browse files Browse the repository at this point in the history
Implement `!global` assignments
  • Loading branch information
mgreter committed Mar 27, 2015
2 parents 50fbda1 + b221b40 commit 937ccf8
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 11 deletions.
12 changes: 6 additions & 6 deletions debugger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ inline void debug_ast(AST_Node* node, string ind = "", Env* env = 0)
case Complex_Selector::ADJACENT_TO: cerr << "{+}"; break;
case Complex_Selector::ANCESTOR_OF: cerr << "{ }"; break;
}
cerr << " <" << prettyprint(selector->pstate().token.ws_before()) << "> X <" << prettyprint(selector->pstate().token.ws_after()) << ">" << endl;
cerr << " <" << prettyprint(selector->pstate().token.ws_before()) << ">" << endl;
debug_ast(selector->head(), ind + " ", env);
debug_ast(selector->tail(), ind + "-", env);
} else if (dynamic_cast<Compound_Selector*>(node)) {
Expand All @@ -81,7 +81,7 @@ inline void debug_ast(AST_Node* node, string ind = "", Env* env = 0)
<< (selector->is_optional() ? " [is_optional]": " -")
<< (selector->has_line_break() ? " [line-break]": " -")
<< (selector->has_line_feed() ? " [line-feed]": " -") <<
" <" << prettyprint(selector->pstate().token.ws_before()) << "> X <" << prettyprint(selector->pstate().token.ws_after()) << ">" << endl;
" <" << prettyprint(selector->pstate().token.ws_before()) << ">" << endl;
for(auto i : selector->elements()) { debug_ast(i, ind + " ", env); }
} else if (dynamic_cast<Propset*>(node)) {
Propset* selector = dynamic_cast<Propset*>(node);
Expand All @@ -105,7 +105,7 @@ inline void debug_ast(AST_Node* node, string ind = "", Env* env = 0)
} else if (dynamic_cast<Type_Selector*>(node)) {
Type_Selector* selector = dynamic_cast<Type_Selector*>(node);
cerr << ind << "Type_Selector " << selector << " <<" << selector->name() << ">>" << (selector->has_line_break() ? " [line-break]": " -") <<
" <" << prettyprint(selector->pstate().token.ws_before()) << "> X <" << prettyprint(selector->pstate().token.ws_after()) << ">" << endl;
" <" << prettyprint(selector->pstate().token.ws_before()) << ">" << endl;
} else if (dynamic_cast<Selector_Placeholder*>(node)) {

Selector_Placeholder* selector = dynamic_cast<Selector_Placeholder*>(node);
Expand Down Expand Up @@ -187,7 +187,7 @@ inline void debug_ast(AST_Node* node, string ind = "", Env* env = 0)
} else if (dynamic_cast<Comment*>(node)) {
Comment* block = dynamic_cast<Comment*>(node);
cerr << ind << "Comment " << block << " " << block->tabs() <<
" <" << prettyprint(block->pstate().token.ws_before()) << "> X <" << prettyprint(block->pstate().token.ws_after()) << ">" << endl;
" <" << prettyprint(block->pstate().token.ws_before()) << ">" << endl;
debug_ast(block->text(), ind + "// ", env);
} else if (dynamic_cast<If*>(node)) {
If* block = dynamic_cast<If*>(node);
Expand Down Expand Up @@ -322,13 +322,13 @@ inline void debug_ast(AST_Node* node, string ind = "", Env* env = 0)
(expression->is_delayed() ? " {delayed}" : "") <<
(expression->sass_fix_1291() ? " {sass_fix_1291}" : "") <<
(expression->quote_mark() != 0 ? " {qm:" + string(1, expression->quote_mark()) + "}" : "") <<
" <" << prettyprint(expression->pstate().token.ws_before()) << "> X <" << prettyprint(expression->pstate().token.ws_after()) << ">" << endl;
" <" << prettyprint(expression->pstate().token.ws_before()) << ">" << endl;
} else if (dynamic_cast<String_Constant*>(node)) {
String_Constant* expression = dynamic_cast<String_Constant*>(node);
cerr << ind << "String_Constant : " << expression << " [" << prettyprint(expression->value()) << "]" <<
(expression->is_delayed() ? " {delayed}" : "") <<
(expression->sass_fix_1291() ? " {sass_fix_1291}" : "") <<
" <" << prettyprint(expression->pstate().token.ws_before()) << "> X <" << prettyprint(expression->pstate().token.ws_after()) << ">" << endl;
" <" << prettyprint(expression->pstate().token.ws_before()) << ">" << endl;
} else if (dynamic_cast<String_Schema*>(node)) {
String_Schema* expression = dynamic_cast<String_Schema*>(node);
cerr << ind << "String_Schema " << expression << " " << expression->concrete_type() <<
Expand Down
20 changes: 20 additions & 0 deletions environment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,29 @@ namespace Sass {
bool current_frame_has(const string key) const
{ return !!current_frame_.count(key); }

void current_frame_set(const string key, T val)
{ current_frame_[key] = val; }

void global_frame_set(const string key, T val)
{ global_frame()->current_frame_[key] = val; }

Environment* grandparent() const
{
if(parent_ && parent_->parent_) return parent_->parent_;
else return 0;
}

Environment* global_frame()
{
Environment* cur = this;
// looks like global variables
// are in the second last parent
while (cur->grandparent()) {
cur = cur->parent_;
}
return cur;
}

bool global_frame_has(const string key) const
{
if(parent_ && !grandparent()) {
Expand All @@ -66,6 +83,7 @@ namespace Sass {
else return current_frame_[key];
}

#ifdef DEBUG
void print()
{
for (typename map<string, T>::iterator i = current_frame_.begin(); i != current_frame_.end(); ++i) {
Expand All @@ -76,6 +94,8 @@ namespace Sass {
parent_->print();
}
}
#endif

};
}

Expand Down
7 changes: 5 additions & 2 deletions eval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,15 @@ namespace Sass {
Expression* Eval::operator()(Assignment* a)
{
string var(a->variable());
if (env->has(var)) {
if (a->is_global()) {
env->global_frame_set(var, a->value()->perform(this));
}
else if (env->has(var)) {
Expression* v = static_cast<Expression*>((*env)[var]);
if (!a->is_guarded() || v->concrete_type() == Expression::NULL_VAL) (*env)[var] = a->value()->perform(this);
}
else {
env->current_frame()[var] = a->value()->perform(this);
env->current_frame_set(var, a->value()->perform(this));
}
return 0;
}
Expand Down
7 changes: 5 additions & 2 deletions expand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,15 @@ namespace Sass {
{
string var(a->variable());
Selector* p = selector_stack.size() <= 1 ? 0 : selector_stack.back();
if (env->has(var)) {
if (a->is_global()) {
env->global_frame_set(var, a->value()->perform(eval->with(p, env, backtrace)));
}
else if (env->has(var)) {
Expression* v = static_cast<Expression*>((*env)[var]);
if (!a->is_guarded() || v->concrete_type() == Expression::NULL_VAL) (*env)[var] = a->value()->perform(eval->with(p, env, backtrace));
}
else {
env->current_frame()[var] = a->value()->perform(eval->with(p, env, backtrace));
env->current_frame_set(var, a->value()->perform(eval->with(p, env, backtrace)));
}
return 0;
}
Expand Down
4 changes: 3 additions & 1 deletion functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ namespace Sass {
static mt19937 rand(static_cast<unsigned int>(GetSeed()));

// features
static set<string> features;
static set<string> features {
"global-variable-shadowing"
};

////////////////
// RGB FUNCTIONS
Expand Down

0 comments on commit 937ccf8

Please sign in to comment.