Commit b4de782d by Aaron Leung

Handling arithmetic with units. Gonna' forget about janky edge cases for now --…

Handling arithmetic with units. Gonna' forget about janky edge cases for now -- people shouldn't be doing arithmetic on lists anyway.
parent 4dd8d913
...@@ -357,7 +357,6 @@ namespace Sass { ...@@ -357,7 +357,6 @@ namespace Sass {
Node expression(line_number, Node::expression, 3); Node expression(line_number, Node::expression, 3);
term1.eval_me = true; term1.eval_me = true;
cerr << "Parsed an initial term: " << term1.to_string("") << endl;
expression << term1; expression << term1;
while (lex< exactly<'+'> >() || lex< exactly<'-'> >()) { while (lex< exactly<'+'> >() || lex< exactly<'-'> >()) {
...@@ -373,7 +372,17 @@ namespace Sass { ...@@ -373,7 +372,17 @@ namespace Sass {
} }
expression.eval_me = true; expression.eval_me = true;
cerr << "parsed an expression: " << expression.to_string("") << endl; // for (int i = 0; i < expression.children->size(); ++i) {
// Node::Type t = expression.children->at(i).type;
// if (t == Node::string_constant ||
// t == Node::uri ||
// t == Node::identifier ||
// t == Node::comma_list ||
// t == Node::space_list ||
// t == Node::nil ||
// t == Node::expression && !expression.children->at(i).eval_me)
// { expression.eval_me = false; }
// }
return expression; return expression;
} }
......
...@@ -7,7 +7,6 @@ namespace Sass { ...@@ -7,7 +7,6 @@ namespace Sass {
Node eval(const Node& expr) Node eval(const Node& expr)
{ {
cerr << "evaluating type " << expr.type << ": " << expr.to_string("") << endl;
switch (expr.type) switch (expr.type)
{ {
case Node::comma_list: case Node::comma_list:
...@@ -22,7 +21,6 @@ namespace Sass { ...@@ -22,7 +21,6 @@ namespace Sass {
Node acc(expr.line_number, Node::expression, eval(expr.children->at(0))); Node acc(expr.line_number, Node::expression, eval(expr.children->at(0)));
Node rhs(eval(expr.children->at(2))); Node rhs(eval(expr.children->at(2)));
accumulate(expr.children->at(1).type, acc, rhs); accumulate(expr.children->at(1).type, acc, rhs);
cerr << "blah" << endl;
for (int i = 3; i < expr.children->size(); i += 2) { for (int i = 3; i < expr.children->size(); i += 2) {
Node rhs(eval(expr.children->at(i+1))); Node rhs(eval(expr.children->at(i+1)));
accumulate(expr.children->at(i).type, acc, rhs); accumulate(expr.children->at(i).type, acc, rhs);
...@@ -78,17 +76,13 @@ namespace Sass { ...@@ -78,17 +76,13 @@ namespace Sass {
double lnum = lhs.numeric_value; double lnum = lhs.numeric_value;
double rnum = rhs.numeric_value; double rnum = rhs.numeric_value;
// cerr << "accumulate's args: " << lhs.to_string("") << "\t" << rhs.to_string("") << endl;
// cerr << "accumulate's arg types: " << lhs.type << "\t" << rhs.type << endl;
// cerr << endl;
if (lhs.type == Node::number && rhs.type == Node::number) { if (lhs.type == Node::number && rhs.type == Node::number) {
Node result(acc.line_number, operate(op, lnum, rnum)); Node result(acc.line_number, operate(op, lnum, rnum));
// cerr << "accumulate just made a node: " << result.to_string("") << "\t" << result.type << endl;
acc.children->pop_back(); acc.children->pop_back();
acc.children->push_back(result); acc.children->push_back(result);
} }
else if (lhs.type == Node::number && rhs.type == Node::numeric_dimension) { else if (lhs.type == Node::number && rhs.type == Node::numeric_dimension) {
// TO DO: disallow division
Node result(acc.line_number, operate(op, lnum, rnum), rhs.token); Node result(acc.line_number, operate(op, lnum, rnum), rhs.token);
acc.children->pop_back(); acc.children->pop_back();
acc.children->push_back(result); acc.children->push_back(result);
...@@ -100,12 +94,16 @@ namespace Sass { ...@@ -100,12 +94,16 @@ namespace Sass {
} }
else if (lhs.type == Node::numeric_dimension && rhs.type == Node::numeric_dimension) { else if (lhs.type == Node::numeric_dimension && rhs.type == Node::numeric_dimension) {
// TO DO: CHECK FOR MISMATCHED UNITS HERE // TO DO: CHECK FOR MISMATCHED UNITS HERE
Node result(acc.line_number, operate(op, lnum, rnum), lhs.token); Node result;
if (op == Node::div)
{ result = Node(acc.line_number, operate(op, lnum, rnum)); }
else
{ result = Node(acc.line_number, operate(op, lnum, rnum), lhs.token); }
acc.children->pop_back(); acc.children->pop_back();
acc.children->push_back(result); acc.children->push_back(result);
} }
else { else {
// cerr << "accumulate: didn't do anything" << endl; // TO DO: disallow division and multiplication on lists
acc.children->push_back(rhs); acc.children->push_back(rhs);
} }
...@@ -114,6 +112,7 @@ namespace Sass { ...@@ -114,6 +112,7 @@ namespace Sass {
double operate(const Node::Type op, double lhs, double rhs) double operate(const Node::Type op, double lhs, double rhs)
{ {
// TO DO: check for division by zero
switch (op) switch (op)
{ {
case Node::add: return lhs + rhs; break; case Node::add: return lhs + rhs; break;
......
...@@ -21,13 +21,12 @@ div { ...@@ -21,13 +21,12 @@ div {
/* and this */ /* and this */
k: 15 / $three; k: 15 / $three;
l: 15 / 5 / $three; l: 15 / 5 / $three;
m: 1/2 + $stuff; m: 1/2, $stuff;
m: 1/2 + (1 2 3); n: 1 2 3, $stuff 4 5 (6, 7 8 9);
n: $stuff + 1/2;
n: (1/2 2/4 3) + 1/2 + 1/2;
n: 1/2 + 1/2 + 1/2;
o: 3px + 3px; o: 3px + 3px;
p: 4 + 1em; p: 4 + 1em;
q: (20pt / 10pt); q: (20pt / 10pt);
r: 16em * 4; r: 16em * 4;
s: (5em / 2);
t: 1 + (2 + (3/4 + (4/5 6/7)));
} }
\ No newline at end of file
...@@ -21,13 +21,12 @@ div { ...@@ -21,13 +21,12 @@ div {
/* and this */ /* and this */
k: 15 / $three; k: 15 / $three;
l: 15 / 5 / $three; l: 15 / 5 / $three;
m: 1/2 + $stuff; m: 1/2, $stuff;
m: 1/2 + (1 2 3); n: 1 2 3, $stuff 4 5 (6, 7 8 9);
n: $stuff + 1/2;
n: (1/2 2/4 3) + 1/2 + 1/2;
n: 1/2 + 1/2 + 1/2;
o: 3px + 3px; o: 3px + 3px;
p: 4 + 1em; p: 4 + 1em;
q: (20pt / 10pt); q: (20pt / 10pt);
r: 16em * 4; r: 16em * 4;
s: (5em / 2);
t: 1 + (2 + (3/4 + (4/5 6/7)));
} }
\ No newline at end of file
...@@ -17,12 +17,11 @@ div { ...@@ -17,12 +17,11 @@ div {
/* and this */ /* and this */
k: 5; k: 5;
l: 1; l: 1;
m: 1/21 2 3; m: 1/2, 1 2 3;
m: 1/21 2 3; n: 1 2 3, 1 2 3 4 5 6, 7 8 9;
n: 1 2 31/2;
n: 0.5 2/4 31/21/2;
n: 1.5;
o: 6px; o: 6px;
p: 5em; p: 5em;
q: 2; q: 2;
r: 64em; } r: 64em;
s: 2.5em;
t: 120.750.8 6/7; }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment