| Package | system.evaluators |
| Class | public class MathEvaluator |
| Inheritance | MathEvaluator Object |
| Implements | Evaluable |
The MathEvaluator implementation, support all of the following :
Decimal, hexadecimal, octal notation :
1 + 1
0.5 + 0.7
0xff + 0xbb
010 + 5
etc.
operators :
% / ∗ + - << >> >>> & ^ | ~
functions :
abs acos asin atan atan2 ceil cos exp
floor log max min pow random round
sin sqrt tan
Those functions replicate exactly what you can find in the Math object.
operators priority : from higher to lower priority
example : multiplication is performed before addition
(14) fcn(...) (...)
function call and expression grouping
example :
sin(4) + 25
sin(4) will be evaluated first
example :
5 ∗ (4 + 0.5)
the expression within the parenthesis will occurs first
(13) ~ + -
Unary operators
ex: +5 - +5 translate to (+5) - (+5)
ex: -5 + -5 translate to (-5) + (-5)
ex: ~3 - 7 translate to (~3) - 7
any unary operators will be evaluated first
(12) ∗ / %
multiplication, division, modulo division
ex: 5 ∗ 3 + 1 translate to (5 ∗ 3) + 1
ex: 2 % 8 - 4 translate to (2 % 8) - 4
(11) + -
addition, subtraction
(10) << >> <<<
bit shifting
(7) &
bitwise AND
(6) ^
bitwise XOR
(5) |
bitwise OR
context
When instanciating the MathEvaluator you can pass a context containing either variables or functions
Example :
var me:MathEvaluator = new MathEvaluator( { x:100, test:function(a:Number):Number {return a*a;} );
trace( me.eval( "test(100) + 1" ) ); // return 10001
but there are some limitations
Example :
test() // OK
test2() // OK
2test() // BAD
te2st() // BAD
we do not support logical ( || && ! ) or assignement operators ( = == += etc. )
reasons:
logical operators deal with boolean, here we want to deal only with numbers and math expression we do not support variables nor variable assignation
Parenthesis are used to alter the order of evaluation determined by operator precedence.
Operators with the same priority are evaluated left to right.
How the parser work :
1) we first filter some multiple char operators to single chars
ex: >> translate to « and other filtering
2) then we parse char by char (top to bottom parsing) to generate tokens in postfix order(reverse polish notation)
the expression 5 + 4 become [5,4,+] but while doing that we also do a lot of other things
3) finally we iterate trough our tokens and evaluate them by operators
ex: [5,4,+]
we find the op +, then addition the 2 values, etc.
till the end of the tokens list
| Method | Defined By | ||
|---|---|---|---|
MathEvaluator(context:Object = null)
Creates a new MathsEvaluator instance. | MathEvaluator | ||
eval(o:*):*
Evaluates the specified object. | MathEvaluator | ||
| currentPos | property |
mathparser var currentPos:uintThe current position.
| expression | property |
mathparser var expression:StringThe expression value.
| tokens | property |
mathparser var tokens:ArrayThe Array representation of the tokens of this evaluator.
| MathEvaluator | () | Constructor |
public function MathEvaluator(context:Object = null)Creates a new MathsEvaluator instance.
Parameterscontext:Object (default = null) — When instanciating the MathEvaluator you can pass a context containing either variables or functions.
|
| addToLastToken | () | method |
mathparser function addToLastToken(value:String):voidAdds the specified value to the last token.
Parameters
value:String |
| addToNextToken | () | method |
mathparser function addToNextToken(value:String):voidAdds the specified value to the next token.
Parameters
value:String |
| eval | () | method |
public function eval(o:*):*Evaluates the specified object.
Parameters
o:* |
* |
| evaluate | () | method |
mathparser function evaluate():NumberLaunchs the evaluation process.
ReturnsNumber |
| filterSpecialChars | () | method |
mathparser function filterSpecialChars(expression:String):StringFilters and returns special char passed-in argument.
Parameters
expression:String |
String — special char passed-in argument.
|
| getChar | () | method |
mathparser function getChar(pos:int = -1):StringReturns the char with the specified position.
Parameters
pos:int (default = -1) |
String — the char with the specified position.
|
| getFunctionValue | () | method |
mathparser function getFunctionValue(name:String, expressions:Array):StringReturns The string function value representation.
Parameters
name:String — The name of the function.
| |
expressions:Array — The arguments as math expression.
|
String — The result string of the evaluated function.
|
| getLastToken | () | method |
mathparser function getLastToken():StringReturns the last token string.
ReturnsString — the last token string.
|
| getNextChar | () | method |
mathparser function getNextChar():StringReturns the next char.
ReturnsString — the next char.
|
| getOperatorPriority | () | method |
mathparser function getOperatorPriority(op:String):uintReturns the operator priority value.
Parameters
op:String |
uint — the operator priority value.
|
| getParenthesisBlock | () | method |
mathparser function getParenthesisBlock():ArrayReturns the array representation of all elements in a parenthesis block.
ReturnsArray — the array representation of all elements in a parenthesis block.
|
| getValue | () | method |
mathparser function getValue(num:String):NumberReturns the value of the specified numeric expression.
Parameters
num:String |
Number — the value of the specified numeric expression.
|
| getVariableValue | () | method |
mathparser function getVariableValue(name:String):StringReturns the variable value with the internal context of the evaluator.
Parameters
name:String |
String — the variable value with the internal context of the evaluator.
|
| hasMoreChar | () | method |
mathparser function hasMoreChar():BooleanIndicates if has more char.
ReturnsBoolean |
| parse | () | method |
mathparser function parse(expression:String):NumberParses the specified expression.
Parameters
expression:String |
Number |
| reset | () | method |
mathparser function reset():voidResets the evaluator.
| toPostfixNotation | () | method |
mathparser function toPostfixNotation():voidThe toPostfixNotation method.
| maxHexValue | Constant |
mathparser const maxHexValue:Number = 0xFFFFFFThe max hexadecimal value.