Class Expression

An instance of the Expression class represents an expression.

Expression are useful to define pure function on run-time.

Constructor
ydn.math.Expression(tokens) version 2
Create an expression in canonical form.

Expression in canonical form consist orderred list of tokens in reverse polish notation (RPN). Tokens are recognized as the following categories:

  1. Predefined values
    1. true : true
    2. false : false
    3. now : current time, i.e, new Date()
  2. Predefined operators
      Unary
    1. abs : absolute function
    2. date : convert integer to Date type
    3. ! : logical not
      Binary
    1. + : plus function
    2. - : minus function
    3. * : multiply function
    4. / : divide function
    5. % : modulo function
    6. in : array test, contains in
    7. of : array find, index of
    8. at : array value, element at
    9. ==, ===, !=, !==, <=, <, >=, >: Comparator
    10. |, & : Logical or, and
      Ternary
    1. ? : conditional
  3. Double quoted value as field name. Which is evaluated to operand to the field value of the with-object.
  4. String starting by $ and follow by digit as operand map to run-time function argument.
  5. Single quoted string as operand.
  6. Anything as operand.

The following example illustrate adding 1 to the value of with-object field 'a':

var e = new ydn.math.Expression(['"a"', 1, '+']);
var with_obj = {a: 5};
var result = e.evaluate(with_obj); // result = 6

The following example illustrate cube function of given argument 1:

var e = new ydn.math.Expression(['$1', '$1', '$1', '*', '*']);
var result = e.evaluate(null, 2); // result = 8
Parameters:
{!Array} tokens
Ordered list of tokens to define an expression.
Method
evaluate(with_object, ...)
Evaluate the expression.
With-object is not evaluate by javascript with statement, but directly accessing field value of given with-object in the spirit of pure function. Any additional argument are used as canned arguments.
Parameters:
{Object} with_object
With object for field value. May be null.
{...} ...
Optional canned arguments. Argument are referred by $1, $2, etc
Returns:
{*} return result of evaluation of the expression.

compile()
Compile the expression to a function.
Returns:
{Function} return function.

parseInfix(expression)
Parse infix notation representation to an expression.

The following example illustrate adding 1 to the value of with-object field 'a':

var e = ydn.math.Expresion.parseInfix('"a" + 1');
result = e.evaluate({a: 4}); // result = 5
Parameters:
{string} expression
Expression in infix notation.
Returns:
{!ydn.math.Expression} return store name.

parseRpn(expression)
Parse reverse polish notation (RPN) representation to an expression.

The following example illustrate adding 1 to the value of with-object field 'a':

var e = ydn.math.Expresion.parseRpn('"a" 2 +');
result = e.evaluate({a: 1}); // result = 3

Arithmetic function

var e = ydn.math.Expression.parseRpn('2 $1 /');
result = e.evaluate(null, 6); // result = 3;

String concatenation

var e = ydn.math.Expression.parseRpn('"a" \'b\' +');
result = e.evaluate({a: 'c'}); // result = 'bc';
Parameters:
{string} expression
Expression in RPN.
Returns:
{!ydn.math.Expression} return store name.

toJSON()
Get expression in JSON format.
Returns:
{!Object} object JSON object.