| 1 |
Now that the basics of using b() have been discussed, it is time to move on to |
| 2 |
a more advanced problem. The above calculators provided only offer the |
| 3 |
operators tt(+, - * /) and tt(^). It would be nice to have a calculator that |
| 4 |
allows us to use some other mathematical functions as well, such as tt(sin), |
| 5 |
tt(cos), etc.. |
| 6 |
|
| 7 |
It is easy to add new operators to the infix calculator as long as they are |
| 8 |
only single-character literals. The parser's member tt(lex) returns all |
| 9 |
non-number characters as tokens, so only some new grammar production rules |
| 10 |
need to be added to the grammar when such tokens must be recognized. But we |
| 11 |
want something more flexible: built-in functions that can be called like this: |
| 12 |
verb( |
| 13 |
function_name (argument) |
| 14 |
) |
| 15 |
At the same time, we add memory to the calculator, allowing us to use |
| 16 |
variables. Here is a sample session with the multi-function calculator: |
| 17 |
verb( |
| 18 |
pi = 3.141592653589 |
| 19 |
3.14159 |
| 20 |
sin(pi) |
| 21 |
7.93266e-13 |
| 22 |
alpha = beta1 = 2.3 |
| 23 |
2.3 |
| 24 |
alpha |
| 25 |
2.3 |
| 26 |
ln(alpha) |
| 27 |
0.832909 |
| 28 |
exp(ln(beta1)) |
| 29 |
2.3 |
| 30 |
) |
| 31 |
Note that multiple assignment and nested function calls are supported. |
| 32 |
|
| 33 |
|