2010-06-16 06:44:59 +08:00
|
|
|
(function (tree) {
|
2010-03-02 04:32:21 +08:00
|
|
|
|
2010-06-19 13:51:26 +08:00
|
|
|
tree.Operation = function (op, operands) {
|
2010-02-24 02:39:05 +08:00
|
|
|
this.op = op.trim();
|
|
|
|
this.operands = operands;
|
|
|
|
};
|
2010-03-02 04:32:21 +08:00
|
|
|
tree.Operation.prototype.eval = function (env) {
|
2010-03-20 06:47:17 +08:00
|
|
|
var a = this.operands[0].eval(env),
|
|
|
|
b = this.operands[1].eval(env),
|
2010-02-27 11:31:04 +08:00
|
|
|
temp;
|
|
|
|
|
2010-03-20 06:47:17 +08:00
|
|
|
if (a instanceof tree.Dimension && b instanceof tree.Color) {
|
|
|
|
if (this.op === '*' || this.op === '+') {
|
|
|
|
temp = b, b = a, a = temp;
|
|
|
|
} else {
|
|
|
|
throw { name: "OperationError",
|
|
|
|
message: "Can't substract or divide a color from a number" };
|
|
|
|
}
|
2010-02-27 11:31:04 +08:00
|
|
|
}
|
2010-03-20 06:47:17 +08:00
|
|
|
return a.operate(this.op, b);
|
2010-02-24 02:39:05 +08:00
|
|
|
};
|
|
|
|
|
2010-03-23 13:19:12 +08:00
|
|
|
tree.operate = function (op, a, b) {
|
|
|
|
switch (op) {
|
|
|
|
case '+': return a + b;
|
|
|
|
case '-': return a - b;
|
|
|
|
case '*': return a * b;
|
|
|
|
case '/': return a / b;
|
|
|
|
}
|
|
|
|
};
|
2010-06-16 06:44:59 +08:00
|
|
|
|
2011-01-06 03:23:28 +08:00
|
|
|
})(require('mess/tree'));
|