Packagesystem.evaluators
Classpublic class MathEvaluator
InheritanceMathEvaluator Inheritance Object
Implements Evaluable

Evaluates mathematical string expressions.

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

  • evaluate function call
  • remove space chars
  • re-order tokens by operators priority
  • evaluate hexadecimal notation to decimal
  • etc.
  • 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



    Public Methods
     MethodDefined By
      
    MathEvaluator(context:Object = null)
    Creates a new MathsEvaluator instance.
    MathEvaluator
      
    eval(o:*):*
    Evaluates the specified object.
    MathEvaluator
    Property Detail
    currentPosproperty
    mathparser var currentPos:uint

    The current position.

    expressionproperty 
    mathparser var expression:String

    The expression value.

    tokensproperty 
    mathparser var tokens:Array

    The Array representation of the tokens of this evaluator.

    Constructor Detail
    MathEvaluator()Constructor
    public function MathEvaluator(context:Object = null)

    Creates a new MathsEvaluator instance.

    Parameters
    context:Object (default = null) — When instanciating the MathEvaluator you can pass a context containing either variables or functions.
    Method Detail
    addToLastToken()method
    mathparser function addToLastToken(value:String):void

    Adds the specified value to the last token.

    Parameters

    value:String

    addToNextToken()method 
    mathparser function addToNextToken(value:String):void

    Adds the specified value to the next token.

    Parameters

    value:String

    eval()method 
    public function eval(o:*):*

    Evaluates the specified object.

    Parameters

    o:*

    Returns
    *
    evaluate()method 
    mathparser function evaluate():Number

    Launchs the evaluation process.

    Returns
    Number
    filterSpecialChars()method 
    mathparser function filterSpecialChars(expression:String):String

    Filters and returns special char passed-in argument.

    Parameters

    expression:String

    Returns
    String — special char passed-in argument.
    getChar()method 
    mathparser function getChar(pos:int = -1):String

    Returns the char with the specified position.

    Parameters

    pos:int (default = -1)

    Returns
    String — the char with the specified position.
    getFunctionValue()method 
    mathparser function getFunctionValue(name:String, expressions:Array):String

    Returns The string function value representation.

    Parameters

    name:String — The name of the function.
     
    expressions:Array — The arguments as math expression.

    Returns
    String — The result string of the evaluated function.
    getLastToken()method 
    mathparser function getLastToken():String

    Returns the last token string.

    Returns
    String — the last token string.
    getNextChar()method 
    mathparser function getNextChar():String

    Returns the next char.

    Returns
    String — the next char.
    getOperatorPriority()method 
    mathparser function getOperatorPriority(op:String):uint

    Returns the operator priority value.

    Parameters

    op:String

    Returns
    uint — the operator priority value.
    getParenthesisBlock()method 
    mathparser function getParenthesisBlock():Array

    Returns the array representation of all elements in a parenthesis block.

    Returns
    Array — the array representation of all elements in a parenthesis block.
    getValue()method 
    mathparser function getValue(num:String):Number

    Returns the value of the specified numeric expression.

    Parameters

    num:String

    Returns
    Number — the value of the specified numeric expression.
    getVariableValue()method 
    mathparser function getVariableValue(name:String):String

    Returns the variable value with the internal context of the evaluator.

    Parameters

    name:String

    Returns
    String — the variable value with the internal context of the evaluator.
    hasMoreChar()method 
    mathparser function hasMoreChar():Boolean

    Indicates if has more char.

    Returns
    Boolean
    parse()method 
    mathparser function parse(expression:String):Number

    Parses the specified expression.

    Parameters

    expression:String

    Returns
    Number
    reset()method 
    mathparser function reset():void

    Resets the evaluator.

    toPostfixNotation()method 
    mathparser function toPostfixNotation():void

    The toPostfixNotation method.

    Constant Detail
    maxHexValueConstant
    mathparser const maxHexValue:Number = 0xFFFFFF

    The max hexadecimal value.