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