PDA

View Full Version : Evaluation Functions


zokemon
03-25-2008, 10:10 PM
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! :)

DustyPorViva
03-25-2008, 10:21 PM
Man, this really sucks. You call this scripting? PSHAWWW.

sux

cbk1994
03-25-2008, 10:22 PM
Nice, I will have to try this out along with PFA's some time.

zokemon
03-25-2008, 10:24 PM
Man, this really sucks. You call this scripting? PSHAWWW.

sux

You wish :cool:.

Toxen
03-25-2008, 11:05 PM
Very nice Zokemon, But what can't we expect from you ;)
Rep++ as usual.

zokemon
03-25-2008, 11:08 PM
Haha, found a little bug and fixed it. New version was uploaded :P

DustyPorViva
03-25-2008, 11:33 PM
found a little bug
Told you so.

Tolnaftate2004
03-25-2008, 11:38 PM
this.evl("(3*(1+2))*2");
Should be 18.
Gives 10 ((3*1+2)*2).
this.evl("3--2");
Should be 5.
Gives 1.
this.evl("3-(-2.2)");
Should be 5.2.
Gives -0.8.

zokemon
03-26-2008, 01:30 AM
I didn't add support for the negative operator yet either. Forgot to say that.
As for the first one, I just fixed it in about 15 seconds. Just needed to add "else v @= c;" in two places. Replacing the txt now.

EDIT:
The reason the second one doesn't work is because it is thinking you are trying to do a pre-decrement of 2 or a post-decrement of 3. Need to figure out a way to let it know that that isn't the case.

Tolnaftate2004
03-26-2008, 01:32 AM
I didn't add support for the negative operator yet either. Forgot to say that.

Yes, noticed that, by fluke, 3+-2 worked fine. ;)

Rapidwolve24
03-26-2008, 01:43 AM
Told you so.

Haterr <3

zokemon
03-26-2008, 02:09 AM
Alright fixed all 3 of those problems you gave PFA as well as added support for arrays. It's all in the top post.

this.evl("(3*(1+2))*2");
This didn't work because there was a problem when sending nested parantesses. It would actually call convertToPostFix(3*1+2) instead of convertToPostFix(3*(1+2)). Works now after adding 2 small lines of code.

this.evl("3--2");
If you put this into Graal, it will give you a compile error actually. Since I added support for negatives, just do 3- -2 or so.

this.evl("3-(-2.2)");
Works now since I added support for negatives. Basically it will use - as an operator if there was no operator proceding it. Otherwise it just adds it to the operand. Probably will not work right if you did something like:
3 - -this.foo
Since it will try to get makevar("-this.foo") which doesn't exist. I will fix that later too.

zokemon
03-26-2008, 02:11 AM
Told you so.

Things not done:
I did very little bug testing so if you find any bugs, please tell me.

I said that in my first post you potato wedge!

DustyPorViva
03-26-2008, 02:16 AM
I said that in my first post you potato wedge!
http://forums.achaea.com/style_emoticons/default/ohno.gif

cbk1994
03-26-2008, 02:43 AM
you potato wedge!

Let's not call names now ...

smirt362
03-26-2008, 09:40 AM
I don't pretend to understand any of this, it's basically like me looking at Aramaic and then trying to decipher a meaning, but what if you want to use numbers that have more than 1 digit...say a 26 or 312?

Tolnaftate2004
03-26-2008, 09:49 AM
I don't pretend to understand any of this, it's basically like me looking at Aramaic and then trying to decipher a meaning, but what if you want to use numbers that have more than 1 digit...say a 26 or 312?

Post-fixed actually looks like "5,3,+" for in-fixed "5+3," so it will work fine with larger numbers.

zokemon
03-26-2008, 05:07 PM
Post-fixed actually looks like "5,3,+" for in-fixed "5+3," so it will work fine with larger numbers.

Well if you use a post fixed calculator (I've never sene one in my life, just was told about them from my C++ teacher), it just has the standard operator keys and an "Enter" key. Since each operator acts as an equals, the "Enter" key is only for telling that the number you have entered is the entire number and you are going to enter a different number. Essentially that is the whole comma thing like PFA said. I just used an array for post-fix to make things easier.

I don't pretend to understand any of this, it's basically like me looking at Aramaic and then trying to decipher a meaning, but what if you want to use numbers that have more than 1 digit...say a 26 or 312?

Written out though that would look like:
26 312 +

Tolnaftate2004
03-26-2008, 07:38 PM
Post-fixed is also called RPN or Reverse Polish Notation. ;)
HP graphing calculators use this notation.

cbk1994
03-26-2008, 10:30 PM
So an array may look like


{ 35, 23, "+", 93, 23, "*" }

How would you tell it to add whatever 35 + 23 equals to whatever 93 * 23 equals?

Or did I do it wrong?

Tolnaftate2004
03-26-2008, 10:35 PM
How would you tell it to add whatever 35 + 23 equals to whatever 93 * 23 equals?

Or did I do it wrong?


{ 35, 23, "+", 93, 23, "*", "+" }


while true
find subarray in array like {v1,v2,op} or break
replace {v1,v2,op} with v1 op v2
end

cbk1994
03-26-2008, 10:43 PM
{ 35, 23, "+", 93, 23, "*", "+" }


Ah, okay.

35+23+(93*23) seems easier to me. Of course, in this case you wouldn't need the parentheses, but anyway ...

zokemon
03-27-2008, 12:32 AM
Ah, okay.

35+23+(93*23) seems easier to me. Of course, in this case you wouldn't need the parentheses, but anyway ...

Since Graal treats arrays as strings, you can just do:
foo = evalPost("35,23,+,93,23,*,+");

smirt362
03-27-2008, 05:32 AM
Written out though that would look like:
26 312 +

Ooooh, I see