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

Refactor contextualize into eval [WIP] #1217

Closed
wants to merge 9 commits 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
2 changes: 0 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,6 @@ SOURCES = \
bind.cpp \
constants.cpp \
context.cpp \
contextualize.cpp \
contextualize_eval.cpp \
cssize.cpp \
listize.cpp \
error_handling.cpp \
Expand Down
2 changes: 0 additions & 2 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ libsass_la_SOURCES = \
bind.cpp bind.hpp \
constants.cpp constants.hpp \
context.cpp context.hpp \
contextualize.cpp contextualize.hpp \
contextualize_eval.cpp contextualize_eval.hpp \
error_handling.cpp error_handling.hpp \
eval.cpp eval.hpp \
expand.cpp expand.hpp \
Expand Down
68 changes: 11 additions & 57 deletions ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,6 @@ namespace Sass {
void Selector_List::adjust_after_pushing(Complex_Selector* c)
{
if (c->has_reference()) has_reference(true);
if (c->has_placeholder()) has_placeholder(true);

#ifdef DEBUG
To_String to_string;
Expand Down Expand Up @@ -559,45 +558,6 @@ namespace Sass {
return false;
}

/* not used anymore - remove?
Selector_Placeholder* Selector_List::find_placeholder()
{
if (has_placeholder()) {
for (size_t i = 0, L = length(); i < L; ++i) {
if ((*this)[i]->has_placeholder()) return (*this)[i]->find_placeholder();
}
}
return 0;
}*/

/* not used anymore - remove?
Selector_Placeholder* Complex_Selector::find_placeholder()
{
if (has_placeholder()) {
if (head() && head()->has_placeholder()) return head()->find_placeholder();
else if (tail() && tail()->has_placeholder()) return tail()->find_placeholder();
}
return 0;
}*/

/* not used anymore - remove?
Selector_Placeholder* Compound_Selector::find_placeholder()
{
if (has_placeholder()) {
for (size_t i = 0, L = length(); i < L; ++i) {
if ((*this)[i]->has_placeholder()) return (*this)[i]->find_placeholder();
}
// return this;
}
return 0;
}*/

/* not used anymore - remove?
Selector_Placeholder* Selector_Placeholder::find_placeholder()
{
return this;
}*/

vector<string> Compound_Selector::to_str_vec()
{
To_String to_string;
Expand Down Expand Up @@ -681,17 +641,17 @@ namespace Sass {

string Number::unit() const
{
stringstream u;
string u;
for (size_t i = 0, S = numerator_units_.size(); i < S; ++i) {
if (i) u << '*';
u << numerator_units_[i];
if (i) u += '*';
u += numerator_units_[i];
}
if (!denominator_units_.empty()) u << '/';
if (!denominator_units_.empty()) u += '/';
for (size_t i = 0, S = denominator_units_.size(); i < S; ++i) {
if (i) u << '*';
u << denominator_units_[i];
if (i) u += '*';
u += denominator_units_[i];
}
return u.str();
return u;
}

bool Number::is_unitless()
Expand Down Expand Up @@ -882,17 +842,11 @@ namespace Sass {

bool Number::operator== (Expression* rhs) const
{
try
{
Number l(pstate_, value_, unit());
Number& r = dynamic_cast<Number&>(*rhs);
l.normalize(find_convertible_unit());
r.normalize(find_convertible_unit());
return l.unit() == r.unit() &&
l.value() == r.value();
if (Number* r = static_cast<Number*>(rhs)) {
return (value() == r->value()) &&
(numerator_units_ == r->numerator_units_) &&
(denominator_units_ == r->denominator_units_);
}
catch (std::bad_cast&) {}
catch (...) { throw; }
return false;
}

Expand Down
63 changes: 26 additions & 37 deletions ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -648,15 +648,15 @@ namespace Sass {
ADD_PROPERTY(Native_Function, native_function);
ADD_PROPERTY(Sass_Function_Entry, c_function);
ADD_PROPERTY(void*, cookie);
ADD_PROPERTY(Context*, ctx);
// ADD_PROPERTY(Context*, ctx);
ADD_PROPERTY(bool, is_overload_stub);
ADD_PROPERTY(Signature, signature);
public:
Definition(ParserState pstate,
string n,
Parameters* params,
Block* b,
Context* ctx,
// Context* ctx,
Type t)
: Has_Block(pstate, b),
name_(n),
Expand All @@ -666,7 +666,7 @@ namespace Sass {
native_function_(0),
c_function_(0),
cookie_(0),
ctx_(ctx),
// ctx_(ctx),
is_overload_stub_(false),
signature_(0)
{ }
Expand All @@ -675,7 +675,7 @@ namespace Sass {
string n,
Parameters* params,
Native_Function func_ptr,
Context* ctx,
// Context* ctx,
bool overload_stub = false)
: Has_Block(pstate, 0),
name_(n),
Expand All @@ -685,7 +685,7 @@ namespace Sass {
native_function_(func_ptr),
c_function_(0),
cookie_(0),
ctx_(ctx),
// ctx_(ctx),
is_overload_stub_(overload_stub),
signature_(sig)
{ }
Expand All @@ -694,7 +694,7 @@ namespace Sass {
string n,
Parameters* params,
Sass_Function_Entry c_func,
Context* ctx,
// Context* ctx,
bool whatever,
bool whatever2)
: Has_Block(pstate, 0),
Expand All @@ -705,7 +705,7 @@ namespace Sass {
native_function_(0),
c_function_(c_func),
cookie_(sass_function_get_cookie(c_func)),
ctx_(ctx),
// ctx_(ctx),
is_overload_stub_(false),
signature_(sig)
{ }
Expand Down Expand Up @@ -1666,26 +1666,10 @@ namespace Sass {
//////////////////////////////////////////////////////////////////////////////////////////
inline Expression* List::value_at_index(size_t i) { return is_arglist_ ? ((Argument*)(*this)[i])->value() : (*this)[i]; }

////////////
// The Parent Selector Expression.
////////////
class Parent_Selector : public Expression {
ADD_PROPERTY(Selector*, selector);
public:
Parent_Selector(ParserState pstate, Selector* r = 0)
: Expression(pstate), selector_(r)
{ concrete_type(SELECTOR); }
virtual Selector* selector() { return selector_; }
string type() { return "selector"; }
static string type_name() { return "selector"; }

ATTACH_OPERATIONS();
};

/////////////////////////////////////////
// Abstract base class for CSS selectors.
/////////////////////////////////////////
class Selector : public AST_Node {
class Selector : public Expression {
ADD_PROPERTY(bool, has_reference);
ADD_PROPERTY(bool, has_placeholder);
// line break before list separator
Expand All @@ -1699,14 +1683,14 @@ namespace Sass {
ADD_PROPERTY(Media_Block*, media_block);
public:
Selector(ParserState pstate, bool r = false, bool h = false)
: AST_Node(pstate),
: Expression(pstate),
has_reference_(r),
has_placeholder_(h),
has_line_feed_(false),
has_line_break_(false),
is_optional_(false),
media_block_(0)
{ }
{ concrete_type(SELECTOR); }
virtual ~Selector() = 0;
// virtual Selector_Placeholder* find_placeholder();
virtual unsigned long specificity() {
Expand Down Expand Up @@ -1748,20 +1732,26 @@ namespace Sass {
};
inline Simple_Selector::~Simple_Selector() { }

/////////////////////////////////////
// Parent references (i.e., the "&").
/////////////////////////////////////
class Selector_Reference : public Simple_Selector {
ADD_PROPERTY(Selector*, selector);

//////////////////////////////////
// The Parent Selector Expression.
//////////////////////////////////
class Parent_Selector : public Simple_Selector {
// ADD_PROPERTY(Selector*, selector);
public:
Selector_Reference(ParserState pstate, Selector* r = 0)
: Simple_Selector(pstate), selector_(r)
Parent_Selector(ParserState pstate, Selector* r = 0)
: Simple_Selector(pstate)// , selector_(r)
{ has_reference(true); }
virtual unsigned long specificity()
{
if (!selector()) return 0;
return selector()->specificity();
return 0;
// if (!selector()) return 0;
// return selector()->specificity();
}
// virtual Selector* selector() { return selector_; }
string type() { return "selector"; }
static string type_name() { return "selector"; }

ATTACH_OPERATIONS();
};

Expand Down Expand Up @@ -1956,8 +1946,7 @@ namespace Sass {
bool is_empty_reference()
{
return length() == 1 &&
typeid(*(*this)[0]) == typeid(Selector_Reference) &&
!static_cast<Selector_Reference*>((*this)[0])->selector();
typeid(*(*this)[0]) == typeid(Parent_Selector);
}
vector<string> to_str_vec(); // sometimes need to convert to a flat "by-value" data structure

Expand Down
24 changes: 24 additions & 0 deletions ast_def_macros.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
#ifndef SASS_AST_DEF_MACROS_H
#define SASS_AST_DEF_MACROS_H

template <class T>
class LocalOption {
private:
T* var; // pointer to original variable
T orig; // copy of the original option
public:
LocalOption(T& var)
{
this->var = &var;
this->orig = var;
}
LocalOption(T& var, T orig)
{
this->var = &var;
this->orig = var;
*(this->var) = orig;
}
~LocalOption() {
*(this->var) = this->orig;
}
};

#define LOCAL_FLAG(name,opt) LocalOption<bool> flag_##name(name, opt)

#define ATTACH_OPERATIONS()\
virtual void perform(Operation<void>* op) { (*op)(this); }\
virtual AST_Node* perform(Operation<AST_Node*>* op) { return (*op)(this); }\
Expand Down
3 changes: 1 addition & 2 deletions ast_fwd_decl.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#ifndef SASS_AST_FWD_DECL_H
#define SASS_AST_FWD_DECL_H


/////////////////////////////////////////////
// Forward declarations for the AST visitors.
/////////////////////////////////////////////
namespace Sass {

enum Output_Style { NESTED, EXPANDED, COMPACT, COMPRESSED, FORMATTED };
Expand Down Expand Up @@ -69,7 +69,6 @@ namespace Sass {
// selectors
class Selector;
class Selector_Schema;
class Selector_Reference;
class Selector_Placeholder;
class Type_Selector;
class Selector_Qualifier;
Expand Down
12 changes: 3 additions & 9 deletions bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ namespace Sass {

void bind(string callee, Parameters* ps, Arguments* as, Context& ctx, Env* env, Eval* eval)
{

// Context& ctx = eval->context();
map<string, Parameter*> param_map;

// Set up a map to ensure named arguments refer to actual parameters. Also
Expand Down Expand Up @@ -227,15 +229,7 @@ namespace Sass {
true);
}
else if (leftover->default_value()) {
// make sure to eval the default value in the env that we've been populating
Env* old_env = eval->env;
Backtrace* old_bt = eval->backtrace;
Contextualize* old_context = eval->contextualize;
Expression* dv = leftover->default_value()->perform(eval->with(env, eval->backtrace));
eval->env = old_env;
eval->backtrace = old_bt;
eval->contextualize = old_context;
// dv->perform(&to_string);
Expression* dv = leftover->default_value()->perform(eval);
env->local_frame()[leftover->name()] = dv;
}
else {
Expand Down
Loading