Comments (2)
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.
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)
- Problem with conditional compilation HOT 7
- Problems with build.xml HOT 3
- invalid markup in asdoc comments HOT 1
- Vector Iterator (FP10 only) HOT 1
- Add new methods in the DateFormatter class to defines the list of week day names and month names. HOT 1
- Refactoring RGB and RGBA methods with static method fromNumber and toNumber HOT 1
- In the ArrayMap class add the getKeyAt and getValueAt methods HOT 1
- Change the namespace of the protected _values and _keys properties in ArrayMap HOT 1
- update system.URI and core.uri do deal with [[IMPORT]] in flash HOT 2
- need to perf test package level function in core HOT 1
- registerClassAlias Missing from avmglue HOT 5
- Problem to create system.evaluators.abc (MathEvaluator failed) HOT 1
- loading SWC bytecode into redtamarin
- Swf İnfo Problem with Ubuntu HOT 3
- Fix eden use E uppercase in Numbers
- Try to parse the number with a "." in the first character
- core.hash.crcTest HOT 1
- same action in different Chain
- improve dump functions in core HOT 1
- add an optino to dumpString
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from maashaack.