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 !global assignments #986

Merged
merged 2 commits into from
Mar 27, 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
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;
Copy link
Contributor

Choose a reason for hiding this comment

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

Cool. I'd fixed this in a branch that I haven't PR'd yet :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks like your using debug_ast too 😉 I hope you like it as much as I do 😄 It actually decreased my time hunting bugs dramatically! I also thought about creating a nicer debug_env function, but I was able to use existing one (altough the output could be better there).

Copy link
Contributor

Choose a reason for hiding this comment

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

I saw you post debub_ast output in an issue and fell in love.
On 28 Mar 2015 23:05, "Marcel Greter" [email protected] wrote:

In debugger.hpp
#986 (comment):

@@ -69,7 +69,7 @@ inline void debug_ast(AST_Node* node, string ind = "", Env* env = 0)
case Complex_Selector::ADJACENT_TO: cerr << "{+}"; break;

Looks like your using debug_ast too [image: 😉] I hope you like it as
much as I do [image: 😄] It actually decreased my time hunting bugs
dramatically! I also thought about creating a nicer debug_env function,
but I was able to use existing one (altough the output could be better
there).


Reply to this email directly or view it on GitHub
https://github.com/sass/libsass/pull/986/files#r27344568.

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 @@ -37,12 +37,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 @@ -63,6 +80,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 @@ -73,6 +91,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"
Copy link
Contributor

Choose a reason for hiding this comment

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

We should remember to mention this in the changelog.

};

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