carto/lib/mess/tree/operation.js

33 lines
836 B
JavaScript
Raw Normal View History

(function (tree) {
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;
};
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),
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-03-20 06:47:17 +08:00
return a.operate(this.op, b);
2010-02-24 02:39:05 +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;
}
};
2011-01-06 03:23:28 +08:00
})(require('mess/tree'));