File: mfcalc.yo

package info (click to toggle)
bisonc%2B%2B 6.09.02-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,984 kB
  • sloc: cpp: 9,375; ansic: 1,505; fortran: 1,134; makefile: 1,062; sh: 526; yacc: 84; lex: 60
file content (33 lines) | stat: -rw-r--r-- 1,174 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
Now that the basics of using b() have been discussed, it is time to move on to
a more advanced problem. The above calculators provided only offer the
operators tt(+, - * /) and tt(^). It would be nice to have a calculator that
allows us to use some other mathematical functions as well, such as tt(sin),
tt(cos), etc..

It is easy to add new operators to the infix calculator as long as they are
only single-character literals. The parser's member tt(lex) returns all
non-number characters as tokens, so only some new grammar production rules
need to be added to the grammar when such tokens must be recognized. But we
want something more flexible: built-in functions that can be called like this:
        verb(
    function_name (argument)
        )
    At the same time, we add memory to the calculator, allowing us to use
variables. Here is a sample session with the multi-function calculator:
        verb(
	pi = 3.141592653589
	        3.14159
	sin(pi)
	        7.93266e-13
	alpha = beta1 = 2.3
	        2.3
	alpha
	        2.3
	ln(alpha)
	        0.832909
	exp(ln(beta1))
	        2.3
        )
Note that multiple assignment and nested function calls are supported.