Skip to content

Commit 5d50c96

Browse files
committed
Cleanup AST values compare operators
1 parent 93c62e8 commit 5d50c96

File tree

3 files changed

+63
-69
lines changed

3 files changed

+63
-69
lines changed

src/ast_values.cpp

Lines changed: 58 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,16 @@ namespace Sass {
8484

8585
bool List::operator== (const Expression& rhs) const
8686
{
87-
if (List_Ptr_Const r = Cast<List>(&rhs)) {
87+
if (auto r = Cast<List>(&rhs)) {
8888
if (length() != r->length()) return false;
8989
if (separator() != r->separator()) return false;
9090
if (is_bracketed() != r->is_bracketed()) return false;
9191
for (size_t i = 0, L = length(); i < L; ++i) {
92-
Expression_Obj rv = r->at(i);
93-
Expression_Obj lv = this->at(i);
94-
if (!lv || !rv) return false;
95-
if (!(*lv == *rv)) return false;
92+
auto rv = r->at(i);
93+
auto lv = this->at(i);
94+
if (!lv && rv) return false;
95+
else if (!rv && lv) return false;
96+
else if (*lv != *rv) return false;
9697
}
9798
return true;
9899
}
@@ -141,13 +142,14 @@ namespace Sass {
141142

142143
bool Map::operator== (const Expression& rhs) const
143144
{
144-
if (Map_Ptr_Const r = Cast<Map>(&rhs)) {
145+
if (auto r = Cast<Map>(&rhs)) {
145146
if (length() != r->length()) return false;
146147
for (auto key : keys()) {
147-
Expression_Obj lv = at(key);
148-
Expression_Obj rv = r->at(key);
149-
if (!rv || !lv) return false;
150-
if (!(*lv == *rv)) return false;
148+
auto rv = r->at(key);
149+
auto lv = this->at(key);
150+
if (!lv && rv) return false;
151+
else if (!rv && lv) return false;
152+
else if (*lv != *rv) return false;
151153
}
152154
return true;
153155
}
@@ -229,19 +231,12 @@ namespace Sass {
229231

230232
bool Binary_Expression::operator==(const Expression& rhs) const
231233
{
232-
try
233-
{
234-
Binary_Expression_Ptr_Const m = Cast<Binary_Expression>(&rhs);
235-
if (m == 0) return false;
234+
if (auto m = Cast<Binary_Expression>(&rhs)) {
236235
return type() == m->type() &&
237-
*left() == *m->left() &&
238-
*right() == *m->right();
236+
*left() == *m->left() &&
237+
*right() == *m->right();
239238
}
240-
catch (std::bad_cast&)
241-
{
242-
return false;
243-
}
244-
catch (...) { throw; }
239+
return false;
245240
}
246241

247242
size_t Binary_Expression::hash() const
@@ -267,9 +262,9 @@ namespace Sass {
267262

268263
bool Function::operator== (const Expression& rhs) const
269264
{
270-
if (Function_Ptr_Const r = Cast<Function>(&rhs)) {
271-
Definition_Ptr_Const d1 = Cast<Definition>(definition());
272-
Definition_Ptr_Const d2 = Cast<Definition>(r->definition());
265+
if (auto r = Cast<Function>(&rhs)) {
266+
auto d1 = Cast<Definition>(definition());
267+
auto d2 = Cast<Definition>(r->definition());
273268
return d1 && d2 && d1 == d2 && is_css() == r->is_css();
274269
}
275270
return false;
@@ -317,20 +312,14 @@ namespace Sass {
317312

318313
bool Function_Call::operator==(const Expression& rhs) const
319314
{
320-
try
321-
{
322-
Function_Call_Ptr_Const m = Cast<Function_Call>(&rhs);
323-
if (!(m && *sname() == *m->sname())) return false;
324-
if (!(m && arguments()->length() == m->arguments()->length())) return false;
325-
for (size_t i =0, L = arguments()->length(); i < L; ++i)
326-
if (!(*(*arguments())[i] == *(*m->arguments())[i])) return false;
315+
if (auto m = Cast<Function_Call>(&rhs)) {
316+
if (*sname() != *m->sname()) return false;
317+
if (arguments()->length() != m->arguments()->length()) return false;
318+
for (size_t i = 0, L = arguments()->length(); i < L; ++i)
319+
if (*arguments()->get(i) != *m->arguments()->get(i)) return false;
327320
return true;
328321
}
329-
catch (std::bad_cast&)
330-
{
331-
return false;
332-
}
333-
catch (...) { throw; }
322+
return false;
334323
}
335324

336325
size_t Function_Call::hash() const
@@ -366,16 +355,10 @@ namespace Sass {
366355

367356
bool Variable::operator==(const Expression& rhs) const
368357
{
369-
try
370-
{
371-
Variable_Ptr_Const e = Cast<Variable>(&rhs);
372-
return e && name() == e->name();
373-
}
374-
catch (std::bad_cast&)
375-
{
376-
return false;
358+
if (auto e = Cast<Variable>(&rhs)) {
359+
return name() == e->name();
377360
}
378-
catch (...) { throw; }
361+
return false;
379362
}
380363

381364
size_t Variable::hash()
@@ -452,21 +435,23 @@ namespace Sass {
452435

453436
bool Number::operator== (const Expression& rhs) const
454437
{
455-
if (auto rhsnr = Cast<Number>(&rhs)) {
456-
return *this == *rhsnr;
438+
if (auto n = Cast<Number>(&rhs)) {
439+
return *this == *n;
457440
}
458441
return false;
459442
}
460443

461444
bool Number::operator== (const Number& rhs) const
462445
{
446+
// unitless or only having one unit are equivalent (3.4)
447+
// therefore we need to reduce the units beforehand
463448
Number l(*this), r(rhs); l.reduce(); r.reduce();
464449
size_t lhs_units = l.numerators.size() + l.denominators.size();
465450
size_t rhs_units = r.numerators.size() + r.denominators.size();
466-
// unitless and only having one unit seems equivalent (will change in future)
467451
if (!lhs_units || !rhs_units) {
468452
return NEAR_EQUAL(l.value(), r.value());
469453
}
454+
// ensure both have same units
470455
l.normalize(); r.normalize();
471456
Units &lhs_unit = l, &rhs_unit = r;
472457
return lhs_unit == rhs_unit &&
@@ -475,21 +460,26 @@ namespace Sass {
475460

476461
bool Number::operator< (const Number& rhs) const
477462
{
463+
// unitless or only having one unit are equivalent (3.4)
464+
// therefore we need to reduce the units beforehand
478465
Number l(*this), r(rhs); l.reduce(); r.reduce();
479466
size_t lhs_units = l.numerators.size() + l.denominators.size();
480467
size_t rhs_units = r.numerators.size() + r.denominators.size();
481-
// unitless and only having one unit seems equivalent (will change in future)
482468
if (!lhs_units || !rhs_units) {
483469
return l.value() < r.value();
484470
}
471+
// ensure both have same units
485472
l.normalize(); r.normalize();
486473
Units &lhs_unit = l, &rhs_unit = r;
487474
if (!(lhs_unit == rhs_unit)) {
488475
/* ToDo: do we always get usefull backtraces? */
489476
throw Exception::IncompatibleUnits(rhs, *this);
490477
}
491-
return lhs_unit < rhs_unit ||
492-
l.value() < r.value();
478+
if (lhs_unit == rhs_unit) {
479+
return l.value() < r.value();
480+
} else {
481+
return lhs_unit < rhs_unit;
482+
}
493483
}
494484

495485
/////////////////////////////////////////////////////////////////////////
@@ -512,7 +502,7 @@ namespace Sass {
512502

513503
bool Color::operator== (const Expression& rhs) const
514504
{
515-
if (Color_Ptr_Const r = Cast<Color>(&rhs)) {
505+
if (auto r = Cast<Color>(&rhs)) {
516506
return r_ == r->r() &&
517507
g_ == r->g() &&
518508
b_ == r->b() &&
@@ -545,7 +535,7 @@ namespace Sass {
545535

546536
bool Custom_Error::operator== (const Expression& rhs) const
547537
{
548-
if (Custom_Error_Ptr_Const r = Cast<Custom_Error>(&rhs)) {
538+
if (auto r = Cast<Custom_Error>(&rhs)) {
549539
return message() == r->message();
550540
}
551541
return false;
@@ -564,7 +554,7 @@ namespace Sass {
564554

565555
bool Custom_Warning::operator== (const Expression& rhs) const
566556
{
567-
if (Custom_Warning_Ptr_Const r = Cast<Custom_Warning>(&rhs)) {
557+
if (auto r = Cast<Custom_Warning>(&rhs)) {
568558
return message() == r->message();
569559
}
570560
return false;
@@ -586,7 +576,7 @@ namespace Sass {
586576

587577
bool Boolean::operator== (const Expression& rhs) const
588578
{
589-
if (Boolean_Ptr_Const r = Cast<Boolean>(&rhs)) {
579+
if (auto r = Cast<Boolean>(&rhs)) {
590580
return (value() == r->value());
591581
}
592582
return false;
@@ -642,13 +632,12 @@ namespace Sass {
642632

643633
bool String_Schema::operator== (const Expression& rhs) const
644634
{
645-
if (String_Schema_Ptr_Const r = Cast<String_Schema>(&rhs)) {
635+
if (auto r = Cast<String_Schema>(&rhs)) {
646636
if (length() != r->length()) return false;
647637
for (size_t i = 0, L = length(); i < L; ++i) {
648-
Expression_Obj rv = (*r)[i];
649-
Expression_Obj lv = (*this)[i];
650-
if (!lv || !rv) return false;
651-
if (!(*lv == *rv)) return false;
638+
auto rv = (*r)[i];
639+
auto lv = (*this)[i];
640+
if (*lv != *rv) return false;
652641
}
653642
return true;
654643
}
@@ -707,10 +696,10 @@ namespace Sass {
707696

708697
bool String_Constant::operator== (const Expression& rhs) const
709698
{
710-
if (String_Quoted_Ptr_Const qstr = Cast<String_Quoted>(&rhs)) {
711-
return (value() == qstr->value());
712-
} else if (String_Constant_Ptr_Const cstr = Cast<String_Constant>(&rhs)) {
713-
return (value() == cstr->value());
699+
if (auto qstr = Cast<String_Quoted>(&rhs)) {
700+
return value() == qstr->value();
701+
} else if (auto cstr = Cast<String_Constant>(&rhs)) {
702+
return value() == cstr->value();
714703
}
715704
return false;
716705
}
@@ -753,10 +742,10 @@ namespace Sass {
753742

754743
bool String_Quoted::operator== (const Expression& rhs) const
755744
{
756-
if (String_Quoted_Ptr_Const qstr = Cast<String_Quoted>(&rhs)) {
757-
return (value() == qstr->value());
758-
} else if (String_Constant_Ptr_Const cstr = Cast<String_Constant>(&rhs)) {
759-
return (value() == cstr->value());
745+
if (auto qstr = Cast<String_Quoted>(&rhs)) {
746+
return value() == qstr->value();
747+
} else if (auto cstr = Cast<String_Constant>(&rhs)) {
748+
return value() == cstr->value();
760749
}
761750
return false;
762751
}
@@ -778,7 +767,7 @@ namespace Sass {
778767

779768
bool Null::operator== (const Expression& rhs) const
780769
{
781-
return rhs.concrete_type() == NULL_VAL;
770+
return Cast<Null>(&rhs) != NULL;
782771
}
783772

784773
size_t Null::hash() const

src/units.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,10 @@ namespace Sass {
266266
return (numerators == rhs.numerators) &&
267267
(denominators == rhs.denominators);
268268
}
269+
bool Units::operator!= (const Units& rhs) const
270+
{
271+
return ! (*this == rhs);
272+
}
269273

270274
double Units::normalize()
271275
{

src/units.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ namespace Sass {
8383
// compare operations
8484
bool operator< (const Units& rhs) const;
8585
bool operator== (const Units& rhs) const;
86+
bool operator!= (const Units& rhs) const;
8687
// factor to convert into given units
8788
double convert_factor(const Units&) const;
8889
};

0 commit comments

Comments
 (0)