cakyrespa
Member
While messing with some ideas, I encountered something I couldn't figure out how to do from an ASH script. In general, I would like to be able to take an arbitrary string, such as "2 * 10" and have it calculate the results.
That is a simplification. In general, I'd like to be able to take something like "mainStat * 2" or even "max(mainStat * 2, 200)" from a map file, substitute mainStat with something useful, and then figure out the results.
And, then after some discussions... we seem to come up with this:
RPN Calculator 4
It isn't algebra per se, but the RPN Calculator implements a Reverse Polish Notation calculator. This is a stack-based calculator that will perform arbitrary math from a given string and return the resulting floating point results.
At the most simple usage, the calculator takes a list of whitespace separated values and splits them into tokens. Numbers are pushed into a stack as are variable substitutions. Operators pop one or more of those numbers from the stack, performs some calculation, and pushes the results. When the tokens are done being processed, the resulting number is returned.
For example:
Adds 1 and 2 and returns 3.
Adds 1 and 2 to get 3, then multiples that by 4 to return 12.
The following operations are supported:
There are two files with these scripts. The first rpn.ash implements the basic calculator. rpn_example.ash shows how to use the script and also runs through a series of calculations to make sure the values are supported.
The original implement was written by the_great_cow_guru and expanded on cakyrespa and Catch-22.
That is a simplification. In general, I'd like to be able to take something like "mainStat * 2" or even "max(mainStat * 2, 200)" from a map file, substitute mainStat with something useful, and then figure out the results.
And, then after some discussions... we seem to come up with this:
RPN Calculator 4
It isn't algebra per se, but the RPN Calculator implements a Reverse Polish Notation calculator. This is a stack-based calculator that will perform arbitrary math from a given string and return the resulting floating point results.
At the most simple usage, the calculator takes a list of whitespace separated values and splits them into tokens. Numbers are pushed into a stack as are variable substitutions. Operators pop one or more of those numbers from the stack, performs some calculation, and pushes the results. When the tokens are done being processed, the resulting number is returned.
For example:
Code:
1 2 +
Adds 1 and 2 and returns 3.
Code:
1 2 + 4 *
Adds 1 and 2 to get 3, then multiples that by 4 to return 12.
The following operations are supported:
- X Y + => Z: Adds X and Y and pushes the results.
- X Y - => Z: Subtracts X from Y and pushes the results.
- X Y * => Z: Multiplies X and Y and pushes the results.
- X Y / => Z: Divides X by Y and pushes the results.
- X Y ^ => Z: Raises X to the Y power and pushes the results.
- X Y min => Z: Pushes the lower of X or Y.
- X Y max => Z: Pushes the higher of X or Y.
- X Y C if => Z: If C is 0, pushes Y, otherwise pushes X.
- C not => Y: If C is 0, pushes 1, otherwise pushes 0.
There are two files with these scripts. The first rpn.ash implements the basic calculator. rpn_example.ash shows how to use the script and also runs through a series of calculations to make sure the values are supported.
The original implement was written by the_great_cow_guru and expanded on cakyrespa and Catch-22.
Attachments
Last edited: