Calculator Softcode

From AdamWiki

Jump to: navigation, search

I wrote a calculator in softcode. It's something I've wanted to do for a while but it involved me laying out a block of a few hours to do the research on parsers. I started with an RPN (postfix) calculator, which was relatively simple once I solved a few problems, then used that to write an expression calculator.

The RPN Calculator is extremely powerful and written in a fairly small amount of code. The expression calculator (@calc) is much simpler in terms of what it can do; it uses the Shunting Yard algorithm to convert an infix math expression to a postfix expression then uses the RPN routines to solve it and print the result.

I wrote a sort of list extension to the stack language for the RPN calculator, too, so you can write "@rpn @ 1 2 3 4 sum" and get 10. The @ starts the list, and any list-based function will pop back to the @ and use the terms between as the list.

Both calculators handle unary plus and minus. The expression calculator handles parentheses and operator precedence. Eventually I want to extend the expression calculator to parse functions like SIN() and so on.

The code is available in two flavors:

Personal tools