Skip to content

Commit 418679b

Browse files
committed
Add to_value class for value handling
So far only needed to convert selectors!
1 parent 3313d98 commit 418679b

File tree

9 files changed

+172
-12
lines changed

9 files changed

+172
-12
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ SOURCES = \
190190
source_map.cpp \
191191
to_c.cpp \
192192
to_string.cpp \
193+
to_value.cpp \
193194
units.cpp \
194195
utf8_string.cpp \
195196
util.cpp

Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ libsass_la_SOURCES = \
7777
source_map.cpp source_map.hpp \
7878
to_c.cpp to_c.hpp \
7979
to_string.cpp to_string.hpp \
80+
to_value.cpp to_value.hpp \
8081
units.cpp units.hpp \
8182
utf8_string.cpp utf8_string.hpp \
8283
util.cpp util.hpp

ast_def_macros.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ virtual Statement* perform(Operation<Statement*>* op) { return (*op)(this); }\
3333
virtual Expression* perform(Operation<Expression*>* op) { return (*op)(this); }\
3434
virtual Selector* perform(Operation<Selector*>* op) { return (*op)(this); }\
3535
virtual string perform(Operation<string>* op) { return (*op)(this); }\
36-
virtual union Sass_Value* perform(Operation<union Sass_Value*>* op) { return (*op)(this); }
36+
virtual union Sass_Value* perform(Operation<union Sass_Value*>* op) { return (*op)(this); }\
37+
virtual Value* perform(Operation<Value*>* op) { return (*op)(this); }
3738

3839
#define ADD_PROPERTY(type, name)\
3940
protected:\

eval.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "environment.hpp"
1616
#include "position.hpp"
1717
#include "sass_values.h"
18+
#include "to_value.hpp"
1819
#include "to_c.hpp"
1920
#include "context.hpp"
2021
#include "backtrace.hpp"
@@ -532,7 +533,10 @@ namespace Sass {
532533
return op_colors(ctx, op_type, l_c, r_c);
533534
}
534535

