As I said I was going to, I've created a handy set of functions for evaluating algorithms.
Features:
- Supports operator hierarchy (took about 50% of the scripting time for me to come up with all the operator levels.
- Accepts either in-fixed or post-fixed expressions.
- Works with arrays.
- Works with and without the use of ()'s
Please note that the way this script works is that it takes two kinds of expressions:
in-fixed and
post-fixed. The difference:
- In-fixed: 6 + 4 * 5
- Post-fixed: 645*+
- In-fixed: 6 * 4 + 5
- Post-fixed: 64*5+
In-fixed is much easier to type out for the average user and is generally easier to read. Post-fix however requires much less processing power and is much better for putting into your code.
There are basically 3 functions that you can use:
- evl(s) - This will take an standard in-fixed gs2 expression, s and return the result. Great for making simple calculations while developing or for calculating standard in-fixed expressions. The expression, s, should be a string.
- evalPost(s) - This function will take a post-fixed expression instead of an in-fixed one and will return the result. Post-fixed expressions can be a little more difficult to type out but don't use nearly as much processing power. This function is much better when you are going to be putting expressions into your code that won't be interpreted by a human. The expression s, must be in an array format.
- convertToPostFix(s) - Converts an in-fixed expression (string) into a post-fixed expression (array). Good for placing your post-fixed expression into your code, that you will be placing into evalPost() once you have decided in your algorithm. This function is only needed when you are trying to turn a in-fixed expression into a post-fixed expression.
Another note:
evl(s) basically converts
s into
post-fixed via
convertToPostFix(s) and then gives the result to
evalPost(s). The result of that is what you receive. For this reason it is recommended that you use
evalPost(s) when ever possible.
Things not done:- Didn't do operator hierarchy for most of the assignment operators.
- Operator assignment is does not yet work. I may add this feature or I may just have a += b just give a + b.
- Need to add more support for strings. So far @ and such works but I didn't add support for quotes and therefor, string constants.
- I did very little bug testing so if you find any bugs, please tell me.
Hope you all like it!
- Zero
TIP:
Put the code both serverside and clientside in a class then join the class to a global object such a new TStaticVar("_"); It's what I did on my server and it works great!