Math expressions

Just simple binary type expressions supported here.  These can be serialized in a variety of formats.  More complex operations to be supported by call expressions.
This commit is contained in:
Tim Schaub
2013-06-10 12:06:31 -06:00
parent b2ff793ea1
commit 153df45f95
2 changed files with 184 additions and 0 deletions

View File

@@ -3,6 +3,8 @@ goog.provide('ol.expression.ComparisonOp');
goog.provide('ol.expression.Expression');
goog.provide('ol.expression.Identifier');
goog.provide('ol.expression.Literal');
goog.provide('ol.expression.Math');
goog.provide('ol.expression.MathOp');
goog.provide('ol.expression.Not');
@@ -175,6 +177,88 @@ ol.expression.Literal.prototype.evaluate = function(scope) {
};
/**
* @enum {string}
*/
ol.expression.MathOp = {
ADD: '+',
SUBTRACT: '-',
MULTIPLY: '*',
DIVIDE: '/',
MOD: '%'
};
/**
* A math expression (e.g. `foo + 42`, `bar % 10`).
*
* @constructor
* @extends {ol.expression.Expression}
* @param {ol.expression.MathOp} operator Math operator.
* @param {ol.expression.Expression} left Left expression.
* @param {ol.expression.Expression} right Right expression.
*/
ol.expression.Math = function(operator, left, right) {
/**
* @type {ol.expression.MathOp}
* @private
*/
this.operator_ = operator;
/**
* @type {ol.expression.Expression}
* @private
*/
this.left_ = left;
/**
* @type {ol.expression.Expression}
* @private
*/
this.right_ = right;
};
goog.inherits(ol.expression.Math, ol.expression.Expression);
/**
* @inheritDoc
*/
ol.expression.Math.prototype.evaluate = function(scope) {
var result;
var rightVal = this.right_.evaluate(scope);
var leftVal = this.left_.evaluate(scope);
/**
* TODO: throw if rightVal, leftVal not numbers - this would require the use
* of a concat function for strings but it would let us serialize these as
* math functions where available elsewhere
*/
switch (this.operator_) {
case ol.expression.MathOp.ADD:
result = leftVal + rightVal;
break;
case ol.expression.MathOp.SUBTRACT:
result = Number(leftVal) - Number(rightVal);
break;
case ol.expression.MathOp.MULTIPLY:
result = Number(leftVal) * Number(rightVal);
break;
case ol.expression.MathOp.DIVIDE:
result = Number(leftVal) / Number(rightVal);
break;
case ol.expression.MathOp.MOD:
result = Number(leftVal) % Number(rightVal);
break;
default:
throw new Error('Unsupported math operator: ' + this.operator_);
}
return result;
};
/**
* A logical not expression (e.g. `!foo`).