Git Product home page Git Product logo

Comments (2)

GoogleCodeExporter avatar GoogleCodeExporter commented on June 17, 2024
once this done we'll be able to start to implementat evaluators in 
Strings.format

Original comment by zwetan on 26 Apr 2008 at 8:51

from maashaack.

GoogleCodeExporter avatar GoogleCodeExporter commented on June 17, 2024
done :)

we 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
    ex: multiplication is performed before addition

    (14) fcn(...) (...)
        function call and expression grouping

    ex: sin(4) + 25
    sin(4) will be evaluated first

    ex: 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

    ex:
    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
    * your variable or function name must me lowercase
    * your variable or function name must contains only letter from a to z
    and can end with only one digit
    ex:
    test()  //OK
    test2() //OK
    2test() //BAD
    te2st() //BAD
    etc.
    * your function can only have one argument
    * your function name can not override default math functions as cos, sin, etc.


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

Original comment by zwetan on 27 Apr 2008 at 7:30

  • Changed state: Fixed

from maashaack.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.