535-
Expression* ex = op_strings(ctx, op_type, lhs, rhs);
536+
To_Value to_value(ctx, ctx.mem);
537+
Value* v_l = dynamic_cast<Value*>(lhs->perform(&to_value));
538+
Value* v_r = dynamic_cast<Value*>(rhs->perform(&to_value));
539+
Value* ex = op_strings(ctx, op_type, v_l, v_r);
536540
if (String_Constant* str = dynamic_cast<String_Constant*>(ex))
537541
{
538542
if (str->concrete_type() != Expression::STRING) return ex;
@@ -1163,7 +1167,7 @@ namespace Sass {
11631167
return l->value() < tmp_r.value();
11641168
}
11651169

1166-
Expression* Eval::op_numbers(Context& ctx, enum Sass_OP op, Number* l, Number* r)
1170+
Value* Eval::op_numbers(Context& ctx, enum Sass_OP op, Number* l, Number* r)
11671171
{
11681172
double lv = l->value();
11691173
double rv = r->value();
@@ -1213,7 +1217,7 @@ namespace Sass {
12131217
return v;
12141218
}
12151219

1216-
Expression* Eval::op_number_color(Context& ctx, enum Sass_OP op, Number* l, Color* r)
1220+
Value* Eval::op_number_color(Context& ctx, enum Sass_OP op, Number* l, Color* r)
12171221
{
12181222
// TODO: currently SASS converts colors to standard form when adding to strings;
12191223
// when https://github.com/nex3/sass/issues/363 is added this can be removed to
@@ -1250,7 +1254,7 @@ namespace Sass {
12501254
return l;
12511255
}
12521256

1253-
Expression* Eval::op_color_number(Context& ctx, enum Sass_OP op, Color* l, Number* r)
1257+
Value* Eval::op_color_number(Context& ctx, enum Sass_OP op, Color* l, Number* r)
12541258
{
12551259
double rv = r->value();
12561260
if (op == Sass_OP::DIV && !rv) error("division by zero", r->pstate());
@@ -1261,7 +1265,7 @@ namespace Sass {
12611265
l->a());
12621266
}
12631267

1264-
Expression* Eval::op_colors(Context& ctx, enum Sass_OP op, Color* l, Color* r)
1268+
Value* Eval::op_colors(Context& ctx, enum Sass_OP op, Color* l, Color* r)
12651269
{
12661270
if (l->a() != r->a()) {
12671271
error("alpha channels must be equal when combining colors", r->pstate());
@@ -1277,7 +1281,7 @@ namespace Sass {
12771281
l->a());
12781282
}
12791283

1280-
Expression* Eval::op_strings(Context& ctx, enum Sass_OP op, Expression* lhs, Expression*rhs)
1284+
Value* Eval::op_strings(Context& ctx, enum Sass_OP op, Value* lhs, Value*rhs)
12811285
{
12821286
To_String to_string(&ctx);
12831287
Expression::Concrete_Type ltype = lhs->concrete_type();

eval.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ namespace Sass {
8989
static bool eq(Expression*, Expression*);
9090
static bool lt(Expression*, Expression*);
9191
// -- arithmetic on the combinations that matter
92-
static Expression* op_numbers(Context&, enum Sass_OP, Number*, Number*);
93-
static Expression* op_number_color(Context&, enum Sass_OP, Number*, Color*);
94-
static Expression* op_color_number(Context&, enum Sass_OP, Color*, Number*);
95-
static Expression* op_colors(Context&, enum Sass_OP, Color*, Color*);
96-
static Expression* op_strings(Context&, enum Sass_OP, Expression*, Expression*);
92+
static Value* op_numbers(Context&, enum Sass_OP, Number*, Number*);
93+
static Value* op_number_color(Context&, enum Sass_OP, Number*, Color*);
94+
static Value* op_color_number(Context&, enum Sass_OP, Color*, Number*);
95+
static Value* op_colors(Context&, enum Sass_OP, Color*, Color*);
96+
static Value* op_strings(Context&, enum Sass_OP, Value*, Value*);
9797

9898
private:
9999
string interpolation(Expression* s);

to_value.cpp

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#include "ast.hpp"
2+
#include "sass_values.h"
3+
#include "to_value.hpp"
4+
#include "to_string.hpp"
5+
6+
namespace Sass {
7+
using namespace std;
8+
9+
Value* To_Value::fallback_impl(AST_Node* n)
10+
{
11+
// throw a runtime error if this happens
12+
// we want a well defined set of possible nodes
13+
throw runtime_error("invalid node for to_value");
14+
// mute warning
15+
return 0;
16+
}
17+
18+
// Boolean is a valid value
19+
Value* To_Value::operator()(Boolean* b)
20+
{
21+
return b;
22+
}
23+
24+
// Number is a valid value
25+
Value* To_Value::operator()(Number* n)
26+
{
27+
return n;
28+
}
29+
30+
// Color is a valid value
31+
Value* To_Value::operator()(Color* c)
32+
{
33+
return c;
34+
}
35+
36+
// String_Constant is a valid value
37+
Value* To_Value::operator()(String_Constant* s)
38+
{
39+
return s;
40+
}
41+
42+
// String_Quoted is a valid value
43+
Value* To_Value::operator()(String_Quoted* s)
44+
{
45+
return s;
46+
}
47+
48+
// List is a valid value
49+
Value* To_Value::operator()(List* l)
50+
{
51+
List* ll = new (mem) List(l->pstate(),
52+
l->length(),
53+
l->separator(),
54+
l->is_arglist());
55+
for (size_t i = 0, L = l->length(); i < L; ++i) {
56+
*ll << (*l)[i]->perform(this);
57+
}
58+
return ll;
59+
}
60+
61+
// Map is a valid value
62+
Value* To_Value::operator()(Map* m)
63+
{
64+
return m;
65+
}
66+
67+
// Null is a valid value
68+
Value* To_Value::operator()(Null* n)
69+
{
70+
return n;
71+
}
72+
73+
// Argument returns its value
74+
Value* To_Value::operator()(Argument* arg)
75+
{
76+
if (!arg->name().empty()) return 0;
77+
return arg->value()->perform(this);
78+
}
79+
80+
// Selector_List is converted to a string
81+
Value* To_Value::operator()(Selector_List* s)
82+
{
83+
To_String to_string(&ctx);
84+
return new (mem) String_Quoted(s->pstate(),
85+
s->perform(&to_string));
86+
}
87+
88+
// Binary_Expression is converted to a string
89+
Value* To_Value::operator()(Binary_Expression* s)
90+
{
91+
To_String to_string(&ctx);
92+
return new (mem) String_Quoted(s->pstate(),
93+
s->perform(&to_string));
94+
}
95+
96+
};

to_value.hpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#ifndef SASS_TO_VALUE_H
2+
#define SASS_TO_VALUE_H
3+
4+
#include "operation.hpp"
5+
#include "sass_values.h"
6+
#include "ast_fwd_decl.hpp"
7+
8+
namespace Sass {
9+
using namespace std;
10+
11+
class To_Value : public Operation_CRTP<Value*, To_Value> {
12+
13+
Value* fallback_impl(AST_Node* n);
14+
15+
private:
16+
17+
Context& ctx;
18+
Memory_Manager<AST_Node>& mem;
19+
20+
public:
21+
22+
To_Value(Context& ctx, Memory_Manager<AST_Node>& mem)
23+
: ctx(ctx), mem(mem)
24+
{ }
25+
virtual ~To_Value() { }
26+
using Operation<Value*>::operator();
27+
28+
Value* operator()(Argument*);
29+
Value* operator()(Boolean*);
30+
Value* operator()(Number*);
31+
Value* operator()(Color*);
32+
Value* operator()(String_Constant*);
33+
Value* operator()(String_Quoted*);
34+
Value* operator()(List*);
35+
Value* operator()(Map*);
36+
Value* operator()(Null*);
37+
38+
// convert to string via `To_String`
39+
Value* operator()(Selector_List*);
40+
Value* operator()(Binary_Expression*);
41+
42+
// fallback throws error
43+
template <typename U>
44+
Value* fallback(U x) { return fallback_impl(x); }
45+
};
46+
47+
}
48+
49+
#endif

win/libsass.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@
120120
<ClCompile Include="..\to_string.cpp">
121121
<Filter>Source Files</Filter>
122122
</ClCompile>
123+
<ClCompile Include="..\to_value.cpp">
124+
<Filter>Source Files</Filter>
125+
</ClCompile>
123126
<ClCompile Include="..\units.cpp">
124127
<Filter>Source Files</Filter>
125128
</ClCompile>
@@ -281,6 +284,9 @@
281284
<ClInclude Include="..\to_c.hpp">
282285
<Filter>Header Files</Filter>
283286
</ClInclude>
287+
<ClInclude Include="..\to_value.hpp">
288+
<Filter>Header Files</Filter>
289+
</ClInclude>
284290
<ClInclude Include="..\to_string.hpp">
285291
<Filter>Header Files</Filter>
286292
</ClInclude>

win/libsass.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@
199199
<ClCompile Include="..\source_map.cpp" />
200200
<ClCompile Include="..\to_c.cpp" />
201201
<ClCompile Include="..\to_string.cpp" />
202+
<ClCompile Include="..\to_value.cpp" />
202203
<ClCompile Include="..\units.cpp" />
203204
<ClCompile Include="..\utf8_string.cpp" />
204205
<ClCompile Include="..\util.cpp" />
@@ -253,6 +254,7 @@
253254
<ClInclude Include="..\token.hpp" />
254255
<ClInclude Include="..\to_c.hpp" />
255256
<ClInclude Include="..\to_string.hpp" />
257+
<ClInclude Include="..\to_value.hpp" />
256258
<ClInclude Include="..\units.hpp" />
257259
<ClInclude Include="..\utf8.h" />
258260
<ClInclude Include="..\utf8\checked.h" />

0 commit comments

Comments
 (0)