Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 03-25-2008, 10:10 PM
zokemon zokemon is offline
That one guy...
zokemon's Avatar
Join Date: Mar 2001
Location: Sonoma County, California
Posts: 2,925
zokemon is a jewel in the roughzokemon is a jewel in the rough
Send a message via ICQ to zokemon Send a message via AIM to zokemon Send a message via MSN to zokemon Send a message via Yahoo to zokemon
Evaluation Functions

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!
Attached Files
File Type: txt eval.txt (5.3 KB, 288 views)

Last edited by zokemon; 03-26-2008 at 02:05 AM..
Reply With Quote
  #2  
Old 03-25-2008, 10:21 PM
DustyPorViva DustyPorViva is offline
Will work for food. Maybe
DustyPorViva's Avatar
Join Date: Sep 2003
Location: Maryland, USA
Posts: 9,589
DustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond repute
Send a message via AIM to DustyPorViva Send a message via MSN to DustyPorViva
Man, this really sucks. You call this scripting? PSHAWWW.

sux
Reply With Quote
  #3  
Old 03-25-2008, 10:22 PM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Nice, I will have to try this out along with PFA's some time.
__________________
Reply With Quote
  #4  
Old 03-25-2008, 10:24 PM
zokemon zokemon is offline
That one guy...
zokemon's Avatar
Join Date: Mar 2001
Location: Sonoma County, California
Posts: 2,925
zokemon is a jewel in the roughzokemon is a jewel in the rough
Send a message via ICQ to zokemon Send a message via AIM to zokemon Send a message via MSN to zokemon Send a message via Yahoo to zokemon
Quote:
Originally Posted by DustyPorViva View Post
Man, this really sucks. You call this scripting? PSHAWWW.

sux
You wish .
__________________
Do it with a DON!
Reply With Quote
  #5  
Old 03-25-2008, 11:05 PM
Toxen Toxen is offline
A Toxic Fellow
Join Date: Jan 2008
Location: Georgia
Posts: 36
Toxen is on a distinguished road
Very nice Zokemon, But what can't we expect from you
Rep++ as usual.
__________________
UN NAT.
Reply With Quote
  #6  
Old 03-25-2008, 11:08 PM
zokemon zokemon is offline
That one guy...
zokemon's Avatar
Join Date: Mar 2001
Location: Sonoma County, California
Posts: 2,925
zokemon is a jewel in the roughzokemon is a jewel in the rough
Send a message via ICQ to zokemon Send a message via AIM to zokemon Send a message via MSN to zokemon Send a message via Yahoo to zokemon
Haha, found a little bug and fixed it. New version was uploaded :P
__________________
Do it with a DON!
Reply With Quote
  #7  
Old 03-25-2008, 11:33 PM
DustyPorViva DustyPorViva is offline
Will work for food. Maybe
DustyPorViva's Avatar
Join Date: Sep 2003
Location: Maryland, USA
Posts: 9,589
DustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond repute
Send a message via AIM to DustyPorViva Send a message via MSN to DustyPorViva
Quote:
Originally Posted by zokemon View Post
found a little bug
Told you so.
Reply With Quote
  #8  
Old 03-25-2008, 11:38 PM
Tolnaftate2004 Tolnaftate2004 is offline
penguin.
Join Date: Jul 2004
Location: Berkeley, CA
Posts: 534
Tolnaftate2004 is a jewel in the roughTolnaftate2004 is a jewel in the rough
Send a message via AIM to Tolnaftate2004
PHP Code:
this.evl("(3*(1+2))*2"); 
Should be 18.
Gives 10 ((3*1+2)*2).
PHP Code:
this.evl("3--2"); 
Should be 5.
Gives 1.
PHP Code:
this.evl("3-(-2.2)"); 
Should be 5.2.
Gives -0.8.
__________________
◕‿‿◕ · pfa · check yer syntax! · src

Killa Be: when i got that locker in 6th grade the only thing in it was a picture of a midget useing a firehose :/
Reply With Quote
  #9  
Old 03-26-2008, 01:30 AM
zokemon zokemon is offline
That one guy...
zokemon's Avatar
Join Date: Mar 2001
Location: Sonoma County, California
Posts: 2,925
zokemon is a jewel in the roughzokemon is a jewel in the rough
Send a message via ICQ to zokemon Send a message via AIM to zokemon Send a message via MSN to zokemon Send a message via Yahoo to zokemon
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.
__________________
Do it with a DON!
Reply With Quote
  #10  
Old 03-26-2008, 01:32 AM
Tolnaftate2004 Tolnaftate2004 is offline
penguin.
Join Date: Jul 2004
Location: Berkeley, CA
Posts: 534
Tolnaftate2004 is a jewel in the roughTolnaftate2004 is a jewel in the rough
Send a message via AIM to Tolnaftate2004
Quote:
Originally Posted by zokemon View Post
I didn't add support for the negative operator yet either. Forgot to say that.
Yes, noticed that, by fluke, 3+-2 worked fine.
__________________
◕‿‿◕ · pfa · check yer syntax! · src

Killa Be: when i got that locker in 6th grade the only thing in it was a picture of a midget useing a firehose :/
Reply With Quote
  #11  
Old 03-26-2008, 01:43 AM
Rapidwolve24 Rapidwolve24 is offline
North*
Join Date: Oct 2007
Location: Massachusetts
Posts: 178
Rapidwolve24 is on a distinguished road
Send a message via AIM to Rapidwolve24 Send a message via MSN to Rapidwolve24
Quote:
Originally Posted by DustyPorViva View Post
Told you so.
Haterr <3
Reply With Quote
  #12  
Old 03-26-2008, 02:09 AM
zokemon zokemon is offline
That one guy...
zokemon's Avatar
Join Date: Mar 2001
Location: Sonoma County, California
Posts: 2,925
zokemon is a jewel in the roughzokemon is a jewel in the rough
Send a message via ICQ to zokemon Send a message via AIM to zokemon Send a message via MSN to zokemon Send a message via Yahoo to zokemon
Alright fixed all 3 of those problems you gave PFA as well as added support for arrays. It's all in the top post.

PHP Code:
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.

PHP 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.

PHP Code:
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:
PHP Code:
- -this.foo 
Since it will try to get makevar("-this.foo") which doesn't exist. I will fix that later too.
__________________
Do it with a DON!
Reply With Quote
  #13  
Old 03-26-2008, 02:11 AM
zokemon zokemon is offline
That one guy...
zokemon's Avatar
Join Date: Mar 2001
Location: Sonoma County, California
Posts: 2,925
zokemon is a jewel in the roughzokemon is a jewel in the rough
Send a message via ICQ to zokemon Send a message via AIM to zokemon Send a message via MSN to zokemon Send a message via Yahoo to zokemon
Quote:
Originally Posted by DustyPorViva View Post
Told you so.
Quote:
Originally Posted by zokemon
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!
__________________
Do it with a DON!
Reply With Quote
  #14  
Old 03-26-2008, 02:16 AM
DustyPorViva DustyPorViva is offline
Will work for food. Maybe
DustyPorViva's Avatar
Join Date: Sep 2003
Location: Maryland, USA
Posts: 9,589
DustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond repute
Send a message via AIM to DustyPorViva Send a message via MSN to DustyPorViva
Quote:
Originally Posted by zokemon View Post
I said that in my first post you potato wedge!
Reply With Quote
  #15  
Old 03-26-2008, 02:43 AM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Quote:
Originally Posted by zokemon View Post
you potato wedge!
Let's not call names now ...
__________________
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 06:40 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.